#include<reg52.h>
#include<stdlib.h>
#include<stdio.h>
#define unint unsigned int
#define unchar unsigned char
sbit en_seg=P2^6;
sbit en_cell=P2^7;
sbit buzzer=P2^3;
sbit signal_out=P3^5;
sbit knob_a=P1^0;
sbit knob_b=P1^1;
char knob_state;
unchar code seg_table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x80,0x48,0x40,0x5e,0x79,0x71,0x00,0x40};
unchar i,j1,math,equal,buz_t;
unchar time_h,time_l,knob_value=150;
unchar digit[]={0,16,16,16,16,16};
void display();
void buzzer_();
void knob();
void out_convert(unchar);
void T0T1_init()
{
TMOD=0x11;
TH1=0xe3;
TL1=0x33;
EA=1;
ET0=1;
ET1=1;
TR1=1;
}
void T1_2ms() interrupt 3
{
TH1=0xf1;
TL1=0x99;
i++;
display();
buzzer_();
knob();
if(i>2)
{
i=0;
TH0=time_h;
TL0=time_l;
signal_out=1;
TR0=1;
}
}
void T0_signal() interrupt 1
{
signal_out=0;
TR0=0;
}
void display()
{
switch(i%3)
{
case 0:
P0=0xFF;
en_cell=1;
P0=0xf7;
en_cell=0;
P0=0x00;
en_seg=1;
P0=seg_table[digit[2]];
en_seg=0;
break;
case 1:
P0=0xFF;
en_cell=1;
P0=0xef;
en_cell=0;
P0=0x00;
en_seg=1;
P0=seg_table[digit[1]];
en_seg=0;
break;
case 2:
P0=0xFF;
en_cell=1;
P0=0xdf;
en_cell=0;
P0=0x00;
en_seg=1;
P0=seg_table[digit[0]];
en_seg=0;
break;
}
}
void buzzer_()
{
if(buzzer==0)
{
buz_t--;
if(buz_t==0)
{
buzzer=1;
}
}
}
void out_convert(unchar dataout)
{
unchar out_convert_j;
digit[0]=dataout%10;
for(out_convert_j=1;out_convert_j<6;out_convert_j++)
{
if(dataout>=10)
{
dataout=dataout/10;
digit[out_convert_j]=dataout%10;
}
else
{
digit[out_convert_j]=16;
}
}
}
void knob()
{
switch(P1&0x03)
{
case 0:
if(knob_state==1)
{
knob_state=2;
}
if(knob_state==-1)
{
knob_state=-2;
}
break;
case 1:
if(knob_state==0)
{
knob_state=1;
}
if(knob_state==-2)
{
knob_state=-3;
}
break;
case 2:
if(knob_state==0)
{
knob_state=-1;
}
if(knob_state==2)
{
knob_state=3;
}
break;
case 3:
if(knob_state==3)
{
buzzer=0;
buz_t=5;
if(knob_value>50)
{
knob_value--;
out_convert(knob_value);
}
}
if(knob_state==-3)
{
buzzer=0;
buz_t=5;
if(knob_value<250)
{
knob_value++;
out_convert(knob_value);
}
}
knob_state=0;
break;
}
}
void main()
{
unint temp;
T0T1_init();
out_convert(knob_value);
buzzer=0;
buz_t=50;
signal_out=0;
while(1){
temp=knob_value/0.05425;
time_h=(65536-temp)/256;
time_l=(65536-temp)%256;
}
}
很久以前写的,现在自己也看不太懂了,希望你用得上
|