I needed to use a couple of HC595 on a Tiny2313 project. Multiple HC595s are cascaded together.
I created the a simple function based on the asm in the data sheets and post it here for others to use.
/* Connecting the HC595 to the Tiny2313 Tiny2313 >> HC595 SCK PB7 >> SCK, Pin 11 MISO PB6 >> SDI, Pin 14 Latch PB4 >> RCK, Pin 12 OP_Enb PB3 >> OPEnb,Pin 13 (active low) +5v >> Reset,Pin 10 MOSI PB5 >> No Connection */ #define PIN_OE PB3 #define PIN_SS PB4 #define PIN_MOSI PB5 #define PIN_MISO PB6 #define PIN_SCK PB7 #define HC595s 2 // number of HC595s in Serial register /* Global Prototypes */ void send_spi_fast(int data);
The outputs of the HC595 can come up in an unknown state, so first we need to flush all 0s ( or 1s )
the code also shows how the function is called.
/* Flush unknown output state of HC595 */ for (l=0;l
This is the actual function implemented - it uses the fast method Fck/2
void send_spi_fast(int data) { int i,j, USICRL, USICRH; USICRL = (1<<USIWM0) | (0<<USICS0) | (1<<USITC); USICRH = (1<<USIWM0) | (0<<USICS0) | (1<<USITC) | (1<<USICLK); USIDR = data; // load data to be transferred via SPI for(i=0;i<8;i++) // transmitts 8 bits { USICR = USICRL; USICR = USICRH; } j = USIDR; // put USI into slave mode }
I hope this is useful to others.