Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.92 KB | None | 0 0
  1. /******************************************************************************
  2. MSP430G2553 Project Creator
  3.  
  4. GE 423  - Dan Block
  5.         Spring(2012)
  6.  
  7.         Written(by) : Steve(Keres)
  8. College of Engineering Control Systems Lab
  9. University of Illinois at Urbana-Champaign
  10. *******************************************************************************/
  11.  
  12. #include "msp430g2553.h"
  13. #include "UART.h"
  14.  
  15. char newprint = 0;
  16. unsigned long timecnt = 0;  
  17. int pin27count = 0;
  18. int pin26count = 0;
  19.  
  20. char debounce27 = 0;
  21. char debounce26 = 0;
  22.  
  23. void main(void) {
  24.  
  25.     WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  26.  
  27.     if (CALBC1_16MHZ ==0xFF || CALDCO_16MHZ == 0xFF) while(1);
  28.  
  29.     DCOCTL = CALDCO_16MHZ;    // Set uC to run at approximately 16 Mhz
  30.     BCSCTL1 = CALBC1_16MHZ;
  31.  
  32.     // initialize LEDs
  33.     P1SEL &= 0x0F; // choosing I/O
  34.     P1SEL2 &= 0x0F;
  35.     P1DIR |= 0xF0; // choosing OUTPUT
  36.     P1OUT = 0x10;
  37.  
  38.     P2SEL &= ~0x03;
  39.     P2SEL2 &= ~0x03;
  40.     P2DIR |= 0x03;
  41.  
  42.  
  43.     // initialize switches
  44.     P2SEL &= 0x3F; // choosing I/O
  45.     P2SEL2 &= 0x3F;
  46.     P2DIR &= 0x3F;  // choosing INPUT
  47.  
  48.     P2REN |= 0xC0;  // enabling pullups
  49.     P2IE |= 0xC0;   // enable interrupts
  50.     P2IES |= 0xC0;  // hi to low trigger
  51.     P2IFG &= ~0xC0; // clear int flag
  52.  
  53.     // Timer A Config
  54.     TACCTL0 = CCIE;             // Enable Periodic interrupt
  55.     TACCR0 = 16000;                // period = 1ms  
  56.     TACTL = TASSEL_2 + MC_1; // source SMCLK, up mode
  57.  
  58.  
  59.     Init_UART(9600,1);  // Initialize UART for 9600 baud serial communication
  60.  
  61.     _BIS_SR(GIE);       // Enable global interrupt
  62.  
  63.     UART_printf("Reboot\r\n");
  64.     while(1)
  65.     {
  66.         if (newprint)
  67.         {
  68.             UART_printf("Sw1 Cnt: %d, Sw2 Cnt: %d\n\r",(int)pin26count, (int)pin27count);
  69.             newprint = 0;
  70.         }
  71.     }
  72. }
  73.  
  74. // Timer A0 interrupt service routine
  75. #pragma vector=TIMER0_A0_VECTOR
  76. __interrupt void Timer_A (void)
  77. {
  78.     timecnt++; // Keep track of time for main while loop.
  79.  
  80.     if ((timecnt%500) == 0) {
  81.         newprint = 1;  // flag main while loop that .25 seconds have gone by.
  82.     }
  83.  
  84.     if (debounce27)
  85.     {
  86.         debounce27++;
  87.  
  88.         if (debounce27 >= 10) // wait 10ms
  89.         {
  90.             P2IE |= 0x80;     // renable interrupt
  91.             debounce27 = 0;   // clear flag
  92.         }
  93.     }
  94.  
  95.     if (debounce26)
  96.     {
  97.         debounce26++;
  98.         if (debounce26 >= 10) // wait 10ms
  99.         {
  100.             P2IE |= 0x40;     // renable interrupt
  101.             debounce26 = 0;   // clear flag
  102.         }
  103.     }
  104.  
  105. }
  106.  
  107. #pragma vector=PORT2_VECTOR
  108. __interrupt void Port_2(void)
  109. {
  110.     if (P2IFG & 0x80) // P2.7
  111.     {
  112.         pin27count++;    // increment count
  113.         P2IFG &= ~0x80;  // clear INT flag
  114.  
  115.         P2OUT ^= 0x02;  // toggle p2.1
  116.  
  117.         debounce27 = 1;  // set debounce flag
  118.         P2IE &= ~0x80;   // INT disable
  119.     }
  120.  
  121.     if (P2IFG & 0x40) // P2.6
  122.     {
  123.         pin26count++;   // increment count
  124.         P2IFG &= ~0x40; // clear INT flag
  125.  
  126.         P2OUT ^= 0x01;  // toggle p2.0
  127.  
  128.         debounce26 = 1; // set debounce flag
  129.         P2IE &= ~0x40;  // INT disable
  130.     }
  131. }
  132.  
  133.  
  134. /*
  135. // ADC 10 ISR - Called when a sequence of conversions (A7-A0) have completed
  136. #pragma vector=ADC10_VECTOR
  137. __interrupt void ADC10_ISR(void) {
  138.  
  139. }
  140. */
  141.  
  142.  
  143. // USCI Transmit ISR - Called when TXBUF is empty (ready to accept another character)
  144. #pragma vector=USCIAB0TX_VECTOR
  145. __interrupt void USCI0TX_ISR(void) {
  146.  
  147.     if(IFG2&UCA0TXIFG) {        // USCI_A0 requested TX interrupt
  148.         if(printf_flag) {
  149.             if (currentindex == txcount) {
  150.                 senddone = 1;
  151.                 printf_flag = 0;
  152.                 IFG2 &= ~UCA0TXIFG;
  153.             } else {
  154.                 UCA0TXBUF = printbuff[currentindex];
  155.                 currentindex++;
  156.             }
  157.         } else if(UART_flag) {
  158.             if(!donesending) {
  159.                 UCA0TXBUF = txbuff[txindex];
  160.                 if(txbuff[txindex] == 255) {
  161.                     donesending = 1;
  162.                     txindex = 0;
  163.                 }
  164.                 else txindex++;
  165.             }
  166.         } else {  // interrupt after sendchar call so just set senddone flag since only one char is sent
  167.             senddone = 1;
  168.         }
  169.  
  170.         IFG2 &= ~UCA0TXIFG;
  171.     }
  172.  
  173.     if(IFG2&UCB0TXIFG) {    // USCI_B0 requested TX interrupt (UCB0TXBUF is empty)
  174.  
  175.         IFG2 &= ~UCB0TXIFG;   // clear IFG
  176.     }
  177. }
  178.  
  179.  
  180. // USCI Receive ISR - Called when shift register has been transferred to RXBUF
  181. // Indicates completion of TX/RX operation
  182. #pragma vector=USCIAB0RX_VECTOR
  183. __interrupt void USCI0RX_ISR(void) {
  184.  
  185.     if(IFG2&UCB0RXIFG) {  // USCI_B0 requested RX interrupt (UCB0RXBUF is full)
  186.  
  187.         IFG2 &= ~UCB0RXIFG;   // clear IFG
  188.     }
  189.  
  190.     if(IFG2&UCA0RXIFG) {  // USCI_A0 requested RX interrupt (UCA0RXBUF is full)
  191.  
  192. //    Uncomment this block of code if you would like to use this COM protocol that uses 253 as STARTCHAR and 255 as STOPCHAR
  193. /*      if(!started) {  // Haven't started a message yet
  194.             if(UCA0RXBUF == 253) {
  195.                 started = 1;
  196.                 newmsg = 0;
  197.             }
  198.         }
  199.         else {  // In process of receiving a message       
  200.             if((UCA0RXBUF != 255) && (msgindex < (MAX_NUM_FLOATS*5))) {
  201.                 rxbuff[msgindex] = UCA0RXBUF;
  202.  
  203.                 msgindex++;
  204.             } else {    // Stop char received or too much data received
  205.                 if(UCA0RXBUF == 255) {  // Message completed
  206.                     newmsg = 1;
  207.                     rxbuff[msgindex] = 255; // "Null"-terminate the array
  208.                 }
  209.                 started = 0;
  210.                 msgindex = 0;
  211.             }
  212.         }
  213. */
  214.  
  215.         IFG2 &= ~UCA0RXIFG;
  216.     }
  217.  
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement