?? 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 + -