Advertisement
Guest User

main.c

a guest
Jan 3rd, 2016
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.74 KB | None | 0 0
  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 = BIT1+BIT4+BIT3;       // P1.3 Pulled up
  57.   P1REN |= 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. // Timer A 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.         setPWM();
  99.       }
  100.     }
  101.    P1OUT &= ~BIT0;
  102.    P1IFG &= ~BIT3;  // P1.3 IFG cleared
  103. }
  104.  
  105. void addSec(){
  106.   if(seconds<649){
  107.     seconds+=11;;
  108.   }
  109.   else{
  110.     addMin();
  111.     seconds = 0;
  112.   }
  113. }
  114. void addMin(void){
  115.   if(minutes<649){
  116.     minutes+=11;
  117.   }
  118.   else{
  119.     addHour();
  120.     minutes = 0;
  121.   }
  122. }
  123. void addHour(void){
  124.   if (hours < 660){
  125.     hours +=60;
  126.   }
  127.   else hours = 0 ;
  128. }
  129. void setPWM(void){
  130.   TA0CCR1 = seconds;
  131.   TA1CCR1 = minutes;
  132.   TA1CCR2 = hours;
  133. }
  134. void flashLED(void){
  135.   // Toggle LED
  136.   P1OUT ^= BIT6;                   
  137.   __delay_cycles(3000);
  138.   P1OUT ^= BIT6;
  139. }
  140. void delay(int time){
  141.     int i =0;
  142.   for(i=0; i<time; i++){
  143.     __delay_cycles(1000);
  144.   }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement