View previous topic :: View next topic |
Author |
Message |
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Thu Jan 16, 2014 6:43 pm Post subject: simple spi soft problem |
|
|
hello,
in order to understand well the SPI protocol
I did 2 programs working on the same dev card: 1 Atmega644A as master, 1 atmega 168 as slave
I doesn't want (for now) use the hard SPI protocol
for M644: master
Config Spi = Soft , Din = Pinc.2 , Dout = Portc.3 , Ss = Portc.4 , Clock = Portc.5
' take care look at the mix of pin = input and port=output
Var = &B10101010
Cls
SPIINIT 'Init SPI state and pins.
Do
Spiout Var , 1 'send 1 byte
Jinteger = Jinteger + 1
Waitms 200
loop
------------------------------
for M168 slave
Config Spi = Soft , Din = Pinb.0 , Dout = Portb.1 , Ss = Portb.6 , Clock = Portb.7
Spiinit 'Init SPI state and pins.
Cls
Do
Spiin Var , 1 'receive 1 byte
Jinteger = Jinteger + 1
Waitms 200
Locate 1 , 1 : Lcd "send:" ; Jinteger
Locate 2 , 1 : Lcd "Var:" ; Var
Loop
result var=255 ?
pin Din(master) to din slave Dout(master) to dout slave and so on
I try to cross Din and dout same result !
where is my fault ?
Thanks JP
(BASCOM-AVR version : 2.0.7.6 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Thu Jan 16, 2014 10:37 pm Post subject: |
|
|
the fault is that the SPI is configures for master SPI.
If you want to do a SPI soft slave check out the spi-softslave.bas sample from samples\SPI. _________________ Mark |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Fri Jan 17, 2014 5:07 pm Post subject: |
|
|
hi Mark,
Thanks for your answers Mark but fortunately I still have not any dialog SPI
I read, read and test the samples
I understand the master must send a byte first to the slave and the the slave return one and so on...
On the help the clockrate must be a 1/4 than slave one, but I saw also :
Config Spi = Hard , Interrupt = On , Data Order = Msb , Master = No , Polarity = Low , Phase = 0 , Clockrate = 128 'clock rate does not matter for a slave
I would like to use soft but hard will be convenient too - It is for two things
use the DS3234 RTC TCXO
Make a wiki about the SPI bus for all of us
So a sample as Master send "abcdefgh"
and a Slave which answers "123456789"
Using new atmega as 644..168. for exemple...
I tried both the 2313 doesn't work for me and the 8515 samples the 8515 give me errors of compilation
I think it will be a real help for all of us and... I will do not the wiki !
Jean-Pierre |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Sun Jan 19, 2014 12:14 pm Post subject: |
|
|
please read
Thanks for your answers Mark but unfortunately I still have not any dialog SPI
and not
Quote: | Thanks for your answers Mark but fortunately I still have not any dialog SPI |
sorry,my english is poor and I lost 30% of one eyes so I have some problem to read on screen.
JP |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Mon Jan 20, 2014 10:03 pm Post subject: |
|
|
The help contains a lot of info about SPI. But what most people do not get is that SPI works with a master and slave. Only the master can initiate transactions. And the slave can only respond to the master.
This means that you can only tell a slave what to do. When you want to read data from a slave, you first need to inform the slave that you want to retrieve data. So you need some protocol.
But let us go one step back : the master and slave form a 16 bit shift register. this means that when you shift/send out a value from the master, you read back the value that was in the slave.
So if you want to make a slave that will increase a byte value that it receives by 1, and send that back you would do this :
Master : send byte with value of 100
Slave : contains a value for example 0 and will send this back to the master.
Master : receives the value 0 from the slave.
Slave : now has receives the value from the master with value 100. So now slave can increase this value with 1 so it becomes 101.
Master : need to send a dummy value of say 0 so it will get back the value from the slave.
Slave : send back 101 to master.
Master now has value 101.
To test things use the sendspi.bas sample from the samples\SPI and spi-slave.bas from samples\SPI.
Do take care not to use STK200. it has some internal connections that does make it fail.
When it works using the SPI hardware you could make a next step to using the soft version. _________________ Mark |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Wed Jan 22, 2014 4:56 pm Post subject: |
|
|
Hi mark,
I use my own dev card, it is a card with 3 kind ok µC 40, 28, and 8 pins - all independent only the power supply is common
The 3 ISP ports are "removable" by jumpers, I did it when the program is in flash
I try to run the both programs on the sample without any success, I use 2 LCD display, one for the master and one for the slave.
I join the program, and the pdf of the card.
Again I'm in the darkness. i'm waiting for the
Best regards
JP |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Wed Jan 22, 2014 10:13 pm Post subject: |
|
|
I did a simulation in Proteus and it worked out fine. I only changed some line (chose m644 since there is no m644A in proteus). I remarked 'Config Lcdmode = Port
And in the slave i used : Config Spi = Hard , Interrupt = On , Data Order = Msb , Master = No , Polarity = Low , Phase = 0 , Clockrate = 16
Please notice how i connected things. _________________ Mark |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Fri Jan 24, 2014 11:00 am Post subject: |
|
|
And the result is....It will work, I have transfert of some data, not all but is coming, I will let you know.
Many thanks
JP |
|
Back to top |
|
|
|