📄 ad7788.c
字号:
#include "common.h"
#include <iom32v.h>
#include "ad7788.h"
#include "display.h"
#include "wh_4008a.h"
/* 向AD7788写入一个字节 */
void WriteByte7788(unsigned char x)
{
uchar i;
ad7788_sclk_high;
//nop;
//nop;
for (i=0; i<8; i++)
{
ad7788_sclk_low;
//nop;
//nop;
if((x&0x80)==0x80)
{
ad7788_din_high;
}
else
{
ad7788_din_low;
}
//nop;
//nop;
ad7788_sclk_high;
//nop;
//nop;
x <<=1;
}
}
/* 从AD7788读一个字 节*/
unsigned char ReadByte7788(void)
{
uchar i;
uchar x ;
x=0;
ad7788_sclk_high;
//nop;
//nop;
for (i = 0 ; i < 8 ; i++)
{
ad7788_sclk_low;
nop;
nop;
nop;
nop;
nop;
nop;
ad7788_sclk_high;
// nop;
// nop;
x<<=1;
if(ad7788_dout_high)
{
x |= 0x01;
}
}
return x;
}
/* AD7788通信端口复位 */
void Reset7788(void)
{
uchar i;
for(i=0;i<50;i++) /* 多于连续32个 DIN=1 使串口复位 */
{
ad7788_sclk_low;
nop;
nop;
ad7788_din_high;
nop;
nop;
ad7788_sclk_high;
nop;
nop;
}
}
uchar adc_error_flag;
//const uchar num_tab[17]="0123456789abcdef";
ulong read_ad7788(void)
{
uchar data_high2,data_low,data_high1;
uchar temp_uchar;
ulong temp_long;
ad7788_csn_low;
//nop;
//nop;
//Reset7788();
//delay_1ms(20);
//对filter register Write
WriteByte7788(0x20);
//对filter register 写的内容:clock-Normal Mode,16.6hz 4 1.5 65 dB @ 50 Hz/60 Hz (Default Setting)
//WriteByte7788(0x04);
//对filter register 写的内容:clock-Normal Mode,33.3hz
WriteByte7788(0x02);
//对filter register 写的内容:clock-Normal Mode,100hz
//WriteByte7788(0x01);
//对filter register 写的内容:clock-Normal Mode,16.6hz 4 1.5 80 dB @ 50 Hz
//WriteByte7788(0x05);
//对mode register Write
//AIN(+) – AIN(–)
WriteByte7788(0x10);
//对mode register 写的内容:Single-conversion mode,unipolar,unbuffer
WriteByte7788(0x84);
//对mode register 写的内容:Single-conversion mode,unipolar,buffer
//WriteByte7788(0x86);
//对mode register 写的内容:Continuous-conversion mode,unipolar,unbuffer
//WriteByte7788(0x04);
//对mode register 写的内容:Continuous-conversion mode,unipolar,buffer
//WriteByte7788(0x06);
//adc_error_flag=false;
//等待DOUT电平拉低
while(ad7788_dout_high);
/*
while(1)
{
WriteByte7788(0x00);
//nop;
//nop;
nop;
temp_uchar=ReadByte7788();
if((temp_uchar&0x80)==0x00)
{
//
//putchar_test(0xbb);
break;
}
}
*/
//nop;
//nop;
//nop;
/*
WriteByte7788(0x00);
temp_uchar=ReadByte7788();
if((temp_uchar&0x40)==0x40)
{
//putchar_test(0xcc);
adc_error_flag=true;
return 0;
}
*/
WriteByte7788(0x38);
data_high2=ReadByte7788();
data_high1=ReadByte7788();
data_low=ReadByte7788();
ad7788_csn_high;
nop;
nop;
ad7788_din_high;
/*
//第二次
ad7788_csn_low;
nop;
nop;
WriteByte7788(0x10);
WriteByte7788(0x84);
WriteByte7788(0x20);
WriteByte7788(0x02);
nop;
nop;
while(ad7788_dout_high);
nop;
nop;
WriteByte7788(0x38);
nop;
nop;
data_high2=ReadByte7788();
data_high1=ReadByte7788();
data_low=ReadByte7788();
ad7788_din_high;
ad7788_csn_high;
*/
temp_long=(ulong)data_high2*0x10000+(ulong)data_high1*0x100+(ulong)data_low;
//return ((float)temp_long)*vref/16777216.0;
return temp_long;
}
ulong value_buf[AD_N];
uchar count_ad_i=0;
float read_filter_ad(void)
{
uchar count;
float sum=0.0;
loop1:
value_buf[count_ad_i++]=read_ad7788();
if(adc_error_flag==true)
{
adc_error_flag=false;
//Reset7788();
goto loop1;
//led_buffer[14]='b';
}
else
{
//led_buffer[14]='a';
}
if (count_ad_i >= AD_N)
{
count_ad_i=0;
}
for ( count=0;count<AD_N;count++)
{
sum =sum+ value_buf[count];
}
return (((float)sum)*vref/(AD_N*16777216.0));
}
//tiaoshi
ulong read_filter_ad_test(void)
{
uchar count;
ulong sum=0.0;
value_buf[count_ad_i++]=read_ad7788();
if (count_ad_i >= AD_N)
{
count_ad_i=0;
}
for ( count=0;count<AD_N;count++)
{
sum =sum+ value_buf[count];
}
return (sum/AD_N);
}
//tiaoshi end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -