#include #include #include #define ENABLE_1 0x04 #define ENABLE_2 0x10 #define OUTA_1 0x04 #define OUTA_2 0x08 #define OUTB_1 0x01 #define OUTB_2 0x02 #define LED 0x20 #define RFIN 0x40 #define interrupt(x) void __attribute__((interrupt (x))) int8_t bits = -1; uint32_t command = 0; uint16_t begin = 0; uint16_t timer_overflow = 0; volatile uint16_t debug[8]; // signal: // preamble = 1 for 1000us // 0 = short pulse = 200us 0, 600us 1 // 1 = long pulse = 600us 0, 200us 1 // interrupt(PORT1_VECTOR) port1_isr() { uint16_t now, diff; if (P1IFG & RFIN) { now = TAR; diff = (timer_overflow == 0) ? (now - begin) : (timer_overflow == 1) ? (65535 - begin + now) : 0xFFFF; if ((P1IES & RFIN) > 0) { // rising if (bits == -1) { // possible beginning of preamble, do nothing } else { command <<= 1; if (100 < diff && diff < 300) { // 0 bit } else if (500 < diff && diff < 700) { // 1 bit command |= 1; } else { //debug[3] = (debug[3] > bits) ? debug[3] : bits; //debug[4]++; // error bits = -1; } } } else { // falling if (bits == -1) { if (diff > 2000) { // end of preamble command = 0; bits = 0; } } else { bits++; if ((100 < diff && diff < 300) && (command & 1)) { // 1 bit } else if ((500 < diff && diff < 700) && !(command & 1)) { // 0 bit } else { //debug[1] = (debug[1] > bits) ? debug[1] : bits; //debug[2]++; // error bits = -1; } if (bits >= 24) { debug[0] = command; debug[1] = command >> 16; bits = -1; P2OUT ^= LED; } } } P1IFG &= ~RFIN; P1IES ^= RFIN; timer_overflow = 0; begin = now; } } interrupt(TIMER0_A1_VECTOR) ta1_isr() { if (TACTL & TAIFG) { timer_overflow++; TACTL &= ~TAIFG; //if (timer_overflow >= 2) // P2OUT &= ~LED; } } void delay_ms(uint16_t ms) { while (ms--) __delay_cycles(1000); } void stop() { //TACCTL1 = OUTMOD_0 | OUT; //TACCTL2 = OUTMOD_0 | OUT; //P1OUT &= ~(ENABLE_1 | ENABLE_2); P2OUT &= ~(OUTA_1 | OUTA_2 | OUTB_1 | OUTB_2); } void forward() { //TACCTL1 = OUTMOD_3; //TACCTL2 = OUTMOD_3; //P1OUT |= ENABLE_1 | ENABLE_2; P2OUT |= OUTA_1 | OUTB_2; } void right() { //TACCTL1 = OUTMOD_3; //TACCTL2 = OUTMOD_3; //P1OUT |= ENABLE_1 | ENABLE_2; P2OUT |= OUTA_1 | OUTB_1; } void left() { //TACCTL1 = OUTMOD_3; //TACCTL2 = OUTMOD_3; //P1OUT |= ENABLE_1 | ENABLE_2; P2OUT |= OUTA_2 | OUTB_2; } int main() { // Disable watchdog WDTCTL = WDTPW | WDTHOLD; // 1MHz clock BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; P1SEL = 0; //ENABLE_1 | ENABLE_2; P1SEL2 = 0; P1DIR = 0xFF & ~RFIN; P1OUT = 0; P1IES = 0; // 0 = rising, 1 = falling P1IE |= RFIN; P2SEL = 0; P2SEL2 = 0; P2DIR = 0xFF; P2OUT = 0x00; P2OUT |= LED; delay_ms(1000); P1OUT = ENABLE_1 | ENABLE_2; // PWM //TACCR0 = 100 - 1; //TACCR1 = 50; //TACCR2 = 50; TACTL = TASSEL_2 | MC_2 | TAIE; __enable_interrupt(); //while(1) ; while (1) { //P2OUT ^= LED; forward(); delay_ms(1000); stop(); delay_ms(500); right(); delay_ms(500); stop(); left(); delay_ms(100); stop(); delay_ms(500); } /*int times, i; while (1) { for (times = 1; times < 10; times++) { for (i = 0; i < times; i++) { P2OUT |= LED; delay_ms(100); P2OUT &= ~LED; delay_ms(100); } delay_ms(100); forward(); delay_ms(500); stop(); delay_ms(100); right(); delay_ms(200); stop(); left(); delay_ms(50); stop(); delay_ms(500); } }*/ }