#ifndef F_CPU
#define F_CPU 16000000UL //16 Mhz clock speeed
#endif
#include <avr/io.h>
#include <util/delay.h>
/*DEFINE SPI PINS */
void spi_init_master(void)
{
DDRB = (1<<5) | (1<<3); //mosi,sck output
// Enable SPI, Set as Master
// Prescaler: Fosc/16, Enable Interrupts
SPCR = (1<<SPIE) | (1<<SPE) | (1<<MSTR)|(1<<SPR0);
}
unsigned char spi_transceiver(unsigned char dataout)
{
unsigned char datain;
//LOAD DATA in the buffer
SPDR=dataout;
//wait until transmission complete
while(!((SPSR)&(1<<SPIF)))
//return received data
datain=SPDR;
return datain;
}
int main(void)
{
unsigned char cnt;
DDRD=0xFF;
PORTD=0x00;
spi_init_master();
cnt = spi_transceiver(0);
for(;;) {
cnt=1;
while(cnt) {
cnt=cnt<<1;
PORTD=spi_transceiver(cnt);
_delay_ms(100);
}
cnt=0x80;
while(cnt) {
cnt=cnt>>1;
PORTD=spi_transceiver(cnt);
_delay_ms(100);
}
}
return 0;
}
If somebody could help me read this module or can say what i'm doing wrong thanks