#include <reg51.H> #include "intrins.h" #define uint unsigned int #define uchar unsigned char //ADC0832的引脚 sbit ADCS=P2^0; //ADC0832 chip seclec sbit ADDI =P3^7; //ADC0832k in sbit ADDO =P3^7; //ADC0832 k out sbit ADCLK =P3^6; //ADC0832 clock signal sbit speaker =P1^4; unsigned char dispbitcode[8]={0xf7,0xfb,0xfd,0xfe,0xef,0xdf,0xbf,0x7f}; //位扫描 unsigned char dispcode[11]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff}; //共阳数码管字段码 unsigned char dispbuf[4]; uint temp; uchar getdata; //获取ADC转换回来的值 void delay_1ms(void) //12mhz delay 1.01ms { unsigned char x,y; x=3;while(x--) { y=40; while(y--); } } void display(void) //数码管显示函数 { char k; for(k=0;k<4;k++) { P1 = dispbitcode[k]; P0 = dispcode[dispbuf[k]]; if(k==1) //加上数码管的dp小数点 P0&=0x7f; delay_1ms(); } } /************ 读ADC0832函数 ************/ //采集并返回 unsigned int Adc0832(unsigned char channel) //AD转换,返回结果 { uchar i=0; uchar j; uint dat=0; uchar ndat=0; if(channel==0)channel=2; if(channel==1)channel=3; ADDI=1; _nop_(); _nop_(); ADCS=0; //拉低CS端 _nop_(); _nop_(); ADCLK=1; //拉高CLK端 _nop_(); _nop_(); ADCLK=0; //拉低CLK端,形成下降沿1 _nop_(); _nop_(); ADCLK=1; //拉高CLK端 ADDI=channel&0x1; _nop_(); _nop_(); ADCLK=0; //拉低CLK端,形成下降沿2 _nop_(); _nop_(); ADCLK=1; //拉高CLK端 ADDI=(channel>>1)&0x1; _nop_(); _nop_(); ADCLK=0; //拉低CLK端,形成下降沿3 ADDI=1;