Help with SPI - MC9S08GT32

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

Help with SPI - MC9S08GT32

1,365 Views
juanb_arg
Contributor I

Hi everyone,
Well, i`m  working with SPI in a proyect with MC9S08GT32 cpu
The problem i have is that i can`t find the facts that debugger gives me  to be consistent with explanation of the datasheet.
First,  after  after writing the data register the one  which sends data to the transmit register,  the debugger always show me the value of the data register to be null.
Second, the secuence i see when I simule  the next piece of code is,


sending secuence
 while ( 1 ){
       
        while ( ! SPI1TF) ;
      
        SPI1D =0xFF;                 /* writes the transmit buffer register of spi */
        
}

the transmit empty flag is set in 0 value after writing two consecutive bytes, and i think it should be set 0 value after 1 writing . Another problem is when the flag is finally set  to 0, it never comes back to 1 value (ready to trasmit another byte)

I really think that the secuence to send a byte is, seeing if the transmit empty  flag is set, then write the data register, and the is suposed that the thata is send after 8 spi clock times. and then repeat the cycle.

well the only
thing i can see is that the SPI is not working, if someone can help me, send me a piece of code that  has used succsesfuly, please i need some help

 

 

Added p/n to subject.



Message Edited by NLFSJ on 2008-09-29 12:09 PM
Labels (1)
0 Kudos
3 Replies

252 Views
peg
Senior Contributor IV
Hello and welcome to the fora, Juan.

You can not read the SPI tx buffer. Attempting this gives you the receive buffer, RTFM.
See this thread for how to do it.

0 Kudos

252 Views
bigmac
Specialist III
Hello,
 
For C programs, the typical function for the master to transfer a single byte in each direction would be -
 
byte SPI_trans( byte val)
{
   while (!SPI1S_SPTEF);
   SPI1D = val;          // Send byte value
   while (!SPI1S_SPRF);  // Wait for completion of transfer
   return SPI1D;
}
 
The final read of SPI1D is necessary under all circumstances, to clear the SPRF flag and prevent an overrun condition.  The manipulation of the SS output is external to the function.  You probably will not be able to single step this code without disrupting its operation.  This is because it is possible for the debugger itself to clear the hardware flags before the code can test the flag status.
 
To send each byte to the SPI slave device (and ignore the returned value), use the following call.  The cast avoids a compiler warning because the returned value is not used.
 
(void)SPI_trans( byte_value);
 
To read the returned data from the slave, it is necessary to send a byte, usually a dummy value.
 
return_value = SPI_trans( 0);  // Dummy send
 
Regards,
Mac
 


Message Edited by bigmac on 2008-09-28 04:30 PM
0 Kudos

252 Views
juanb_arg
Contributor I

Hi again!
Well i must say that i appreciate a lot your help, it is really useful.
I`ve done a little piece of code  and  tested it with full chip simulator, I think it`s going to work.
now i can see that i didnt understand prpeperly the manual...  
well  thanks again!

regards
Juan Bautista


0 Kudos