这是第三次写了,都挺顺畅的,就是长按键那里卡了一下,就是发现还可以把按键写在定时器里,这样就方便多了,我之前写的长按键确实麻烦了些。
main.c
#include "sys.h"
#include "iic.h"
uint volt1,volt3;
//void Led();
void main()
{
InitSystem();
InitTimer0();
Timer1Init();
while(1)
{
KeyScans();
if(t200ms)
{
t200ms = 0;
Operate_key_val();
}
ReadAIN1();
ReadAIN3();
volt1 = rd1*1.96;
volt3 = rb2*1.96;
DisplaySMG();
if((puse == 0))
Led();
}
}
void Led()
{
if(volt3_p != 0)
{
if(volt3 > volt3_p)
OperateLed(1,0);
else
OperateLed(1,1);
}
if(freq_p != 0)
{
if(freq > freq_p)
OperateLed(2,0);
else
OperateLed(2,1);
}
if(interface == 0)
OperateLed(3,0);
else
OperateLed(3,1);
if(interface == 1)
OperateLed(4,0);
else
OperateLed(4,1);
if(interface == 2)
OperateLed(5,0);
else
OperateLed(5,1);
}
sys.c
#include "sys.h"
void Select_74HC138(uchar channel)
{
switch(channel)
{
case 0: P2 = P2 & 0X1F; break;
case 4: P2 = (P2 & 0X1F) | 0X80; break;
case 5: P2 = (P2 & 0X1F) | 0XA0; break;
case 6: P2 = (P2 & 0X1F) | 0XC0; break;
case 7: P2 = (P2 & 0X1F) | 0XE0; break;
}
}
void InitSystem()
{
Select_74HC138(4);
P0 = 0XFF;
Select_74HC138(5);
P0 = 0X00;
Select_74HC138(0);
}
void Delay(uint ms)
{
uint i;
for(ms; ms > 0; ms--)
for(i = 921; i > 0; i--);
}
void OperateLed(uchar who,status)
{
static uchar temp;
uchar zt;
switch(status)
{
case 0:
temp = temp | _crol_(0x01,who-1);
P0 = ~temp;
break;
case 1:
if(who == 0xff)
{
P0 = 0XFF;
temp = 0;
}
else
{
zt = (~temp) | _crol_(0x01,who-1);
if(temp != (~zt))
temp = ~zt;
P0 = ~temp;
}
break;
case 2:
P0 = ~who;
temp = who;
break;
default: break;
}
Select_74HC138(4);
Select_74HC138(0);
}
uint freq_count;
void InitTimer0()
{
AUXR |= 0X80;
TMOD &= 0XF0;
TMOD |= 0X04;
TH0 = 0XFF;
TL0 = 0XFF;
TF0 = 0;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void ServiceTimer0() interrupt 1
{
freq_count++;
}
uchar t1 = 0;
uchar count = 0;
uchar puse = 0;
uchar t200ms = 0;
void Timer1Init() //50毫秒@12.000MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xB0; //设置定时初值
TH1 = 0x3C; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1;
EA = 1;
}
void ServiceTimer1() interrupt 3
{
t1++;
if(t1 % 4 == 0)
{
t200ms = 1;
}
if(t1 == 20)
{
t1 = 0;
freq = freq_count;
freq_count = 0;
}
if(puse == 0)
{
if(s7 == 0)
{
count++;
if(count == 40)
{
count = 0;
Select_74HC138(4);
P0 = 0XFF;
Select_74HC138(0);
puse = 1;
}
}
}
if(puse == 1)
{
if(s7 == 0)
{
count++;
if(count == 40)
{
count = 0;
puse = 0;
if(puse == 0)
Led();
}
}
}
}
nixie.c
#include "sys.h"
uchar code nixie[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x8e,0xc8,0xc1,0xbf,0xff};
uint freq;
uint cycle;
uchar channel = 1;
uchar interface;
void DisplayBit(uchar pos,value,bit k)
{
P0 = 0X00;
Select_74HC138(6);
Select_74HC138(0);
if(k)
P0 = nixie[value] & 0x7f;
else
P0 = nixie[value];
Select_74HC138(7);
Select_74HC138(0);
P0 = _crol_(0x01,pos);
Select_74HC138(6);
Select_74HC138(0);
Delay(1);
}
void DisplayFreq()
{
DisplayBit(0,10,0);
if(freq > 999999)
DisplayBit(1,freq / 1000000,0);
if(freq > 99999)
DisplayBit(2,freq / 100000 % 10,0);
if(freq > 9999)
DisplayBit(3,freq / 10000 % 10,0);
if(freq > 999)
DisplayBit(4,freq / 1000 % 10,0);
if(freq > 99)
DisplayBit(5,freq / 100 % 10,0);
if(freq > 9)
DisplayBit(6,freq / 10 % 10,0);
DisplayBit(7,freq % 10,0);
}
void DisplayCycle()
{
cycle = 1000000 / freq;
DisplayBit(0,11,0);
if(cycle > 999999)
DisplayBit(1,cycle / 1000000,0);
if(cycle > 99999)
DisplayBit(2,cycle / 100000 % 10,0);
if(cycle > 9999)
DisplayBit(3,cycle / 10000 % 10,0);
if(cycle > 999)
DisplayBit(4,cycle / 1000 % 10,0);
if(cycle > 99)
DisplayBit(5,cycle / 100 % 10,0);
if(cycle > 9)
DisplayBit(6,cycle / 10 % 10,0);
DisplayBit(7,cycle % 10,0);
}
void DisplayVolt()
{
DisplayBit(0,12,0);
DisplayBit(1,13,0);
DisplayBit(2,channel,0);
if(channel == 1)
{
DisplayBit(5,volt1 / 100,1);
DisplayBit(6,volt1 / 10 % 10,0);
DisplayBit(7,volt1 % 10,0);
}
else if(channel == 3)
{
DisplayBit(5,volt3 / 100,1);
DisplayBit(6,volt3 / 10 % 10,0);
DisplayBit(7,volt3 % 10,0);
}
}
void DisplaySMG()
{
switch(interface)
{
case 0: DisplayFreq(); break;
case 1: DisplayCycle(); break;
case 2: DisplayVolt(); break;
}
}
key.c
#include "sys.h"
uchar key_val = 20;
uint freq_p;
uint volt3_p;
void KeyScans()
{
if(key_val == 20)
{
if(s4 == 0)
{
Delay(10);
key_val = 4;
while(s4 == 0)
{
DisplaySMG();
}
}
if(s5 == 0)
{
Delay(10);
key_val = 5;
while(s5 == 0)
{
DisplaySMG();
}
}
if(s6 == 0)
{
Delay(10);
key_val = 6;
while(s6 == 0)
{
DisplaySMG();
}
}
if(s7 == 0)
{
Delay(10);
key_val = 7;
while(s7 == 0)
{
DisplaySMG();
}
}
}
}
void Operate_key_val()
{
if(key_val != 20)
{
switch(key_val)
{
case 4:
interface = (interface + 1) % 3;
if(interface == 0)
channel = 1;
break;
case 5:
if(interface == 2)
{
if(channel == 1)
channel = 3;
else if(channel == 3)
channel = 1;
}
break;
case 6:
volt3_p = volt3;
break;
case 7:
freq_p = freq;
break;
}
}
key_val = 20;
}