AD转换(C语言)
# include "STC12C5A.H"# include "stdio.h"
#define uint unsigned int
#define FOSC 11059200L
#define SETTIME 1
sbit start=P0^0;
sbit eoc=P0^1;
sbit oe=P0^2;
sbit clk=P0^3;
sbit gewei=P3^5 ;
sbit shiwei=P3^4 ;
sbit baiwei=P3^3 ;
sbit qianwei=P3^2 ;
sbit wanwei=P3^1 ;
sbit shiwanwei=P3^0 ;
char shuzi[6]={0x00};
char gongyang[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
char gongying[11]={0x00};
long counttimes=0;
void delay_ms(unsigned int m ) // 延时程序
{
uint i,j;
for(i=0;i<m;i++)
for(j=0;j<111;j++);
}
void zijian() //自检程序
{
P3=0XFF;
P2=gongyang[8]; //^0xff;
delay_ms(1000);
P2=gongyang[10]; //^0xff;
delay_ms(1000);
P3=0X00;
}
void timer_init() //定时器初始化
{
TMOD=TMOD&0xf0|0x01;
TL0= (65536-SETTIME*FOSC/12000)%256;
TH0= (65536-SETTIME*FOSC/12000)/256;
TR0=1;
}
void inter_init() //中断初始化
{
ET0=1;
EA=1;
}
void timer0_inter() interrupt 1 //定时器0中断函数
{
TL0= (65536-SETTIME*FOSC/12000)%256;
TH0= (65536-SETTIME*FOSC/12000)/256;
clk=!clk;
counttimes++;
}
void main()
{
char c,jishu=0;
unsigned int rtadc;
timer_init();
inter_init();
start=0;
start=1;
start=0;
while(1)
{ rtadc=0;
if(eoc==1)
{
oe=1;
rtadc=P1;
rtadc=rtadc*100/51;//这一句和后面3句都不明白求解析
shuzi[2]=rtadc/100;
shuzi[1]=rtadc%100/10;
shuzi[0]=rtadc%100%10;
start=0;
start=1;
start=0;
}
P3=0X00;
gewei=1;
c=shuzi[0];
P2=gongyang[c];
delay_ms(4);
P3=0X00;
shiwei=1;
P2=gongyang[shuzi[1]];
delay_ms(4);
P3=0X00;
baiwei=1;
P2=gongyang[shuzi[2]]&0x7f;
delay_ms(4);
P3=0X00;
qianwei=1;
P2=gongyang[shuzi[3]];
delay_ms(4);
P3=0X00;
wanwei=1;
P2=gongyang[shuzi[4]];
delay_ms(4);
P3=0X00;
shiwanwei=1;
P2=gongyang[shuzi[5]];
delay_ms(4);
}
}