SHARE
TWEET

main.c

a guest Jan 5th, 2016 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This program was written by Daniel Johnson and Doug Jensen for the MSP430 Analog Gauge Clock Project
  3. Inputs and outputs:
  4. P1.1, P1.3, P1.4 are used as push button inputs
  5. P1.3 is the set button, while this button is held down, the clock can be set by resetting the time to 12:00
  6. P1.4 is the hours button - pressing this while holding down the set button adds hours to the time
  7. P1.1 is the minutes button - pressing this while holding down the set button adds minutes to the time
  8. There is no AM/PM designation on this clock.
  9.  
  10. hours go from 0-660, increments of 60, Where 0 indicates 1:00 and 660 indicates 12:00
  11. minutes go from 0-649, increments of 11, where 0=0 and 649=59
  12. seconds go from 0-649, increments of 11, where 0=0 and 649=59
  13.  
  14. P1.2 is the PWM output for seconds
  15. P2.1 is the PWM output for minutes
  16. P2.4 is the PWM output for hours
  17. */
  18.  
  19. #include "msp430g2553.h"
  20.  
  21. unsigned int seconds, minutes, hours = 0;
  22. void flashLED(void);
  23. void addSec(void);
  24. void addMin(void);
  25. void addHour(void);
  26. void delay(int);
  27. void setPWM(void);
  28. int main( void )
  29. {
  30.   // Configure clock
  31.   BCSCTL3 |= XCAP_1;            // enabling built in 6 pF capacitance for crystal
  32.  
  33.   // Configure the Watch dog timer for the RTC
  34.   WDTCTL = WDT_ADLY_1000;       // watchdog interval timer mode ACLK
  35.   IE1 = WDTIE;                  // enable watchdog timer interrupt
  36.  
  37.   // Configure timers for PWM, timer A1 and A2 will be used
  38.   TA0CTL = TASSEL_1 + MC_1;      
  39.   TA1CTL = TASSEL_1 + MC_1;
  40.   TA0CCR0 = 660-1;              // Periods for both timers
  41.   TA1CCR0 = 660-1;
  42.   TA0CCTL0 = OUTMOD_7;          // Reset/Set
  43.   TA0CCTL1 = OUTMOD_7;          // Reset/Set
  44.   TA1CCTL0 = OUTMOD_7;          // Reset/Set
  45.   TA1CCTL1 = OUTMOD_7;
  46.   TA1CCTL2 = OUTMOD_7;
  47.   setPWM();
  48.  
  49.   // Configure pins for PWM, and Pushbuttons
  50.   P1DIR = BIT2+BIT6;
  51.   P1SEL = BIT2;
  52.   P1OUT = 0x00;
  53.   P2OUT = 0x00;
  54.   P2DIR = BIT1+BIT4;
  55.   P2SEL |= BIT1+BIT4;
  56.   P1OUT = BIT0+BIT1+BIT4+BIT3;  // P1.3 Pulled up
  57.   P1REN |= BIT0+BIT1+BIT4+BIT3;      // Enable internal pull-up/down resistor on P1.3
  58.   P1IE |= BIT3;                 // P1.3 interrupt enabled
  59.   P1IES |= BIT3;                // P1.3 Falling edge
  60.   P1IFG &= ~BIT3;               // P1.3 IFG cleared
  61.  
  62.   _BIS_SR(GIE);     // global interrupts enabled
  63. }
  64. // watchdog timer interrupt
  65. #pragma vector=WDT_VECTOR
  66. __interrupt void watchdog_timer(void)
  67. {
  68.   flashLED();
  69.   addSec();
  70.   setPWM();
  71. }
  72.  
  73. // Push Button Interrupt
  74. #pragma vector=PORT1_VECTOR
  75. __interrupt void Port_1(void)
  76. {
  77.     delay(100);          // Debounce delay
  78.     if((P1IN & BIT3)== 0){   // Check if button still pressed
  79.       P1OUT |= BIT0;
  80.       // Reset clock to 1:00
  81.       seconds = 0;
  82.       minutes = 0;
  83.       hours = 0;
  84.       while((P1IN & BIT3) == 0){
  85.         // Poll minute and hour buttons
  86.         if((P1IN & BIT1) == 0){
  87.           delay(100); // debounce
  88.           if((P1IN & BIT1) == 0){
  89.             addMin();
  90.           }
  91.         }
  92.         if((P1IN & BIT4) == 0){
  93.           delay(100); // debounce
  94.           if((P1IN & BIT4) == 0){
  95.             addHour();
  96.           }
  97.         }
  98.         if((P1IN & BIT0)==0){ // Enter a calibration mode where each meter is maxed out.
  99.             while((P1IN & BIT0)==0){
  100.                 hours = 660;
  101.                 minutes = 660;
  102.                 seconds = 660;
  103.                 setPWM();
  104.             }
  105.             hours = 0;
  106.             minutes = 0;
  107.             seconds = 0;
  108.             setPWM();
  109.         }
  110.         setPWM();
  111.       }
  112.     }
  113.    P1OUT &= ~BIT0;
  114.    P1IFG &= ~BIT3;  // P1.3 IFG cleared
  115. }
  116.  
  117. void addSec(){
  118.   if(seconds<649){
  119.     seconds+=11;;
  120.   }
  121.   else{
  122.     addMin();
  123.     seconds = 0;
  124.   }
  125. }
  126. void addMin(void){
  127.   if(minutes < 649){
  128.     minutes+=11;
  129.   }
  130.   else{
  131.     addHour();
  132.     minutes = 0;
  133.   }
  134. }
  135. void addHour(void){
  136.   if (hours < 660){
  137.     hours +=60;
  138.   }
  139.   else hours = 0 ;
  140. }
  141. void setPWM(void){
  142.   TA0CCR1 = seconds;
  143.   TA1CCR1 = minutes;
  144.   TA1CCR2 = hours;
  145. }
  146. void flashLED(void){
  147.   // Toggle LED
  148.   P1OUT ^= BIT6;                   
  149.   __delay_cycles(3000);
  150.   P1OUT ^= BIT6;
  151. }
  152. void delay(int time){
  153.     int i =0;
  154.   for(i=0; i<time; i++){
  155.     __delay_cycles(1000);
  156.   }
  157. }
RAW Paste Data
Top