|
这是我的单片机实验课的实验课题
使用到的A/D芯片为 A/D0809,在数码管上显示电压值,电压范围为0~5v
- #include "reg51.h"
- #include "absacc.h"
- #define COUNT 5000
- #define AIN0 XBYTE[0x0000] //定义外部地址
- sbit EOC=P3^3; //转换结束信号
- unsigned char LED_seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- //unsigned char YLED_seg[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0xf7,0xff,0xef};
- unsigned char LED_bit[4]={0x01,0x02,0x04,0x08};
- unsigned char LED_buf[3]={0};
- //fosc=11.0592MHz,9600bps
- //串口初始化函数9600bps
- void Sbuf_Init(void)
- {
- SCON=0x50; //10位方式
- //波特率设置
- PCON=0x00; //波特率不倍增
- TMOD=(TMOD&0x0f)|0x20;//T1方式2
- TH1=0xfd; //T1计数初值
- TL1=0xfd;
- //ES=1;EA=1; //串口中断使能
- TR1=1; //启动定时器
- }
- void T0_ISR(void) interrupt 1
- {
- static unsigned char i=0;
- TH0=(65536-COUNT)/256;
- TL0=(65536-COUNT)%256;
- if(i==2)//显示小数点
- {
- P2=~LED_bit[i]; //先送位码
- P1=LED_seg[LED_buf[i]]|0x80;//再送段码;0x80对应的是段码口的dp位
- }
- else
- {
- P2=~LED_bit[i]; //先送位码
- P1=LED_seg[LED_buf[i]]; //再送段码
- }
- i++;
- if(i==3)
- i=0;
- }
- void Timer0_Init(void)
- {
- TMOD=(TMOD&0xf0)|0x01;//定时器0为工作方式1
- TH0=(65536-COUNT)/256;//装初值 高八位
- TL0=(65536-COUNT)%256;//低八位
- ET0=1;//开定时器0中断
- EA=1; //开总中断
- TR0=1;//启动定时器0
- }
- void delay(unsigned int n)
- {
- unsigned int i;
- for(i=0;i<n;i++)
- ;
- }
- void main(void)
- {
- unsigned int i=0,ad,pre_ad=0;
- Timer0_Init();
- Sbuf_Init();
- while(1)
- {
- AIN0=0; //启动转换
- i=i;
- i=i;
- delay(20);
- while(EOC==0); //等待AD转换结束
- ad=AIN0; //读AD转换结果
- if(ad!=pre_ad)
- {
- //ad=ad/51;8位A/D转换精度为1/225*5V=19.6mV
- LED_buf[2]=(ad*100/51)/100;
- LED_buf[1]=(ad*100/51)/10%10;
- LED_buf[0]=(ad*100/51)%10;
- }
- pre_ad=ad;
- }
复制代码
|
|