CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

SPI Output Buffer Has Garbage value After Reset

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
boulder



Joined: 15 Mar 2008
Posts: 53

View user's profile Send private message

SPI Output Buffer Has Garbage value After Reset
PostPosted: Mon Dec 15, 2008 7:54 pm     Reply with quote

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

View user's profile Send private message

Re: SPI Output Buffer Has Garbage value After Reset
PostPosted: Mon Dec 15, 2008 8:35 pm     Reply with quote

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








PostPosted: Mon Dec 15, 2008 8:49 pm     Reply with quote

Should I reset the buffer right before the while() loop? Like,
Code:

SSPBUF = 0x00;


Thanks.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Dec 16, 2008 12:43 am     Reply with quote

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







PostPosted: Tue Dec 16, 2008 3:19 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Dec 16, 2008 1:29 pm     Reply with quote

Thanks, I got it.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
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


Powered by phpBB © 2001, 2005 phpBB Group