Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. //-------------------------------------------------------------------------------
  2. // Example demo C code for ECE 3362 Micrcontrollers class
  3. // Provides simple example of using both Timers with Interrupts in C code
  4. //
  5. // Function: Sits in an IDLE LOOP and the LED's are controlled
  6. // using the two timers independently. Timing of the two LED's is
  7. // independent and can be controlled by changing the timer parameters, or
  8. // by changing the MAX_COUNT values for each of the LED's
  9. //
  10. // PB interrupt on Port 1 is set up, but only used to increment global
  11. // variable Count. Could be used to control mode of operation - that is
  12. // left as an exercise for the student
  13. //
  14. //
  15. //
  16. // Target: TI LaunchPad development board with MSP430G2553 device
  17. //
  18. // Date: Nov 13, 2015
  19. // Last Revision: 1.0
  20. // Written by: Dr. Michael Helm, ECE dept, Texas Tech University
  21. // Adapted from: TI example code, non-specific
  22. // Assembler/IDE: IAR Embedded Workbench 5.5
  23. //
  24. // HW I/O assignments:
  25. // P1.0 LED1 (Active HIGH)RED
  26. // P1.1 not used
  27. // P1.2 not used
  28. // P1.3 PushButton (Active LOW) (internal Pullup Enabled)
  29. // P1.4 not used
  30. // P1.5 not used
  31. // P1.6 LED2 (Active HIGH)GREEN
  32. // P1.7 not used
  33. //
  34. // P2.0 not used
  35. // P2.1 not used
  36. // P2.2 not used
  37. // P2.3 not used
  38. // P2.4 not used
  39. // P2.5 not used
  40. // P2.6 not used
  41. // P2.7 not used
  42.  
  43. #include <msp430g2553.h>
  44.  
  45. //----------------------------------------------------------------------
  46. // CONSTANT DEFINITIONS
  47.  
  48. #define MAX_RED_COUNT 2 // max number of Timer0 interrupts
  49. #define MAX_GRN_COUNT 10 // max number of Timer1 interrupts
  50.  
  51.  
  52. #define MAX_TA0_COUNT 5000 // max count for Timer0
  53. #define MAX_TA1_COUNT 8000 // max count for Timer1
  54.  
  55. #define LED1RED (0x01u) //(%00000001) //Port pin position P1.0
  56. #define LED2GRN (0x40u)//(%01000000) //Port pin position P1.6
  57.  
  58. #define PUSHBUTTON (0x08u) //(%00001000) //Port pin position P1.3
  59.  
  60. //----------------------------------------------------------------------
  61. // GLOBAL VARIABLE definition
  62. int Count = 0;
  63. int CountRED = 0;
  64. int CountGRN = 0;
  65. int CurrentDigitPos = 0;
  66. int CurrentDigitValue = 0;
  67. int DisplayValue = 0;
  68. int DispCopy = 0;
  69. int SegPatternTable[14] = {0xC0u, 0xF9u, 0xA4u, 0xB0u, 0x99u, 0x92u, 0xF8u, 0x80u, 0x98u, 0x86u, 0xC6u, 0xBFu, 0xFFu};
  70.  
  71. //----------------------------------------------------------------------
  72. // FUNCTIONs
  73. // delay_1 - delays by an amount proportional to the argument passed in
  74. // returns- nothing
  75. //----------------------------------------------------------------------
  76. void delay_1(int DelayTime)
  77. {
  78. for(int i = 0;i < DelayTime;i++);
  79. }; // end delay_1 function
  80.  
  81. //----------------------------------------------------------------------
  82. //WriteNextDigitToDisplay
  83. // accomplishes - Writes next digit to the expansion bd display
  84. // uses: R15, global variable CurrentDigitPos, CurrentDigitValue, DisplayValue, DispCopy
  85. //----------------------------------------------------------------------
  86.  
  87.  
  88. void WriteNextDigitToDisplay(void)
  89. {
  90. P2OUT = 31; //set digport to turn LED's off to fix ghosting
  91.  
  92. if (CurrentDigitPos == 0)
  93. {
  94. DispCopy = DisplayValue;
  95. DispCopy &= 0x000Fu;
  96. P1OUT = SegPatternTable[DispCopy];
  97. P2OUT = 0x1u;
  98. }
  99. if (CurrentDigitPos == 1)
  100. {
  101. DispCopy = DisplayValue;
  102. DispCopy &= 0x00F0u;
  103. DispCopy >>= 4;
  104. P1OUT = SegPatternTable[DispCopy];
  105. P2OUT = 0x02u;
  106. }
  107. if (CurrentDigitPos == 2)
  108. {
  109. DispCopy = DisplayValue;
  110. DispCopy &= 0x0F00u;
  111. DispCopy >>= 8;
  112. P1OUT = SegPatternTable[DispCopy];
  113. P2OUT = 0x04u;
  114. }
  115. if (CurrentDigitPos == 3)
  116. {
  117. DispCopy = DisplayValue;
  118. DispCopy &= 0xF000u;
  119. DispCopy >>= 12;
  120. P1OUT = SegPatternTable[DispCopy];
  121. P2OUT = 0x08u;
  122. }
  123.  
  124. CurrentDigitPos++; // set up for next digit position for next time
  125. if(CurrentDigitPos==4)
  126. CurrentDigitPos=0;
  127.  
  128. }; // end WriteNextDigitToDisplay
  129. //HowtoCALL
  130. //WriteNextDigitToDisplay(); // write the next digit
  131. // none used here
  132.  
  133. // MAIN PROGRAM
  134. void main( void )
  135. {
  136. // Stop watchdog timer to prevent time out reset
  137. WDTCTL = WDTPW + WDTHOLD; // Stop WDT
  138. P1DIR = 0xFFu; // both LEDs
  139. P2DIR = 0x1Fu;
  140. P2OUT = 0xE0;
  141. P2SEL &= 0xC0;
  142. P2REN |= 0xC0; // resistor enable for PB position
  143. P1OUT = 0XFF;
  144. P2OUT = 0XFF;
  145. P2IE |= 0xC0; // PB interrupt enabled
  146. P2IES |= 0xC0; // PB Hi/lo edge
  147. P2IFG = 0; // clear ALL the Int Flags on Port 1
  148. // end if
  149. // end while
  150.  
  151. // Set up Timers
  152. // TimerA0
  153. TA0CCR0 = MAX_TA0_COUNT; // load a count "up to"value into timer
  154. TA0CTL = TASSEL_2+ID_3 + MC_1; // SMCLK, input div = 8, upmode
  155. TA0CCTL0 = CCIE; // interrupt enabled for Timer0
  156. // TimerA1
  157. TA1CCR0 = MAX_TA1_COUNT; // load a count "up to"value into timer
  158. TA1CTL = TASSEL_2+ID_3 + MC_1; // SMCLK, input div = 8, upmode
  159. TA1CCTL0 = CCIE; // interrupt enabled for Timer1
  160.  
  161. Count = 0;
  162. CountRED = 0; // initialize CountRED to zero
  163. CountGRN = 0; // initialize CountGRN to zero
  164. _BIS_SR(GIE); // enable general interrupts
  165.  
  166. while(1){ // forever idle loop - Timer interrupts control the action
  167.  
  168. WriteNextDigitToDisplay();
  169.  
  170. }
  171.  
  172. } // end of MAIN
  173. //ISR's
  174. //Timer0_A0 ISR
  175. #pragma vector=TIMER0_A0_VECTOR // this line tells the C compiler to put
  176. // the start address of the following ISR
  177. // into the Interupt Vector table
  178.  
  179. __interrupt void Timer_A0_ISR (void) // required syntax for first line of ISR
  180. { WriteNextDigitToDisplay();
  181.  
  182. }
  183. //ISR's
  184. //Timer0_A1 ISR
  185. #pragma vector=TIMER1_A0_VECTOR // this line tells the C compiler to put
  186. // the start address of the following ISR
  187. // into the Interupt Vector table
  188. __interrupt void Timer_A1_ISR (void) // required syntax for first line of ISR
  189. {
  190.  
  191.  
  192.  
  193.  
  194. }
  195. // Port 1 interrupt service routine
  196. #pragma vector=PORT1_VECTOR
  197. __interrupt void Port_2(void)
  198. {
  199. // Keep track of number of times PB is pressed
  200. P1IFG = 0; // clear ALL the Int Flag bits on Port 1
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement