X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=s88w%2Fs88w-r.c;h=b508b4fa053b48cfda0bc3cb03a3b18ff676c2f5;hb=5d6f425971266accc2e3d1c6de96a0ba031adb8d;hp=15365b43500d117d7a714b6f4cc383b8948575e6;hpb=5fdf476c101b0a3a414519798eb72fcc13825167;p=model-railway-devices.git diff --git a/s88w/s88w-r.c b/s88w/s88w-r.c index 15365b4..b508b4f 100644 --- a/s88w/s88w-r.c +++ b/s88w/s88w-r.c @@ -37,26 +37,29 @@ void check_input(); uint8_t hexdigit(uint8_t); uint8_t decode_hex(uint8_t); -volatile uint8_t rx_buf[7]; -volatile uint8_t rx_fill = 0xFF; -volatile uint8_t input[128] = { 0 }; -volatile uint8_t latch[128] = { 0 }; -volatile uint8_t lcd_enabled = 0; +uint8_t clock_high = 0; +uint8_t rx_buf[7]; +uint8_t rx_fill = 0xFF; +uint8_t input[128] = { 0 }; +uint8_t latch[128] = { 0 }; +uint8_t output[128] = { 0 }; +uint8_t out_offset = 0; +uint8_t out_bits = 0; +uint8_t out_fill = 0; +volatile uint8_t load_pos = 0xFF; +volatile uint8_t reset_pos = 0xFF; +uint8_t lcd_enabled = 0; uint8_t log_row = 0; uint8_t log_col = 0; int main() { - uint8_t clock_high = 0; - uint8_t bits = 0; - uint8_t n_bits = 8; - uint8_t offset = 0; - uint8_t i; - DDRD = 0x06; // 00000110 PORTD = 0xC0; // 11000000 DDRB = 0x20; // 00100000 PORTB = 0x1F; // 00011111 + PCMSK2 = BIT(PCINT19) | BIT(PCINT21); + PCICR = BIT(PCIE2); serial_init(9600); lcd_init(); @@ -65,53 +68,27 @@ int main() while(1) { - uint8_t b_pins, d_pins; - check_input(); - b_pins = PINB; - d_pins = PIND; - - if(d_pins&BIT(CLOCK)) + uint8_t i = load_pos; + if(i!=0xFF) { - if(!clock_high) - { - if(d_pins&BIT(LOAD)) - { - offset = 0; - bits = latch[0]; - n_bits = 8; - } - else - { - bits >>= 1; - if(!--n_bits) - { - ++offset; - bits = latch[offset]; - n_bits = 8; - } - } - - if(bits&1) - PORTD |= BIT(DATA_OUT); - else - PORTD &= ~BIT(DATA_OUT); - - clock_high = 1; - } + output[i] = latch[i]; + if(++i>=sizeof(input)) + i = 0xFF; + load_pos = i; } - else if(clock_high) - clock_high = 0; - if(d_pins&BIT(RESET)) + i = reset_pos; + if(i!=0xFF) { - uint8_t i; - for(i=0; i<128; ++i) - latch[i] = input[i]; + latch[i] = input[i]; + if(++i>=sizeof(input)) + i = 0xFF; + reset_pos = i; } - if(b_pins&BIT(LCD_DISABLE)) + if(PINB&BIT(LCD_DISABLE)) { if(lcd_enabled) { @@ -137,11 +114,10 @@ int main() void check_input() { - uint8_t c; if(!serial_read_available()) return; - c = serial_read(); + uint8_t c = serial_read(); if(rx_fill==0xFF) { if(c==':') @@ -151,28 +127,24 @@ void check_input() { if(rx_fill>=4) { - uint16_t offset; - uint8_t nibbles; - uint8_t i; - - offset = (decode_hex(rx_buf[0])<<8) | (decode_hex(rx_buf[1])<<4) | decode_hex(rx_buf[2]); - nibbles = (offset&3); + uint8_t offset = (decode_hex(rx_buf[0])<<8) | (decode_hex(rx_buf[1])<<4) | decode_hex(rx_buf[2]); + uint8_t nibbles = (offset&3); offset >>= 2; if(rx_fill>3+nibbles) { - for(i=0; i<=nibbles; ++i) + for(uint8_t i=0; i<=nibbles; ++i) { uint16_t j = offset+nibbles-i; uint8_t shift = 4*(j&1); uint8_t bits = decode_hex(rx_buf[3+i]); input[j/2] = (input[j/2]&~(0xF<>= 1; + if(!--out_fill) + { + ++out_offset; + out_bits = output[out_offset]; + out_fill = 8; + } + + clock_high = 1; + } + } + else + clock_high = 0; + + if(d&BIT(RESET)) + reset_pos = 0; +}