CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

an SPI DAC MCP4921/4922

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jaethelegend
Guest







an SPI DAC MCP4921/4922
PostPosted: Wed Jun 10, 2009 1:57 am     Reply with quote

Hi, I am trying to use MCP 4921 DAC.
I got it to work, but have some questions
1) I tried to use it without connecting one of my pins to LDAC, to save pins, using same code as below leaving out commands associated with LDAC. The spec sheet states that if i coonnect LDAC pin to the ground , the data will transfer on the rising edge of the clock. However, it doesn't seems to be working
2)I don't understand what BUF bit is. The data sheet states that BUF bit is a Vref Input buffer control bit. and when it's 1, it is buffered and when it is 0 it is unbuffered. I don't know the difference between the two.
3) In the below code, when i leave out the
while(!BF)
the program doesn't seem to be working.
The data sheet states

bit 0 BF: Buffer Full Status bit
Receive (SPI and I2 C modes):
1 = Receive complete, SSPBUF is full
0 = Receive not complete, SSPBUF is empty
Transmit (I2 C mode only):
1 = Data transmit in progress (does not include the ACK and Stop bits), SSPBUF is full
0 = Data transmit complete (does not include the ACK and Stop bits), SSPBUF is empty
Since I am transmitting to DAC, in SPI mode(not I2C mode), can't i be care less?

Thank you in advance



Code:

#include<C:\Documents and Settings\Microsoft\My Documents\PIC\Header\16f886.h>
#include<C:\Documents and Settings\Microsoft\My Documents\PIC\Header\def_16f886.h>
#fuses INTRC_IO
#use delay(clock = 1000000)

#define CS PORTC6
#DEFINE LDAC PORTC7


void init_rtn(void){
   osccon = 0x41;
   SSPSTAT = 0b01000000; //SMP = 0,CKE = 1
   SSPCON = 0x22;
   TRISC6 = 0;
   TRISC5 = 0;
   TRISC3 = 0;
   TRISC7 = 0;
}

void main(){
   int8 dummy;
   init_rtn();
   LDAC = 1;
   //while(1){
   CS = 0;
   SSPBUF = 0b00110111;
   while(!BF) /* loop */ ;
   dummy = SSPBUF;
   SSPBUF = 0xFF;
   while(!BF) /* loop */
   LDAC = 0;
   LDAC = 1;
       //power down mode
   CS = 1;
   CS = 0;
   SSPBUF = 0b00100000;
   CS = 0;
   
   //}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 10, 2009 3:24 pm     Reply with quote

I notice you're asking the same question in the Microchip forum (2 times).

The answer is:
Even though the data sheet says the BF bit is only valid in Receive mode
for SPI, it is also valid for transmit mode. The BF bit goes high when
the byte has been transmitted from the MSSP module.

Or, another way to think of it is:
When the MSSP module is transmitting, it is also receiving. That's because
SPI is just two 8-bit shift registers (one in the master and one in the
slave) that are connected to each other in a loop. When the master
transmits a byte to the slave, it also receives a byte from the slave.
So when it receives a byte, the "Buffer Full" bit will go true (high).
At that moment, it has also just finished transmitting a byte.

With regard to your problem:
You are using the Chip Select signal (\CS). You must not set \CS to a
high level before the byte been transmitted. It takes time for the MSSP
to transmit the byte. That's why you must check the Buffer full bit,
and see that it goes high, before you set \CS high. If you set \CS high
before the byte has finished transmitting, the slave will not receive a
complete byte.

A comment about your programming style.
You are writing low-level code, as if you have the Hi-Tech compiler or
some other compiler than CCS. Why ? The CCS spi_write() function
checks the BF bit. It only returns when the BF bit goes high. If you had
used the CCS functions to write your code, you would not have all these
problems. I suggest that you use the CCS functions. They will prevent
errors in your code.
jaethelegend
Guest







Thanks
PostPosted: Thu Jun 11, 2009 4:49 am     Reply with quote

Thanks for the reply.
I always post same question on both forums so that i can get as much resources as possible. Sorry if i offended some of you.

Right now, I am practicing low level coding instead of using the built in functions in CCS-C because I just started with PIC and would like to learn better about how register files and etc. works.

One remaining question is that I am trying to save a port by grounding DAC latch as the data sheet suggests. However, it doesn't seem to be working.
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: Thanks
PostPosted: Thu Jun 11, 2009 6:55 am     Reply with quote

jaethelegend wrote:
Thanks for the reply.
One remaining question is that I am trying to save a port by grounding DAC latch as the data sheet suggests. However, it doesn't seem to be working.

If it is not working, then it probably has nothing to do with the DAC latch signal. Grounding it works fine for me.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
Franck26



Joined: 29 Dec 2007
Posts: 122
Location: Ireland

View user's profile Send private message

PostPosted: Fri Jun 12, 2009 4:34 am     Reply with quote

DAC latch grounded works fine for me too.

What do you mean by not working?
Is the output = 0V, 5V, or high impedance?

Franck.
jaethelegend
Guest







Bad chip
PostPosted: Mon Jun 15, 2009 12:27 am     Reply with quote

It started working fine with a new chip. Thanks
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group