bernardinim
Joined: 07 Dec 2005 Posts: 10 Location: Italy
|
AD7705 - CODICE FUNZIONANTE |
Posted: Thu Feb 02, 2006 10:34 am |
|
|
//connection pins to the PIC
#define ADC_DRDY PIN_B0
#define ADC_CS PIN_B5
#define ADC_DI PIN_C5
#define ADC_DO PIN_C4
#define ADC_CLK PIN_C3
//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 0x00
#define ADC_UNIPOLAR 0x04
//Buffer mode
#define ADC_BUFFER_OFF 0x00
#define ADC_BUFFER_ON 0x02
//fsync
#define ADC_FSYNC_RESET 0x01
#define ADC_FSYNC_START 0x00
//update rates
#define ADC_20 0x00
#define ADC_25 0x01
#define ADC_100 0x02
#define ADC_200 0x03
#define ADC_50 0x04//no
#define ADC_60 0x05//no
#define ADC_250 0x06//no
#define ADC_500 0x07//no
//master clock
#define ADC_MASTERCLK_ENABLE 0x00
#define ADC_MASTERCLK_DISABLE 0x10
//clock divider
#define ADC_CLKDIVIDER_ON 0x08
#define ADC_CLKDIVIDER_OFF 0x00
#define ADC_CHANNEL1 0x00
#define ADC_CHANNEL2 0x01
void write_adc_byte(BYTE data)
{
BYTE i;
//output_high(ADC_CLK);//modif
output_low(ADC_CS);
spi_write(data);
output_high(ADC_CS);
}
long int read_adc_word()
{
BYTE i;
int data_low,data_high;
int16 data;
//output_high(ADC_CLK);//modif
output_low(ADC_CS);
data_high=spi_read(0);
data_low =spi_read(0);
output_high(ADC_CS);
data=make16(data_high,data_low);
return data;
}
//setup register settings
void adc7705_setup_setupregister(int adcmode, int gainsetting, int operation, int buffersetting, int fsync, int channel)
{
write_adc_byte( 0x10|channel );//Communications Register set to write of setup register
write_adc_byte( adcmode|gainsetting|operation|buffersetting|fsync );//Setup Register info here
}
//clock regster settings
void adc7705_setup_clockregister(int masterclock, int clockdivider, int clock_filterselection, int channel)
{
write_adc_byte( 0x20|channel );//Communications Register set to write of clock register
write_adc_byte( 0x00|masterclock|clockdivider|clock_filterselection );//Clock Register info here
}
//init routine
void adc7705_init()
{
adc7705_setup_setupregister(ADC_SELF,ADC_GAIN_1,ADC_UNIPOLAR,ADC_BUFFER_ON,ADC_FSYNC_START,ADC_CHANNEL1);
adc7705_setup_clockregister(ADC_MASTERCLK_ENABLE,ADC_CLKDIVIDER_ON,ADC_100,ADC_CHANNEL1);
delay_ms(3000);
adc7705_setup_setupregister(ADC_SELF,ADC_GAIN_1,ADC_UNIPOLAR,ADC_BUFFER_ON,ADC_FSYNC_START,ADC_CHANNEL2);
adc7705_setup_clockregister(ADC_MASTERCLK_ENABLE,ADC_CLKDIVIDER_ON,ADC_100,ADC_CHANNEL2);
delay_ms(3000);
}
//read an adc value from the specified channel
long int adc7705_read_value(int ch)
{
long int value;
while (!input(ADC_DRDY) );
if(ch==1) {write_adc_byte(0x38);//communications register set to read of data register of channel 1
//output_high(TX_ENABLE);printf("\r\n\write0x38\r\n");output_low(TX_ENABLE);
}
if(ch==2) {write_adc_byte(0x39);//communications register set to read of data register of channel 2
//output_high(TX_ENABLE);printf("\r\n\write0x39\r\n");output_low(TX_ENABLE);
}
while ( input(ADC_DRDY) );
value=read_adc_word();
return value;
}
//disable the a/d conversion
void adc7705_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 adc7705_convert_to_volts(long data){
return ((float)data*2.5/0xffff);
} |
|