我是用模拟I/O口调试的时候,当读数据时,DRDY不会为0,程序卡在那里了。 数据就没法读了
#include<reg51.h> #include<stdio.h>
sbit ADCLK=P1^0; sbit ADCS=P1^1; sbit ADDRDY=P1^2; sbit ADDOUT=P1^3; sbit ADDIN=P1^4;
void delay(unsigned int k) { for(;k>0;k--){;} } void WriteTo_adc(unsigned char byte) { unsigned char token,i; token=0x80; ADCS=0; //ADCLK=0; for(i=0;i<8;i++) { ADCLK=0; if(byte&token) ADDIN=1; else ADDIN=0; ADCLK=1; token=token>>1; } ADCLK=1; ADDIN=1; ADCS=1; } unsigned long Read_adc(void) { unsigned long temp; unsigned char i; //ADDRDY=0; //ADDRDY=0;
while(ADDRDY);//{delay(10);} WriteTo_adc(0x58);
ADCS=0; temp=0; //ADCLK=0; for(i=0;i<24;i++) { ADCLK=0; temp=(temp|ADDOUT); ADCLK=1; temp=temp<<1; } temp=temp>>1; ADCLK=1; ADCS=1; return(temp); } void init() { //ADDRDY=0; WriteTo_adc(0x24); WriteTo_adc(0xC1); WriteTo_adc(0x34); WriteTo_adc(0x80); WriteTo_adc(0x14); WriteTo_adc(0x21); delay(1000); } void main() { unsigned long addata=0; unsigned char i,a[3]; init(); delay(10); TMOD=0x20; TL1=0xfd;TH1=0xfd; SCON=0xd8;PCON=0x00; TR1=1; while(1) { addata=Read_adc(); a[0]=(addata>>16)&0xff; a[1]=(addata>>8)&0xff; a[2]=addata&0xff; for(i=0;i<3;i++) { SBUF=a; while(TI==0); TI=0; }
} } |