View previous topic :: View next topic |
Author |
Message |
steamydave
Joined: 06 May 2009 Posts: 39
|
Posted: Tue Feb 21, 2017 9:47 pm Post subject: SPI unexpected results - any suggestions please |
|
|
Hi,
I have 2 mega8 chips running without crystal and have linked the spi cables directly from the hardware ports. They also share 5v and 0v. One is a master, one is a slave. For this test the SS lines are directly connected.
sck -> sck
miso -> miso
mosi -> mosi
ss -> ss
Once every 5 seconds, the master transmits "Hello". The slave sits in a NOP loop and is triggered by the SPI interrupt. The slave displays "Ready" until triggered. It displays the content of the spi transmission for 1 second, then clears the lcd display and ends the interrupt routine back to the loop.
This is what happens...
Init.
Slave displays Ready
1. Master Lights the led, sends Hello and holds the led on for 1 second, then switches off the led and wait 5 seconds....... Slave still displays "Ready"
2. Master led on, send Hello wait 1s, led off, wait 5s ....Slave still displays "Ready"
3. Master led on, send Hello wait1s led off, wait 5s... Slave displays "olleH" for 1 second the cleared the message ("-----")
4. As above ... Slave displays "Hello" for 1 second then clears the display
5. Master as above ... slave does not update the display
6. Master as above ... slave displays "olleH" again
And the sequence continues to repeat nothing, olleH, Hello, nothing.....etc
Master Code
Code: |
$regfile = "m8def.dat"
$crystal = 1000000
$hwstack = 32
$swstack = 8
$framesize = 24
config Lcd = 20 * 4
Config Lcdpin = Pin , Rs = PortB.6 , E = PortB.7 , Db4 = PortD.4 , Db5 = PortD.5 , Db6 = PortD.6 , Db7 = PortD.7
Config Portb = &B11100011 'Servos, pwm, switches
'Portb = &B00011100 'Set pull-ups
portb.2=1
portb.3=1
portb.4=1
Config Portc = &B11111111 'LEDs & Lines
Config Portd = &B11111111 ' rxd,txd,int0/1,LCD data 4-7
'Portd = &B00001101 'pull-ups on the track switches
Config Spi = Hard, Interrupt = Off, Data_Order = Msb, Master = Yes, Polarity = high, Phase = 1, Clockrate = 16, Noss = 0
Spiinit
dim s as string * 5
portd=255
s="Hello"
mainloop:
portd.3=0
spiout s,5
wait 1
portd.3=1
wait 5
goto mainloop
end
|
Slave code
Code: |
$regfile = "m8def.dat"
$crystal = 1000000
$hwstack = 32
$swstack = 8
$framesize = 24
config Lcd = 20 * 4
Config Lcdpin = Pin , Rs = PortB.6 , E = PortB.7 , Db4 = PortD.4 , Db5 = PortD.5 , Db6 = PortD.6 , Db7 = PortD.7
Config Portb = &B11100011 'Servos, pwm, switches
'Portb = &B00011100 'Set pull-ups
portb.2=1
portb.3=1
portb.4=1
Config Portc = &B11111111 'LEDs & Lines
Config Portd = &B11110010 ' rxd,txd,int0/1,LCD data 4-7
Portd = &B00001101 'pull-ups on the track switches
Config Spi = Hard, Interrupt = Off, Data_Order = Msb, Master = no, Polarity = high, Phase = 1, Clockrate = 16, Noss = 0
Spiinit
on spi spi_isr
Enable SPI
Enable Interrupts
dim var as string * 5
lcd "Ready"
do
nop
loop
end
spi_isr:
spiin var , 5
waitms 50
locate 1,1
lcd var
wait 1
locate 1,1
lcd "______"
return
|
(BASCOM-AVR version : 2.0.7.9 , Latest : 2.0.7.8 ) |
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Tue Feb 21, 2017 10:43 pm Post subject: |
|
|
From Help: "All BASCOM SPI statements are master mode routines." (See CONFIG SPI).
Look to the Samples -> SPI Slave to get an idea how it works. |
|
Back to top |
|
|
steamydave
Joined: 06 May 2009 Posts: 39
|
Posted: Tue Feb 21, 2017 11:21 pm Post subject: |
|
|
Sorry, the setting has the option for Hardware SPI...
MASTER | Selects if the SPI is run in master or slave mode so I presumed this was an option as it is a setting in the config command.
I can't find the samples - are these installed on the PC or online somewhere.
Thanks for your help
David |
|
Back to top |
|
|
hgrueneis
Joined: 04 Apr 2009 Posts: 902 Location: A-4786 Brunnenthal
|
Posted: Wed Feb 22, 2017 6:07 pm Post subject: |
|
|
David,
Samples are in:
USERS/'Your Computer name'/Documents/MCS-Electronics/Bascom-AVR/Samples/
regards
Hubert |
|
Back to top |
|
|
steamydave
Joined: 06 May 2009 Posts: 39
|
Posted: Wed Feb 22, 2017 7:04 pm Post subject: |
|
|
Thanks,
found them here C:\Users\UserName\Documents\MCS Electronics\BASCOM-AVR\Samples\SPI
I don't like it when files are installed in user profiles rather than with the programs.
Now maybe I can sort the SPI out.
Thanks again,
David |
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Wed Feb 22, 2017 8:09 pm Post subject: |
|
|
The - more or less secret - trick for looking to samples is using the menu in Bascom: file -> open -> sample
Of course you can choose either MASTER or SLAVE in config, but the routines like SPIIN an SPIOUT are master - aka spi clock generator - only.
There are no specific slave routines as this is IMHO rarely used by normal Bascom hackers. And receiving a spi message is quite simple. You only have to read the spi data register SPDR (and put back data, if desired) in the isr.
I personal don't like the sample code in spi-slave.bas, as it is using NOSAVE, pushing/popping r24 only and then using normal bascom statements for reading the register and setting a flag. This is potentially dangerous, as you don't know what register are really used and will be used in future. So this seems to be a very old sample. And using $asm .. $end asm is now mandatory.
If you don't have any experience in assembler I would suggest not using NOSAVE and write the isr in basic. As you have control over both sender and receiver code you can easily make sure that the timing will work and you're not loosing data. |
|
Back to top |
|
|
steamydave
Joined: 06 May 2009 Posts: 39
|
Posted: Wed Feb 22, 2017 8:25 pm Post subject: |
|
|
Great, thanks, looking forward to having another go with this.
Just for info, i'm planning on controlling a 16mm railway with it. A master chip will control devices (like automatic turntables, points, signals etc) so having a master that can send instructions to multiple devices with selective SS lines seems to be a good solution.
Many thanks again,
David |
|
Back to top |
|
|
|
|
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
|
|