Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "msp430g2231.h"
- #include "stdbool.h"
- #define TXD BIT1 // TXD on P1.1
- #define RXD BIT2 // RXD on P1.2
- #define REC BIT3 // start recording
- #define Bit_time 104 // (1MHz/9600)=104
- #define Bit_time_5 52 // Time for half a bit.
- #define SAMPLES 100 // MAX
- unsigned int BitCnt; // Bit count, used when transmitting byte
- unsigned int TXByte; // Value sent over UART when Transmit() is called
- unsigned int RXByte; // Value recieved once hasRecieved is set
- bool isReceiving = false; // Status for when the device is receiving
- bool isRecording = false;
- bool hasReceived = false; // Lets the program know when a byte is received
- bool MSG[SAMPLES] = {0};
- bool CARD[SAMPLES] = {0};
- bool CARD2[SAMPLES] = {8};
- void ClearMSG(void);
- bool Verify(bool DATA[]);
- void main(void)
- {
- WDTCTL = WDTPW + WDTHOLD; // Stop WDT
- BCSCTL1 = CALBC1_1MHZ; // Set range
- DCOCTL = CALDCO_1MHZ; // SMCLK = DCO = 1MHz
- P1SEL |= TXD;
- P1DIR |= TXD;
- P1IES |= RXD; // RXD Hi/lo edge interrupt
- P1IFG &= ~RXD; // Clear RXD (flag) before enabling interrupt
- P1IE |= RXD; // Enable RXD interrupt
- P1IFG &= ~REC; // Clear RXD (flag) before enabling interrupt
- P1IE |= REC; // Enable RXD interrupt
- __bis_SR_register(GIE); // interrupts enabled
- while(1)
- {
- if (hasReceived) // If the device has recieved a value
- {
- if (Verify(CARD) == true){
- P1OUT |= BIT0;
- }else{
- P1OUT &= ~BIT0;
- }
- hasReceived = false; // Clear the flag
- }
- if (~hasReceived) // Loop again if another value has been received
- __bis_SR_register(CPUOFF + GIE);
- }
- }
- // Port 1 interrupt service routine
- #pragma vector=PORT1_VECTOR
- __interrupt void Port_1(void) // start recording
- {
- isReceiving = true;
- if ((P1IN & REC) == REC){
- isRecording = true;
- }
- P1IE &= ~RXD; // Disable RXD interrupt
- P1IFG &= ~RXD; // Clear RXD IFG (interrupt flag)
- P1IE &= ~REC; // Disable RXD interrupt
- P1IFG &= ~REC; // Clear RXD IFG (interrupt flag)
- TACTL = TASSEL_2 + MC_2; // SMCLK, continuous mode
- CCR0 = TAR; // Initialize compare register
- CCR0 += Bit_time_5; // Set time till first bit
- CCTL0 = OUTMOD1 + CCIE; // Dissable TX and enable interrupts
- ClearMSG();
- BitCnt = SAMPLES - 1; // Load Bit counter, 8 bits + ST
- }
- // Timer A0 interrupt service routine
- #pragma vector=TIMERA0_VECTOR
- __interrupt void Timer_A (void)
- {
- if(isReceiving) // receiving
- {
- CCR0 += Bit_time; // Add Offset to CCR0
- if ( BitCnt == SAMPLES - 1) //last bit
- {
- TACTL = TASSEL_2; // SMCLK, timer off (for power consumption)
- CCTL0 &= ~ CCIE ; // Disable interrupt
- isReceiving = false;
- P1IFG &= ~RXD; // clear RXD IFG (interrupt flag)
- P1IE |= RXD; // enabled RXD interrupt
- if (isRecording){ //dump
- int i;
- for (i=0; i<SAMPLES; i++){
- CARD[i] = MSG[i];
- MSG[i] = 0;
- }
- isRecording = false;
- }
- hasReceived = true;
- __bic_SR_register_on_exit(CPUOFF); // Enable CPU so the main while loop continues
- }
- else {
- if ( (P1IN & RXD) == RXD) // If bit is set?
- MSG[BitCnt] = 1;
- BitCnt ++;
- }
- }
- else //done receiving, turn off
- {
- CCR0 += Bit_time; // Add Offset to CCR0
- if ( BitCnt == SAMPLES - 1) // If all bits TXed
- {
- TACTL = TASSEL_2; // SMCLK, timer off (for power consumption)
- CCTL0 &= ~ CCIE ; // Disable interrupt
- }
- }
- }
- void ClearMSG(){
- int i;
- for (i=0; i<SAMPLES; i++){
- MSG[i] = 0;
- }
- }
- bool Verify(bool *DATA){
- int i;
- for (i=0; i<SAMPLES; i++){
- if (MSG[i] != DATA[i])
- return false;
- }
- return true;
- }
Add Comment
Please, Sign In to add comment