Rahalph
Joined: 01 Mar 2014 Posts: 36
|
Posted: Wed Oct 08, 2014 7:56 pm Post subject: SPI slave IRQ doesn't work |
|
|
Hello everybody,
I want to connect 2 atMega168 using SPI. I've connected GND and PB3..5 pins (MOSI/MOSI/SCK) directly (no cross over!).
I took the sample code for the slave, but the slave will never receive any data. The ISR is never triggered.
Anybody help, please?
Thanks
Ralph
Code for the master:
Code: |
$regfile = "M168DEF.DAT" ' atMEGA168
$crystal = 8000000 ' 8 MHz Takt
Config Portb.3 = Output ' MOSI
Config Portb.4 = Input ' MISO
Config Portb.5 = Output ' SCK (8MHz / 128 = 62.5kHz)
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 128 , Noss = 1
Spiinit
Dim A(1) As Byte
A(1) = 42
Do
Wait 1
Spiout A(1) , 1
Loop
End
|
Code for the slave:
Code: |
$regfile = "M168DEF.DAT" ' atMEGA168
$crystal = 8000000 ' 8 MHz Takt
$baud = 115200
Config Pinb.3 = Input ' MOSI
Config Pinb.4 = Output ' MISO
Config Pinb.5 = Input ' SCK
Config Spi = Hard , Interrupt = On , Data Order = Msb , Master = No , Polarity = Low , Phase = 0 , Clockrate = 128
Spiinit
On Spi Spi_isr Nosave ' specify the SPI interrupt
Enable Spi ' enable spi interrupt
Enable Interrupts ' enable global interrupts
Dim B As Byte , Rbit As Bit , Bsend As Byte
Print "start" ' show that we started
Spdr = 0 ' start with sending 0 the first time
Do
If Rbit = 1 Then ' when data received
Print B ' print it
Reset Rbit ' wait for more data
Bsend = Bsend + 1
Spdr = Bsend ' increase SPDR
End If
Loop
' Interrupt routine
Spi_isr:
push r24 ' save used register
in r24, sreg ' save SREG
push r24
B = Spdr ' Load data
Set Rbit ' we received something
pop r24
!out sreg, r24 ' restore SREG
pop r24 ' and the used register
Return
|
(BASCOM-AVR version : 2.0.7.7 ) |
|