question on SPI timer

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

question on SPI timer

1,340 Views
ScorpioIce
Contributor I
hello, I just begin the programming SPI with DEMO9S08LC60. Now, I am confused about the time counter. Every time the output SP1CLK is 16-bit (It is not 8-bit?), Is this right? and temp also stops at 2.
Thank you for yourhelp
The sample code shows as follows.
void SPISendChar (unsigned char data)
{
       while (!SPI1S_SPTEF);    //Wait until transmit buffer is empty
       SPI1D =data;              //Transmit data
}
 
void main ()
{
   SPI1C1=0xD5;  //set the SPI1 as a master
   SPI1BR= 0x45;
 
    for (temp=0;temp<100000;temp++)
     {
      
        CurrentByte =0x42;
        SPISendChar(CurrentByte) ;
     } 
}
 
 
Labels (1)
0 Kudos
3 Replies

348 Views
ScorpioIce
Contributor I
Thank you very much, Bigmic, and Peg. Yesterday, when I read register SPI1S1 from memory, it is the same as Bigmic said. Now the problem is solved :smileyhappy:
 


Message Edited by Scorpio Ice on 2007-06-08 06:20 AM
0 Kudos

348 Views
bigmac
Specialist III
Hello,
 
For every byte sent to the SPI, a data byte is returned via MISO (whether you actually make use of the data, or not).  Because you are not explicitly clearing the SPRF flag, I assume a receive overrun error would be occurring after three bytes.
 
Modifying your function as follows should fix this problem.
 
unsigned char SPISendChar (unsigned char data)
{
   while (!SPI1S_SPTEF);    // Wait until transmit buffer is empty
   SPI1D = data;            // Send data
   while (!SPI1S_SPRF);     // Wait for flag set
   return SPI1D             // Also clears flag
}
 
Your code does not show the handling of the /SS signal to the remote slave.  If you need to send a 16-bit value to the slave, this would be handled as two separate bytes, and with /SS set active prior to sending the first byte, and set inactive only after the second byte is completed.  In this instance, you would need to use GP output as the SS signal, and not the "automatic" SS operation available within the SPI module.
 
Regards,
Mac
 
0 Kudos

348 Views
peg
Senior Contributor IV
Hi,
 
What Mac said plus....
I think you are saying you are seeing a 16-bit transfer and think this is strange. Well once you fix the reason for stopping you will see a continuous 100000 bit transfer!
If you keep the buffered SCID port filled, it will all clock out continuously with no inter-byte pauses. Only if your device is busy with other things for approx. two character times will you see a pause in the clock/data streams.
Sorry if I have misunderstood you and are stating the obvious (but it is your first post)
 
0 Kudos