Advertisement
Guest User

main.c

a guest
Jan 22nd, 2016
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <msp430G2553.h>
  2. #include "MAX6921.h"
  3.  
  4. //declare functions
  5. void delay(unsigned int);
  6. void printTime(void);
  7. void addSec(void);
  8. void addMin(void);
  9. void addHour(void);
  10. void flashLED(void);
  11.  
  12. // global variables
  13. unsigned int hours = 12;
  14. unsigned int minutes = 0;
  15. unsigned int seconds = 0;
  16. unsigned int pm = 0;
  17.  
  18. int main( void ){
  19.   // Configure clock
  20.   BCSCTL3 |= XCAP_2;            // enabling built in 6 pF capacitance for crystal
  21.  
  22.   // Configure the Watch dog timer for the RTC
  23.   WDTCTL = WDT_ADLY_1000;       // watchdog interval timer mode ACLK
  24.   IE1 = WDTIE;                  // enable watchdog timer interrupt
  25.   setupMax6921();
  26.  
  27.   P1DIR |= BIT0+BIT2;
  28.   P1DIR &= ~BIT1;
  29.   P1OUT |= BIT1;                // P1.1 Pulled Up
  30.   P1REN |= BIT1;                // P1.1 Resistor enabled
  31.   P1IE |= BIT1;                 // P1.1 interrupt enabled
  32.   P1IES |= BIT1;                // P1.1 Falling edge
  33.   P1IFG &= ~BIT1;               // P1.1 IFG cleared
  34.   P1SEL |= BIT2;                // P1.2 TA0
  35.  
  36.   P2DIR &= ~BIT0;
  37.   P2OUT |= BIT0;                // P2.0 Pulled Up
  38.   P2REN |= BIT0;                // P2.0 Resistor enabled
  39.   P2IE |= BIT0;                 // P2.0 interrupt enabled
  40.   P2IES |= BIT0;                // P2.0 Falling edge
  41.   P2IFG &= ~BIT0;               // P2.0 IFG cleared
  42.  
  43.   // Configure timers for boost converter
  44.   TA0CCR0 = 32-1;               // PWM Period ~500kHz
  45.   TA0CCTL1 = OUTMOD_7;          // CCR1 reset/set
  46.   TA0CCR1 = 8;                  // CCR1 PWM duty cycle
  47.   TA0CTL = TASSEL_2 + MC_1;     // SMCLK, up mode
  48.  
  49.   _BIS_SR(GIE);                 // global interrupts enabled
  50.  
  51.   printTime();
  52. }
  53.  
  54. // watchdog timer interrupt
  55.  #pragma vector=WDT_VECTOR
  56.  __interrupt void watchdog_timer(void){
  57.    addSec();
  58.  }
  59. void delay(unsigned int ms){
  60.  while (ms--){
  61.         __delay_cycles(16000); // set for 1ms at 16MHz
  62.     }
  63. }
  64.  
  65. // Push Button Interrupt
  66. #pragma vector=PORT1_VECTOR
  67. __interrupt void Port_1(void)
  68. {
  69.     delay(1);          // Debounce delay
  70.     if((P1IN & BIT1)== 0){   // Check if button still pressed
  71.         seconds = 0;
  72.         addHour();
  73.     }
  74.     P1IFG &= ~BIT1;  // P1.1 IFG cleared
  75. }
  76.  
  77. #pragma vector=PORT2_VECTOR
  78. __interrupt void Port_2(void)
  79. {
  80.     delay(1);          // Debounce delay
  81.     if((P2IN & BIT0)== 0){   // Check if button still pressed
  82.         seconds = 0;
  83.         addMin();
  84.     }
  85.     P2IFG &= ~BIT0;  // P1.1 IFG cleared
  86. }
  87.  
  88. void printTime(){
  89.     // takes hours minutes and seconds (6-digits) and prints them on the VFD
  90.     // HH-MM-SS
  91.     unsigned int h1,h2,m1,m2,s1,s2;
  92.     for(;;){
  93.     h1 = hours/10;
  94.     h2 = hours%10;
  95.     m1 = minutes/10;
  96.     m2 = minutes%10;
  97.     s1 = seconds/10;
  98.     s2 = seconds%10;
  99.         print(8,h1,0);
  100.         print(7,h2,0);
  101.         print(6,10,0);
  102.         print(5,m1,0);
  103.         print(4,m2,0);
  104.         print(3,10,0);
  105.         print(2,s1,0);
  106.         print(1,s2,0);
  107.         if(pm==1) print(0,0,1);
  108.     }
  109. }
  110.  
  111. void addSec(){
  112.   if(seconds < 59){
  113.     seconds++;
  114.   }
  115.   else{
  116.     addMin();
  117.     seconds = 0;
  118.   }
  119. }
  120. void addMin(void){
  121.   if(minutes < 59){
  122.     minutes++;
  123.   }
  124.   else{
  125.     addHour();
  126.     minutes = 0;
  127.   }
  128. }
  129. void addHour(void){
  130.   if(hours < 12){
  131.       if(hours == 11){
  132.           hours ++;
  133.           pm^=BIT0; //toggle AM/PM
  134.       }
  135.       else{
  136.           hours++;
  137.       }
  138.   }
  139.   else{
  140.       hours = 1;
  141.   }
  142. }
  143. void flashLED(void){
  144.   // Toggle LED
  145.   P1OUT |= BIT0;
  146.   __delay_cycles(1000);
  147.   P1OUT ^= BIT0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement