松翰单片机SN程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#define MAIN_GLOBAL
#include "..\inc\config.h"
//*****************************************************************// // 主函数
//
//*****************************************************************// void main(void)
{
InitCPU();
Ram_init();
LED1_LEAVE_OFF();
LED2_LEAVE_OFF();
FGIE = 1;//开启中断
while(1)
{
_clrwdt();
_nop();
if(TimeFlag)
{
TimeFlag=0;
Time_Fun(); //基准时间函数
}
// Red_Fun();
}
}
//*****************************************************************// // 继电器负载输出函数
//
//*****************************************************************// void Work_scan(void)
{
static uchar CaiDeng_Cnt = 0;
if(Open_Flag && !Error_Flag)
{
MOTOR2_ON();//同步电机
if(Heat_Leave == 1)
{
if(Fan_ON_Flag && !YanShi_Cnt)
YanShi_Cnt = 4;
else if(!Fan_ON_Flag)
{
FENG_MOTOR1_ON();//风机
Fan_ON_Flag = 1;
}
if(YanShi_Cnt >= 4)
{
HEAT1_ON();
Heat1_ON_Flag = 1;
}
HEAT2_OFF();
Heat2_ON_Flag = 0;
}
else if(Heat_Leave == 2)
{
if(Fan_ON_Flag && !YanShi_Cnt)
YanShi_Cnt = 4;
else if(!Fan_ON_Flag)
{
FENG_MOTOR1_ON();//风机
Fan_ON_Flag = 1;
}
if(YanShi_Cnt >= 4)
{
HEAT1_ON();
Heat1_ON_Flag = 1;
}
if(YanShi_Cnt >= 8)
{
HEAT2_ON();
Heat2_ON_Flag = 1;
}
}
else
{
if(YanShi_Cnt >= 60)
{
Fan_ON_Flag = 0;
FENG_MOTOR1_OFF();
}
HEAT1_OFF();
Heat1_ON_Flag = 0;
HEAT2_OFF();
Heat2_ON_Flag = 0;
}
if(CaiDeng_Leave == 1)
{
LED_RED = 1;
LED_GREEN = 0;
LED_BLUE = 0;
}
else if(CaiDeng_Leave == 2)
{
LED_RED = 0;
LED_GREEN = 1;
LED_BLUE = 0;
}
else if(CaiDeng_Leave == 3)
{
LED_RED = 0;
LED_GREEN = 0;
LED_BLUE = 1;
}
else if(CaiDeng_Leave == 4)
{
LED_RED = 1;
LED_GREEN = 1;
LED_BLUE = 1;
}
else if(CaiDeng_Leave == 5)
{
if(!CaiDeng_Cnt)
{
LED_RED = 1;
LED_GREEN = 0;
LED_BLUE = 0;
}
else if(CaiDeng_Cnt == 30)
{
LED_RED = 0;
LED_GREEN = 1;
LED_BLUE = 0;
}
else if(CaiDeng_Cnt == 60)
{
LED_RED = 0;
LED_GREEN = 0;
LED_BLUE = 1;
}
else if(CaiDeng_Cnt == 90)
{
LED_RED = 1;
LED_GREEN = 1;
LED_BLUE = 1;
}
CaiDeng_Cnt ;
if(CaiDeng_Cnt >= 120)
{
CaiDeng_Cnt = 0;
}
}
else
{
LED_RED = 0;
LED_GREEN = 0;
LED_BLUE = 0;
}
}
else
{
MOTOR2_OFF();//同步电机
HEAT1_OFF();//加热器1
Heat1_ON_Flag = 0;
HEAT2_OFF();//加热器2
Heat2_ON_Flag = 0;
LED1_LEAVE_OFF();//2档灯
LED2_LEAVE_OFF();//1档灯
LED_HUOYAN_OFF();//火焰
LED_RED = 0;
LED_GREEN = 0;
LED_BLUE = 0;
if(YanShi_Cnt >= 60 || Error_Flag)
{
Fan_ON_Flag = 0;
FENG_MOTOR1_OFF();//关闭风机
}
}
}
/***************************************************************** 红外输入判断函数
__MAIN_EXTERN uchar Red_In_Cnt;//按键值
__MAIN_EXTERN uchar Red_Data;//按键值
__MAIN_EXTERN uchar Old_Red_Data;//按键值
__MAIN_EXTERN uchar Red_ID;
*****************************************************************/ void Red_Fun(void)
{
// static uchar Red_Data_Bit_Cnt = 0;
// TEST = !TEST;
// if(Red_In_Cnt >= 100)
// {
// Red_ID = 0;
// Red_Data = 0;
// Red_Data_Bit_Cnt = 0;
// Red_In_Cnt = 0;
// }
// if(!Red_ID)
// {
// if(RED_IN)
// {
// if(Red_In_Cnt >= 60&&Red_In_Cnt <= 84)
// {
// Red_ID = 1;
// }
// Red_In_Cnt = 0;
// }
// }
// else if(Red_ID == 1)
// {
// if(!RED_IN)
// {
// if(Red_In_Cnt >= 26&&Red_In_Cnt <= 46)
// {
// Red_ID = 2;
// }
// else
// Red_ID = 0;
// Red_In_Cnt = 0;
// }
// }
// else if(Red_ID == 2)
// {
// if(RED_IN)
// Red_ID = 3;
// Red_In_Cnt = 0;
// }
// if(Red_ID == 3)
// {
// if(!RED_IN)
// {
// if(Red_In_Cnt>=3)
// {
// Red_Data <<= 1;
// if(Red_In_Cnt >=10)
// Red_Data |= 1;
// Red_Data_Bit_Cnt ;
// if(Red_Data_Bit_Cnt == 8)
// {
// if(Red_Data != _0100_0000b) // {
// Red_ID = 0;
// Red_Data_Bit_Cnt = 0; // }
// Red_Data = 0;
// }
// if(Red_Data_Bit_Cnt == 24)
// {
// Red_Down_Flag = 1;
// Old_Red_Data = Red_Data; // Red_ID = 0;
// Red_Data_Bit_Cnt = 0;
// Red_Data = 0;
// }
// Red_In_Cnt = 0;
// }
// }
// }
}
//*****************************************************************// // 时基走时函数
//
//*****************************************************************// void Time_Fun(void)
{
static uchar Time5MS_Cnt = 0,Time10MS_Cnt = 0,Time100MS_Cnt = 0,Time500MS_Cnt = 0;
static uchar T10S_Cnt = 0,T30min_Cnt;
static uchar Buzz_ON_Cnt = 0,Error_Cnt = 0,PingDao_Cnt = 0;
Time5MS_Cnt ;
if(Time5MS_Cnt >= 5) //5MS时间基准
{
Time5MS_Cnt = 0;
// AD_fun();
}
Time10MS_Cnt ;
if(Time10MS_Cnt >= 10) //10MS时间基准
{
Time10MS_Cnt = 0;
KeyFun();
// LedDisplay();
if(Buzz_Flag)
{
Buzz_ON_Cnt ;
if(Buzz_ON_Cnt >= 15)
{
Buzz_ON_Cnt=0;
Buzz_Flag = 0;
}
}
}
Time100MS_Cnt ;
if(Time100MS_Cnt >= 100)//100MS时间基准
{
Time100MS_Cnt = 0;
Time500MS_Cnt ;
if(Time500MS_Cnt >= 5)
{
Time500MS_Cnt = 0;
if(YanShi_Cnt <70)
{
YanShi_Cnt ;
}
if(SanSuo_Time)
{
SanSuo_Time --;
}
else
SanSuo_Flag = 0;
if(YuYue_Time)
{
T10S_Cnt ;
if(T10S_Cnt >= 20)//10S
{
T10S_Cnt = 0;
T30min_Cnt ;
if(T30min_Cnt >= 180)//180=30分钟
{
T30min_Cnt = 0;
YuYue_Time --;
if(!YuYue_Time)
{
Open_Flag = 0;
Buzz_Flag = 1;
Heat_Leave = 0;
YanShi_Cnt = 0;
HuoYan_Leave = 3;
YuYue_Time = 0;
}
}
}
}
}
Work_scan();
// if(!PINGDAO_IN)
// {
// PingDao_Cnt ;
// if(PingDao_Cnt >= 10)
// {
// Error_Flag = 1;
// Open_Flag = 0;
// }
// }
if(Error_Flag)
{
if(Error_Cnt <= 100)
Error_Cnt ;
if(Error_Cnt == 10||Error_Cnt == 20||Error_Cnt == 30)
{
Buzz_Flag = 1;
}
}
}
}
/*****************************************************************
按键处理
//--------------------按键值-----------------------------------
#define RED_KEY_ON_OFF _1101_1000b #define RED_KEY_HEATER1 _0010_1000b
#define RED_KEY_FLAME_UP _0110_1000b
#define RED_KEY_TIME
_0011_0000b
#define RED_KEY_HEATER2 _0000_1000b #define RED_KEY_FLAME_DOWN _0100_1000b
#define KEY_ON_OFF 1
#define KEY_HEATER1 6
#define KEY_FLAME_UP 5
#define KEY_TIME 4
#define KEY_HEATER2 3
#define KEY_COLDURS 2
*****************************************************************/ void KeyFun(void) //单片机初始化
{
static uchar Key_Cnt = 0;
if(!Error_Flag)
{
Scan_Key();
if(Red_Down_Flag)//红外按键赋值
{
Red_Down_Flag = 0;
switch(Old_Red_Data)
{
case RED_KEY_ON_OFF:
{
Key_Data = KEY_ON_OFF;
}
break;
case RED_KEY_HEATER1:
{
Key_Data = KEY_HEATER1;
}
break;
case RED_KEY_FLAME_UP:
{
Key_Data = KEY_FLAME_UP;
}
break;
case RED_KEY_TIME:
{
Key_Data = KEY_HEATER_OFF;
}
break;
case RED_KEY_HEATER2:
{
Key_Data = KEY_HEATER2;
}
break;
case RED_KEY_FLAME_DOWN:
{
Key_Data = KEY_COLDURS;
}
break;
default:
break;
}
Old_Red_Data = 0;
}
switch(Key_Data)
{
case KEY_ON_OFF://开机按键
{
Buzz_Flag = 1;
YanShi_Cnt = 0;
if(!Open_Flag)
{
Heat_Leave = 0;
HuoYan_Leave = 3;
CaiDeng_Leave = 0;
YuYue_Time = 0;
Open_Flag = 1;
}
else
{
Open_Flag = 0;
}
}
break;
case KEY_HEATER1://1档
{
if(Open_Flag)
{
if(Heat_Leave != 1 )
{
Buzz_Flag = 1;
YanShi_Cnt = 0;
Heat_Leave = 1;
LED1_LEAVE_ON();
LED2_LEAVE_OFF();
}
}
}
break;
case KEY_HEATER2://2档
{
if(Open_Flag)
{
if(Heat_Leave != 2)
{
Buzz_Flag = 1;
if(!Heat_Leave)
{
YanShi_Cnt = 0;
}
Heat_Leave = 2;
LED2_LEAVE_ON();
LED1_LEAVE_OFF();
}
}
}
break;
case KEY_HEATER_OFF://加热关闭按键{
if(Open_Flag)
{
if(Heat_Leave)
{
Buzz_Flag = 1;
Heat_Leave = 0;
YanShi_Cnt = 0;
LED1_LEAVE_OFF();
LED2_LEAVE_OFF();
}
}
}
break;
case KEY_FLAME_UP://火焰 -
{
if(Open_Flag)
{
Buzz_Flag = 1;
HuoYan_Leave ;
if(HuoYan_Leave >= 4)
HuoYan_Leave = 1;
}
}
break;
case KEY_COLDURS://彩灯
{
if(Open_Flag)
{
Buzz_Flag = 1;
HuoYan_Leave = 2;
CaiDeng_Leave ;
if(CaiDeng_Leave >= 6)
CaiDeng_Leave = 0;
}
}
break;
default:
break;
}
Key_Data = 0;
}
}
/*****************************************************************
按键扫描
*****************************************************************/
void Scan_Key(void)
{
static uchar Scan_Key_ID = 0;
static uchar Key1_Out_Cnt = 0,Key2_Out_Cnt = 0,Key4_Out_Cnt = 0;
static uchar
Scan_Key_data,Key1_Old_data,Key2_Old_data,Key4_Old_data;
uchar i,Tmp;
Scan_Key_ID ;
if(Scan_Key_ID >= 3)
Scan_Key_ID = 0;
if(!Scan_Key_ID)
{
KEY1_OUT();
KEY2_IN();
KEY3_IN();
KEY4_IN();
KEY1 = 0;
for(i=0;i<50;i )
{
_nop();
}
Tmp = P0;
Scan_Key_data = Tmp | _1011_1011b;
if(Scan_Key_data==Key1_Old_data&&Scan_Key_data!=_1111_1111b)
{
Key1_Out_Cnt ;
if(Key1_Out_Cnt >= 50)
Key1_Out_Cnt = 40;
if(Key1_Out_Cnt == 3)
{
if(Key1_Old_data == _1111_1011b)
Key_Data = 1;
else if(Key1_Old_data == _1011_1111b)
Key_Data = 2;
}
}
else
{
Key1_Old_data=Scan_Key_data;
Key1_Out_Cnt = 0;
}
KEY1 = 1;
}
else if(Scan_Key_ID==1)
{
KEY1_IN();
KEY2_OUT();
KEY3_IN();
KEY4_IN();
KEY2 = 0;
for(i=0;i<50;i )
{
_nop();
}
Tmp = P0;
Scan_Key_data = Tmp | _1010_1111b;
if(Scan_Key_data==Key2_Old_data&&Scan_Key_data!=_1111_1111b) {
Key2_Out_Cnt ;
if(Key2_Out_Cnt >= 50)
Key2_Out_Cnt = 40;
if(Key2_Out_Cnt == 3)
{
if(Key2_Old_data == _1110_1111b)
Key_Data = 3;
else if(Key2_Old_data == _1011_1111b)
Key_Data = 4;
}
}
else
{
Key2_Old_data=Scan_Key_data;
Key2_Out_Cnt = 0;
}
KEY2 = 1;
}
else if(Scan_Key_ID==2)
{
KEY1_IN();
KEY2_IN();
KEY3_IN();
KEY4_OUT();
KEY4 = 0;
for(i=0;i<50;i )
{
_nop();
}
Tmp = P0;
Scan_Key_data = Tmp | _1011_1101b;
if(Scan_Key_data==Key4_Old_data&&Scan_Key_data!=_1111_1111b)
{
Key4_Out_Cnt ;
if(Key4_Out_Cnt >= 50)
Key4_Out_Cnt = 40;
if(Key4_Out_Cnt == 3)
{
if(Key4_Old_data == _1111_1101b)
Key_Data = 5;
else if(Key4_Old_data == _1011_1111b)
Key_Data = 6;
}
}
else
{
Key4_Old_data=Scan_Key_data;
Key4_Out_Cnt = 0;
}
KEY4 = 1;
}
}
//*****************************************************************//
// 硬件初始化
//
//*****************************************************************// void InitCPU(void) //单片机初始化
{
FGIE=0; //关总中断
//------------------------------P0-----------------
FP00M=1;//红灯
FP01M=1;//按键
FP02M=1;//按键
// FP03M=1;//
FP04M=1;//按键
FP05M=1;//蜂鸣器
FP06M=0;//
FP07M=1;//LED1档火焰
P0UR =_0101_0110b;//
P0 = 0;
//------------------------------P4-----------------
FP40M=1;//蓝灯
FP41M=1;//温保开关
FP42M=1;//同步电机
FP43M=1;//火焰
FP44M=1;//绿灯
P4UR =0;//
P4 = 0;
//------------------------------P5-----------------
FP50M=1;//LED2档火焰
FP51M=0;//红外输入
FP52M=1;//加热2
FP53M=1;//加热1
FP54M=1;//风机
P5UR =0;//
P5 = 0;
//------------------------------T0--------------------------------
// TC0M = 0X60;
// T0C=6; //T0 间隔为16M,250uS
// FALOAD0 = 1;
// FTC0IEN=1; //允许响应T0 中断
// FTC0IRQ=0; //T0 中断请求标志清零
// FTC0ENB=1; //开启T0运行
//------------------------------TC0--------------------------------
TC0M = 0X64;//4分频
TC0C=131; //T0 间隔为16M,250uS
TC0R=131;
FALOAD0 = 1;
FTC0IEN=1; //允许响应T0 中断
FTC0IRQ=0; //T0 中断请求标志清零
FTC0ENB=1; //开启T0运行
//-----------------------ADC---------------------------------------
P4CON = _0001_0000b; //P41 P44为AD--纯模拟输入
ADR = _0100_0000b;//时钟为FCPU/1 //4M时,转换时间为16uS
FGIE=1; //开总中断
}
//*****************************************************************// // 变量初始化函数
//
//*****************************************************************// void Ram_init(void)
{
Buzz_Flag = 1;
HuoYan_Leave = 3;
Key_Data = 0;
Error_Flag = 0;
}
/*****************************************************************
中断函数
*****************************************************************/
__interrupt isr(void) //中断入口
{
static uchar Time1MS = 0;
static uchar HuoYan_Cnt = 0,HuoYan_Leave_Data;
static uchar WenBao_Cnt = 0,WenBao_ID = 0;
static uchar Red_Data_Bit_Cnt = 0;
if(FTC0IRQ) //TC0 //定时0.25ms
{
if(WenBao_Pin == WenBao_Pin_Flag)
{
WenBao_Cnt ;
}
else
{
WenBao_Pin_Flag = WenBao_Pin;
if(WenBao_Cnt >= 40&&WenBao_Cnt <= 120)
{
WenBao_ID ;
if(WenBao_ID >= 100)
{
Error_Flag = 1;
Open_Flag = 0;
}
}
WenBao_Cnt = 0;
}
Red_In_Cnt ;
if(Red_In_Cnt >= 100)
{
Red_ID = 0;
Red_Data = 0;
Red_Data_Bit_Cnt = 0;
Red_In_Cnt = 0;
}
if(!Red_ID)
{
if(RED_IN)
{
if(Red_In_Cnt >= 60&&Red_In_Cnt <= 84)
{
Red_ID = 1;
}
Red_In_Cnt = 0;
}
}
else if(Red_ID == 1)
{
if(!RED_IN)
{
if(Red_In_Cnt >= 26&&Red_In_Cnt <= 46)
{
Red_ID = 2;
}
else
Red_ID = 0;
Red_In_Cnt = 0;
}
}
else if(Red_ID == 2)
{
if(RED_IN)
Red_ID = 3;
Red_In_Cnt = 0;
}
else if(Red_ID == 3)
{
if(!RED_IN)
{
if(Red_In_Cnt>=3)
{
Red_Data <<= 1;
if(Red_In_Cnt >=8)
Red_Data |= 1;
Red_Data_Bit_Cnt ;
if(Red_Data_Bit_Cnt == 8)
{
if(Red_Data != _0100_0000b)
{
Red_ID = 0;
Red_Data_Bit_Cnt = 0;
}
Red_Data = 0;
}
if(Red_Data_Bit_Cnt == 24)
{
Red_Down_Flag = 1;
Old_Red_Data = Red_Data;
Red_ID = 0;
Red_Data_Bit_Cnt = 0;
Red_Data = 0;
}
}
Red_In_Cnt = 0;
}
}
if(Buzz_Flag)
BUZZ = !BUZZ;
else
BUZZ_OFF();
Time1MS ;
if(Time1MS >= 8) //1MS中断函数
{
Time1MS = 0;
TimeFlag = 1;
}
if(Open_Flag)
{
HuoYan_Cnt ;
if(HuoYan_Cnt >= 12)
{
HuoYan_Leave_Data = HuoYan_Leave*4;
HuoYan_Cnt = 0;
}
if(HuoYan_Cnt >= HuoYan_Leave_Data)
{
LED_HUOYAN_OFF();
}
else
LED_HUOYAN_ON();
}
}
//--------------------------------------------------
INTRQ = 0;
//-------------------------------------------------- }。