📄 minpnalarmlock2.c.bak
字号:
//包含所需头文件
#include <avr/io.h>
#include <avr/interrupt.h>
//定义可移植的关键字
#define uint8 unsigned char
#define int8 signed char
#define uint16 unsigned int
#define int16 signed int
//#define uint32 unsigned long
//#define int32 signed long
//嵌入汇编宏定义
//#define NOP() asm("nop") //空操作
#define WDR() asm("wdr") //清看门狗
//#define SWAP(R) asm("swap %0":"+r"(R)) //高低半字节交换
//位操作宏定义
#define SET_BIT(PORT,BIT) (PORT|=(1<<BIT)) //置位
#define CLR_BIT(PORT,BIT) (PORT&=~(1<<BIT)) //清零
#define GET_BIT(PIN,BIT) (PIN&(1<<BIT)) //读位
#define CPL_BIT(PORT,BIT) (PORT^=(1<<BIT)) //取反
#define BIT(x) (1<<(x)) //移位
//全局变量定义
volatile uint8 total=0; //系统“心跳”计数总标志
volatile uint8 receiveflag=0; //数据接收标志
volatile uint8 start=0; //接收起始标志
volatile uint8 _60mstimer=0; //60ms定时器
//功能:E2PROM单字节读函数
uint8 e2prom_read(uint8 addr)
{
while(EECR & (1<<EEPE));
EEAR = addr;
//EECR|=BIT(EERE);
EECR|=(1<<EERE);
return EEDR;
}
//功能:单字节写函数
void e2prom_write(uint8 addr,uint8 wData)
{
while(EECR & (1<<EEPE));
EEAR=addr;
EEDR=wData;
EECR|=(1<<EEMPE);
EECR|=(1<<EEPE);
}
//定时T0初始化
/*void timer0_init(void)
{
TCCR0B = 0x00;//停止定时器
TIMSK0 |= 0x02;//中断允许
TCNT0 = 0x7e;//初始值
TCCR0A = 0x00;
TCCR0B = 0x01;//启动定时器
}*/
//定时器T0中断
SIGNAL(SIG_OVERFLOW0)
{
//TCNT0 = 0x7d; //测试
TCNT0 = 0x7e; //重装值高位
total++;
//CPL_BIT(PORTB,PB2);//测试
}
//外中断初始化
/*void int_init(void)
{
MCUCR |= 0x00;
GIMSK |= 0x40;
}*/
//外中断0服务程序
SIGNAL(SIG_INTERRUPT0)
{
TCCR0B = 0x00;//停止定时器
//TCNT0 = 0x7e; //测试
//TCNT0 = 0x96;//中断到退出用了25个周期
TCNT0 = 0x97;//重装值高位
total=0;
GIMSK &= 0xBF;
start=1;
if(_60mstimer==0){
_60mstimer=240;
}
TCCR0B = 0x01;//启动定时器
}
//INTERRUPT(SIG_PIN_CHANGE0)
//{
//}
/*void port_init(void)
{
PORTB = 0x16;
DDRB = 0x01;
}*/
//看门狗初始化
/*void watchdog_init(void)
{
WDR();//喂狗
WDTCR = 0x0E;//使能看门狗
}*/
/*void init_devices(void)
{
cli(); //禁止所有中断
MCUCR = 0x00;
//MCUSR = 0x80;//禁止JTAG
//int_init();
MCUCR |= 0x00;
GIMSK |= 0x40;
//port_init();
PORTB = 0x16;
DDRB = 0x01;
//timer0_init();
TCCR0B = 0x00;//停止定时器
TIMSK0 |= 0x02;//中断允许
TCNT0 = 0x7e;//初始值
TCCR0A = 0x00;
TCCR0B = 0x01;//启动定时器
//watchdog_init();
WDR();//喂狗
WDTCR = 0x0E;//使能看门狗
sei();//开全局中断
}
*/
int main(void)
{
volatile uint8 temp;
volatile uint8 receivebuf[3]; //接收数据缓冲区
volatile uint8 receivetemp[3]; //辅助接收
volatile uint8 label1=0; //数组下标
volatile uint8 databuf[3]; //待处理数据缓冲区
volatile uint8 eewrite=0; //EEPROM写入允许标志
volatile uint8 eeaddr; //EEPROM地址
volatile uint8 eedata; //EEPROM数据
volatile uint8 setstatus=0; //防止30分钟重复撤防
volatile uint8 _30min=0; //30分钟定时器
volatile uint16 counttimer; //定时
volatile uint8 setprevent; //布防、撤防状态指示
//volatile uint8 badcount=0; //测试
cli(); //禁止所有中断
//MCUCR = 0x00;
//MCUCR |= 0x00;
GIMSK = 0x40;
PORTB = 0x16;
DDRB = 0x01;
TCCR0B = 0x00;//停止定时器
TIMSK0 = 0x02;//中断允许
TCNT0 = 0x7e;//初始值
TCCR0A = 0x00;
TCCR0B = 0x01;//启动定时器
WDR();//喂狗
WDTCR = 0x0E;//使能看门狗
sei();//开全局中断
receivebuf[2]=0;
//temp=e2prom_read(0x01);//测试
setprevent=e2prom_read(0x05);
if(setprevent==0x55){
CLR_BIT(PORTB,PB0);
setstatus=1;
}
else{
SET_BIT(PORTB,PB0);
}
//e2prom_write(0x01,0x00);//测试
while(1){
while(total==2){
total=0;
WDR();//喂狗
if(_60mstimer!=0){
if(--_60mstimer>160){
label1=0;
}
else{
if(_60mstimer>80){
label1=1;
}
else{
label1=2;
}
}
}
if(receiveflag!=0){
receiveflag--;
temp=GET_BIT(PINB,PB4);
temp<<=3;
switch(receiveflag){
case 78://起始位
if(temp!=0){//起始位不为0退出接收状态
receiveflag=0;
_60mstimer=0;
GIMSK |= 0x40;
}
break;
case 70://数据位
case 62:
case 54:
case 46:
case 38:
case 30:
case 22:
case 14:
receivebuf[label1]>>=1;
receivebuf[label1]|=temp;
break;
case 71://
case 63://
case 55://
case 47://
case 37://
case 29://
case 21://
case 13://
receivetemp[label1]>>=1;//
receivetemp[label1]|=temp;//
break;//
case 6://结束位
if(label1==2){
receivebuf[0]&=receivetemp[0];//*
receivebuf[1]&=receivetemp[1];//*
receivebuf[2]&=receivetemp[2];//*
temp=receivebuf[0]^receivebuf[1];
if(temp==receivebuf[2]){
databuf[0]=receivebuf[0];
databuf[1]=receivebuf[1];
databuf[2]=0x01;
}
//else{
//NOP();
//}
_60mstimer=0;
}
GIMSK |= 0x40;
break;
}
}
if(eewrite!=0){//EEPROM写入
eewrite=0;
cli();
e2prom_write(eeaddr,eedata);
sei();
}
temp=GET_BIT(PINB,PB3);
if((temp!=0)&&(setprevent==0x55)){//ACC开且在布防状态
if(++counttimer==60000){
counttimer=0;
if(++_30min==120){
if(setstatus==1){
setstatus=0;
setprevent=0xaa;
SET_BIT(PORTB,PB0);
eeaddr=0x05;
eedata=0xaa;
eewrite=2;
}
}
}
}
else{
counttimer=0;
_30min=0;
}
if(databuf[2]!=0){//数据处理
databuf[2]=0;
switch(databuf[0]){
case 0x55://设防
temp=GET_BIT(PINB,PB3);//ACC ON时不布防
if(temp==0){//ACC ON时不布防
temp=e2prom_read(0x05);
if(temp!=0x55){
temp=e2prom_read(0x07);
if(temp==databuf[1]){
setprevent=0x55;
CLR_BIT(PORTB,PB0);
eeaddr=0x05;
eedata=0x55;
eewrite=1;
setstatus=1;
}
}
}//ACC ON时不布防
//CLR_BIT(PORTB,PB0); //测试
break;
case 0xaa://撤防
temp=e2prom_read(0x05);
if(temp!=0xaa){
temp=e2prom_read(0x07);
if(temp==databuf[1]){
setprevent=0xaa;
setstatus=0;
SET_BIT(PORTB,PB0);
eeaddr=0x05;
eedata=0xaa;
eewrite=2;
}
}
//SET_BIT(PORTB,PB0); //测试
break;
case 0x5a://学习
if(setprevent!=0x55){
eeaddr=0x07;
eedata=databuf[1];
eewrite=3;
}
break;
//case 0xa5://保留
//break;
}
}
}
if(start!=0){
start=0;
receiveflag=80;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -