如何用C代码为AD7705编写驱动程序

时间:2018-08-27 10:59:08

标签: c driver adc

我想通过ATMEGA8微控制器运行AD7705,并且使用了以下C.code: `

DDRB = 0x6e;     //PinB.1(DRDY')=output , PinB.2(SS')=output , PinB.3(MOSI) = output , PinB.4(MISO)=input , PinB.5(SCK)=output , PinB.6(Reset') = output
PORTB = 0x91;//outputs=0 & inputs= pullup

if (EXTRF == 1) {PORTB.6=0;delay_ms(1);}//Reset the ADC with Reseting the micro

SPCR = 0x70;//SPI Enabled, MSB Data Order, Master, Sampling by Rising Edge, SCKF=fosc/4

SPSR = 0x00;

//Serially reset the Chip 
for (j = 0; j < 5; j++)
{
    SPDR = 0xff;
    delay_ms(10);
}
while(!(SPSR & (1<<SPIF)));//wait for transfer

SPDR = 0b00100000;//Send to Comm Reg: Next Write Clk =0x20
while(!(SPSR & (1<<SPIF)));//wait for transfer

SPDR = 0b00000100;//Send to ClkReg: Clock Bits and Update rate =0x04 ->CLKDIV=0,CLK=1,Filter Selection=00(output update rate = 50Hz)
while(!(SPSR & (1<<SPIF)));//Wait for transfer 

SPDR = 0b00010000;//Send to Comm Reg: Next Write Setup reg =0x10 
while(!(SPSR & (1<<SPIF)));//Wait for transfer

// set setup register: Self Calibration Mode,gain=1,
// unipolar operation,buffer enabled,
// filter synchronization in set state =0x06
SPDR = 0b01000110;
while(!(SPSR & (1<<SPIF)));//Wait for transfer

SPDR = 0b00001000;//Send to Comm: next read Comm reg =0x08 
while(!(SPSR & (1<<SPIF)));//Wait for transfer        

SPDR = 0b00111000;//Send to Comm: next read Data reg =0x38 MS-Byte
while(!(SPSR & (1<<SPIF)));//Wait for transfer

read = SPDR;

SPDR = 0b00111000;//send to Comm: next read Data reg =0x38 LS-Byte
while(!(SPSR & (1<<SPIF)));//Wait for transfer

read <<= 8//shift to MS-Byte of int read
read = read|SPDR;// make 16 bit data
read2 = read;    
read2 = (read2*2.5)/65535;
ftoa(read2,6,str);
lcd_gotoxy(0,1);
lcd_puts("V=");
lcd_gotoxy(2,1);
lcd_puts(str);

我的问题是在输出中我总是得到常数(2.00或2.22)!我不知道该如何更改才能获得正确的输出。 作为基准电压,我施加2.5 V,给AD7705上电时施加3V。 增益= 1,更新速率= 50 Hz,通道1,单极,时钟= 2.4576 MHz AD7705 with ATMEGA8A

0 个答案:

没有答案