Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

SPI slave IRQ doesn't work

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
Rahalph

Bascom Member



Joined: 01 Mar 2014
Posts: 36

PostPosted: Wed Oct 08, 2014 7:56 pm    Post subject: SPI slave IRQ doesn't work Reply with quote

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
 

Code:
 


(BASCOM-AVR version : 2.0.7.7 )
Back to top
View user's profile
laborratte

Bascom Expert



Joined: 27 Jul 2005
Posts: 299
Location: Berlin

germany.gif
PostPosted: Thu Oct 09, 2014 1:17 pm    Post subject: Reply with quote

a) you need to use the /SS pin for INT-driven SPI slave (see Datasheet)
b) you should not use "NOSAVE" if you plan to use BASCOM-Statements in your ISR. You can never be shure what registers your BASCOM statements are using and that the register usage will remain the same after a BASCOM update or when changing the controller type.
c) you should not use BIT vars in isr for flags, use BYTE instead. Otherwise you need to encapsulate every use of every BITVAR in main loop like
Code:
disable interrupts
if BITVAR = 1 then
  BITVAR = 0
end if
enable interrupts
 

because other BIT vars can be corrupted by the isr.
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum