|
----------------------------------ADC7705.H---------------------------
#ifndef _ADC7705_H_
#define _ADC7705_H_
#define ADC_gain 0x28
#define ad_sck P1_1
#define ad_dout P1_2
#define ad_din P1_3
// инициализация АЦП
void adc_ini( bit chnl );
// чтение значения с АЦП
unsigned int adc_read( bit chnl );
#endif
----------------------------------ADC7705.C--------------------------- //#pragma disable //#pragma disable void adc_ini( bit chnl ) unsigned int adc_read( bit chnl )
E-mail:
info@telesys.ru
// ***************************>>>ADC routines*******************
#include
#include "ADC7705.h"
void send_byte_spi( unsigned char byte )
{ // отправка по SPI одного байта
register unsigned char i;
for ( i = 8; i; i--)
{ // пишем поразрядно byte в ad_dout, стробируя ad_sck
ad_sck = 0;
ad_dout = (bit)( byte & 0x80 ) ;
ad_sck = 1;
byte <<= 1;
}
}
unsigned int read_word_spi( void )
{ // чтение слова по SPI
unsigned int word;
register unsigned char i;
for ( i = 16; i; i-- )
{ // читаем поразрядно ad_din, стробируя ad_sck,
// и формируем значение word_in
word <<= 1;
ad_sck = 0;
word += (unsigned int)ad_din;
ad_sck = 1;
}
return word;
}
{ // инициализация АЦП
send_byte_spi( 0x20 | (unsigned char)chnl ); //0010 0000 Active channel Ain1(+) Ain1(-)
//next operation as write to clock reg of AD, stby=0,
send_byte_spi( 0x0c); //0 clkdis,1 clkdiv,1 clk(4.9 MHz)
//00 25Hz output update rate & notch, 6.55Hz cutoff
send_byte_spi( 0x10 | (unsigned char)chnl ); //0010 0000 Active channel Ain(+) Ain(-)
//next operation as write to setup
send_byte_spi( 0x42 | ADC_gain );
// send_byte_spi(0x72); //01 perform selfcalibration, 110 gain=64,
//0 bipolar operation,1 buffer on, 0 filter synchronization off
}
{ // чтение значения с требуемого канала АЦП
send_byte_spi( 0x38 | (unsigned char)chnl ); //Set next operation for 16 bit read from the data register
return read_word_spi();
}
Ответы