Hi Freaks!
I have a simple test program for SPI.
On the device - It doesn't work at all - no port B outputs, when monitored with a scope
On the Studio simulator, when SPIF is set after the appropriate number of clocks (and you can see the data being clocked out of port B), SPSR is set to 0x80, but the lds r16, SPSR line does not change r16 from 0x00 - so it sits in the wait_spi_transmit loop for ever
What gives?
.include "m168def.inc" ;ATmega168 definitions .equ MOSI = 3 ;port B3 .equ SCK = 5 ;port B5 .equ SS = 2 ;port B2 .cseg .org 0x0000 jmp INTH_RESET INTH_RESET: ;stack pointer initialisation ldi r16,high(RAMEND) out SPH,r16 ldi r16,low(RAMEND) out SPL,r16 ;Set up port B spi lines as outputs ldi r16, (1 << SS) | (1 << SCK) | (1 << MOSI) out DDRB, r16 ;set SS high (as it is active low) sbi PortB, SS ;Enable SPI, set clock rate fck/4 ldi r16, (1 << SPE ) | (1 << MSTR ) out SPCR, r16 loop: ;send 0x09 to spi device ldi r16, 0x09 rcall spi_transmit rjmp loop ;spi transmit******************* spi_transmit: ; cbi PortB, SS ;low SS out SPDR, r16 ;output r16 to the SPI data register wait_spi_transmit: lds r16, SPSR ;wait on SPI status register SPIF flag sbrs r16, SPIF rjmp wait_spi_transmit sbi PortB, SS ;high SS ret