cs
#include <STC15F2K60S2.H>
#include "mywave.h"
#include "display.h"
#include "pcf8591.h"
#include "eeprom.h"
#include "intrins.h"
void DisplaySMG_info();
void control_led_jdq();
sbit s4 = P3^3;
sbit s5 = P3^2;
sbit s6 = P3^1;
sbit s7 = P3^0;
code unsigned char Seg_dot[] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
code unsigned char Seg_Table[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
unsigned char count_200ms = 0;
unsigned char count_1s = 0;
unsigned int distance = 0;//距离值
unsigned int count_f = 0;//频率值
unsigned int adc_smg = 0;
float adc_volt = 0;
unsigned char humidity = 0;//湿度
unsigned char count_100ms = 0;
unsigned char f_param = 90;//频率参数
unsigned char h_param = 40;//湿度参数
unsigned char dis_param = 6;//距离参数
unsigned char UI = 0;// 0-频率界面 1-湿度界面 2-测距界面 3-参数界面
unsigned char UI_param = 0;//0-频率参数 1-湿度参数 2-距离参数
unsigned char f_mode = 0;//0-Hz显示 1-KHz显示
unsigned char dis_mode = 0;//0-CM显示 1-M显示
unsigned int f_int = 0;//数码管显示KHz单位的频率
float f_temp = 0;//频率单位转KHz
unsigned char stat_led = 0xff;
unsigned char stat_jdq = 0x00;
unsigned char count_01s = 0;
unsigned char num_jdq = 0;//继电器开关次数
unsigned char count_s7 = 0;//长按计数
unsigned char stat_s7 = 0;//按键S7按下和松开的状态
unsigned char stat_on_off = 0;//防止继电器开计数多次频繁计数
unsigned char pwm = 0;//周期计数
unsigned char duty_pwm = 0;//占空比
unsigned char count_50ms = 0;
unsigned char stat_on_off1 = 0;//防止继电器关计数多次频繁计数
void Delay20ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 1;
j = 234;
k = 113;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Timer1Init(void) //200微秒@12.000MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TMOD |= 0x10; //设置定时器模式
TL1 = 0x38; //设置定时初值
TH1 = 0xFF; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1;
EA = 1;
}
void Sevice_timer1() interrupt 3
{
EA = 0;
TL1 = 0x38; //设置定时初值
TH1 = 0xFF; //设置定时初值
pwm++;
if(pwm == duty_pwm)
{
stat_jdq &= 0xdf;
}
else if(pwm == 5)
{
pwm = 0;
stat_jdq |= 0x20;
}
SelectHC573(5,stat_jdq);
EA = 1;
}
void Init_timer0()
{
AUXR &= 0x7f;
TMOD &= 0xf0;
TMOD |= 0x05;
TH0 = TL0 = 0;
TR0 = 1;
ET0 = 0;
}
void Init_timer2()
{
AUXR &= 0xf3;
T2L = 0xf0;
T2H = 0xd8;
AUXR |= 0x10;
IE2 |= 0x04;
EA = 1;
}
void Sevice_timer2() interrupt 12
{
DisplaySMG_info();
count_200ms++;
count_1s++;
count_100ms++;
count_50ms++;
if(count_50ms == 5)
{
count_50ms = 0;
control_led_jdq();
}
if(count_100ms == 5)
{
count_100ms = 0;
adc_volt = read_myadc();
}
if(count_1s == 100)
{
TR0 = 0;
count_1s = 0;
count_f = (TH0 << 8) | TL0;
TH0 = TL0 = 0;
TR0 = 1;
}
if(count_200ms == 20)
{
count_200ms = 0;
distance = measure_distan();
}
if(UI == 3)
{
count_01s++;
if(count_01s == 10)
{
count_01s = 0;
if(UI_param == 0)
{
stat_led |= 0x04;
if((stat_led | 0xfe) == 0xfe)
{
stat_led |= 0x01;
}
else
{
stat_led &= 0xfe;
}
}
else if(UI_param == 1)
{
stat_led |= 0x01;
if((stat_led | 0xfd) == 0xfd)
{
stat_led |= 0x02;
}
else
{
stat_led &= 0xfd;
}
}
else if(UI_param == 2)
{
stat_led |= 0x02;
if((stat_led | 0xfb) == 0xfb)
{
stat_led |= 0x04;
}
else
{
stat_led &= 0xfb;
}
}
}
SelectHC573(4,stat_led);
}
if(stat_s7 == 1)
{
count_s7++;
}
else if(stat_s7 == 2)
{
if(count_s7 > 100)
{
num_jdq = 0;
save_data(0x00,num_jdq);
count_s7 = 0;
stat_s7 = 0;
}
}
}
void DisplaySMG_info()
{
switch(UI)
{
case 0:
DisplaySMG_Bit(0,0x8e);
if(f_mode == 0)
{
if(count_f > 99999)
DisplaySMG_Bit(2,Seg_Table[count_f / 100000]);
if(count_f > 9999)
DisplaySMG_Bit(3,Seg_Table[count_f / 10000 % 10]);
if(count_f > 999)
DisplaySMG_Bit(4,Seg_Table[count_f / 1000 % 10]);
if(count_f > 99)
DisplaySMG_Bit(5,Seg_Table[count_f / 100 % 10]);
if(count_f > 9)
DisplaySMG_Bit(6,Seg_Table[count_f / 10 % 10]);
DisplaySMG_Bit(7,Seg_Table[count_f % 10]);
}
else if(f_mode == 1)
{
f_temp = (float)count_f / 1000;
f_int = f_temp * 10;
if(f_int > 99999)
DisplaySMG_Bit(2,Seg_Table[f_int / 100000]);
if(f_int > 9999)
DisplaySMG_Bit(3,Seg_Table[f_int / 10000 % 10]);
if(f_int > 999)
DisplaySMG_Bit(4,Seg_Table[f_int / 1000 % 10]);
if(f_int > 99)
DisplaySMG_Bit(5,Seg_Table[f_int / 100 % 10]);
DisplaySMG_Bit(6,Seg_dot[f_int / 10 % 10]);
DisplaySMG_Bit(7,Seg_Table[f_int % 10]);
}
break;
case 1:
DisplaySMG_Bit(0,0x89);
DisplaySMG_Bit(6,Seg_Table[humidity / 10]);
DisplaySMG_Bit(7,Seg_Table[humidity % 10]);
break;
case 2:
DisplaySMG_Bit(0,0x88);
if(dis_mode == 0)
{
if(distance > 99)
DisplaySMG_Bit(5,Seg_Table[distance / 100]);
if(distance > 9)
DisplaySMG_Bit(6,Seg_Table[distance / 10 % 10]);
DisplaySMG_Bit(7,Seg_Table[distance % 10]);
}
else if(dis_mode == 1)
{
DisplaySMG_Bit(5,Seg_dot[distance / 100]);
DisplaySMG_Bit(6,Seg_Table[distance / 10 % 10]);
DisplaySMG_Bit(7,Seg_Table[distance % 10]);
}
break;
case 3:
if(UI_param == 0)
{
DisplaySMG_Bit(0,0x8c);
DisplaySMG_Bit(1,Seg_Table[1]);
if(f_param > 99)
DisplaySMG_Bit(5,Seg_Table[f_param / 100 % 10]);
DisplaySMG_Bit(6,Seg_dot[f_param / 10 % 10]);
DisplaySMG_Bit(7,Seg_Table[f_param % 10]);
}
else if(UI_param == 1)
{
DisplaySMG_Bit(0,0x8c);
DisplaySMG_Bit(1,Seg_Table[2]);
DisplaySMG_Bit(6,Seg_Table[h_param / 10]);
DisplaySMG_Bit(7,Seg_Table[h_param % 10]);
}
else if(UI_param == 2)
{
DisplaySMG_Bit(0,0x8c);
DisplaySMG_Bit(1,Seg_Table[3]);
DisplaySMG_Bit(6,Seg_dot[dis_param / 10]);
DisplaySMG_Bit(7,Seg_Table[dis_param % 10]);
}
break;
}
}
void scan_key()
{
if(s4 == 0)
{
Delay20ms();
if(s4 == 0)
{
if(UI == 0)
{
UI = 1;
}
else if(UI == 1)
{
UI = 2;
}
else if(UI == 2)
{
UI_param = 0;
UI = 3;
}
else if(UI == 3)
{
UI = 0;
}
while(s4 == 0);
}
}
else if(s5 == 0)
{
Delay20ms();
if(s5 == 0)
{
if(UI == 3)
{
if(UI_param == 0)
{
UI_param = 1;
}
else if(UI_param == 1)
{
UI_param = 2;
}
else if(UI_param == 2)
{
UI_param = 0;
}
}
while(s5 == 0);
}
}
else if(s6 == 0)
{
Delay20ms();
if(s6 == 0)
{
if((UI == 3) && (UI_param == 0))
{
if(f_param == 120)
{
f_param = 10;
}
else
{
f_param += 5;
}
}
else if((UI == 3) && (UI_param == 1))
{
if(h_param == 60)
{
h_param = 10;
}
else
{
h_param += 10;
}
}
else if((UI == 3) && (UI_param == 2))
{
if(dis_param == 12)
{
dis_param = 1;
}
else
{
dis_param += 1;
}
}
if(UI == 2)
{
if(dis_mode == 0)
{
dis_mode = 1;
}
else if(dis_mode == 1)
{
dis_mode = 0;
}
}
while(s6 == 0);
}
}
else if(s7 == 0)
{
Delay20ms();
if(s7 == 0)
{
if((UI == 3) && (UI_param == 0))
{
if(f_param == 10)
{
f_param = 120;
}
else
{
f_param -= 5;
}
}
else if((UI == 3) && (UI_param == 1))
{
if(h_param == 10)
{
h_param = 60;
}
else
{
h_param -= 10;
}
}
else if((UI == 3) && (UI_param == 2))
{
if(dis_param == 1)
{
dis_param = 12;
}
else
{
dis_param -= 1;
}
}
if(UI == 0)
{
if(f_mode == 0)
{
f_mode = 1;
}
else
{
f_mode = 0;
}
}
if(UI == 1)
{
stat_s7 = 1;
}
while(s7 == 0);
if(UI == 1)
{
stat_s7 = 2;
}
}
}
}
void deal_data()
{
unsigned char dac_value = 0;
if(adc_volt < 5.0)
humidity = 20 * adc_volt;
if(humidity < h_param)
{
Set_mydac(51);
}
else if(humidity <= 80)
{
dac_value = (((4.0/(80-h_param))*humidity + ((float)(80-(5*(char)h_param))/(80-h_param))) * 51);
Set_mydac(dac_value);
}
else
{
Set_mydac(255);
}
}
void control_led_jdq()
{
if(distance > (dis_param * 10))
{
stat_jdq |= 0x10;
stat_led &= 0xdf;
stat_on_off1 = 0;
if(stat_on_off == 0)//继电器开计数
{
num_jdq++;
stat_on_off = 1;
save_data(0x00,num_jdq);
}
}
else
{
stat_jdq &= 0xef;
stat_led |= 0x20;
stat_on_off = 0;
if(stat_on_off1 == 0)//继电器关计数
{
num_jdq++;
stat_on_off1 = 1;
save_data(0x00,num_jdq);
}
}
if(UI == 0)
{
stat_led |= 0x04;
stat_led &= 0xfe;
}
else if(UI == 1)
{
stat_led |= 0x01;
stat_led &= 0xfd;
}
else if(UI == 2)
{
stat_led |= 0x02;
stat_led &= 0xfb;
}
if(count_f > (f_param * 100))
{
stat_led &= 0xf7;
duty_pwm = 4;
}
else
{
stat_led |= 0x08;
duty_pwm = 1;
}
if(humidity > h_param)
{
stat_led &= 0xef;
}
else
{
stat_led |= 0x10;
}
SelectHC573(5,stat_jdq);
SelectHC573(4,stat_led);
}
void main()
{
Init_timer2();
Init_timer0();
SelectHC573(5,0x00);
SelectHC573(4,0xff);
DisplaySMG_All(0xff);
Timer1Init();
while(1)
{
deal_data();
scan_key();
}
}
需要完整工程可私信