Ok, so the time has finally come for me to write my first C program which is optimized for as much speed as possible.
I am trying to use an attiny13 driven at 14MHz (but with some hacking on the rest of my hardware I COULD get a 20MHZ signal to it....) to emulate a shift register which is being driven quite fast (about 500ns pulse width).
The master sends out a LATCH pulse to signify that it wants data, followed by 16 CLK pulses. The tiny should present a new bit on the rising edge of each CLK pulse.
Here's my first attempt at code, but my logic analyzer says that the tiny is not responding fast enough....any suggestions as to how I could speed it up would be greatly appreciated!
#include#include #include #include #define LATCH PB2 #define CLK PB1 #define DATA PB4 #define setPinForOutput(pin) (DDRB |= byte(pin)) #define setPin(pin) (PORTB |= byte(pin)) #define clearPin(pin) (PORTB &= ~(byte(pin))) #define testPin(bit) (PINB & (1 << bit)) #define byte(bit) (1 << bit) // byte with bit set int main(void) { int writing; int dataIndex; int keyData[16] = {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; //Some test data writing = 0; dataIndex = 0; //set clock divider to /1 CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); setPinForOutput(DATA); clearPin(DATA); for(;;) { if(testPin(LATCH)) { setPin(DATA); writing = 1; switch(keyData[dataIndex]) { case 0: clearPin(DATA); break; case 1: setPin(DATA); break; } dataIndex++; } if(testPin(CLK) && writing == 1) { switch(keyData[dataIndex]) { case 0: clearPin(DATA); break; case 1: setPin(DATA); break; } dataIndex++; } if (dataIndex >= 16) { //not sure if i have time to read the last button state here. clearPin(DATA); writing = 0; dataIndex = 0; } } return 0; /* never reached */ }