Guest User

Untitled

a guest
Jul 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. #include "msp430g2231.h"
  2. #include "stdbool.h"
  3.  
  4. #define TXD BIT1 // TXD on P1.1
  5. #define RXD BIT2 // RXD on P1.2
  6. #define REC BIT3 // start recording
  7. #define Bit_time 104 // (1MHz/9600)=104
  8. #define Bit_time_5 52 // Time for half a bit.
  9. #define SAMPLES 100 // MAX
  10.  
  11. unsigned int BitCnt; // Bit count, used when transmitting byte
  12. unsigned int TXByte; // Value sent over UART when Transmit() is called
  13. unsigned int RXByte; // Value recieved once hasRecieved is set
  14.  
  15. bool isReceiving = false; // Status for when the device is receiving
  16. bool isRecording = false;
  17. bool hasReceived = false; // Lets the program know when a byte is received
  18.  
  19. bool MSG[SAMPLES] = {0};
  20. bool CARD[SAMPLES] = {0};
  21. bool CARD2[SAMPLES] = {8};
  22.  
  23. void ClearMSG(void);
  24. bool Verify(bool DATA[]);
  25.  
  26. void main(void)
  27. {
  28. WDTCTL = WDTPW + WDTHOLD; // Stop WDT
  29.  
  30. BCSCTL1 = CALBC1_1MHZ; // Set range
  31. DCOCTL = CALDCO_1MHZ; // SMCLK = DCO = 1MHz
  32.  
  33. P1SEL |= TXD;
  34. P1DIR |= TXD;
  35.  
  36. P1IES |= RXD; // RXD Hi/lo edge interrupt
  37. P1IFG &= ~RXD; // Clear RXD (flag) before enabling interrupt
  38. P1IE |= RXD; // Enable RXD interrupt
  39.  
  40. P1IFG &= ~REC; // Clear RXD (flag) before enabling interrupt
  41. P1IE |= REC; // Enable RXD interrupt
  42.  
  43. __bis_SR_register(GIE); // interrupts enabled
  44. while(1)
  45. {
  46. if (hasReceived) // If the device has recieved a value
  47. {
  48. if (Verify(CARD) == true){
  49. P1OUT |= BIT0;
  50. }else{
  51. P1OUT &= ~BIT0;
  52. }
  53.  
  54. hasReceived = false; // Clear the flag
  55. }
  56.  
  57. if (~hasReceived) // Loop again if another value has been received
  58. __bis_SR_register(CPUOFF + GIE);
  59. }
  60. }
  61.  
  62. // Port 1 interrupt service routine
  63. #pragma vector=PORT1_VECTOR
  64. __interrupt void Port_1(void) // start recording
  65. {
  66. isReceiving = true;
  67. if ((P1IN & REC) == REC){
  68. isRecording = true;
  69. }
  70.  
  71. P1IE &= ~RXD; // Disable RXD interrupt
  72. P1IFG &= ~RXD; // Clear RXD IFG (interrupt flag)
  73. P1IE &= ~REC; // Disable RXD interrupt
  74. P1IFG &= ~REC; // Clear RXD IFG (interrupt flag)
  75.  
  76. TACTL = TASSEL_2 + MC_2; // SMCLK, continuous mode
  77. CCR0 = TAR; // Initialize compare register
  78. CCR0 += Bit_time_5; // Set time till first bit
  79. CCTL0 = OUTMOD1 + CCIE; // Dissable TX and enable interrupts
  80.  
  81. ClearMSG();
  82. BitCnt = SAMPLES - 1; // Load Bit counter, 8 bits + ST
  83. }
  84.  
  85. // Timer A0 interrupt service routine
  86. #pragma vector=TIMERA0_VECTOR
  87. __interrupt void Timer_A (void)
  88. {
  89. if(isReceiving) // receiving
  90. {
  91. CCR0 += Bit_time; // Add Offset to CCR0
  92.  
  93. if ( BitCnt == SAMPLES - 1) //last bit
  94. {
  95. TACTL = TASSEL_2; // SMCLK, timer off (for power consumption)
  96. CCTL0 &= ~ CCIE ; // Disable interrupt
  97.  
  98. isReceiving = false;
  99.  
  100. P1IFG &= ~RXD; // clear RXD IFG (interrupt flag)
  101. P1IE |= RXD; // enabled RXD interrupt
  102.  
  103. if (isRecording){ //dump
  104. int i;
  105. for (i=0; i<SAMPLES; i++){
  106. CARD[i] = MSG[i];
  107. MSG[i] = 0;
  108. }
  109. isRecording = false;
  110. }
  111.  
  112. hasReceived = true;
  113.  
  114. __bic_SR_register_on_exit(CPUOFF); // Enable CPU so the main while loop continues
  115. }
  116. else {
  117. if ( (P1IN & RXD) == RXD) // If bit is set?
  118. MSG[BitCnt] = 1;
  119. BitCnt ++;
  120. }
  121. }
  122. else //done receiving, turn off
  123. {
  124. CCR0 += Bit_time; // Add Offset to CCR0
  125. if ( BitCnt == SAMPLES - 1) // If all bits TXed
  126. {
  127. TACTL = TASSEL_2; // SMCLK, timer off (for power consumption)
  128. CCTL0 &= ~ CCIE ; // Disable interrupt
  129. }
  130. }
  131. }
  132.  
  133. void ClearMSG(){
  134. int i;
  135. for (i=0; i<SAMPLES; i++){
  136. MSG[i] = 0;
  137. }
  138. }
  139.  
  140. bool Verify(bool *DATA){
  141. int i;
  142. for (i=0; i<SAMPLES; i++){
  143. if (MSG[i] != DATA[i])
  144. return false;
  145. }
  146. return true;
  147. }
Add Comment
Please, Sign In to add comment