AnsweredAssumed Answered

MC9S12C128 SPI Interface 24-bit word transfers

Question asked by Tim Riley on Apr 13, 2011
Latest reply on Apr 18, 2011 by Tim Riley

I am having trouble using the SPI interface to communicate with the MAX5481 digital pot. The 5481 requires a 24-bit word to set the pot value. To accomplish using one of the PTAD lines as the SS output and am controlling it manually while 

sending the 24-bit word out in 3 8-bit chunks.

 

I am attempting to do the transfer in the following manner:

1) set PTAD line low

2) do 1st 8-bit SPI transfer (i.e. load SPIDR and wait until SPISR == SPITEF)

3) do 2nd 8-bit SPI transfer (i.e. load SPIDR and wait until SPISR == SPITEF)

4) do 3rd 8-bit SPI transfer (i.e. load SPIDR and wait until SPISR == SPIF)

5) set PTAD line high

 

In theory I thought this would work, but since I am not transfering any data into the MC9s12C device, the code just hangs on step 4.

 

If I replace step 4 above with this line

4) do 2nd 8-bit SPI transfer (i.e. load SPIDR and wait until SPISR == SPITEF)

the SS line gets set high before the transfer is complete because SPITEF does not mean that the data transfer to the device has occured.

 

What is the best mechanism to solve this issue.

 

See example code below

 

void MAX5481(unsigned long mosi){

unsigned int tt;

 

mosi = mosi << 6;

//wiperPos = (unsigned long)wiperPos << 6;

PTAD = PTAD &~SSB; //Pull SSB line low

 

//Data is a 24 bit write

tt = (mosi >> 16) & 0x00FF;

SPIDR = tt; //write upper 8 bits to SPI data register

while((SPISR & SPITEF) == 0); //Wait here until transfer complete

 

tt = (mosi >> 8) & 0x00FF;

SPIDR = tt; //write middle 8 bits to SPI data register

while((SPISR & SPITEF) == 0); //Wait here until transfer complete

 

tt = mosi & 0x00FF;

SPIDR = tt; //write lower 8 bits to SPI data register

while((SPISR & SPIF) == 0); //Wait here until transfer complete

 

for(tt=0; tt<10; tt++){

asm("NOP");

}

PTAD = PTAD | SSB; //Set SSB line back high

}

Outcomes