// Copyright 2006 Yamane Akira //ICC-AVR application builder : 2005/07/15 0:20:24 // Target : M8 // Crystal: 4.0000Mhz // SPI Sample Program for Master // Master Transmit Data from 0 to 7 on MOSI // Also, Receive Data from Slave(MISO). // Received Data is outputted to PORTD. // Programmed by Akira Yamane #include #include void port_init(void) { PORTB = 0xFF; DDRB = 0x2C; // Set output with SCK, MOSI, SS PORTC = 0x00; DDRC = 0x00; PORTD = 0xFF; DDRD = 0xFF; } //SPI initialize // clock rate: 1000000hz void spi_init(void) { SPCR = 0x50; // (1 << SPE) | (1 << MSTR); SPSR = 0x00; // Set SCLK to fOSC/4. } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); spi_init(); MCUCR = 0x00; GICR = 0x00; TIMSK = 0x00; //timer interrupt sources // SEI(); //re-enable interrupts //all peripherals are now initialized } unsigned char SPImaTx(unsigned char tdata) { SPDR = tdata; while(!(SPSR & (1<