From 678adc33f84beb91dbc44dd38729df155d880003 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 1 Nov 2013 00:11:49 +0200 Subject: [PATCH] Use pin change interrupt to react faster to the clock --- s88w/s88w-r.c | 109 ++++++++++++++++++++++++++------------------------ 1 file changed, 57 insertions(+), 52 deletions(-) diff --git a/s88w/s88w-r.c b/s88w/s88w-r.c index 15365b4..1716532 100644 --- a/s88w/s88w-r.c +++ b/s88w/s88w-r.c @@ -37,26 +37,27 @@ 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 out_offset = 0; +uint8_t out_bits = 0; +uint8_t out_fill = 0; +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 +66,20 @@ 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 = reset_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; - } + latch[i] = input[i]; + if(++i>=sizeof(input)) + i = 0xFF; + reset_pos = i; } - else if(clock_high) - clock_high = 0; - if(d_pins&BIT(RESET)) - { - uint8_t i; - for(i=0; i<128; ++i) - latch[i] = input[i]; - } - - if(b_pins&BIT(LCD_DISABLE)) + if(PINB&BIT(LCD_DISABLE)) { if(lcd_enabled) { @@ -222,3 +190,40 @@ uint8_t decode_hex(uint8_t c) else return 0; } + +ISR(PCINT2_vect) +{ + uint8_t d = PIND; + if(d&BIT(CLOCK)) + { + if(!clock_high) + { + if(d&BIT(LOAD)) + { + out_offset = 0; + out_bits = latch[0]; + out_fill = 8; + } + + if(out_bits&1) + PORTD |= BIT(DATA_OUT); + else + PORTD &= ~BIT(DATA_OUT); + + out_bits >>= 1; + if(!--out_fill) + { + ++out_offset; + out_bits = latch[out_offset]; + out_fill = 8; + } + + clock_high = 1; + } + } + else + clock_high = 0; + + if(d&BIT(RESET)) + reset_pos = 0; +} -- 2.45.2