各位大佬,我想用AD读取下电压,通过LCD显示,单片机用的STC12C5A60S2,20M外接晶振,1T的工作模式,AD用的AD7091R,这个AD是SPI的方式通信,但是写完程序调试不出结果,有没有输入都是显示4.99V,感觉是AD的DO引脚接的单片机引脚一直高电平的结果,AD貌似没有工作,AD前面接了AD820滤波,当有输入的时候测820输出和AD7091输入都是没问题的,麻烦各位大佬给看看程序有啥问题,感激不尽。
这个图引脚不对,AD的几个引脚从上到下是3.2-3.5
- </blockquote></div><div><div class="blockcode"><blockquote>#include "STC12C5A60S2.H"
- #include "tool.h"
- //ADC signal pin defination
- sbit ad_do = P3^2;
- sbit ad_cs = P3^3;
- sbit ad_cvt = P3^4;
- sbit ad_clk = P3^5;
- //ADC7091R Reset
- void adc_reset(void){
- unsigned char i;
-
- ad_cvt = 0; Delay1us();
- ad_cvt = 1; Delay1us();
- ad_cs = 0; Delay1us();
-
-
- for(i=0;i<4;i++){
- ad_clk = 1; Delay1us();
- ad_clk = 0; Delay1us();
- }
- ad_cs = 1;
- ad_cvt = 0; Delay1us();
- ad_cvt = 1;
-
- for(i=0;i<12;i++){
- ad_clk = 1; Delay1us();
- ad_clk = 0; Delay1us();
- }
- }
- //ADC7091 Initialization
- void adc_init(void){
-
- ad_cvt = 1;
- ad_cvt = 0;
- ad_cvt = 1;Delay1us();
- ad_cs = 0;
-
-
- }
- //ADC7091 Converst progress
- uint adc_conv(void){
- uchar i=0;
- uint dat=0;
- adc_init();
- for(i=0; i<12; i++){
- ad_clk = 1;
-
- dat <<= 1;Delay1us();
- if(ad_do) dat |= 0x0001;
- Delay1us();
- ad_clk = 0;Delay1us();
-
- }
-
- ad_cs = 1;
- return dat;
- }
复制代码
主程序
- #include "adc7091.h"
- #include "IIC_1602.h"
- void Delay1000ms() //@20.000MHz
- {
- unsigned char i, j, k;
- i = 76;
- j = 255;
- k = 241;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
- void main(){
- //数据
- uchar i=0;
- uint ad_res;
- uint ad_view = 0;
- //IO口和LCD初始化
- IO_Init();
- init();
- init_lcd();
- adc_reset();
- //adc_init();
- while(1){
- ad_res = adc_conv();
-
- ad_view = (ad_res*250.0/4095.0);
- bit_dis(ad_view);
- yuanshi(ad_res);
- ShowString(1,1,"V:");
- write_date(yuanshi_arr[3]);
- write_date(yuanshi_arr[2]);
- write_date(yuanshi_arr[1]);
- write_date(yuanshi_arr[0]);
-
- ShowString(2,1,"Voltage:");
- write_date(res_arr[2]);
- write_date('.');
- write_date(res_arr[1]);
- write_date(res_arr[0]);
- //init_lcd();
- //Delay1000ms();Delay1000ms();
- }
- }
复制代码
AD7091R_cn.pdf
(1.25 MB, 下载次数: 4)
|