📄 minpnalarmlock1.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; //接收起始标志
//功能:E2PROM单字节读函数
uint8 e2prom_read(uint8 addr)
{
while(EECR & (1<<EEPE));
EEAR = addr;
EECR|=BIT(EERE);
return EEDR;
}
//功能:单字节写函数
void e2prom_write(uint8 addr,uint8 wData)
{
while(EECR & BIT(EEPE));
EEAR=addr;
EEDR=wData;
EECR|=BIT(EEMPE);
EECR|=BIT(EEPE);
}
//定时T0初始化
void timer0_init(void)
{
TCCR0B = 0x00;//停止定时器
TIMSK0 |= 0x02;//中断允许
TCNT0 = 0x7d;//初始值
TCCR0A = 0x00;
TCCR0B = 0x01;//启动定时器
}
//定时器T0中断
SIGNAL(SIG_OVERFLOW0)
{
TCNT0 = 0x7d; //重装值高位
total++;
//CPL_BIT(PORTB,PB2);//测试
}
//外中断初始化
void int_init(void)
{
MCUCR |= 0x00;
GIMSK |= 0x40;
}
//外中断0服务程序
SIGNAL(SIG_INTERRUPT0)
{
TCCR0B = 0x00;//停止定时器
//TCNT0 = 0xc0; //重装值高位
TCNT0 = 0x96;//中断到退出用了25个周期
total=0;
GIMSK &= 0xBF;
start=1;
TCCR0B = 0x01;//启动定时器
}
//INTERRUPT(SIG_PIN_CHANGE0)
//{
//}
void port_init(void)
{
PORTB = 0x1E;
DDRB = 0x01;
}
//看门狗初始化
void watchdog_init(void)
{
WDR();//喂狗
WDTCR = 0x0E;//使能看门狗
}
void init_devices(void)
{
cli(); //禁止所有中断
MCUCR = 0x00;
MCUSR = 0x80;//禁止JTAG
int_init();
port_init();
timer0_init();
watchdog_init();
sei();//开全局中断
}
int main(void)
{
volatile uint8 temp;
volatile uint8 receivebuf[3]; //接收数据缓冲区
volatile uint8 label=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; //30分钟定时器
volatile uint16 counttimer; //定时
volatile uint8 badcount=0; //测试
init_devices();
receivebuf[2]=0;//测试
temp=e2prom_read(0x01);
temp=e2prom_read(0x05);
if(temp==0x55){
CLR_BIT(PORTB,PB0);
}
else{
SET_BIT(PORTB,PB0);
}
e2prom_write(0x01,0x00);
while(1){
while(total==2){
total=0;
WDR();//喂狗
if(receiveflag!=0){
receiveflag--;
if(receiveflag>=130){
label=0;
}
else{
if(receiveflag>=65){
label=1;
}
else{
label=2;
}
}
temp=GET_BIT(PINB,PB4);
temp<<=3;
switch(receiveflag){
case 198:
if(temp!=0){//起始位不为0退出接收状态
receiveflag=0;
GIMSK |= 0x40;
}
break;
case 190:
case 182:
case 174:
case 166:
case 158:
case 150:
case 142:
case 134:
case 126:
case 118:
case 110:
case 102:
case 94:
case 86:
case 78:
case 70:
case 62:
case 54:
case 46:
case 38:
case 30:
case 22:
case 16:
case 8:
receivebuf[label]>>=1;
receivebuf[label]|=temp;
break;
case 0: //结束位
temp=receivebuf[0]^receivebuf[1];
if(temp==receivebuf[2]){
databuf[0]=receivebuf[0];
databuf[1]=receivebuf[1];
databuf[2]=0x01;
}
else{//测试
badcount++;
eeaddr=0x01;
eedata=badcount;
eewrite=2;
}
GIMSK |= 0x40;
break;
}
}//接收结束
if(eewrite!=0){//EEPROM写入
eewrite=0;
cli();
e2prom_write(eeaddr,eedata);
sei();
}
temp=GET_BIT(PINB,PB3);
if(temp==0){
if(++counttimer==60000){
counttimer=0;
if(++_30min==2){
if(setstatus==1){
setstatus=0;
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=e2prom_read(0x05);
if(temp!=0x55){
temp=e2prom_read(0x07);
if(temp==databuf[1]){
CLR_BIT(PORTB,PB0);
eeaddr=0x05;
eedata=0x55;
eewrite=1;
setstatus=1;
}
} */
CLR_BIT(PORTB,PB0); //测试
break;
case 0xaa://撤防
/*temp=e2prom_read(0x05);
if(temp!=0xaa){
temp=e2prom_read(0x07);
if(temp==databuf[1]){
SET_BIT(PORTB,PB0);
eeaddr=0x05;
eedata=0xaa;
eewrite=2;
}
} */
SET_BIT(PORTB,PB0); //测试
break;
case 0x5a://学习
eeaddr=0x07;
eedata=databuf[1];
eewrite=3;
break;
case 0xa5://保留
break;
}
}
}
if(start!=0){
start=0;
receiveflag=200;
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -