SPI for MAX5250

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

SPI for MAX5250

3,565 Views
AlbertH
Contributor II
Hello, All
         I am using DP256 SPI to communicate with MAX5250(DAC) to output analog voltage. The following is my code:

PTS &= ~PTS3_msk; //clear pin3 of port S to select SPI for the first MAX5250

// ????
SPI_Data_HIGH_Tmp = (unsigned char)(0x30 + ((Current1_uc &0xF0) >>4));
SPI_Data_LOW_Tmp
= (unsigned char)((Current1_uc &0x3F) <<4);

SPI0DR
= SPI_Data_HIGH_Tmp; // send the High byte data of SPI Data
while(!(SPI0SR &SPTEF0_msk)); //wait till data transfer finish
while(!(SPI0SR &SPIF0_msk ));
SPI_Data_HIGH_Tmp
= SPI0DR;

SPI0DR
= SPI_Data_LOW_Tmp; // send the Low byte data of SPI Data
while(!(SPI0SR &SPTEF0_msk )); //wait till data transfer finish
while(!(SPI0SR &SPIF0_msk ));
SPI_Data_LOW_Tmp
= SPI0DR;

PTS |= PTS3_msk;  //disable the Max5250

Using the osilliscope, I can check the SCLK and MOSI wave is correct. And the Data in MOSI is exactly what I sent.But I can get the analog voltage output, Does anybody know the reason? Thanks in advance!

 

Albert

 

Labels (1)
0 Kudos
9 Replies

916 Views
bigmac
Specialist III
Hello Albert,
 
It would seem that the variable Current1_uc should be an unsigned int, with the 10-bit data left-justified.  Is this the case?
 
Assuming so, I think your code should be corrected as follows -
 
// Upper 4 data bits -> right justified
SPI_Data_HIGH_Tmp = (unsigned char)(0x30 + (Current1_uc >> 12));
// Lower 6 data bits -> low byte position, left justified 
SPI_Data_LOW_Tmp = (unsigned char)((Current1_uc 0x0FC0) >> 4);
 
Otherwise, I can't see a problem with your code, assuming the initialisation of the SPI and PTS3 is correct.
 
Regards,
Mac
 

Message Edited by bigmac on 2007-04-2602:00 PM

0 Kudos

916 Views
AlbertH
Contributor II
Thanks Rocco and Mac!
The issue was solved. As Rocco said, the problem is in initialize, CPHA bit in register SPICR1 should be cleared in init. Also my Reference Voltage in Max5250 is wrong, it should be VDD-1.4V, but I used VDD.
 
I have another question want to ask is that if I want to change the Max5250 DAC output A & B in the same time. How it should do?
 
I wrote the following code:
 

PTS &= ~PTS3_msk; //clear pin3 of port S to select SPI for MAX5250

// DAC A

SPI_Data_HIGH_Tmp = (unsigned char)(0x30 + ((Current1_uc &0xF0) >>4));

SPI_Data_LOW_Tmp = (unsigned char)((Current1_uc &0x3F) <<4);

SPI0DR = SPI_Data_HIGH_Tmp; // send the High byte data of SPI Data

while(!(SPI0SR &SPTEF0_msk)); //wait till data transfer finish

while(!(SPI0SR &SPIF0_msk ));

SPI_Data_HIGH_Tmp = SPI0DR;

SPI0DR = SPI_Data_LOW_Tmp; // send the Low byte data of SPI Data

while(!(SPI0SR &SPTEF0_msk )); //wait till data transfer finish

while(!(SPI0SR &SPIF0_msk )); SPI_Data_LOW_Tmp = SPI0DR;

// DAC B

SPI_Data_HIGH_Tmp = (unsigned char)(0x70 + ((Current2_uc &0xF0) >>4));

SPI_Data_LOW_Tmp = (unsigned char)((Current2_uc &0x3F) <<4);

SPI0DR = SPI_Data_HIGH_Tmp; // send the High byte data of SPI Data

while(!(SPI0SR &SPTEF0_msk)); //wait till data transfer finish

while(!(SPI0SR &SPIF0_msk ));

SPI_Data_HIGH_Tmp = SPI0DR;

SPI0DR = SPI_Data_LOW_Tmp; // send the Low byte data of SPI Data

while(!(SPI0SR &SPTEF0_msk )); //wait till data transfer finish

while(!(SPI0SR &SPIF0_msk ));

SPI_Data_LOW_Tmp = SPI0DR;

PTS |= PTS3_msk; //SET pin3 of port S to deselect SPI for the first MAX5250

 

But it only DAC B have the output voltage, DAC A remains Zero. Any Ideas?

Big Thanks!

 

Albert

 

0 Kudos

916 Views
AlbertH
Contributor II
Thanks All. The problem is solved. The reason is that I should put MAX5250 chip select a rising edge to lock the DAC A before writting data to DAC B.
 
Albert
0 Kudos

916 Views
bigmac
Specialist III
Hello Albert,
 
I think you stil have a problem with your low and high byte calculations, that would give the incorrect output voltage.  For left-justified 10-bit data within Current1_uc, the corrected calculation would be per my previous post.
 
However, if the 10-bit data is right-justified, the calculations shown are still not correct.  I think that the following would be required in this case.
 
SPI_Data_HIGH_Tmp = (unsigned char)(0x30 + ((Current1_uc & 0x3C0) >> 6));
SPI_Data_LOW_Tmp = (unsigned char)((Current1_uc & 0x3F) << 2);

Regards,
Mac

Message Edited by bigmac on 2007-04-2706:26 AM

0 Kudos

916 Views
rocco
Senior Contributor II
Hi, Mac:

I didn't read either Albert's or your code (it makes me dizzy), but the data from the MAX5250 is neither left nor right justified. :smileysurprised:


The data is alligned to be compatible with the MAX525, which is a 12-bit device (with a 24-bit price). So you need to load the data as 12-bit, right-justified, and load zeros into the low two bits.

Message Edited by rocco on 2007-04-2603:54 PM

0 Kudos

916 Views
bigmac
Specialist III
Hello Rocco,
 
The data format for the MAX5250 is understood, and the modified code attempts to achieve what you suggest.  The left or right justification actually refers to the incoming data word that needs to be manipulated to get to the special data format.
 
Regards,
Mac
 
 
 
0 Kudos

916 Views
rocco
Senior Contributor II
Oh. . . sorry.

That's what I get for not reading the code.

Message Edited by rocco on 2007-04-2608:20 PM

0 Kudos

916 Views
AlbertH
Contributor II
Thanks Mac and rocco,
            Actively, I just want to use 8 bit data to 10bit DAC. and my goal is to get some fixed point value like 0.5v, 1v, 1.5v, 2.0v, so I used "8bit + 2bit ( 0 0 )" to get the closed value. Anyway, Thanks a lot!
 
Albert
0 Kudos

916 Views
rocco
Senior Contributor II
Hi, Albert:

I use the Max 5250 as well and have no problems with it now. But here is something that I had trouble with:

How does the chip-enable signal look? I can't tell from your code whether chip-enable is correct, but the MAX5250 is particular about it. It will not respond to the data you transmit until chip select goes high, and even then it doesn't seem to respond if there were less than 16 clocks while it was low.

Also, check your SPI clock phase. I can't tell from your code how the SPI is initialized, but I seem to remember that it was picky about the clock phase, as well.

Message Edited by rocco on 2007-04-2505:33 PM

0 Kudos