|
#include <stc15.h>
sbit key_up = P1^7;
sbit key_left = P1^6;
sbit key_center = P1^5;
sbit key_down = P1^4;
sbit key_right = P1^3;
sbit Led = P1^2;
sbit Beep = P0^4;
//1
bit flag = 0;
bit flash_flag = 0;
//2
code unsigned char dis_d[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
code unsigned char dis_w[] = {0x0e,0x0d,0x0b,0x07};
//3
char Key_Value = 0;
unsigned char time = 0,Key_Num =0, Location =0;
unsigned int AD_Value = 0,UP_Value = 4500,Down_Value = 1500;
//4
void InitBoard(void)
{
P0M1 =0x00;
P0M0 =0xFF;
TMOD =0x01;
TH0 =0xd8;
TL0 =0xf0;
IE =0xA2;
TR0 = 1;
}
void InitADC(void)
{
P1ASF =0x01;
ADC_RES = 0;
ADC_CONTR = ADC_POWER | ADC_SPEEDLL |ADC_START | 0;
Delay_ADC(20);
}
void adc_isr()interrupt 5
{
ADC_CONTR &= !ADC_FLAG; //Clear ADC interrupt flag
AD_Value = (ADC_RES << 2)+ADC_LOW2;
}
//5
void timero () interrupt 1
{
TH0 = 0xd8;
TL0 = 0xf0;
time ++;
if (flag)
{
if (time >50)
{
Led = ~Led;
Beep = ~Beep;
time = 0;
}
}
else
{
if (time > 100)
{
Led = ~Led;
flash_flag = ~flash_flag;
time = 0;
}
}
}
//6
void Display(unsigned int Value)
{
unsigned char dis[4],i;
dis[0] = Value /1000;
dis[1] = Value /100 % 10;
dis[2] = Value /10 % 10;
dis[3] = Value % 10;
if ((Location == 0)|| ((Location != 0) && (flash_flag ==0)))
{
for (i=0; i<4; i++)
{
P0 = P0 & 0xf0;
if(i == 0)
P3 = dis_d[dis[i]] & 0x7F;
else
P3 = dis_d[dis[i]];
P0 = P0 | dis_w[i];
Delay();
}
}
if ((Location ! = 0) && (flash_flag == 1))
for (i = 0; i<4 ;i++)
{
if (i == (Location - 1))
{
P0 = P0 & 0xf0;
P3=0xFF;
P0 = P0 | dis_w[i];
Delay();
}
else
{
P0 = P0 & 0xf0;
if(i == 0)
P3 = dis_d[dis[i]] & 0x7F;
else
P3 = dis_d[dis[i]];
P0 = P0 | dis_w[i];
Delay();
}
}
}
//7
if (Key_Num == 0)
{
AD_V =(AD_value * 5000000) /1024 ;
Voltage = AD_V /100;
if ((Voltage > UP_Value ) || (Voltage < DOWN_Value}
flag = 1;
else
{
flag = 0;
Beep = 1;
}
Display(Voltage);
}
//8
if ((!key_center) && (flag == 0)
{
while (!key_center)
;
Key_Num++;
Location = 4;
if (Key_ Num == 1)
{
ge = UP_Value % 10;
sh = UP_Value / 10% 10;
ba = UP_Value / 100% 10;
qi= UP_Value / 1000;
}
if (Key_ Num == 2)
{
ge = UP_Value % 10;
sh = UP_Value / 10% 10;
ba = UP_Value / 100% 10;
qi= UP_Value / 1000;
}
if (Key_ Num > 2)
{
Key_Num = 0;
Location = 0;
}
//9
if ((Key_Num == 1))
{
UP_Value =qi *1000 + ba*100 + sh*10 + ge;
Display(UP_Value);
}
if ((Key_Num == 2))
{
UP_Value =qi *1000 + ba*100 + sh*10 + ge;
Display(UP_Value);
}
//10
if (!key_left)
{
while(!key_left)
;
KEY_Value = 0;
Location--;
if (Location < 1)
Location = 4;
}
{
while(!key_right)
;
KEY_Value = 0;
Location++;
if (Location > 4)
Location = 1;
}
// 11
if(!key_up)
{
while (!key_up)
;
if(Location == 4)
{
ge++;
if (ge >9)
ge=0;
}
if (Location == 3)
{
sh++;
if (sh >9)
sh =0;
}
if (Location == 2)
{
ba++;
if (ba >9)
ba =0;
}
if (Location == 1)
{
qi++;
if (qi >9)
qi =0;
}
}
// 12
if(!key_down)
{
while (!key_down)
;
if(Location == 4)
{
ge--;
if (ge <0)
ge=9;
}
if (Location == 3)
{
sh--;
if (sh < 0)
sh = 9;
}
if (Location == 2)
{
ba--;
if (ba <0)
ba =9;
}
if (Location == 1)
{
qi--;
if (qi <0)
qi =9;
}
}
|
|