PDA

View Full Version : How to configure SPI in PICBASIC PRO?



moogle
- 14th March 2007, 18:40
I tried to configure two PIC16F877As as SPI master and SPI slave. Pls refer to schematic shown in figures in attachment. Master will send a '?' to acknowledge the slave. Then, the slave will reply with a '!'. Once master receives '!', it will start to receive data from slave, which is a '5' character. The charater will then be displayed on the LCD.

The SPI communication between the master/slave did not work. Pin B0 of master is connected to A5/SS of slave. Pls advise if you find mistakes in the schematic as well as the source code.

For your information, I'm using PicBasic Pro 2.47 compiler.


Source Code (MASTER)


' PicBasic program to demonstrate operation of an LCD in 4-bit mode
'
' LCD should be connected as follows:
'-------------------------------------
' LCD PIC
'-------------------------------------
' DB4 PortD.4
' DB5 PortD.5
' DB6 PortD.6
' DB7 PortD.7
' RS PortD.2
' E PortD.3
' RW Ground
' Vdd 5 volts
' Vss Ground
' Vo 10K potentiometer (or ground)
'
' PicBasic Pro program to read and write to SPI slave
' using the synchronous serial port
'
' Connect SDI(master) to SDO(slave), SDO(master) to
' SDI(slave), AND SCK(master) to SCK(slave).
' Common Ground is required.
'
' Sends ascii "?" to request data, waits for a "!" to
' begin receiving data. Expect to receive "5" (without quotes)
' from slave and display it LCD.

DEFINE LCD_LINES 4 'number of LCD lines
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTD
DEFINE LCD_EBIT 3
DEFINE LCD_BITS 4
DEFINE LCD_RSBIT 2
DEFINE LCD_EREG PORTD
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 40

SSPEN VAR SSPCON.5 'SSP Enable bit
CKP VAR SSPCON.4 'Clock Polarity Select
SMP VAR SSPSTAT.7 'Data input sample phase
CKE VAR SSPSTAT.6 'Clock Edge Select bit
SSPIF VAR PIR1.3 'SPI interrupt flag
a VAR BYTE

TRISC = 0 'set PORTC I/O
TRISB = %11111110 'set PORTB.0 as output
SSPEN = 1 'enable SPI pins
CKP = 0 'clock idle low
CKE = 0 'transmit on idle to active transition
SSPIF = 0 'clear buffer full status
SMP = 0 'sample in middle of data
LOW PORTB.0

loop:
GoSub getdata 'initiate conversion and receive data
LCDOut $fe, 1,
LCDOut #a
Pause 1000
GoTo loop 'do it forever

getdata:
HIGH PORTB.0 'enable ss pin
SSPBUF = "?" 'send ? to start conversion
GoSub letclear 'wait for buffer to clear
IF SSPBUF<>"!" Then getdata 'wait for reply (!)
SSPBUF = 0 'write to SSPBUF to start clock
GoSub letclear 'wait for receipt
a = SSPBUF 'store received character in array
LOW PORTB.0
Return

letclear:
IF SSPIF = 0 Then letclear 'wait for SPI interupt flag
PauseUs 25 '25uS fudge factor
SSPIF = 0 'reset flag
Return



Source Code (SLAVE)


' PicBasic Pro SPI slave program (see SPImast.bas for connections)

dataout VAR WORD
SSPEN VAR SSPCON.5 'SSP Enable bit
CKP VAR SSPCON.4 'Clock Polarity Select
SMP VAR SSPSTAT.7 'Data input sample phase
CKE VAR SSPSTAT.6 'Clock Edge Select bit
SSPIF VAR PIR1.3 'interrupt flag - last bit set

TRISC = %11011111 'set PORTC I/O
TRISA = %11011111 'set PORTA.5 as input.
SSPCON = %00000100 'configure SPI slave, enable SS pin
CKP = 0 'clock idle low
CKE = 0 'transmit on idle to active transition
SSPIF = 0 'clear SPI interrupt
SMP = 0 'sample in middle of data
dataout[0] = "5"

loop:
SSPEN = 0 'disable/enable SSP to reset port
SSPEN = 1
GoSub letclear 'wait for byte received
IF (SSPBUF <> "?") Then loop 'wait for ? to start conversion
GoSub senddata 'send "!" and string of data
GoTo loop 'do it forever

senddata:
GoSub letclear 'wait until buffer ready
SSPBUF = "!" 'send reply
GoSub letclear 'wait until buffer ready
SSPBUF = dataout[0] 'send variable
Return

letclear:
IF SSPIF = 0 Then letclear 'wait for interrupt flag
SSPIF = 0 'reset flag
Return

NatureTech
- 15th March 2007, 15:53
Had you tried placing pull down resistor on SS pin.There is a chance slave get "crazy" as the SS pin will sometimes float (OC).

moogle
- 16th March 2007, 14:20
Had you tried placing pull down resistor on SS pin.There is a chance slave get "crazy" as the SS pin will sometimes float (OC).
Yes, I have added the pull down resistor at SS pin but it still could not work.

I have some doubts on how to write and read SSPBUF in PICBASIC PRO.

When I execute the following code, which I assume it as 'write to SSPBUF':
SSPBUF = "?"
I could see a 8-pulse train at SCK of the master.

If I'm not mistaken, communication between master/slave is initiated by SCK (clock) of the master, right? If that is the case, how do we read (in PICBASIC PRO) from the slave? Pls refer to the code below:
IF SSPBUF<>"!" Then getdata
Pls advise if there is any read operation above? How does the master start the clock during read operation?

Thanks.

moogle
- 16th March 2007, 14:22
Latest version of code... but still can't work... Pls advise.



I tried to configure two PIC16F877As as SPI master and SPI slave. Pls refer to schematic shown in figures in attachment. Master will send a '?' to acknowledge the slave. Then, the slave will reply with a '!'. Once master receives '!', it will start to receive data from slave, which is a '5' character. The charater will then be displayed on the LCD.

The SPI communication between the master/slave did not work. Pin B0 of master is connected to A5/SS of slave. Pls advise if you find mistakes in the schematic as well as the source code.

For your information, I'm using PicBasic Pro 2.47 compiler.


Source Code (MASTER)


' PicBasic program to demonstrate operation of an LCD in 4-bit mode
'
' LCD should be connected as follows:
'-------------------------------------
' LCD PIC
'-------------------------------------
' DB4 PortD.4
' DB5 PortD.5
' DB6 PortD.6
' DB7 PortD.7
' RS PortD.2
' E PortD.3
' RW Ground
' Vdd 5 volts
' Vss Ground
' Vo 10K potentiometer (or ground)
'
' PicBasic Pro program to read and write to SPI slave
' using the synchronous serial port
'
' Connect SDI(master) to SDO(slave), SDO(master) to
' SDI(slave), AND SCK(master) to SCK(slave).
' Common Ground is required.
'
' Sends ascii "?" to request data, waits for a "!" to
' begin receiving data. Expect to receive "5" (without quotes)
' from slave and display it LCD.

DEFINE LCD_LINES 4 'number of LCD lines
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTD
DEFINE LCD_EBIT 3
DEFINE LCD_BITS 4
DEFINE LCD_RSBIT 2
DEFINE LCD_EREG PORTD
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 40

SSPEN VAR SSPCON.5 'SSP Enable bit
CKP VAR SSPCON.4 'Clock Polarity Select
SMP VAR SSPSTAT.7 'Data input sample phase
CKE VAR SSPSTAT.6 'Clock Edge Select bit
SSPIF VAR PIR1.3 'SPI interrupt flag
a VAR BYTE

TRISC = 0 'set PORTC I/O
TRISB = %11111110 'set PORTB.0 as output
SSPEN = 1 'enable SPI pins
CKP = 0 'clock idle low
CKE = 0 'transmit on idle to active transition
SSPIF = 0 'clear buffer full status
SMP = 0 'sample in middle of data
HIGH PORTB.0

loop:
GoSub getdata 'initiate conversion and receive data
LCDOut $fe, 1,
LCDOut #a
Pause 1000
GoTo loop 'do it forever

getdata:
LOW PORTB.0 'enable ss pin
SSPBUF = "?" 'send ? to start conversion
GoSub letclear 'wait for buffer to clear
IF SSPBUF<>"!" Then getdata 'wait for reply (!)
SSPBUF = 0 'write to SSPBUF to start clock
GoSub letclear 'wait for receipt
a = SSPBUF 'store received character in array
HIGH PORTB.0
Return

letclear:
IF SSPIF = 0 Then letclear 'wait for SPI interupt flag
PauseUs 25 '25uS fudge factor
SSPIF = 0 'reset flag
Return



Source Code (SLAVE)


' PicBasic Pro SPI slave program (see SPImast.bas for connections)

dataout VAR WORD
SSPEN VAR SSPCON.5 'SSP Enable bit
CKP VAR SSPCON.4 'Clock Polarity Select
SMP VAR SSPSTAT.7 'Data input sample phase
CKE VAR SSPSTAT.6 'Clock Edge Select bit
SSPIF VAR PIR1.3 'interrupt flag - last bit set

TRISC = %11011111 'set PORTC I/O
TRISA = %11011111 'set PORTA.5 as input.
SSPCON = %00000100 'configure SPI slave, enable SS pin
CKP = 0 'clock idle low
CKE = 0 'transmit on idle to active transition
SSPIF = 0 'clear SPI interrupt
SMP = 0 'sample in middle of data
dataout[0] = "5"

loop:
SSPEN = 0 'disable/enable SSP to reset port
SSPEN = 1
GoSub letclear 'wait for byte received
IF (SSPBUF <> "?") Then loop 'wait for ? to start conversion
GoSub senddata 'send "!" and string of data
GoTo loop 'do it forever

senddata:
GoSub letclear 'wait until buffer ready
SSPBUF = "!" 'send reply
GoSub letclear 'wait until buffer ready
SSPBUF = dataout[0] 'send variable
Return

letclear:
IF SSPIF = 0 Then letclear 'wait for interrupt flag
SSPIF = 0 'reset flag
Return

mister_e
- 16th March 2007, 14:57
a good exercice would be to use a SPI device and read/write to it. Anything like a EEPROM 93C06, 93C46 etc etc. Once you have something working, you should understand better how stuff work.

Don't give up!

Russ Kincaid
- 16th March 2007, 16:07
The datasheet says that setting TRISA = 1 sets the port as input. I believe you have it backward.

moogle
- 16th March 2007, 16:25
The datasheet says that setting TRISA = 1 sets the port as input. I believe you have it backward.
Noted...
Should be...
TRISA = %11111111 'set PORTA.5 as input.

Thanks...

moogle
- 16th March 2007, 18:27
The datasheet says that setting TRISA = 1 sets the port as input. I believe you have it backward.
Ooopss... PORTA.5 should be set as input to enable the SS. Pls correct me if I'm wrong. Thanks.

virolay
- 14th April 2007, 04:00
Hi, moogle,

I have followed your "pilgrimage" through several forums looking for helping to make to work your SPI routines.

I also have verified the little interest that some manufacturers put in helping their clients, for example Microchip and Crownhill, this last is incapable to put a simple example of a Masters and a Slave working in SPI communications.

I have begun to try to start up your routines but they do not work and I do not dare to write in the forums since the answers that they gave you, have been sufficiently frustrating for my.

Well, your routines seem enough to learn a PIC-to-PIC SPI, so, if you have them working, please, put them here in the forum or send me them by mail.

Regards,

skimask
- 14th April 2007, 05:13
So let me see if I've got this right:


I also have verified the little interest that some manufacturers put in helping their clients, for example Microchip and Crownhill, this last is incapable to put a simple example of a Masters and a Slave working in SPI communications.

So Microchip doesn't have any examples? There's plenty to be found in the datasheets. And any problems with the SPI module (and there are a couple here and there) are found in the errata sheets, complete with workarounds...
And Crownhill? Well, I don't use the Proton compiler they sell. But it seems to me that if the compiler works and it's still selling well, then it's probably a good bet that the SPI routines it comes with probably work fairly well also. And besides that, how about trying http://www.picbasic.org/forum/ or http://www.google.com . The Search functions work great and I'll bet you can find loads of good examples for code at either.


I have begun to try to start up your routines but they do not work and I do not dare to write in the forums since the answers that they gave you, have been sufficiently frustrating for my.

So, even though the original poster wrote in the forums looking for help because the program didn't work, you assumed that it would actually work for you and it didn't? Imagine that...


Well, your routines seem enough to learn a PIC-to-PIC SPI, so, if you have them working, please, put them here in the forum or send me them by mail.

So what you actually want is somebody else to do all the legwork for you.

I am correct so far?

virolay
- 15th April 2007, 11:46
Hi skimask,

Thanks you very much!, your answer is the answer that anybody can expect from people as you!.

First of all, you say that there are a lot of examples in the Datasheets of Microchip, yes, with many errors, enough , even, to create controversies, see the PIClist, for instance. You suggest to visit the Proton+ forum or to search in Google. These suggestions are offensives. Of course!, I have search though Google, I have visited a dozen of specialized forums and I have not found a simple (only simple!) working SPI routine for a simple PIC to PIC communication in Assembler, nor in PICBasic Pro, nor in PICBasic Plus, nor in Proton, the only thing I have found is a lot of people looking for any good suggestion and all of them received suggestions as yours, nothing. So, you are not correct!.

Second, no, sir, no, I do not assume that anybody works for me, I assume that Internet is a good site to learn anything (I am a simple hobbyst), I assume that if moogle have posted a simple routine looking for help, probably he has not problem ( I say "probably, he has not problem...", I do not say "he is forced to...") to send or to post the same routine free of errors (their routines seem very good but I think there are not NASA's designs. Sorry, moogle, that is for skimask). Moreover, before I have decided to post my message to moogle, I have spent more than one week testing my own and other routines. So, you are not correct!.

Finally, instead of spending your precious time saying the idiot who I am, it will been simpler to put a simple link to one of the many sites in which according with you, there is a simple SPI routine of PIC to PIC. I am correct so far?

Acetronics2
- 15th April 2007, 13:24
I have followed your "pilgrimage" through several forums looking for helping to make to work your SPI routines.

I also have verified the little interest that some manufacturers put in helping their clients, for example Microchip and Crownhill, this last is incapable to put a simple example of a Masters and a Slave working in SPI communications.


Regards,

My Dear Virolay,

IF you were a PicBasicPro fan ... ( pfffff !!!!) you should have had YOURSELF your first look HERE :

http://www.melabs.com/resources/samples.htm

... I'm really not used to link pics between themselves ( LOL ) ... but straight found THIS:

spimast routine for ... spimaster
Spislave routine for ... spislave !!!

Now, I can understand you're a very great man AND/OR think you really are ...

BUT, that doesn't allow you to write stupidities about suppliers from your second post on this forum ...

" I want this " is definitly not the good key here ... so far we don't know ANYTHING about what you had hoped to realize.

Have a nice afternoon

Alain

skimask
- 15th April 2007, 17:55
Thanks you very much!, your answer is the answer that anybody can expect from people as you!.

Search the posts here. If you can find less than say, 50 answers I've given out to others, I'll write your code for you, test it, and hand deliver it, straight to your workshop, program your chip, and cook dinner for you.


Finally, instead of spending your precious time saying the idiot who I am, it will been simpler to put a simple link to one of the many sites in which according with you, there is a simple SPI routine of PIC to PIC. I am correct so far?

No, you are not correct...so far.
Did I call you an idiot? Nope. Anybody else see that? Nope. Did I imply that? Nope.
Would it have been simpler for me to link you to an SPI routine? Yep. Would you have actually learned something in the end? Nope.
In my little world, it runs right back the old 'teach a man to fish' thing.

Now then, post some code that YOU have written (not just some cut-and-paste from somebody else). And when you can't figure out why it doesn't work or why something isn't doing what you want it to do, you'll figure out what sort of answers I (or anybody that frequents these forums) can give out.

Acetronics2
- 15th April 2007, 18:31
Hi, Skimask

Take it easy ...

Alain