#define F_CPU 8000000 #include #include #include #include "fanbus.h" //A bit frame is 640 microseconds //A long bit is 540 microseconds with 50us on each side //A short is 160 microseconds, 50us before, 430 after //If outlet > 0, set that outlet to state volatile char outlet = 0; volatile char state = 0; //Generate a long bit void bit_long() { PORTD |= 0b00001000; _delay_us(1700); PORTD &= ~0b00001000; _delay_us(900); } //Generate a short bit void bit_short() { PORTD |= 0b00001000; _delay_us(500); PORTD &= ~0b00001000; _delay_us(2100); } //Send channel b preamble void ch_b_preamble() { bit_short(); bit_long(); bit_long(); bit_short(); bit_long(); bit_short(); } //Send channel b end-of-frame void ch_b_end() { bit_short(); bit_long(); bit_short(); bit_short(); } //Data for Outlet 1 on/off void ch_b_out_1_on() { bit_short(); bit_short(); bit_long(); bit_short(); bit_short(); bit_short(); } void ch_b_out_1_off() { bit_short(); bit_short(); bit_short(); bit_long(); bit_short(); bit_short(); } //Data for Outlet 2 on/off void ch_b_out_2_on() { bit_short(); bit_short(); bit_short(); bit_short(); bit_long(); bit_short(); } void ch_b_out_2_off() { bit_short(); bit_short(); bit_short(); bit_short(); bit_short(); bit_long(); } //Data for Outlet 3 on/off void ch_b_out_3_on() { bit_short(); bit_long(); bit_short(); bit_short(); bit_short(); bit_short(); } void ch_b_out_3_off() { bit_long(); bit_short(); bit_short(); bit_short(); bit_short(); bit_short(); } int main() { DDRD |= 0b00001000; PORTD |= 0b00001000; fanbus_init(12); while(1) { if(outlet == 1 && state == 1) { cli(); for(char i = 0; i < 10; i++) { ch_b_preamble(); ch_b_out_1_on(); ch_b_end(); _delay_ms(10); } sei(); state = 0; outlet = 0; } if(outlet == 1 && state == 0) { cli(); for(char i = 0; i < 10; i++) { ch_b_preamble(); ch_b_out_1_off(); ch_b_end(); _delay_ms(10); } sei(); state = 0; outlet = 0; } if(outlet == 2 && state == 1) { cli(); for(char i = 0; i < 10; i++) { ch_b_preamble(); ch_b_out_2_on(); ch_b_end(); _delay_ms(10); } sei(); state = 0; outlet = 0; } if(outlet == 2 && state == 0) { cli(); for(char i = 0; i < 10; i++) { ch_b_preamble(); ch_b_out_2_off(); ch_b_end(); _delay_ms(10); } sei(); state = 0; outlet = 0; } if(outlet == 3 && state == 1) { cli(); for(char i = 0; i < 10; i++) { ch_b_preamble(); ch_b_out_3_on(); ch_b_end(); _delay_ms(10); } sei(); state = 0; outlet = 0; } if(outlet == 3 && state == 0) { cli(); for(char i = 0; i < 10; i++) { ch_b_preamble(); ch_b_out_3_off(); ch_b_end(); _delay_ms(10); } sei(); state = 0; outlet = 0; } } return 0; } /* bit_long(); bit_long(); bit_short(); bit_short(); bit_short(); bit_long(); bit_short(); bit_long(); bit_short(); bit_long(); bit_short(); bit_long(); bit_short(); bit_long(); bit_short(); bit_short(); bit_short(); bit_short(); bit_short(); bit_short(); bit_short(); bit_short(); bit_short(); bit_short(); bit_long();*/