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

driver AD7705.C is false!!

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



Joined: 10 Sep 2003
Posts: 2
Location: St-Etienne, France

View user's profile Send private message Visit poster's website

driver AD7705.C is false!!
PostPosted: Fri Jan 27, 2006 4:21 am     Reply with quote

I have lost a lot of hours with CCS's driver for AD7705 (V 3.227, 2005).
I think there are 2 errors (of course, I verified with Analog device data-sheets).
1: Inversion of the sign of signal DRDY
2: inversion of channel 1 & 2
Here is the corrected driver:
And now my AD7705 is running!

Code:

/////////////////////////////////////////////////////////////////////////////
////                              AD7705.C                               ////
////                     Driver for analog device AD7705                 ////
////                                                                     ////
//// adc_init()                    Call after power up                   ////
////                                                                     ////
//// read_adc_value(channel)Read adc value from the specified channel    ////
////                                                                     ////
//// adc_disable()    Disables the adc conversion                   ////
/////////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2003 Custom Computer Services             ////
//// This source code may only be used by licensed users of the CCS C    ////
//// compiler.  This source code may only be distributed to other        ////
//// licensed users of the CCS C compiler.  No other use, reproduction   ////
//// or distribution is permitted without written permission.            ////
//// Derivative programs created using this software in object code      ////
//// form are not restricted in any way.                                 ////
/////////////////////////////////////////////////////////////////////////////
//// Driver routines for the AD7705 chip
//Assuming a 2.4576 crystal ocsillator is used between MCLK IN and MCLK OUT

// corrected P Breuil, Ecole des Mines saint-Etienne France

//connection pins to the PIC
#define ADC_DRDY  PIN_B1
#define ADC_DO    PIN_B2
#define ADC_DI    PIN_B3
#define ADC_RESET PIN_B4
#define ADC_CS    PIN_B5
#define ADC_CLK   PIN_B6

//Operation modes
#define ADC_NORMAL 0x00
#define ADC_SELF 0x40
#define ADC_ZERO_SCALE 0x80
#define ADC_FULL_SCALE 0xc0

//Gain settings
#define ADC_GAIN_1 0x00
#define ADC_GAIN_2 0x08
#define ADC_GAIN_4 0x10
#define ADC_GAIN_8 0x18
#define ADC_GAIN_16 0x20
#define ADC_GAIN_32 0x28
#define ADC_GAIN_64 0x30
#define ADC_GAIN_128 0x38

//Polar operations
#define ADC_BIPOLAR 0x04
#define ADC_UNIPOLAR 0x00

//update rates
#define ADC_50 0x04
#define ADC_60 0x05
#define ADC_250 0x06
#define ADC_500 0x07
void write_adc_byte(BYTE data)
{
   BYTE i;

   output_low(ADC_CS);
   for(i=1;i<=8;++i) {
      output_low(ADC_CLK);
      output_bit(ADC_DI, shift_left(&data,1,0));
      output_high(ADC_CLK);
   }
   output_high(ADC_CS);
}


long int read_adc_word()
{
   BYTE i;
   long data;

   output_low(ADC_CS);
   for(i=1;i<=16;++i) {
      output_low(ADC_CLK);
      output_high(ADC_CLK);
      shift_left(&data,2,input(ADC_DO));
   }
   output_high(ADC_CS);
   return data;
}


//setup the device paramaters(mode, gainsetting, polar operation and output rate)
void setup_adc_device(int calmode, int gainsetting, int operation, int rate)
{
    write_adc_byte( 0x20 );//Communications Register set to write of clock register
   write_adc_byte( rate );//Clock Register info here
     write_adc_byte( 0x10 );//Communications Register set to write of setup register
   write_adc_byte( calmode|gainsetting|operation);//Setup Register info here
}

//initailaization routine

void adc_init()
{
   output_low(ADC_RESET);
     output_high(ADC_CLK);
   output_high(ADC_CS);   //Set low to AD7715 chip select low pin
   output_high(ADC_RESET);   //Set high to AD7715 reset low pin
   setup_adc_device(ADC_SELF,ADC_GAIN_1,ADC_BIPOLAR,ADC_50);
   delay_ms(3000);
}

//read an adc  value from the specified channel
long int read_adc_value(int1 ch)
{
   long int value;
 //  while ( !input(ADC_DRDY) );  False!! must wait a falling slope!
  while ( input(ADC_DRDY) );
   if(ch)
 //     write_adc_byte(0x38);//communications register set to read of data             
register of channel 1  False!
     write_adc_byte(0x39);//communications register set to read of data ch1   
else
 //     write_adc_byte(0x39);//communications register set to read of data              register of channel 0  false!
write_adc_byte(0x38);//communications register set to read of data
   value=read_adc_word();
 //  while ( input(ADC_DRDY) );  False again, rising slope!
  while ( !input(ADC_DRDY) ); // but this line has no utility!
   return value;
}

//disable the a/d conversion
void adc_disable()
{
    write_adc_byte( 0x20 );//Communications Register set to write of clock register
   write_adc_byte( 0x10 );//Clock Register info here
}

//Convert the value read to volts
float convert_to_volts(long data){
   return ((float)data*2.5/0xffff);
}

Mad Mad Mad Mad
draghi
Guest







PostPosted: Sun Mar 11, 2007 3:57 am     Reply with quote

I think the following definitions are also wrong (inverted)

//Polar operations
#define ADC_BIPOLAR 0x04
#define ADC_UNIPOLAR 0x00

should be

//Polar operations
#define ADC_BIPOLAR 0x00
#define ADC_UNIPOLAR 0x04

...

BTW, has anybody some code which reads all three channels on a AD7706?
championx



Joined: 28 Feb 2006
Posts: 151

View user's profile Send private message

PostPosted: Wed May 21, 2014 8:29 pm     Reply with quote

Hi! I'm using your driver but i can't read from the channel 2. I always get 0.
Code:

adc_init();

adc2 = read_adc_value(1);
delay_ms(300);
adc1 = read_adc_value(0);
delay_ms(300);
printf("ADC %010lu %010lu",adc1,adc2 );
delay_ms(1000);

Any ideas? The 2 channels are configured identically (hardware).
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 22, 2014 10:39 am     Reply with quote

Quote:
The 2 channels are configured identically (hardware).

Post a link to the schematic for your AN7705 board. Show the voltages
that you have connected to pins AN1(+) and AN1(-), and also for
AN2(+) and AN2(-). Hopefully, you have connected stable DC voltages
to these pins for your initial testing.

The schematic should show everything, such as the crystal frequency
for MCLK, and all external component values.

Also, don't post to multiple old threads. It won't help you.
championx



Joined: 28 Feb 2006
Posts: 151

View user's profile Send private message

PostPosted: Tue May 27, 2014 6:38 am     Reply with quote

Besides, when configures the adc it only configures de adc channel 0. So, if you want to use the 2 channels you cant. You need to configure the seccond channel and then you are ready to read it.
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