#include "stc15f2k60s2.h"
#include "stdio.h"
#include "bsp_timerinit.h"
#include "bsp_init.h"
#include "bsp_led.h"
#include "bsp_key.h"
#include "bsp_seg.h"
#include "bsp_CSB.h"
#include "iic.h"
#include "bsp_NE_555.h"
sbit out = P3^6;
//定时器
unsigned int t;
unsigned char newt;
unsigned int newt1;
//继电器开关参数
bit flag = 1, flag_old = 1;
//频率开关
bit off_on;
//按键
unsigned char key_down, key_value, key_old, key_up;
//数码管
unsigned char seg_string[10], seg_buf[8], pos;
//led
unsigned char ucled;
//eeprom
unsigned char eeprom_buf[10];
//pcf8591
unsigned char ad, date;
float ad1;
//超声波
unsigned char dist;
bit flag_led1;
bit flag_led2;
bit flag_led3;
//ne555
unsigned int freq;
unsigned int freq1;
//参数
unsigned char para_dist = 60;
unsigned char para_ad = 40;
unsigned int para_freq = 9000;
//页面
unsigned char page;
unsigned char para_page;
//模式
bit mode_freq, mode_dist;
bit flag;
//消抖
unsigned char key_slow, seg_slow;
void Key_Proc();
void Seg_Proc();
void Led_Proc();
void main()
{
System_Init();
EEPROM_Read(eeprom_buf, 0 , 1);
Timer1Init();
NE555_Init();
EA = 1;
while(1)
{
Key_Proc();
Seg_Proc();
Led_Proc();
}
}
void Key_Proc()
{
if(key_slow != 0) return;
key_slow = 1;
key_value = Key_Read();
key_down = key_value & (key_old ^ key_value);
key_up = ~key_value & (key_old ^ key_value);
key_old = key_value;
switch(key_down)
{
case 4:
{
page++;
if(page == 2) off_on = 1;
if(page == 4) page = 0;
if(page == 0)
{
NE555_Init();
off_on = 0;
TR0 = 1;
}
para_page = 0;
}break;
case 5:
{
para_page++;
if(para_page == 3) para_page = 0;
}break;
case 6:
{
if(page == 2)
{
mode_dist ^= 1;
}
if(page == 3)
{
if(para_page == 0)
{
para_freq += 500;
if(para_freq == 12500)
para_freq = 1000;
}
if(para_page == 1)
{
para_ad += 10;
if(para_ad == 70)
para_ad = 10;
}
if(para_page == 2)
{
para_dist += 10;
if(para_dist == 130)
para_dist = 10;
}
}
}break;
case 7:
{
if(page == 1) newt1 = t;
if(page == 0)
{
mode_freq ^= 1;
}
if(page == 3)
{
if(para_page == 0)
{
para_freq -= 500;
if(para_freq == 500)
para_freq = 12000;
}
if(para_page == 1)
{
para_ad -= 10;
if(para_ad == 0)
para_ad = 60;
}
if(para_page == 2)
{
para_dist -= 10;
if(para_dist == 0)
para_dist = 120;
}
}
}break;
}
if(key_up == 7 && page == 1)
{
if(t - newt >= 1000)
{
eeprom_buf[0] = 0;
EEPROM_Write(eeprom_buf, 0 , 1);
}
}
}
void Seg_Proc()
{
if(seg_slow != 0) return;
seg_slow = 1;
ad = PCF8591_ADC(3);
ad1 = (float)ad/51;
switch(page)
{
case 0:
{
if(freq >= 65535) freq = 65535;
if(mode_freq == 0) sprintf(seg_string, "F %6d", (unsigned int)freq);
if(mode_freq == 1)
{
if((float)freq/1000.0 >= 10) sprintf(seg_string, "F %3.1f", (float)freq/1000.0);
if((float)freq/1000.0 < 10) sprintf(seg_string, "F %2.1f", (float)freq/1000.0);
}
}break;
case 1:
{
sprintf(seg_string, "H %2d", (unsigned int)(ad1*20));
if(ad1*20 > 99)
sprintf(seg_string, "H 99");
}break;
case 2:
{
TR0 = 0;
CSB_Init();
dist = Wave_Recv();
if( mode_dist == 0) sprintf(seg_string, "A %3d", (unsigned int)dist);
if( mode_dist == 1) sprintf(seg_string, "A %3.2f", (float)dist/100.00);
}break;
case 3:
{
if(para_page == 0)
{
if((float)para_freq/1000.0 >= 10)
sprintf(seg_string, "P1 %3.1f",(float)para_freq/1000.0);
else
sprintf(seg_string, "P1 %2.1f",(float)para_freq/1000.0);
}
if(para_page == 1) sprintf(seg_string, "P2 %2d",(unsigned int)para_ad);
if(para_page == 2) sprintf(seg_string, "P3 %2.1f",(float)para_dist/100.0);
}break;
}
Seg_Tran(seg_string, seg_buf);
if(ad1*20 >= para_ad) ucled |= 0x10;
if(ad1*20 < para_ad)
{
date = 1.0*51;
ucled &= 0xef;
}
if(dist > para_dist)
{
P0 = 0x10;
P2 = P2 & 0x1f | 0xa0;
P2 &= 0x1f;
flag = 0;
ucled |= 0x20;
}
if(dist <= para_dist)
{
P0 = 0x00;
P2 = P2 & 0x1f | 0xa0;
P2 &= 0x1f;
flag = 1;
ucled &= 0xdf;
}
if(flag != flag_old)
{
if(flag == 1)
{
eeprom_buf[0]++;
}
flag_old = flag;
}
if(para_ad == 10 && ad1*20 > para_ad) date = (3*ad1*20+15);
if(para_ad == 20 && ad1*20 > para_ad) date = (3.4*ad1*20-17);
if(para_ad == 30 && ad1*20 > para_ad) date = (4*ad1*20-65);
if(para_ad == 40 && ad1*20 > para_ad) date = (5*ad1*20-145);
if(para_ad == 50 && ad1*20 > para_ad) date = (7*ad1*20-305);
if(para_ad == 60 && ad1*20 > para_ad) date = (10*ad1*20-545);
PCF8591_DAC(date);
EEPROM_Write(eeprom_buf,0,1);
}
void Led_Proc()
{
if(page == 0)
{
ucled |= 0x01;
flag_led1 = 0;
}
else if(page == 3 && para_page == 0)
flag_led1 = 1;
else
{
ucled &= 0xfe;
flag_led1 = 0;
}
if(page == 1)
{
ucled |= 0x02;
flag_led2 = 0;
}
else if(page == 3 && para_page == 1)
flag_led2 = 1;
else
{
ucled &= 0xfd;
flag_led2 = 0;
}
if(page == 2)
{
ucled |= 0x04;
flag_led3 = 0;
}
else if(page == 3 && para_page == 2)
flag_led3 = 1;
else
{
ucled &= 0xfb;
flag_led3 = 0;
}
}
void Server_Timer1() interrupt 3
{
t++;
newt++;
key_slow++;
if(key_slow >= 50) key_slow = 0;
seg_slow++;
if(seg_slow >= 50) seg_slow = 0;
if(freq > para_freq)
{
ucled |= 0x08;
if(newt < 80)
{
out = 1;
}
if(newt >= 80)
{
out = 0;
}
if(newt == 100)
{
newt = 0;
}
}
if(freq <= para_freq)
{
ucled &= 0xf7;
if(newt < 20)
{
out = 1;
}
if(newt >= 20)
{
out = 0;
}
if(newt == 100)
{
newt = 0;
}
}
if(t % 1000 == 0 && off_on == 0)
{
freq = ((TH0<<8)|TL0);
TH0 = 0;
TL0 = 0;
}
if(flag_led1 == 1)
{
if(t % 100 == 0)
ucled ^= 0x01;
}
if(flag_led2 == 1)
{
if(t % 100 == 0)
ucled ^= 0x02;
}
if(flag_led3 == 1)
{
if(t % 100 == 0)
ucled ^= 0x04;
}
Led_Disp(ucled);
Seg_Disp(seg_buf, pos);
pos++;
if(pos >= 8) pos = 0;
}
评论0