#ifdef ATMEGA32 #include #elif defined(AT90S8515) #include <90S8515.h> #define SS 4 #define MOSI 5 #define SCK 7 #elif defined(ATMEGA8) #include #define SS 2 #define MOSI 3 #define SCK 5 #elif defined(ATMEGA48) #include #define SS 2 #define MOSI 3 #define SCK 5 #endif #include #include "led_driver.h" void led_init(void) { //CPOL=0,CPHA=0 -> Sample on rising edge, standard phase. #ifdef ATMEGA32 SPCR=0b01010001;//Enable SPI, MSB first, master mode, 16 MHz/16 = 1 MHz clock. #elif defined(AT90S8515)||defined(ATMEGA8)||defined(ATMEGA48) SPCR=0b01010000;//Enable SPI, MSB first, master mode, 4 MHz/4 = 1 MHz clock. #endif //FIXME Flicker disappears when these lines are removed! DDRB.SS=1;//SS (LE). DDRB.MOSI=1;//MOSI. DDRB.SCK=1;//SCK. PORTB.SS=1;//Data latched on LE low (keep high). } void led_set(unsigned int on) { unsigned char temp; PORTB.SS=1;//Data register transparent. SPDR=(on>>8)&0xFF;//MSB while(!(SPSR&0x80));//Wait for transfer complete. temp=SPDR; SPDR=on&0xFF;//LSB while(!(SPSR&0x80));//Wait for transfer complete. temp=SPDR;//Access SPDR to clear second SPIF flag. PORTB.SS=0;//Latch register. }