#include<reg51.h>
#include <intrins.h>
#define Disdata P3
#define discan P0
sbit adrd=P2^7; //IO口定义
sbit adwr=P2^6;
sbit csad=P2^4;
sbit DIN=P3^7; //LED小数点控制
unsigned char j,k,ad_data,t;
unsigned char dis[4]={0x00,0x00,0x00,0x00};
unsigned char code dis_7[12]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x40};
/* 共阳LED段码表对应 "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "不亮" "-" */
unsigned char code scan_con[4]={0xfe,0xfd,0xfb,0xf7}; // 列扫描控制字
void delay(unsigned int t)
{
for(;t>0;t--)
{
;
}
} //11微秒延时函数
void scan()
{
char k;
for(k=0;k<4;k++) //四位LED扫描控制
{
Disdata=dis_7[dis[k]];
if(k==1)
{
DIN=1; //加入小数点
}
discan=scan_con[k];
delay(90);
discan=0xff;
}
}
void ad0804()
{
P1=0xff; //读取P1口之前先给其写全1
csad=0; //选通ADCS
adrd=0; //AD读使能
ad_data=P1; //AD数据读取赋给P1口
adrd=1;
csad=1; //关闭ADCS
adwr=0;
}
void ad_compute() //u=2.55+T/100, 2.55反映在AD上为0x83
{
unsigned char t_temp;
//if(ad_data>=0x83)
//{
ad_data=ad_data-0x83;
t_temp=ad_data*2-4;
if(t_temp<=110)
{
dis[3]=t_temp/100;
dis[2]=t_temp/10-dis[3]*10;
dis[1]=t_temp%10;
dis[0]=t%5*2;
}
else
{
t_temp=256-t_temp;
dis[3]=11;
dis[2]=t_temp/10;
dis[1]=t_temp%10;
dis[0]=t%5*2;
}
//
// }
}
void main() // 主程序
{
while(1)
{
ad0804();
ad_compute();
scan();
}
} |