#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uint aa,b,bb;
sbit p1=P2^7;
sbit p2=P2^6;
sbit p3=P2^5;
sbit p4=P2^4;
sbit dp=P0^7;
sbit an1=P1^4;
sbit an2=P1^5;
sbit an3=P1^6;
sbit CLK=P1^2;/*??DAC????*/
sbit CS=P1^1;
sbit DIN=P1^3;
uchar code SegCode[12] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xc1};
int moshi=0;
void DelayMs(uchar n)
{
uchar j;
while (n--)
{
for (j = 0; j < 113; j++);
}
}
void keyscan1() /*??*/
{
if(an1==0)
{ aa=0;
bb=0;
}
while(an1==0);
}
void keyscan2() /*??1???1*/
{
if(an2==0)
{
if(aa<100)
{
aa=aa+1;
bb++;
}
}
while(an2==0);
}
void keyscan3() /*??2???1*/
{
if(an3==0)
{
if(aa>0)
{
aa=aa-1;
bb--;
}
}
while(an3==0);
}
/*????*/
void display(void)
{ p1=0;p2=0;p3=0;p4=0;
if(bb/100%10==0)
P0=SegCode[10];
else
P0=SegCode[bb/100%10];
p1=1;p2=0;p3=0;p4=0;
DelayMs(3);
p1=0;p2=0;p3=0;p4=0;
P0=SegCode[bb/10%10];
dp=0;
p1=0;p2=1;p3=0;p4=0;
DelayMs(3);
p1=0;p2=0;p3=0;p4=0;
P0=SegCode[bb%10];
p1=0;p2=0;p3=1;p4=0;
DelayMs(3);
p1=0;p2=0;p3=0;p4=0;
P0=SegCode[11];
p1=0;p2=0;p3=0;p4=1;
DelayMs(3);
p1=0;p2=0;p3=0;p4=0;
}
void DA_Conver(unsigned int DAValue)
{
unsigned char i;
DAValue <<= 6;
CS = 0;
CLK = 0;
for(i = 0; i < 12; i++)
{
DIN = (bit)(DAValue & 0x8000);
CLK = 1;
DAValue <<= 1;
CLK = 0;
}
CS = 1;
CLK = 0;
}
float zh (float k)
{
float DA;
DA=k*1024/500; /*2.5v????!*/
return DA; }
void init_timer()
{
TMOD=0x01;
TH0=(65536-3500)/256;
TL0=(65536-3500)%256;
EA=1;
ET0=1;
TR0=1;
}
void main()
{long DAValue=0;
aa=0;
init_timer();
while(1)
{
DAValue=zh(aa); /*????*/
DA_Conver(DAValue);
keyscan1();
keyscan2();
keyscan3();
}
}
void time0() interrupt 1
{ TH0=(65536-3500)/256;
TL0=(65536-3500)%256;
b++ ;
if(b==2)
{b=0;
display(); }
}
|