***********************************************************************************/
#include<pic.h> //包含单片机内部资源预定义
__CONFIG(0x3F32);
#define uchar unsigned char
#define uint unsigned int
#define LED_PWR RD1 //将LED_BUSY宏定义为RD1端口,输出“0”灯亮
#define PWR_CTRL RC1 //RC1定义为PWR_CTRL名,整机电源控制,输出“1”有效
#define PWR_ON RC2 //RC2定义为PWR_ON名,送给DSP开机信号,输出“1”脉冲有效
#define LED_BUSY RC3 //RC3定义为LED_BUSY,作自动模式准备就绪指示,输出“1”灯亮
//#define PIR_DET RB0 //RB0定义为PIR_DET,检测PIR触发用,“0”低有效
bit Set_S; //定义set变量,手动模式为开机脉冲作判断条件用
bit Set_A,Set_A1;
bit PIR_DET; //定义PIR中断检测信号变量PIR_DET
bit uart;
//**************************调用初始化设置*****************
void init () //
{
//**********配制输入端口***********************
TRISC4 = 1; //SW1,设置RC4和5为输入口,两者组合为模式开关检测
TRISC5 = 1; //SW2,10为"OFF",01为“手动”,11为自动;
//**********配制输出端口***********************
TRISD1 = 0; //LED_POWER,设置D1口全为输出(LED灯指示工作)
//TRISD = 0x00; //LED_POWER,设置D口方向全为输出(LED灯指示工作)
TRISC1 = 0; //PWR_CTRL,设置RC1为输出口,“1”有效,整机总电源控制
TRISC2 = 0; //PWR_ON,设置RC2为输出口,500ms"1"电平有效,送给DSP开机的信号
TRISC3 = 0; //LED_BUSY,设置RC3为输出口,"1""0"交替,自动模式状态输出指示
//**********配制串口设置***********************
SPBRG=25; //4M晶振SPBRG值为25对应设置的波特率为9600BPS
TXSTA=0X24; //使能串口发送,选择高速波特率
RCSTA=0X90; //使能串口工作,连续接收
RCIE=0X1; //使能接收中断
GIE=0X1; //开放全局中断
PEIE=0X1; //使能外部中断
INTE=1; //RB0/INT0外部中断允许位,“1”允许RB0/INT0外部中断
OPTION=0X00; //选项寄存器,由RABPU+INTEDG+T0CS+T0SE+PSA+PS2+PS1+PS0组成
}
//**************************调用串口中断函数*****************
void interrupt usart(void) //串口中断函数,usart自己定义的名称;
{
if(RCIE&&RCIF) //RCIE=1接收中断允许,RCIF=1接收中断判断是否为串口接收中断
{
uart = 1;
TXREG=RCREG; //把接收到的数据发送回去
//while (!TRMT);//TRMT为发送移位寄存器状态位,1=空,0=满(数据未发完)
}
}
//**************************调用延时函数*****************
void delay1ms (uint i)
{
uchar j;
while (i--)
{
for (j = 0; j < 244 ; j ++ ){} //j < 244,11.0592M晶振对应1ms基准延时;
}
}
//********************调用自动模式灯闪函数***********************
void LED_BUSY_AUTO () //自动模式BUSY灯闪5次提示
{
LED_BUSY = 1,delay1ms (500),LED_BUSY = 0,delay1ms (500);
LED_BUSY = 1,delay1ms (500),LED_BUSY = 0,delay1ms (500);
LED_BUSY = 1,delay1ms (500),LED_BUSY = 0,delay1ms (500);
LED_BUSY = 1,delay1ms (500),LED_BUSY = 0,delay1ms (500);
LED_BUSY = 1,delay1ms (500),LED_BUSY = 0,delay1ms (500);
}
//********************主函数***********************
void main()
{
init (); //调用端口配制初始化
LED_PWR = 1,LED_BUSY = 0,PWR_CTRL = 0,PWR_ON = 0; //输出端口赋初值
Set_S = 0, Set_A = 0; //定义变量赋初值为0;
// Set_A1 = 0; //为Set_A1赋初值为0;
while(1)
{
//****************进入开机模式状态*************
if (RC4&&RC5 == 1)
{
delay1ms (20); //延时20ms再次检测;
if (RC4&&RC5 == 1)
{
if (Set_A == 0)
{
LED_PWR = 1,PWR_CTRL = 0,PWR_ON = 0; //
LED_BUSY_AUTO ();
Set_A == 1,Set_S = 0;
PIR_DET = 0; //PIR中断检测信号变量默认置“0”,进入中断
}
while (RC4&&RC5) //检测模式开关是否拨动
{
if (PIR_DET == 1)
{
PWR_CTRL = 1;LED_PWR = 0;
if(Set_A1 == 0)
{
PWR_ON = 1; delay1ms (500); PWR_ON = 0;
}
delay1ms (10000);
LED_PWR = 1,PWR_CTRL = 0;
PIR_DET = 0; //PIR中断检测信号变量清零,等待中断
}
else
{
PWR_CTRL = 0,Set_A1 = 0;
}
}
}
}
//*****************************
if (uart)
{
TXREG=RCREG;
uart = 0;
}
}
}
//**************************调用RB0外部中断函数(PIR检测)*****************
void interrupt PIRINT(void) //RB0外部中断函数,PIR_INT自己定义的名称;
{
INTF = 0; //INTF为RB0外部中断标志位,"1"表示外部中断有发生(必须软件清零)
PIR_DET = 1; //PIR中断检测信号变量输出“1”,启动自动模式开机
}
|