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

PIC driving an external DAC?

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



Joined: 21 Jul 2005
Posts: 36

View user's profile Send private message

PIC driving an external DAC?
PostPosted: Fri Aug 12, 2005 3:49 am     Reply with quote

Hiya,

I realise this isn't strictly PIC C related, but maybe you can shed some light.

I am using a PIC to control a DAC (MAX5712). To diagnose this problem, I've connected digital in (DIN) to the PIC (it would normally be connected to an opto-coupler), as well as SCLK and CS.

My code is as follows:

Code:


#define Fosc 40000000           
#include <18F8621.h>
#DEVICE *=16 ADC=8
#include <string.h>
#include <stdio.h>

//-------------------------------------------------------------------------------------
// DEFINES
#define LED PIN_C0
#define DLED PIN_C1
#define ADC_SCLK PIN_E0
#define ADC_CS PIN_E2
#define DAC_SCLK PIN_H6
#define DAC_CS PIN_H7      
#define DAC_PIC_CTRL PIN_H5
//-------------------------------------------------------------------------------------
// COMPILER DIRECTIVES and HARDWARE CONFIGURATION
#use delay(clock = Fosc)

#fuses EC_IO
#fuses NOOSCSEN      // Oscillator System Clock Switch Disabled
#fuses NODEBUG      // No Background Debugger
#fuses NOLVP         // Low Voltage ICSP Disabled
#fuses NOPROTECT    // No Code Protect
#fuses NOWDT         // No onboard watchdog
#fuses PUT            // Power Up Timer Enabled
#fuses BROWNOUT      // Brown Out Reset enabled

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,parity=N,bits=8)     

static short ADCFlag = 1;
static short DACFlag = 1;
static short DACCS = 1;
static short ADCCS = 1;

int trcnt = 0;
int trackk=0;
int8 MaxV[16]={0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1};

// MAIN
void main()
{
   while(true)
{
if(trcnt==0){output_bit(DAC_CS,1); };
if(trcnt==4){  DACFlag=1; ADCFlag=1; output_bit(DAC_CS,0); DACCS=0; output_bit(ADC_CS,0);  ADCCS=0; output_bit(DAC_SCLK,1);  };

      if((trcnt>=4)&&(trcnt<=37)){ output_bit(DAC_CS,0);  DACCS=0; if(DACFlag==0){output_bit(DAC_PIC_CTRL,MaxV[trackk]); trackk=++trackk;}; output_bit(DAC_SCLK,DACFlag);};

if(trcnt==38){output_bit(ADC_CS,1); ADCCS=1; output_bit(DAC_CS,1); DACCS=1;};
   
      ADCFlag=~ADCFlag;
      DACFlag=~DACFlag;
      trcnt=++trcnt;   
       delay_cycles(75); //set back to  50
      if(trcnt==45){trcnt=0; trackk=0;}; //set back to 30
};
}


Edit:

Bottom of my post appears to have been lopped off.
The DAC datasheet specifies 4 leading zeros, which it reads in on the falling edge of SCLK, followed by a 12bit DAC code, i.e. 12 1's for maximum output.
The oscilloscope output seems fine, 4 leading zero's on the falling edge of SCLK, followed by the 12 bits...

Is it something obvious I'm overlooking? Have tried different DACs, so I doubt the DAC is faulty....
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Aug 12, 2005 6:13 am     Reply with quote

And your problems is?????????????
Ttelmah
Guest







PostPosted: Fri Aug 12, 2005 6:16 am     Reply with quote

I drove this chip some time ago.
The driver routine was:
Code:


void send_dac(int16 val) {
   int8 mask;
   int8 ctr;
   mask=0x8000;
   DAC_CS=0;
   for (ctr=0;ctr<32;) {
      if (ctr&1) {
          DAC_CLK=0;
      }
      else {
           DAC_CLK=1;
           if (val & mask) output_bit(DAC_DAT,1);
           else output_bit(DAC_DAT,0);
           mask /=2;
      }
      ctr++;
   }
   DAC_CLK=1;
   DAC_CS=1;
}


DAC_DAT, DAC_CS, and DAC_CLK were connected to and defined as the three control pins.

This was called with:
Code:

#define WAKE (0xF000)

   send_dac(WAKE);
   send_dac(val_to_send);


Your code looks bulkier than this (and requires sixteen numbers, instead of just a single 16bit value). However I'd suspect the 'key' difference is sending the 'WAKE' value first. If I remember correctly, the chip has the ability to go into a low power 'sleep' mode, and you must wake it up, before sending a value.

Best Wishes
Guest








PostPosted: Fri Aug 12, 2005 10:44 am     Reply with quote

WAKE was indeed the problem Embarassed

But that code looks much more flexible than mine, will take a look at it thanks Smile
bwgames



Joined: 21 Jul 2005
Posts: 36

View user's profile Send private message

PostPosted: Tue Aug 16, 2005 4:23 am     Reply with quote

Been trying to get your code above working, and can't seem to get any data output, I get the CS and SCLK out fine, but nothing on the DATA, even for WAKE.

Only modifications I've made is to replace (e.g.) DAC_CS =1 with output_bit(DAC_CS,1) etc.

I'm assuming you are doing bit masking, hence the val & mask, but what does the mask /= 2; line do? Can't seem to find any reference to /= Question

thanks Very Happy
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 16, 2005 6:02 am     Reply with quote

Standard C

mask = mask / 2;
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Tue Aug 16, 2005 10:24 am     Reply with quote

Mark wrote:
Standard C

mask = mask / 2;


Same as:

mask >>= 1;

or

mask = mask >> 1;
_________________
-Matt
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