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

ad7706 pic16f877

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







ad7706 pic16f877
PostPosted: Mon Jul 19, 2004 8:36 am     Reply with quote

nobody can help me please??

I've got a souce code:
it is corect??




#include <16F877.h>
#device adc=10
#use Delay(Clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7)
#use i2c(Master,Fast,sda=PIN_C4, scl=PIN_C3)
#fuses XT, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP


//Connexions
#define ADC_DRDY PIN_B1 //
#define ADC_DO PIN_B2 // sortie digitale
#define ADC_DI PIN_B3 // registre de donn�es (clock.....)
#define ADC_RESET PIN_B4 // reset si 0
#define ADC_CS PIN_B5 // s�lection du device si 0
#define ADC_CLK PIN_B6 // horloge provenant du pic

//Operation modes
#define ADC_NORMAL 0x00 // MD1 = 0 MD0 = 0
#define ADC_SELF 0x40 // MD0 = 1 MD1 = 0
#define ADC_ZERO_SCALE 0x80 // MD1 = 1 MD0 = 0
#define ADC_FULL_SCALE 0xc0 // MD1 = 1 MD0 = 1

//Gain
#define ADC_GAIN_1 0x00 //choix de gain, voir registre (doc AD7706)
#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 // FS0=0 FS1=0 CLK=1
#define ADC_60 0x05 // FS0=1 FS1=0 CLK=1
#define ADC_250 0x06 // FS0=0 FS1=1 CLK=1
#define ADC_500 0x07 // FS0=1 FS1=1 CLK=1

void adc_init();
void write_adc_byte(byte);
long int read_adc_word();
void setup_adc_device(int,int,int,int);
long int read_adc_value(int1);
void adc_disable();
float convert_to_volts(long);

float tension, courant;
long data;


//----------------------------------------------------------------------
//----------------- PROG PRINCIPAL -----------------
//----------------------------------------------------------------------


void main()
{

long int adc_ch0;

printf("HELLO!!\n\r");
adc_init();

while (1)
{
printf("HELLO!!\n\r");
adc_init();
adc_ch0 = read_adc_value(0);
printf(" ADC CH0: %lx\n\r",adc_ch0 );
adc_disable();
}

}

//---------------------------------------------------------------------------------
//-------------------------- INITIALISATION --------------
//---------------------------------------------------------------------------------

void adc_init()
{
output_low(ADC_RESET); // RESET � 0
output_high(ADC_CLK); // CLK � 1
output_high(ADC_CS); // s�lection device
output_high(ADC_RESET); // Set high to AD7715 reset low pin
setup_adc_device(ADC_SELF,ADC_GAIN_1,ADC_UNIPOLAR,ADC_50); // gain = 1 ..... r�solution de 76�V (ref � 5v)
delay_ms(3000); // tempo 3s
}


void write_adc_byte(byte data)
{
byte i;

output_low(ADC_CS); // s�lection du device
for(i=1;i<=8;++i) // compte de 1 � 8
{
printf("i= %u \r",i);
output_low(ADC_CLK); // horloge �tat bas
output_bit(ADC_DI, shift_left(&data,1,0)); //
output_high(ADC_CLK); // horloge �tat haut
}
output_high(ADC_CS); // le device n'est plus s�lectionn�
}


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

output_low(ADC_CS); //s�lection device
for(i=1;i<=16;++i)
{
output_low(ADC_CLK); // horloge �tat bas
output_high(ADC_CLK); // horloge �tat haut
shift_left(&data,2,input(ADC_DO));
}
output_high(ADC_CS); // le device n'est plus s�lectionn�
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 ); //00100000 //Communications Register set to write of clock register
write_adc_byte( rate ); //0x16 //Clock Register info here
write_adc_byte( 0x10 ); //00010000 //Communications Register set to write of setup register
write_adc_byte( calmode|gainsetting|operation); //0x01 //Setup Register info here
}

//----------------------------------------------------------------------------------
//---------------------- LECTURE ADC --------------
//----------------------------------------------------------------------------------

long int read_adc_value(int1 ch)
{
long int value;
while ( !input(ADC_DRDY) );
if(ch)

write_adc_byte(0x38); //00111000 //communications register set to read of data register of channel 1

else

write_adc_byte(0x39); //00111001 //communications register set to read of data register of channel 0
printf("go!!");
value=read_adc_word();
printf("value: %lx",value);

while ( input(ADC_DRDY) );
return value;
}

//----------------------------------------------------------------------------------
//---------- DISABLE A/D CONVERSION ----------------
//----------------------------------------------------------------------------------

void adc_disable()
{
write_adc_byte( 0x20 ); //00100000 //Communications Register set to write of clock register
write_adc_byte( 0x10 ); //00010000 //Clock Register info here
}

//---------------------------------------------------------------------------------
//----------- CONVERTIR VALEUR LUE EN VOLT ---------------
//---------------------------------------------------------------------------------

float convert_to_volts(long data)
{
return ((float)data*5/0xffff); // 2.5 = VREF et FFFF=65535
}

//------------------------------------------------- END ------------------------------------------------------
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

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

PostPosted: Mon Jul 19, 2004 8:59 am     Reply with quote

You don't give any description of your problem but the first thing I see is your fuse statement. Your #use Delay() statement says you are going to use a 20MHz xtal but you have set the crystal drive circuit for XT which typically poops out at 4MHz. You should be using HS for a 20MHz xtal.

Another tip, try modifying your #use i2c to be the software version instead of the HW version and also run at the "slow" speed. I dont' have a datasheet in front of me to check your pin assignments for SDA and SCL.

Are you using appropriate pull-up resistors on the SDA and SCL? Typicall values are 2K to 50K depending on line length, speed and what is hanging on the other end of the lines. The AD7706 datasheet might have some suggestions.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
nicolas33
Guest







ad7706 pic16f877
PostPosted: Mon Jul 19, 2004 9:31 am     Reply with quote

I don't use i2C for the AD7706...
I'm ok with you for HS instead of XT..
but the program doesn't work
I have value = 0000
thank you for your help!!
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