#include "hw.h" #include #include #include #include "hbw.h" static uint16_t crc16shift(uint16_t crc, uint8_t data) { int stat, i; for (i=0; i<8; i++) { stat = crc & 0x8000; crc = crc << 1; if (data & 0x80) crc = crc | 1; if (stat) crc = crc ^ 0x1002; data = data << 1; } return crc; } void hbw_send(uint8_t i,uint8_t crc) { if (bit_is_set(UCSR0A, UDRE0)) { if (sesc) { UDR0 = i & 0x7F; sind++; return; } if (crc == 1) { scrc = crc16shift(scrc, i); } if (crc == 2) { scrc = crc16shift(scrc, 0); scrc = crc16shift(scrc, 0); i = (scrc >> 8) & 0xFF; } if ((i == 0xFD) || (i == 0xFC) || (i == 0xFE)) { UDR0 = 0xFC; sesc = 1; return; } UDR0 = i; sind++; } } void hbw_loop(void) { if (!sind) return; if (sdisc) { if (sind == 1) { hbw_send(0x04,1); return; } if (sind == 2) { hbw_send(0x01,1); return; } if (sind == 3) { hbw_send((scrc >> 8) & 0xFF,2); return; } if (sind == 4) { hbw_send(scrc & 0xFF,0); return; } if (bit_is_set(UCSR0A, TXC0)) { PORTD &= ~(1<> 8) & 0xFF,2); return; } if (sind == slen+12) { hbw_send(scrc & 0xFF,0); return; } if (bit_is_set(UCSR0A, TXC0)) { PORTD &= ~(1< 64)) { rind = 255; return; } if ((rind < rlen + 10)) { rmessage[rind-12] = c; return; } if (rind == rlen + 10) return; if (rind == rlen + 11) { rind = 255; if (rcrc) return; rready = 1; return; } } ISR(TIMER2_COMPA_vect) { cli(); if (rlast < rwant) rlast++; sei(); }