由AD7712(24位)模数转换到AT89S52中,串口助手显示AD_SDATA脚输出一直为1,求大侠探讨
时间:10-02
整理:3721RD
点击:
代码见下楼
- #include <AT89x51.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- #define ENABLE_INT EA = 1
- #define DISABLE_INT EA = 0
- //AD7712
- sbit AD_SCLK = P0^7;
- sbit AD_A0 = P0^6;
- sbit AD_SDATA = P0^5;
- sbit AD_DRDY = P0^4;
- sbit AD_RFS = P0^3;
- sbit AD_TFS = P0^2;
- void Uart_send(uchar dat)
- {
- DISABLE_INT;
- SBUF = dat;
- // while(TI==0);
- // TI = 0;
- ENABLE_INT;//?
- }
- void delay_nus(unsigned int n)
- {
- unsigned int i;
- for(i = n; i > 0; i--)
- {
- ;
- }
- }
- unsigned long AD7712_read_dat()
- {
- ulong res = 0;
- char index = 0;
- ulong bit_dat;
-
- DISABLE_INT;
-
- AD_RFS = 1;
- AD_SDATA = 1;
- AD_A0 = 0;
- AD_SCLK = 0;
- AD_DRDY = 1;
-
- while(AD_DRDY);
- AD_A0 = 1;
- AD_RFS = 0;
- for(index = 0;index < 24; index++)
- {
- delay_nus(10);
- AD_SCLK = 1;
- delay_nus(10);
- bit_dat = AD_SDATA;
- Uart_send(bit_dat);//输出一直为1,未解决?
- delay_nus(10);
- AD_SCLK = 0;
- res <<= 1;
- res +=bit_dat;
- //Uart_send(res);//输出一直为11111111111,未解决
- }
- AD_RFS = 1;
- AD_A0 = 0;
- ENABLE_INT;
- return res;
- }
- void main()
- {
- ulong uart_adc_dat;
- uint idx;
- ulong adc_dat;
- TMOD=0x20; //TMOD=0010 0000B,定时器T1工作于方式2
- SCON=0x40; //SCON=0100 0000B,串口工作方式1
- PCON=0x00; //PCON=0000 0000B,波特率9600
- TH1=0xfd; //根据规定给定时器T1赋初值
- TL1=0xfd; //根据规定给定时器T1赋初值
- TR1=1; //启动定时器T1
- while(1)
- {
- if(idx == 10)
- {
- idx = 0;
- adc_dat =AD7712_read_dat();
- }
- idx++;
- delay_nus(400);
- }
- }
求大侠