#include #include #define RFBIT BIT0 #define ENBIT BIT1 #define SWBIT BIT2 #define interrupt(x) void __attribute__((interrupt (x))) #define PULSE (184) volatile uint16_t debug[20]; void delay_ms(uint16_t ms) { while (ms--) __delay_cycles(1000); } void short_pulse() { P1OUT |= RFBIT; __delay_cycles(PULSE); P1OUT &= ~RFBIT; __delay_cycles(PULSE * 3); } void long_pulse() { P1OUT |= RFBIT; __delay_cycles(PULSE * 3); P1OUT &= ~RFBIT; __delay_cycles(PULSE); } void send_rf(uint16_t data, uint8_t bits, uint8_t twos) { uint16_t bit; for (bit = (1 << (bits - 1)); bit; bit >>= 1) { if (!twos) short_pulse(); if (data & bit) long_pulse(); else short_pulse(); if (twos) long_pulse(); } } void command(uint16_t house, uint16_t unit, uint16_t on) { uint16_t times, times2; //__disable_interrupt(); P1OUT |= ENBIT; delay_ms(100); P1OUT |= RFBIT; delay_ms(1); // __delay_cycles(400); // for (times2 = 0; times2 < 5; times2++) { for (times = 0; times < 20; times++) { send_rf(house, 5, 1); send_rf(unit, 4, 0); send_rf(1, 1, 0); send_rf(((on & 1) << 1) | ((~on) & 1), 2, 0); short_pulse(); P1OUT &= ~RFBIT; delay_ms(6); } P1OUT &= ~RFBIT; // delay_ms(50); // } P1OUT &= ~(RFBIT | ENBIT); //__enable_interrupt(); } int main() { // Disable watchdog WDTCTL = WDTPW | WDTHOLD; // 1MHz internal timer BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; // P2 (XOUT/XIN) as GPIO P2SEL = 0x00; P2SEL2 = 0x00; P1OUT = 0; P1DIR = ~SWBIT; P1IE |= SWBIT; P2DIR = 0xFF; P1OUT |= BIT3; delay_ms(500); P1OUT &= ~BIT3; if (P1IN & SWBIT) P1IES |= SWBIT; command(4 | 8, ~(1 << 2), !!(P1IN & SWBIT)); while (1) { __bis_status_register(LPM3_bits | GIE); } return 0; } interrupt(PORT1_VECTOR) port1_isr() { char last_on, on; on = !(P1IES & SWBIT); debug[0]++; do { debug[1]++; command(4 | 8, ~(1 << 2), on); last_on = on; on = !!(P1IN & SWBIT); } while (on != last_on); if (on) P1IES |= SWBIT; else P1IES &= ~SWBIT; P1IFG = 0; }