S12 SPI read and write 93lc76c

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

S12 SPI read and write 93lc76c

Jump to solution
2,716 Views
xiaolin_gao
Contributor I

hi all, 

 

How to clear SPISR_SPTEF bit?

 

 DATASHHET the following description:

 

 If set, this bit indicates that the transmit data register is empty. To clear
this bit and place data into the transmit data register, SPISR has to be read with SPTEF = 1, followed by a write
to SPIDR. Any write to the SPI Data Register without reading SPTEF = 1, is effectively ignored.

 

Can provide some routine? thank you!

Labels (1)
0 Kudos
Reply
1 Solution
923 Views
bigmac
Specialist III

Hello,

 

When processing a single byte at a time, such as with the previous code snippet, the period that SPTEF flag remains low will be very short, typically a few bus cycles only.  During debug, it is therefore unlikely  that you would ever observe the flag being cleared.

 

However, you should not single step through the function for another reason.  The monitoring of the SPISR and SPIDR registers by the debugger can inadvertently cause the SPIF flag to be cleared prior to  the wait loop being exited.  The function will then never exit.

 

Regards,

Mac

 

View solution in original post

0 Kudos
Reply
4 Replies
923 Views
xiaolin_gao
Contributor I

hi mac,

i use oscilloscope track mosi pin and observe the data had outed after "SPIDR = value",:smileyhappy:

thank you,mac:smileyvery-happy:.

0 Kudos
Reply
923 Views
bigmac
Specialist III

Hello,

 

The SPTEF flag will be cleared and will then become set during the normal course of sending a byte.  You need to test the flag, to make sure it is set, prior to sending each byte.  More important is the clearing of the SPIF flag following the completion of each byte transfer.  The following code would be typical for the transfer of a single byte in both directions.

 

byte SPI_trans( byte value)

{

   while ((SPISR & 0x20) == 0);  // Wait until SPTEF flag is set

   SPIDR = value;

   while ((SPISR & 0x80) == 0);  // Wait until SPIF flag is set

   return SPIDR;                 // Also clears flag

}

 

Regards,

Mac

 

0 Kudos
Reply
923 Views
xiaolin_gao
Contributor I

hi mac,

 

 

thank your routine,but the SPTEF is not change too?
Message Edited by xiaolin.gao on 2009-07-08 10:52 PM
0 Kudos
Reply
924 Views
bigmac
Specialist III

Hello,

 

When processing a single byte at a time, such as with the previous code snippet, the period that SPTEF flag remains low will be very short, typically a few bus cycles only.  During debug, it is therefore unlikely  that you would ever observe the flag being cleared.

 

However, you should not single step through the function for another reason.  The monitoring of the SPISR and SPIDR registers by the debugger can inadvertently cause the SPIF flag to be cleared prior to  the wait loop being exited.  The function will then never exit.

 

Regards,

Mac

 

0 Kudos
Reply