I have modified and revised the following code which passes through the build tool check without any errors and seems, to my logic, to be correct. I have added notes to the top of the code to explain what it is designed to achieve. I would be grateful if anyone could have a look through it and let me know whether I have made any glaring or silly errors anywhere that will prevent it from operating.
Thank you.
James.
/* * Driver Code for Solenoid-operated Clock Counting Seconds and Minutes * * The clock will have two solenoids: one operating every second, the other each minute. * The "seconds" solenoid needs to operate at full power each time. * The "minutes" solenoid needs to increase its power gradually. * The basic operation is by counting the Watchdog "ticks". * The "seconds" solenoid has a variable that is made active each second, operates the solenoid and then resets. * The "minutes" solenoid is operated once the seconds count reaches a minute's worth, when it is reset. * Each coil is connected to a different output pin. The minute coil is on pin 4 and the seconds coil on pin 6. */ /* * ======== Standard MSP430 includes ======== */ #include <msp430.h> /* * ======== Grace related includes ======== */ #include <ti/mcu/msp430/csl/CSL.h> /* * ======== Definitions and variables ======== */ #define RedLED (BIT0) // Red LED on the LaunchPad (LEDs used during debug) #define GreenLED (BIT6) // Green LED on the LaunchPad #define Minute_Coil (BIT2) // Port 1 Bit 2 is high-active to the base of an NPN coil driver transistor #define Seconds_Coil (BIT4) // Port 1 Bit 2 is high-active to the base of an NPN coil driver transistor #define TestMode (BIT3) // Jumper to ground puts the circuit into Test Mode // Test Mode runs the coil at a faster than normal rate to check out a newly-built clock #define WDticksPerSecond 64 // Number of Watchdog Timer ticks (interrupts) per second // The coil is initially turned on at partial power, then power is increased in increments every 15.2 mS to 100% power. // Total on time is 155 mS and average power is 85%. #define PWM_bump 25 // incremental PWM value (2.5%) #define PWM_init 700 // initial PWM value (70% power) #define PWM_full 1000 // full power PWM value 100% unsigned int WDTticks = 0; // count of Watchdog Timer (interval timer) interrupt service calls unsigned int seconds = 0; // elapsed seconds counted from WDTticks unsigned int PWM; // percent of time to turn on the coil. 1000 = 100%. unsigned int Finger_Tapper = 0; // VARIABLE TO CONTROL THE SECOND COIL /* * ======== Interrupt handlers ======== */ /* * Watchdog Timer Interrupt handler * * The microcontroller is awakened from low-power mode by the Watchdog Timer. * The Watchdog Timer is set to interrupt every 15.2 mS or 64 times per second. * Each interrupt or "tick" of the timer is counted, and after 64 ticks the seconds counter is incremented. This is the basic * time-keeping function of the clock. * * The coil is turned on once per minute in normal mode and once per second in test mode. * Test mode is set by installing a jumper connected to a port pin. * * The coil is turned on and off by enabling or disabling the bit in the Port Selection Register which connects the Timer A2 PWM * output to the port. * When disabled, the port pin is connected to the Port Output which is always set low (inactive) and the transistor is turned * off. * When enabled, the Timer A2 PWM output is connected to the port pin and the transistor turned on at the PWM duty cycle rate. * Also when enabled, at each tick of the Watchdog Timer, or every 15.2 mS, the PWM percentage is increased a small amount. * The current into the coil is thus gradually ramped up from partial to full power (100% duty cycle). This ramp up causes the * solenoid plunger to activate more slowly and help prevent the clock's pawls from skipping a tooth on the ratchet wheel. * The pawl would operate even more smoothly ramping up from 0%, but that would take more current and shorter battery life. The * design choice is a trade-off between smoothness and battery life. * * */ void WDTISRHandler(void) { // If the Port Selection Register bit is active, the Timer A2 PWM output is connected to the port pin and the coil //is on. if (P1SEL & Minute_Coil) // is the coil on or off? { // The coil is on - ramp up the PWM percentage at each Watchdog Timer tick CCR1 += PWM_bump; //increase the PWM percent a modest amount if (CCR1 >= PWM_full) // are we at full power? { P1SEL &= ~Minute_Coil; // yes, turn off the coil } } WDTticks++; // count the number of Watchdog Timer ticks to keep basic time if (WDTticks >= WDticksPerSecond) { seconds++; // count seconds // Once per second. This sets Finger_Tapper to activates a solenoid to count the seconds if (WDTticks >= WDticksPerSecond) { Finger_Tapper = 1; if (Finger_Tapper == 1) { CCR1 = PWM_full;// set the PWM to full value P1SEL |= Seconds_Coil; // turn the second coil on } Finger_Tapper = 0; //Reset Finger_Tapper P1SEL &= ~ Seconds_Coil; // turn the second coil off } // once per minute in Normal mode, once per second in Test mode if ((seconds >= 60) | (((P1IN & TestMode) == 0) & (seconds)) ) { seconds = 0; // reset the seconds count CCR1 = PWM_init; // set the PWM to initial value P1SEL |= Minute_Coil; // turn the coil on } WDTticks = 0; // reset the Watchdog Timer ticks counter } } /* * ======== main ======== */ int main(int argc, char *argv[]) { CSL_init(); // Activate Grace-generated configuration _BIS_SR(LPM0_bits); // Enter Low-power mode and wait for interrupts return (0); }