|
|
View previous topic :: View next topic |
Author |
Message |
boulder
Joined: 15 Mar 2008 Posts: 53
|
SPI Output Buffer Has Garbage value After Reset |
Posted: Mon Dec 15, 2008 7:54 pm |
|
|
Hi,
Every time after I reset power there is always different garbage value in the output SPI buffer before writing any bytes. The below is my code, could anyone give me an explanation and direction? The PIC18F44J10 chip is used as slave.
Code: |
#include <stdio.h>
#include <stdlib.h>
#include <Meter.c>
#use delay(clock=8000000, crystal)
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7)
#define __DEBUG
#ifndef __DEBUG
#fuses WDT, STVREN, NOXINST, NODEBUG, NOPROTECT, H4_SW, NOFCMEN, NOIESO, WDT32768
#endif
#ifdef __DEBUG
#fuses NOWDT, STVREN, NOXINST, DEBUG, NOPROTECT, H4_SW, NOFCMEN, NOIESO
#endif
unsigned int8 read=0, temp2=0, a = 0;
#byte SSPBUF = 0xFC9
#byte SSPCON = 0xFC6
#byte SSPSTAT = 0xFC7
#bit BF = SSPSTAT.0
void main()
{
setup_spi(SPI_SLAVE | SPI_L_TO_H | SPI_XMIT_L_TO_H);
while(1)
{
while(!BF);
read = SSPBUF;
SSPBUF = temp2;
temp2++;
}
}
|
Thanks. |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: SPI Output Buffer Has Garbage value After Reset |
Posted: Mon Dec 15, 2008 8:35 pm |
|
|
boulder wrote: | Hi,
Every time after I reset power there is always different garbage value in the output SPI buffer before writing any bytes. The below is my code, could anyone give me an explaination... |
The datasheet says that SSPBUF is unknown at power-on reset. So it this behavior is expected. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
Guest
|
|
Posted: Mon Dec 15, 2008 8:49 pm |
|
|
Should I reset the buffer right before the while() loop? Like,
Thanks. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Dec 16, 2008 12:43 am |
|
|
If you intend to transmit defined data from the beginning, it seems obvious, that SSPBUF has to be loaded before SPI data exchange... |
|
|
Ttelmah Guest
|
|
Posted: Tue Dec 16, 2008 3:19 am |
|
|
Though the buffer contains garbage, the SSPIF, won't be set to say there _is_ data, unless clocks have been received. As soon as the master clocks a byte to you, the buffer will contain the data clocked in, and the interrupt flag will be set.
With the transaction from the master end, the byte received 'back', for the first transaction basically always will be garbage. It'll be this undefined value on the first transaction, and the last byte sent by the master, for all future transactions.
The way the transactions work, is (fixed pitch font required):
Code: |
MASTER SLAVE
Start transaction
master sends byte
master receives byte in slave buffer - garbage Receives byte
loads byte for _next_
transaction
Repeat as needed:
Master sends next byte Receives byte
Receives byte loaded on _last_ transaction. loads next byte.
|
At the end of this, the buffer will contain the last byte sent by the master, which it'll receive, when it next starts a transaction.
With SPI, the data received, always lags the transmission by one character, and so the byte received on the first transaction wll always be effectively 'garbage'.
Best Wishes |
|
|
boulder
Joined: 15 Mar 2008 Posts: 53
|
|
Posted: Tue Dec 16, 2008 1:29 pm |
|
|
Thanks, I got it. |
|
|
|
|
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
|
Powered by phpBB © 2001, 2005 phpBB Group
|