main.c
#include "Init.h"
#include "Key.h"
#include "Smg.h"
#include "ultrasonic.h"
#include "Led.h"
#include <stdio.h>
#include "Timer.h"
#include "iic.h"
//函数声明
void Key_Proc();
void Led_Proc();
void Smg_Proc();
void Record();
void ADC_mode();
//按键变量
unsigned char Key_Old;
//Led显示变量
unsigned char ucled;
//数码管显示变量
unsigned char string[10];
unsigned char buf[8]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char pos=0;
//显示刷新变量
unsigned char Key_Slow;
unsigned char Record_Slow;
unsigned char Smg_Slow;
//定时器计数
unsigned long Ms_Tick;
unsigned long Ms_Led;
//模式切换
unsigned char S4_mode=0; //0-测距,1-参数界面,2-记录界面
bit S5_mode=0; //0-按键模式,1-旋钮模式
unsigned char K_Para_High=60;
unsigned char K_Para_Low=10;
unsigned char R_Para_High=60;
unsigned char R_Para_Low=10;
unsigned char my_alarm=0;
bit old_flag=0;
bit new_flag=0;
bit old_flag_K=0;
bit new_flag_K=0;
bit flashing=0;
bit shan=0;
unsigned char dis;
unsigned char ADC_mode_rol=0;
int main()
{
Sys_Init();
Timer1Init();
Timer0Init();
EA=1;
while(1)
{
Record();
ADC_mode();
Key_Proc();
Led_Proc();
Smg_Proc();
}
}
void tm1_isr() interrupt 3
{
Ms_Tick++;
if(++Key_Slow==10) Key_Slow=0;
if(++Record_Slow==100) Record_Slow=0;
if(++Smg_Slow==100) Smg_Slow=0;
if(Ms_Tick-Ms_Led > 100)
{
Ms_Led=Ms_Tick;
shan^=1;
}
Led_Disp(ucled);
Smg_Disp(buf,pos);
if(++pos==8) pos=0;
}
void Key_Proc()
{
unsigned char Key_Down,Key_Value;
if(Key_Slow) return;
Key_Slow=1;
Key_Value=Read_KBD();
Key_Down=Key_Value&(Key_Value^Key_Old);
Key_Old=Key_Value;
switch(Key_Down)
{
case 4:
if(++S4_mode == 3) S4_mode=0;
break;
case 5:
switch(S4_mode)
{
case 1:
S5_mode^=1;
break;
case 2:
my_alarm=0;
// new_flag=0;
// old_flag=0;
break;
}
break;
case 9:
if(S4_mode == 1)
{
switch((unsigned char)S5_mode)
{
case 0:
ADC_mode_rol=0;
K_Para_High+=10;
if(K_Para_High == 100)
K_Para_High=50;
break;
case 1:
ADC_mode_rol=1;
break;
}
}
break;
case 8:
if(S4_mode == 1)
{
switch((unsigned char)S5_mode)
{
case 0:
ADC_mode_rol=0;
K_Para_Low-=10;
if(K_Para_Low > 200)
K_Para_Low=40;
break;
case 1:
ADC_mode_rol=2;
break;
}
}
break;
}
}
void Led_Proc()
{
switch(S4_mode)
{
case 0:
ucled = ucled & 0xf8 |0x01;
break;
case 1:
ucled = ucled&0xf8|0x02;
break;
case 2:
ucled = ucled&0xf8|0x04;
break;
}
if(flashing)
{
if(shan == 1)
ucled |= 0x80;
else
ucled &= 0x7f;
}
else
{
ucled |= 0x80;
}
}
void Smg_Proc()
{
if(Smg_Slow) return;
Smg_Slow=1;
switch(S4_mode)
{
case 0:
sprintf(string,"A %3d",(unsigned int)dis);
break;
case 1:
if(S5_mode == 0)
{
sprintf(string,"P1 %02d-%02d",(unsigned int)K_Para_Low,(unsigned int)K_Para_High);
}
else
sprintf(string,"P2 %02d-%02d",(unsigned int)R_Para_Low,(unsigned int)R_Para_High);
break;
case 2:
if(my_alarm <10)
{
sprintf(string,"E %d",(unsigned int)my_alarm);
}
else
sprintf(string,"E -");
break;
}
Smg_Trans(string,buf);
}
void Record()
{
if(Record_Slow) return;
Record_Slow=1;
dis=Wave_Recv();
//旋钮模式
if(S5_mode == 1)
{
if(dis >= R_Para_Low && dis <= R_Para_High)
{
new_flag=0;
old_flag=0;
flashing=0;
}
else if(dis < R_Para_Low)
{
flashing=1;
if(new_flag == 0)
{
my_alarm+=1;
}
new_flag = 1;
old_flag=0;
}
else
{
flashing=1;
if(old_flag == 0)
{
my_alarm+=1;
}
old_flag=1;
new_flag=0;
}
}
else
{
if(dis >= K_Para_Low && dis <= K_Para_High)
{
new_flag_K=0;
old_flag_K=0;
flashing=0;
}
else if(dis < K_Para_Low)
{
flashing=1;
if(new_flag_K == 0)
{
my_alarm+=1;
}
new_flag_K = 1;
old_flag_K=0;
}
else
{
flashing=1;
if(old_flag_K == 0)
{
my_alarm+=1;
}
old_flag_K=1;
new_flag_K=0;
}
}
}
void ADC_mode()
{
unsigned char ADC;
ADC=Pcf_8591_ADC(Res_Ctrl);
switch(ADC_mode_rol)
{
case 1:
if(ADC < 51)
R_Para_High = 50;
else if(ADC >= 51 && ADC < 102)
R_Para_High=60;
else if(ADC >= 102 && ADC < 153)
R_Para_High=70;
else if(ADC >=153 && ADC <204)
R_Para_High = 80;
else
R_Para_High = 90;
break;
case 2:
if(ADC < 51)
R_Para_Low = 0;
else if(ADC >= 51 && ADC < 102)
R_Para_Low=10;
else if(ADC >= 102 && ADC < 153)
R_Para_Low=20;
else if(ADC >=153 && ADC <204)
R_Para_Low = 30;
else
R_Para_Low = 40;
break;
}
}