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 CCS Technical Support

MCP4922 DAC not working
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 05, 2007 1:11 pm     Reply with quote

For some reason, CCS wrote their driver with a 3-byte array.
Because only 2 bytes are used with the MCP4921/22, they have to
skip some of the bits in the array when they transmit them to the DAC.
The code that you posted will skip the upper 4 bits of cmd[2] and
also it will skip the upper 4 bits of cmd[1].

Here is the layout of the array used by CCS:
Code:

cmd[2] = xxxx0011
cmd[1] = xxxxnnnn
cmd[0] = nnnnnnnn

The "xxxx" bits are skipped (not sent to the DAC). The "0011" bits are
the upper 4 bits of the command word. The "nnnnnnnnnnnn" bits
are the 12-bits of data sent to the DAC.
The Puma



Joined: 23 Apr 2004
Posts: 227
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Nov 05, 2007 1:19 pm     Reply with quote

What must i do to use only 2 cmd bytes?

Is this good
Code:
void mcp4922_write(int8 channel,int16 data) {
   int8 cmd[2],i;
   
   cmd[0]=(data>>8);
   cmd[1]=0x03;
      
   if(channel)
      cmd[1]|=0x08;
      
   output_low(MCP4922_CS);
   output_low(MCP4922_CLK);
   output_high(MCP4922_LDAC);
   
   for(i=0;i<=15;++i) {
      output_bit(MCP4922_DI,shift_left(cmd,3,0));
      output_high(MCP4922_CLK);
      output_low(MCP4922_CLK);
   }
   output_high(MCP4922_CS);
   output_low(MCP4922_LDAC);
   delay_us(10);
   output_HIGH(MPC4922_LDAC);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 05, 2007 1:24 pm     Reply with quote

Write your own driver. I've had enough.
The Puma



Joined: 23 Apr 2004
Posts: 227
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Nov 06, 2007 12:17 pm     Reply with quote

Now this my code, hopes that it works Wink

I can't test it now, my board from futurlec comes in a week


Code:

#include <18F4620.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOBROWNOUT,NOPUT,NOLVP,DEBUG,NOSTVREN,NOPROTECT

#include <mcp4922.h>

void main() {
   mcp4922_init();
   
   while(TRUE) {
      mcp4922_write(MCP4922_DAC_A_BUFFERED_GAIN_1X_NON_SHDN,4095);
      delay_ms(5000);
   }
}

Code:

#ifndef MCP4922_CS

#define MCP4922_CS    PIN_C0
#define MCP4922_CLK   PIN_C3
#define MCP4922_DIN   PIN_C5
#define MCP4922_LDAC  PIN_C6
#define MCP4922_SHDN  PIN_C7

#endif

// Write command register
#define MCP4922_DAC_A_BUFFERED_GAIN_1X_NON_SHDN      0x70  // DAC A, Buffered, Gain 1x, Output power control
#define MCP4922_DAC_A_BUFFERED_GAIN_2X_NON_SHDN      0x50  // DAC A, Buffered, Gain 2x, Output power control
#define MCP4922_DAC_A_NON_BUFFERED_GAIN_1X_NON_SHDN  0x30  // DAC A, Non buffered, Gain 1x, Output power control
#define MCP4922_DAC_A_NON_BUFFERED_GAIN_2X_NON_SHDN  0x10  // DAC A, Non buffered, Gain 2x, Output power control
#define MCP4922_DAC_B_BUFFERED_GAIN_1X_NON_SHDN      0xF0  // DAC B, Buffered, Gain 1x, Output power control
#define MCP4922_DAC_B_BUFFERED_GAIN_2X_NON_SHDN      0xD0  // DAC B, Buffered, Gain 2x, Output power control
#define MCP4922_DAC_B_NON_BUFFERED_GAIN_1X_NON_SHDN  0xB0  // DAC B, Non buffered, Gain 1x, Output power control
#define MCP4922_DAC_B_NON_BUFFERED_GAIN_2X_NON_SHDN  0x90  // DAC B, Non buffered, Gain 2x, Output power control

void mcp4922_init() {
   output_high(MCP4922_CS);
   output_high(MCP4922_CLK);
   output_high(MCP4922_DIN);
   output_high(MCP4922_LDAC);
   output_high(MCP4922_SHDN);
}
   
void mcp4922_write(int8 control,int16 data) {
   int8 cmd[2],i;
   
   cmd[0]=data;
   cmd[1]=(data>>8)|control;
   
   output_low(MCP4922_CS);
   output_low(MCP4922_CLK);
   output_high(MCP4922_LDAC);
   
   for(i=0;i<=15;++i) {
      output_bit(MCP4922_DIN,shift_left(cmd,2,0));
      output_high(MCP4922_CLK);
      output_low(MCP4922_CLK);
   }
   output_high(MCP4922_CS);
   output_low(MCP4922_LDAC);
   delay_us(10);
   output_HIGH(MCP4922_LDAC);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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