X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=s88w%2Fs88w-r.c;h=f94a1be0e0e1038f128c844d5219270d0d6694a8;hb=e828083600fc8033aeb1768acabe98461347b906;hp=15365b43500d117d7a714b6f4cc383b8948575e6;hpb=5fdf476c101b0a3a414519798eb72fcc13825167;p=model-railway-devices.git diff --git a/s88w/s88w-r.c b/s88w/s88w-r.c index 15365b4..f94a1be 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,29 @@ int main() while(1) { - uint8_t b_pins, d_pins; + uint8_t i; check_input(); - b_pins = PINB; - d_pins = PIND; - - if(d_pins&BIT(CLOCK)) + 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) { @@ -166,7 +145,7 @@ void check_input() 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; +}