#include "芯片型号不公开.h" #define u8 unsigned char #define u16 unsigned int #define PASS 0 #define FAIL 1 //============Define Flag================= typedef union { unsigned char byte; struct { u8 bit0 : 1; u8 bit1 : 1; u8 bit2 : 1; u8 bit3 : 1; u8 bit4 : 1; u8 bit5 : 1; u8 bit6 : 1; u8 bit7 : 1; } bits; } bit_flag; volatile bit_flag flag1; #define FLAG_1MS flag1.bits.bit0 #define FLAG_KEY_PRESS flag1.bits.bit1 #define FLAG_KEY_PRESS1 flag1.bits.bit2 #define FLAG_POWER_ON flag1.bits.bit3 #define FLAG_MOTO_ON flag1.bits.bit4 #define FLAG_MOTO_DIR flag1.bits.bit5 #define FLAG_MOTO_ERROR flag1.bits.bit6 #define FLAG_MOTO_ERROR1 flag1.bits.bit7 volatile bit_flag flag2; #define FLAG_LED_RED_ON flag2.bits.bit0 #define FLAG_LED_GREEN_ON flag2.bits.bit1 //#define FLAG_BAT_LOW flag2.bits.bit2 #define FLAG_HAVA_MAN flag2.bits.bit3 #define FLAG_IR_SEND_ED flag2.bits.bit4 #define FLAG_CLR_FIRST flag2.bits.bit5 #define FLAG_OPEN_JG_TIMER flag2.bits.bit6 #define FLAG_HAVA_MAN1 flag2.bits.bit7 volatile bit_flag flag3; #define FLAG_MOTO_OPEN_ED flag3.bits.bit0 #define FLAG_GREEN_ON flag3.bits.bit1 #define FLAG_IR_SLEEP_SEND flag3.bits.bit2 #define FLAG_IR_SLEEP_SEND_ED flag3.bits.bit3 #define FLAG_KEY_OPEN flag3.bits.bit4 #define FLAG_KEY_MODE flag3.bits.bit5 #define FLAG_ZHENDONG_JISUAN flag3.bits.bit6 #define FLAG_HAVA_ZHENDONG flag3.bits.bit7 volatile bit_flag flag4; #define FLAG_KJ_OVER flag4.bits.bit0 #define FLAG_LOW_BAT flag4.bits.bit1 #define FLAG_LED_ONCE flag4.bits.bit2 #define FLAG_JNMP_ONCE flag4.bits.bit3 #define FLAG_SLEEP_ED flag4.bits.bit4 #define FLAG_HAVA_ZHENDONG_LOCK flag4.bits.bit5 #define OPEN_KEY_IO P03D #define CLOSE_KEY_IO P02D #define IR_SEND_IO P53D #define RED_LED_IO P54D #define GREEN_LED_IO P40D #define H_BRIDGE_H_IO P42D #define H_BRIDGE_L_IO P41D #define IR_RX_IO P00D #define MERCURY_KEY_IO P01D //滚珠开关 #define SET_RX_IO_PULL_ON P00PU=1 #define SET_RX_IO_PULL_OFF P00PU=0 #define SET_RX_ON P43D=1 #define SET_RX_OFF P43D=0 unsigned char KEY_VALUE_TEMP; unsigned char KEY_VALUE; //unsigned int MOTO_AD_VALUE; //unsigned int MOTO_AD_TEMP; //unsigned char MOTO_AD_COUNT; unsigned int BAT_AD_VALUE; unsigned int BAT_AD_TEMP; unsigned char BAT_AD_COUNT; unsigned int MOTO_ON_OR_OFF_DELAY; unsigned char MOTO_CHECK_AD_DELAY; unsigned int KJ_DELAY; unsigned char CESHI; unsigned int IR_TIEMR_COUNT,HIGHT_COUNT; unsigned char LOW_COUNT; unsigned char GREEN_BLINK_COUNT; unsigned char SCAN_ZHENDONG_KEY; unsigned char AAAA; unsigned int HAVA_ZHENDONG_LOCK_DEALY; void CLR_RAM(void) //清除RAM { __asm movai 0x3F movra FSR0 clrr FSR1 clrr INDF DJZR FSR0 goto $ -2 clrr INDF __endasm; } // 定时器初始化 void TIMER0_INT(void) { /*******************************************************/ TMRCR = 0; //T1CLK=FCPU/2 T0CLK=FCPU/2 关闭T0溢出唤醒 T1ICKS=1; T0CR = 0xF4; //开启T0 32分频 T0CLK内部时钟 自动重载 关闭PWM T0CNT = 256 - 125; T0LDR = 256 - 125; // 1ms T0IE = 1; T0EN=1; /*********************************************************/ /* T1CR = 0xe1; T1LDR =5; //开启T1 1分频 T1CLK内部时钟 开启PWM 256个周期 T1LDEN=1; BUZ1OE=0; T1IE=1; */ } void IO_INIT() { IOP0 = 0x00; IOP4 = 0x00; IOP5 = 0x00; OEP0 = 0x00; //0000 0000 PUP0 = 0x0C; //0000 1100 OEP4 = 0x0F; //0000 1111 PUP4 = 0x00; // P4CON=0x10; OEP5 = 0x18; //0001 1000 PUP5 = 0x00; // P04OE=1; /* H_BRIDGE_H_IO=0; H_BRIDGE_L_IO=0; */ IR_SEND_IO=1; SET_RX_ON; SET_RX_IO_PULL_ON; } void ADC_Init(void) { ADM &= 0x00; ADM |= 0x90; VREFCR = 0; //内部2V ADR = 0x50; //adc时钟 Fcpu/64 ADCHS2=1; ADCHS1=0; ADCHS0=0; } unsigned char ADC_Zero_ADJ(void) { //零点校准 ADT = 0x80; //零点校准 ADTR[4:0]=0 VREFCR = 0; //内部2V ADR = 0; //64分频 GCHS = 1; ADEN = 1; ADEOC = 0; ADSTR = 1; while (!ADEOC); if ((ADB == 0) && ((ADR & 0x0F) == 0)) //结果是否为0 { ADT |= 0x1F; ADEOC = 0; ADSTR = 1; while (!ADEOC); if ((ADB == 0) && ((ADR & 0x0F) == 0)) //结果是否为0 { ADT &= 0x3f; //正常模式 ADEN=0; return PASS; } else { while (1) { if (ADT & 0x0f) { ADT--; ADEOC = 0; ADSTR = 1; while (!ADEOC); if ((ADB == 0) && ((ADR & 0x0F) == 0)) //结果是否为0 { ADT &= 0x3f; //正常模式 ADEN=0; return PASS; } } else { ADT &= 0x3f; //正常模式 ADEN=0; return FAIL; } } } } else { while (1) { if ((ADT & 0x0f) == 0x0f) { ADT &= 0x3f; //正常模式 ADEN=0; return FAIL; } else { ADT++; ADEOC = 0; ADSTR = 1; while (!ADEOC); if ((ADB == 0) && ((ADR & 0x0F) == 0)) //结果是否为0 { ADT &= 0x3f; //正常模式 ADEN=0; return PASS; } } } } } void DEAL_SCAN_MOTO_AD() //扫描电机电流 { // unsigned int temp_value = 0; ADM = (ADM & 0xf8) | 4; // ADC 使能 AD转换通道开启 通道 CH4 ADEOC = 0; ADSTR = 1; // 开始转换 while (!ADEOC); //等待转换完成 // temp_value = ADB; // temp_value = temp_value << 4 | (ADR & 0x0f); if(ADB==0x02&&ADR>=0x00) //到时候改 { if(FLAG_MOTO_ON) { MOTO_CHECK_AD_DELAY++; if(MOTO_CHECK_AD_DELAY>=200) //时间需要调整 { MOTO_CHECK_AD_DELAY=0; // FLAG_MOTO_ON=0; if(!FLAG_MOTO_ERROR1) { FLAG_MOTO_ERROR1=1; FLAG_MOTO_ERROR=1; FLAG_LED_RED_ON=1; } } } } else { MOTO_CHECK_AD_DELAY=0; FLAG_MOTO_ERROR1=0; // FLAG_MOTO_ERROR=0; } /* MOTO_AD_COUNT++; if(MOTO_AD_COUNT>4) { MOTO_AD_TEMP+=temp_value; } if(MOTO_AD_COUNT>=20) { MOTO_AD_COUNT=0; MOTO_AD_VALUE=MOTO_AD_TEMP>>4; MOTO_AD_TEMP=0; if(MOTO_AD_VALUE>512) //到时候改 { if(FLAG_MOTO_ON) { MOTO_CHECK_AD_DELAY++; if(MOTO_CHECK_AD_DELAY>=15) //时间需要调整 { MOTO_CHECK_AD_DELAY=0; // FLAG_MOTO_ON=0; if(!FLAG_MOTO_ERROR1) { FLAG_MOTO_ERROR1=1; FLAG_MOTO_ERROR=1; FLAG_LED_RED_ON=1; } } } } else { MOTO_CHECK_AD_DELAY=0; FLAG_MOTO_ERROR1=0; // FLAG_MOTO_ERROR=0; } } */ if(!FLAG_MOTO_ON) { FLAG_MOTO_ERROR=0; MOTO_CHECK_AD_DELAY=0; } } void DEAL_SCAN_BAT_AD() { unsigned int temp_value = 0; static unsigned char LOW_BAT_DELAY; ADM = (ADM & 0xf8) | 5; // ADC 使能 AD转换通道开启 通道 CH4 ADEOC = 0; ADSTR = 1; // 开始转换 while (!ADEOC); //等待转换完成 temp_value = ADB; temp_value = temp_value << 4 | (ADR & 0x0f); BAT_AD_COUNT++; if(BAT_AD_COUNT>4) { BAT_AD_TEMP+=temp_value; } if(BAT_AD_COUNT>=20) { BAT_AD_COUNT=0; BAT_AD_VALUE=BAT_AD_TEMP>>4; BAT_AD_TEMP=0; if(BAT_AD_VALUE<1177) //小于2.3V { LOW_BAT_DELAY++; if(LOW_BAT_DELAY>=250) { LOW_BAT_DELAY=0; FLAG_LOW_BAT=1; } } else { FLAG_LOW_BAT=0; LOW_BAT_DELAY=0; } } } void DEAL_SCAN_KEY() //按键扫描 { static unsigned char SCAN_KEY_STEP,SCAN_KEY_DELAY ; static unsigned char ZHENDONG_LOW_COUNT,ZHENDONG_HIGHT_COUNT; if(!OPEN_KEY_IO) { KEY_VALUE_TEMP=1; } else if(!CLOSE_KEY_IO) { KEY_VALUE_TEMP=2; } else { KEY_VALUE_TEMP=0; } if(KEY_VALUE_TEMP>0) { switch (SCAN_KEY_STEP) { case 0: KEY_VALUE=KEY_VALUE_TEMP; SCAN_KEY_STEP=1; break; case 1: SCAN_KEY_DELAY++; if(SCAN_KEY_DELAY>=50) { SCAN_KEY_DELAY=0; if(KEY_VALUE_TEMP==KEY_VALUE) { if(!FLAG_KEY_PRESS1) { FLAG_KEY_PRESS1=1; FLAG_KEY_PRESS=1; } } } break; default: break; } } else { SCAN_KEY_STEP=0; KEY_VALUE=0; SCAN_KEY_DELAY=0; FLAG_KEY_PRESS1=0; FLAG_KEY_PRESS=0; } if(!FLAG_HAVA_ZHENDONG_LOCK) { if(!MERCURY_KEY_IO) { if(!FLAG_MOTO_ON) { if(!FLAG_ZHENDONG_JISUAN) { FLAG_ZHENDONG_JISUAN=1; SCAN_ZHENDONG_KEY++; } } ZHENDONG_HIGHT_COUNT=0; ZHENDONG_LOW_COUNT++; if(ZHENDONG_LOW_COUNT>=100) { ZHENDONG_LOW_COUNT=0; SCAN_ZHENDONG_KEY=0; } } else { if(!FLAG_MOTO_ON) { if(FLAG_ZHENDONG_JISUAN) { FLAG_ZHENDONG_JISUAN=0; SCAN_ZHENDONG_KEY++; } } ZHENDONG_LOW_COUNT=0; ZHENDONG_HIGHT_COUNT++; if(ZHENDONG_HIGHT_COUNT>=100) { ZHENDONG_HIGHT_COUNT=0; SCAN_ZHENDONG_KEY=0; } } if(SCAN_ZHENDONG_KEY>=6) { SCAN_ZHENDONG_KEY=0; if(!FLAG_LOW_BAT) { FLAG_HAVA_ZHENDONG=1; FLAG_HAVA_ZHENDONG_LOCK=1; } } } else { SCAN_ZHENDONG_KEY=0; FLAG_ZHENDONG_JISUAN=0; } if(FLAG_MOTO_OPEN_ED||FLAG_MOTO_ON||FLAG_IR_SLEEP_SEND_ED) { SCAN_ZHENDONG_KEY=0; FLAG_HAVA_ZHENDONG=0; } if(!FLAG_MOTO_OPEN_ED&&FLAG_HAVA_ZHENDONG_LOCK) { if(FLAG_HAVA_ZHENDONG_LOCK) { HAVA_ZHENDONG_LOCK_DEALY++; if(HAVA_ZHENDONG_LOCK_DEALY>=1800) { HAVA_ZHENDONG_LOCK_DEALY=0; FLAG_HAVA_ZHENDONG_LOCK=0; } } }else { HAVA_ZHENDONG_LOCK_DEALY=0; } } void DEAL_KEY_PRESS() { if(FLAG_KEY_PRESS) { FLAG_KEY_PRESS=0; if(!FLAG_LOW_BAT) { if(KEY_VALUE==1) //开启按键按下 { if(!FLAG_MOTO_OPEN_ED) { if(!FLAG_KEY_OPEN) { FLAG_KEY_OPEN=1; MOTO_ON_OR_OFF_DELAY=0; FLAG_MOTO_ON=1; FLAG_MOTO_DIR=1; FLAG_KEY_MODE=1; // FLAG_HAVA_MAN=1 //暂时不用 } } } else if(KEY_VALUE==2) //关闭按键按下 { if(FLAG_KEY_OPEN||FLAG_MOTO_OPEN_ED) { FLAG_KEY_OPEN=0; MOTO_ON_OR_OFF_DELAY=0; FLAG_MOTO_ON=1; FLAG_MOTO_DIR=0; GREEN_BLINK_COUNT=0; FLAG_HAVA_MAN=0; FLAG_HAVA_MAN1=0; FLAG_HAVA_ZHENDONG=0; } } } KEY_VALUE=0; } } void DEAL_OPEN_IR() //红外发送数据 { unsigned char i; for (i=0; i<20; i++) { IR_SEND_IO=0; IR_SEND_IO=0; Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); IR_SEND_IO=1; Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); } } void DEAL_MOTO_WORK() //电机驱动 { if(FLAG_KEY_MODE) { FLAG_HAVA_MAN=0; FLAG_HAVA_MAN1=0; FLAG_HAVA_ZHENDONG=0; SCAN_ZHENDONG_KEY=0; } if(FLAG_HAVA_MAN||FLAG_HAVA_ZHENDONG) { FLAG_HAVA_ZHENDONG=0; if(!FLAG_HAVA_MAN1) { FLAG_HAVA_MAN1=1; if(!FLAG_MOTO_OPEN_ED) { if(!FLAG_LOW_BAT) { FLAG_MOTO_ON=1; FLAG_MOTO_DIR=1; } FLAG_MOTO_OPEN_ED=1; } if(!FLAG_LOW_BAT) { FLAG_GREEN_ON=1; } } } else { if(FLAG_HAVA_MAN1) { FLAG_HAVA_MAN1=0; GREEN_BLINK_COUNT=4; FLAG_GREEN_ON=0; } if(FLAG_LOW_BAT) // { GREEN_BLINK_COUNT=0; FLAG_MOTO_OPEN_ED=0; } if(FLAG_MOTO_OPEN_ED) //关闭 { if(GREEN_BLINK_COUNT==0) { FLAG_MOTO_ON=1; FLAG_MOTO_DIR=0; GREEN_BLINK_COUNT=0; } } } if(FLAG_MOTO_ON) { MOTO_ON_OR_OFF_DELAY++; if(!FLAG_MOTO_DIR) //关的方向 { if(FLAG_MOTO_ERROR||FLAG_LOW_BAT) { H_BRIDGE_H_IO=0; H_BRIDGE_L_IO=0; } else { if(MOTO_ON_OR_OFF_DELAY<=350) { H_BRIDGE_H_IO=1; H_BRIDGE_L_IO=0; } else { H_BRIDGE_H_IO=0; H_BRIDGE_L_IO=0; } } if(MOTO_ON_OR_OFF_DELAY>=1000) { MOTO_ON_OR_OFF_DELAY=0; FLAG_KEY_MODE=0; FLAG_MOTO_ON=0; FLAG_MOTO_OPEN_ED=0; //完全关闭才开始打开检测 FLAG_HAVA_ZHENDONG_LOCK=1; GREEN_BLINK_COUNT=0; } } else //开的方向 { if(FLAG_MOTO_ERROR||FLAG_LOW_BAT) { H_BRIDGE_H_IO=0; H_BRIDGE_L_IO=0; } else { H_BRIDGE_H_IO=0; H_BRIDGE_L_IO=1; } MOTO_ON_OR_OFF_DELAY++; if(MOTO_ON_OR_OFF_DELAY>=2300) //到时候改时间 { MOTO_ON_OR_OFF_DELAY=0; FLAG_MOTO_ON=0; } } SCAN_ZHENDONG_KEY=0; } else { H_BRIDGE_H_IO=0; H_BRIDGE_L_IO=0; MOTO_ON_OR_OFF_DELAY=0; } } void DEAL_LED() { static unsigned int GREEN_BLINK_DLEAY; static unsigned int RED_BLINK_DLEAY; //灯控制 if(FLAG_GREEN_ON||FLAG_MOTO_ON) { FLAG_LED_GREEN_ON=1; GREEN_BLINK_DLEAY=0; } else { if(GREEN_BLINK_COUNT>0) { GREEN_BLINK_DLEAY++; if(GREEN_BLINK_DLEAY<500) { FLAG_LED_GREEN_ON=1; } else { FLAG_LED_GREEN_ON=0; } if(GREEN_BLINK_DLEAY>1000) { GREEN_BLINK_DLEAY=0; GREEN_BLINK_COUNT--; } } else { FLAG_LED_GREEN_ON=0; GREEN_BLINK_DLEAY=0; } } if(FLAG_KJ_OVER) { if(FLAG_MOTO_ERROR) { AAAA=0; FLAG_LED_RED_ON=1; } else if(FLAG_LOW_BAT&&FLAG_HAVA_MAN) { if(AAAA<250) { AAAA++; FLAG_LED_RED_ON=1; FLAG_LED_GREEN_ON=0; } else { FLAG_LED_RED_ON=0; FLAG_LED_GREEN_ON=0; } } else { AAAA=0; FLAG_LED_RED_ON=0; } } if(FLAG_LED_RED_ON) //到时候不对才改 { RED_LED_IO =0; GREEN_LED_IO =1; } else if(FLAG_LED_GREEN_ON) { RED_LED_IO =1; GREEN_LED_IO =0; } else { RED_LED_IO =0; GREEN_LED_IO =0; } } void DEAL_SLEEP() { if(!FLAG_HAVA_ZHENDONG_LOCK&&!FLAG_SLEEP_ED&&!FLAG_MOTO_OPEN_ED&&!FLAG_LED_RED_ON&&!FLAG_LED_GREEN_ON&&FLAG_KJ_OVER&&OPEN_KEY_IO&&CLOSE_KEY_IO) { /********************切到低频时钟**********************/ TMRCR=0x06; //T1CLK=FCPU/2 T0CLK=Fosc 允许T0溢出唤醒 T0CR=0x44; //开启T0 128分频 T0CLK内部时钟 自动重载 关闭PWM T0CNT=256-125; T0LDR=256-125; // 进入低频后 32k/4/128=2s //低频模式固定4t /*********************************************/ ADEN=0; //关闭外设 CLKS=1; //切换到低频 4t Nop(); Nop(); Nop(); HFDE=1; //关闭高频 T0CNT=256-125; T0EN=1; T0IE=1; INTF=0; FLAG_IR_SLEEP_SEND=1; FLAG_IR_SLEEP_SEND_ED=0; SET_RX_IO_PULL_OFF; SET_RX_OFF; //开启唤醒条件(部分在初始化已经配置) Nop(); Nop(); OSCM&=0xE7; OSCM|=0x10; //绿色模式 Nop(); Nop(); Nop(); SET_RX_IO_PULL_ON; SET_RX_ON; T0IE=0; T0EN=0; Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); HFDE=0; //打开高频 (用户自己选择) Nop(); Nop(); CLKS=0; //切换到高频 (用户自己选择) Nop(); Nop(); /****************************切回125us***************************/ /* TMRCR = 0; //T1CLK=FCPU/2 T0CLK=FCPU/2 关闭T0溢出唤醒 T1ICKS=1; T0CR = 0xF4; //开启T0 32分频 T0CLK内部时钟 自动重载 关闭PWM T0CNT = 256 - 125; T0LDR = 256 - 125; // 1ms T0IE = 1; T0EN=1; */ TIMER0_INT(); /*********************************************************/ ADEN=1; LOW_COUNT=0; HIGHT_COUNT=0; FLAG_CLR_FIRST=0; // FLAG_OPEN_JG_TIMER=0; IR_TIEMR_COUNT=0; } } void main() { GIE = 0; CLR_RAM(); IO_INIT(); while(ADC_Zero_ADJ()); //校准失败一直校准 ADC_Init(); TIMER0_INT(); GIE = 1; FLAG_IR_SLEEP_SEND_ED=1; while (1) { WDTCR=0x5A; if(FLAG_1MS) { FLAG_1MS=0; if(KJ_DELAY<1000) { KJ_DELAY++; FLAG_LED_RED_ON=1; } else { if(!FLAG_KJ_OVER) { FLAG_KJ_OVER=1; FLAG_LED_RED_ON=0; FLAG_HAVA_MAN=1; } } DEAL_SCAN_KEY(); DEAL_KEY_PRESS(); DEAL_SCAN_BAT_AD(); DEAL_SCAN_MOTO_AD(); DEAL_LED(); DEAL_MOTO_WORK(); DEAL_SLEEP(); if(FLAG_IR_SLEEP_SEND) //唤醒发一次 0.1S { FLAG_IR_SLEEP_SEND=0; DEAL_OPEN_IR(); } if(FLAG_IR_SLEEP_SEND_ED) //32MS { CESHI++; if(CESHI>32) { CESHI=0; DEAL_OPEN_IR(); } } } } } void int_isr(void) __interrupt { static unsigned char _1MS_COUNT; __asm push __endasm; if (T0IF && T0IE) { T0IF = 0; _1MS_COUNT++; if(_1MS_COUNT>=8) { _1MS_COUNT=0; FLAG_1MS=1; } if(!IR_RX_IO) { LOW_COUNT++; if(LOW_COUNT>254) LOW_COUNT=0; //防止溢出 FLAG_CLR_FIRST=1; HIGHT_COUNT=0; } else { HIGHT_COUNT++; if(HIGHT_COUNT>=300) //260 { HIGHT_COUNT=0; FLAG_CLR_FIRST=0; FLAG_HAVA_MAN=0; FLAG_SLEEP_ED=0; } IR_TIEMR_COUNT++; if(IR_TIEMR_COUNT>=260) { IR_TIEMR_COUNT=0; } if(FLAG_CLR_FIRST) { FLAG_CLR_FIRST=0; if(LOW_COUNT>=2&&LOW_COUNT<=6) //4 6 { FLAG_IR_SLEEP_SEND_ED=1; FLAG_SLEEP_ED=1; if(!FLAG_OPEN_JG_TIMER) { FLAG_OPEN_JG_TIMER=1; IR_TIEMR_COUNT=0; } else //第二次 { if(IR_TIEMR_COUNT>=250&&IR_TIEMR_COUNT<=268) { FLAG_HAVA_MAN=1; } IR_TIEMR_COUNT=0; FLAG_OPEN_JG_TIMER=0; } } } LOW_COUNT=0; } } __asm pop __endasm; }