本帖最后由 我是土匪 于 2015-9-18 17:17 编辑
更新GPRS开、关机代码:
enum
{
GPRS_POWERON_STAR = 0, //拉低POWERKEY开始
GPRS_POWERON_END , //拉低POWERKEY结束,认为开机动作结束
GPRS_SYNC, //GPRS串口同步成功,成功开机
GPRS_POWEROFF_STAR, //开始关机,拉低POWERKEY
GPRS_POWEROFF_END //断电
};
#define GPRS_POWERKEY_OUTPUT_LOW() GPIO_WriteBit(GPRS_POWERKEY_PORT, GPRS_POWERKEY_PIN, SET)
#define GPRS_POWERKEY_OUTPUT_HIGH() GPIO_WriteBit(GPRS_POWERKEY_PORT, GPRS_POWERKEY_PIN, RESET)
#define GPRS_VBAT_ON() GPIO_WriteBit(GPRS_POWER_PORT, GPRS_POWER_PIN, SET)
#define GPRS_VBAT_OFF() GPIO_WriteBit(GPRS_POWER_PORT, GPRS_POWER_PIN, RESET)
#define STATUS_LED_ON() GPIO_WriteBit(LED_STATUS_PORT, LED_STATUS_PIN,LED_ON)
#define STATUS_LED_OFF() GPIO_WriteBit(LED_STATUS_PORT, LED_STATUS_PIN,LED_OFF)
#define GPRS_POWER_CTR_EN //使能电源控制
/********************************************
执行开机动作(powerkey)后,发送AT去同步模块串口,最多重复3次,大于3依旧没同步上,证明本次开机失败,或者断电重启或者报警处理等。
发送:AT\r\n
返回:OK\r\n 则表示同步成功
*********************************************/
void Sent_AT(void)
{
static char count = 0;
const char AT_cmd[]="AT\r\n";
if((GPRS_POWERON_STAR == Gprs_Status) || (GPRS_POWERON_END == Gprs_Status))
{
count ++;
if(count > 3)
{
bsp_StopTimer(GPRS_SYNC_TIMER_ID);
count = 0;
/*
本次开机失败,添加自己代码,断电重启,或其他处理
*/
}
else
{
Sent_String((char *)AT_cmd,strlen(AT_cmd));
}
}
else
{
bsp_StopTimer(GPRS_SYNC_TIMER_ID);
}
}
/* ----------------------------------------------------------------------------
检测status引脚状态:PD4
1开机
0关机
----------------------------------------------------------------------------- */
char Check_GPRS_StatusPIN(void)
{
char status = 0;
status = GPIOD->IDR & GPIO_Pin_4;
return (status?1:0);
}
/* ----------------------------------------------------------------------------
GPRS关机延时
POWER\断电和状态切换
----------------------------------------------------------------------------- */
void PowerOff_Gprs_Dely(void)
{
GPRS_POWERKEY_OUTPUT_HIGH(); //POWERKEY PULL UP
/*断电*/
#ifdef GPRS_POWER_CTR_EN
GPRS_VBAT_OFF();
#endif
Gprs_Status = GPRS_POWEROFF_END;
}
/* ----------------------------------------------------------------------------
流程:
GPRS上电->延时200mS->拉低POWERKEY->3S后拉起POWERKEY->5S后AT去同步串口
----------------------------------------------------------------------------- */
volatile char PowerOn_GPRS_Count = 0;
#define POWERON_DELY 30
void PowerOn_GPRS(void)
{
if((GPRS_POWEROFF_STAR == Gprs_Status) || (GPRS_POWEROFF_END == Gprs_Status))
{
bsp_StopTimer(GPRS_POWEROFF_ID); //如果正在关机,停止该进程
#ifdef GPRS_POWER_CTR_EN
GPRS_VBAT_ON();//供电
/* 供电需持续一段时间 ,相当于delyMs(200)*/
PowerOn_GPRS_Count = 0;
while(PowerOn_GPRS_Count < POWERON_DELY); //经验值,200ms左右
#endif
GPRS_POWERKEY_OUTPUT_LOW();//开始拉低POWERKEY
//定时3s( >1.2S),定时到拉起POWERKEY
bsp_StopTimer(POWERKEY_LOW_TIMERID);
bsp_StartTimer(POWERKEY_LOW_TIMERID, POWERKEY_LOW_TIMERPERIOD, (bspTimerHandler)PullUp_PowerKey);
//单次执行的定时器
//定时5S(>3.2S)判断单片机和GPRS模块是否同步成功: AT
bsp_StopTimer(GPRS_SYNC_TIMER_ID);
bsp_StartAutoTimer(GPRS_SYNC_TIMER_ID, GPRS_SYNC_TIMEPERIOD,(bspTimerHandler)Sent_AT); //周期性的定时器
Gprs_Status = GPRS_POWERON_STAR;
//避免串口串电,如果控制电源,有必要这样做。
#ifdef GPRS_POWER_CTR_EN
USART_Config();
#endif
}
}
/* ----------------------------------------------------------------------------
关机流程:
拉低POWERKEY ->6S后拉高POWERKEY->断电
(关机拉低1S以上模块会关机,6S左右会彻底关机,此时断电对模块不会造成伤害)
----------------------------------------------------------------------------- */
void PowerOff_GPRS(char cmd)
{
BitStatus res = 0;
if((GPRS_POWEROFF_STAR != Gprs_Status) || (GPRS_POWEROFF_END != Gprs_Status))
{
//开始拉低POWERKEY
GPRS_POWERKEY_OUTPUT_LOW();
Gprs_Status = GPRS_POWEROFF_STAR;
bsp_StopTimer(GPRS_POWEROFF_ID);
bsp_StartTimer(GPRS_POWEROFF_ID, POWEROFF_GPRS_TIMERPERIOD, (bspTimerHandler)PowerOff_Gprs_Dely);
}
Led_Cmd = LED_OFF;
}
/* ----------------------------------------------------------------------------
开机时序中拉起POWERKEY功能
----------------------------------------------------------------------------- */
void PullUp_PowerKey(void)
{
GPRS_POWERKEY_OUTPUT_HIGH();
Gprs_Status = GPRS_POWERON_END;
}
|