//包含所需頭文件
#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;
}