//====================
//数码管显示电压
//0.0~5.0v
//====================
#include "STC15F2K.h"
#include
#define uchar unsigned char
#define uint unsigned int
***it s7=P3^0;
***it s6=P3^1;
***it led1=P0^0;
***it led2=P0^1;
***it ply=P2^1;
uchar counter=0;
uint GetADC;
#define URMD 0 //0:使用定时器2作为波特率发生器
//1:使用定时器1的模式0(16位自动重载模式)作为波特率发生器
//2:使用定时器1的模式2(8位自动重载模式)作为波特率发生器
#define ADC_POWER 0x80 //ADC电源控制位
#define ADC_FLAG 0x10 //ADC完成标志
#define ADC_START 0x08 //ADC起始控制位
#define ADC_SPEEDLL 0x00 //540个时钟
#define ADC_SPEEDL 0x20 //360个时钟
#define ADC_SPEEDH 0x40 //180个时钟
#define ADC_SPEEDHH 0x60 //90个时钟
void Delay(uint n);
uchar ch = 5; //ADC通道号,电位器ch=4,光ch=5
//数码管========================
uchar bdata set;
***it rck=P4^1; //读出
***it sck=P4^2; //写入
***it ds=P4^4; //数据
***it high7=set^7;
uchar i,location,k=0; //第i个数码管亮
uchar code tab1[40]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f, //0~F共阴极,无小数点
//h,p,u,全灭,无小数点
0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1, //0~F共阴极,有小数点
0xf6,0xf3,0xbe,0x80}; //h,p,u,全灭,有小数点
/*----------------------------
ADC中断服务程序
---------------------------*/
void adc_isr() interrupt 5 using 3
{
ADC_CONTR &= !ADC_FLAG; //清除ADC中断标志
GetADC=(ADC_RES<<2)| (ADC_RESL&0x03);
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
}
/*----------------------------
初始化ADC
----------------------------*/
void InitADC()
{
P1ASF = 0x20; //设置P1口为AD口
ADC_RES = 0; //清除结果寄存器
ADC_RESL= 0;
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START | ch;
ADC_CONTR = 0x8C;
Delay(2); //ADC上电并延时
}
void Delay(uint n)
{
uint x;
while (n--)
{
x = 5000;
while (x--);
}
}
void delay1s(void) //误差 0us
{
unsigned char a,b,c;
for(c=167;c>0;c--)
for(b=171;b>0;b--)
for(a=16;a>0;a--);
_nop_(); //if Keil,require use intrins.h
}
void timer0_initialize()
{
counter=0;
TR0=0;
TMOD=0x01; //定时器0
TH0=(65535-5000)/256; //5ms
TL0=(65535-5000)%256;
TR0=1;
}
void timer0()interrupt 1 //中断服务子程序
{
TH0=(65535-5000)/256; //5ms
TL0=(65535-5000)%256;
counter++;
}
//=====================
//一个数码管输出
//=====================
void oneled_out(k,location)
{
uchar j;
set=location; //显示位置
for(j=0;j<8;j++)
{
ds=high7;
sck=0;sck=1;sck=0;
set=set<<1;
}
set=~tab1[k]; //显示第k个数
for(j=0;j<8;j++)
{
ds=high7;
sck=0;sck=1;sck=0;
set=set<<1;
}
rck=0;rck=1;rck=0; // Delay(20);
}
void main()
{ uchar k,location,i=0,adc_flag=0;
uint v,num1=50,num2=0;
k=0;location=0x02;
InitADC(); //初始化ADC
IE=0xa2; //使能ADC中断
IP=0x02;
//开始AD转
timer0_initialize();
while(1)
{
adc_flag=0;
v=50*GetADC/1024;
if(v>25)led2=0;
else led2=1;
if(s7==0)
{led1=0;
while(!s7);
ply=1;}
while(!led1)
{if(counter==200){counter=0;num2=num2+10;}
if(i==0)
{k=num1/100;oneled_out(k+10,location);}
else if(i==1)
{k=num1/10;oneled_out(k,location);}
else if(i==2)
{k=num1%10;oneled_out(k,location);}
else if(i==3)
{k=num2/1000;oneled_out(k,location);}
else if(i==4)
{k=(num2%1000)/100;oneled_out(k+10,location);}
else if(i==5)
{k=num2%1000%100/10;oneled_out(k,location);}
else if(i==6)
{k=num2%1000%100%10;oneled_out(k,location);}
location=location<<1;
i++;
if(i==7){i=0;location=0x02;}
if(s6==0)
{led1=1;
while(!s6);
ply=0;
num2=num2/2;
while(led1)
{if(i==0)
{k=num1/100;oneled_out(k+10,location);}
else if(i==1)
{k=num1/10;oneled_out(k,location);}
else if(i==2)
{k=num1%10;oneled_out(k,location);}
else if(i==3)
{k=num2/1000;oneled_out(k,location);}
else if(i==4)
{k=(num2%1000)/100;oneled_out(k+10,location);}
else if(i==5)
{k=num2%1000%100/10;oneled_out(k,location);}
else if(i==6)
{k=num2%1000%100%10;oneled_out(k,location);}
location=location<<1;
i++;
if(i==7){i=0;location=0x02;}
}
}
}
|