STOP休眠模式

  1. #include "stm32f10x_conf.h"
  2. #include "bsp_include.h"
  3. uint8_t GeneralAwakeFlag = 0; //退出休眠标志
  4. Sleep_Struct SleepStruct;
  5. //*************************************************进入STOP休眠后RTC唤醒配置
  6. static void RTC_Configuration(void)
  7. {
  8. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP, ENABLE);
  9. PWR_BackupAccessCmd(ENABLE);
  10. BKP_DeInit();
  11. RCC_LSICmd(ENABLE); /*使用内部40K振荡器*/
  12. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // 配置内部时钟
  13. RCC_RTCCLKCmd(ENABLE); /*使能RTC*/
  14. RTC_WaitForSynchro(); /*等待时钟同步*/
  15. RTC_WaitForLastTask(); /*等待写操作完成*/
  16. RTC_SetPrescaler(40000); /*1秒计数*/
  17. RTC_WaitForLastTask();
  18. }
  19. static void RTC_Alarm_EXTI_Config(uint16_t sec)
  20. {/*配置RTC闹钟事件到中断17*/
  21. EXTI_InitTypeDef EXTI_InitStructure;
  22. EXTI_ClearITPendingBit(EXTI_Line17);
  23. EXTI_InitStructure.EXTI_Line = EXTI_Line17;
  24. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  25. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  26. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  27. EXTI_Init(&EXTI_InitStructure);
  28. RTC_SetAlarm(RTC_GetCounter()+sec);/*设置N秒后唤醒*/
  29. RTC_WaitForLastTask();
  30. }
  31. static void RTC_Alarm_NVIC_Config(void)
  32. {/*配置RTC闹钟中断*/
  33. NVIC_InitTypeDef NVIC_InitStructure;
  34. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  35. NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
  36. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  37. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  38. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  39. NVIC_Init(&NVIC_InitStructure);
  40. RTC_ITConfig(RTC_IT_ALR, ENABLE);/*使能中断*/
  41. RTC_WaitForLastTask();
  42. }
  43. void RTC_IRQHandler(void)
  44. {// RTC中断
  45. if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
  46. {
  47. RTC_ClearITPendingBit(RTC_IT_SEC);
  48. RTC_WaitForLastTask();
  49. }
  50. }
  51. void RTCAlarm_IRQHandler(void)
  52. {// RTC告警中断
  53. if(RTC_GetFlagStatus(RTC_IT_ALR))
  54. {
  55. RTC_ClearITPendingBit(RTC_IT_ALR);
  56. RTC_WaitForLastTask();
  57. EXTI_ClearITPendingBit(EXTI_Line17);/*清除闹钟中断*/
  58. if(PWR_GetFlagStatus(PWR_FLAG_WU) != RESET)
  59. {
  60. PWR_ClearFlag(PWR_FLAG_WU);/*清除唤醒标志*/
  61. }
  62. }
  63. }
  64. //*************************************************RTC唤醒配置
  65. //static void LowPower_InStandby(void)
  66. //{/*进入待机模式,待机模式下唤醒,程序复位运行*/
  67. // RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR , ENABLE);
  68. // RTC_Configuration(); /*配置RTC时钟*/
  69. // RTC_Alarm_EXTI_Config(10); /*配置闹钟唤醒*/
  70. // //PWR_WakeUpPinCmd(ENABLE); /*使能WAKEUP脚唤醒*/
  71. // IWDG_Config(); /*使用独立看门狗唤醒*/
  72. // PWR_EnterSTANDBYMode(); /*进入待机模式*/
  73. //}
  74. //中断配置,只需要配置唤醒的中断
  75. static void Wake_EXTI_Intit(void)
  76. {
  77. NVIC_InitTypeDef NVIC_InitStructure;
  78. EXTI_InitTypeDef EXTI_InitStructure;
  79. GPIO_InitTypeDef GPIO_InitStructure;
  80. /* 设置中断向量表是在RAM中或是flash中,并指出器偏移量 */
  81. #ifdef VECT_TAB_RAM
  82. /* Set the Vector Table base location at 0x20000000 */
  83. NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  84. #else /* VECT_TAB_FLASH */
  85. #ifdef USER_BOOTLOADER
  86. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000); // 中断向量偏移
  87. #else
  88. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  89. #endif
  90. #endif
  91. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  92. // PA0 充电唤醒中断配置
  93. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
  94. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ;//GPIO_Mode_AIN GPIO_Mode_IN_FLOATING
  95. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  96. GPIO_Init(GPIOA, &GPIO_InitStructure);
  97. GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
  98. EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  99. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//EXTI_Mode_Event EXTI_Mode_Interrupt
  100. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  101. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  102. EXTI_Init(&EXTI_InitStructure);
  103. EXTI_ClearITPendingBit(EXTI_Line0);
  104. NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
  105. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  106. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  107. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  108. NVIC_Init(&NVIC_InitStructure);
  109. // PA1 通信唤醒口
  110. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
  111. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ;//GPIO_Mode_AIN GPIO_Mode_IN_FLOATING
  112. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  113. GPIO_Init(GPIOA, &GPIO_InitStructure);
  114. GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
  115. EXTI_InitStructure.EXTI_Line = EXTI_Line1;
  116. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//EXTI_Mode_Event EXTI_Mode_Interrupt
  117. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  118. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  119. EXTI_Init(&EXTI_InitStructure);
  120. EXTI_ClearITPendingBit(EXTI_Line1);
  121. NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
  122. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  123. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  124. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  125. NVIC_Init(&NVIC_InitStructure);
  126. // RTC
  127. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  128. NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
  129. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  130. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  131. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  132. NVIC_Init(&NVIC_InitStructure);
  133. }
  134. //休眠前管脚关闭或开启
  135. void ALL_GPIO_Sleep_Init(void)
  136. {
  137. GPIO_InitTypeDef GPIO_InitStructure;
  138. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  139. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
  140. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All ;
  141. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN ;
  142. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  143. GPIO_Init(GPIOA, &GPIO_InitStructure);
  144. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  145. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All ;
  146. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN ;
  147. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  148. GPIO_Init(GPIOB, &GPIO_InitStructure);
  149. //预充
  150. RCC_APB2PeriphClockCmd(PRECHARGE_APB,ENABLE);
  151. GPIO_InitStructure.GPIO_Pin = PRECHARGE_PIN ;
  152. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;
  153. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  154. GPIO_Init(PRECHARGE_PORT, &GPIO_InitStructure);
  155. Relay_PreChargeOff(0);
  156. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  157. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All ;
  158. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN ;
  159. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  160. GPIO_Init(GPIOC, &GPIO_InitStructure);
  161. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
  162. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All ;
  163. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN ;
  164. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  165. GPIO_Init(GPIOD, &GPIO_InitStructure);
  166. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
  167. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All ;
  168. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN ;
  169. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  170. GPIO_Init(GPIOE, &GPIO_InitStructure);
  171. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);
  172. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All ;
  173. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN ;
  174. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  175. GPIO_Init(GPIOF, &GPIO_InitStructure);
  176. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE);
  177. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All ;
  178. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN ;
  179. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  180. GPIO_Init(GPIOG, &GPIO_InitStructure);
  181. /*放电mos控制*/
  182. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  183. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;
  184. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;
  185. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  186. GPIO_Init(GPIOA, &GPIO_InitStructure);
  187. GPIOA->BSRR = GPIO_Pin_10;// 打开放电mos
  188. // 单片机给运放供电
  189. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  190. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  191. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;
  192. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  193. GPIO_Init(GPIOB, &GPIO_InitStructure);
  194. GPIOB->BSRR = GPIO_Pin_3;// 打开放电mos
  195. }
  196. #if INSERT_DOG_ENABLE /*使用内部看门狗*/
  197. /**
  198. * @brief 独立看门狗配置
  199. * @param NONE
  200. * @retval NONE
  201. */
  202. void WatchDog_Config(void)
  203. { /*看门狗配置*/
  204. RCC_LSICmd(ENABLE);
  205. while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
  206. {
  207. ;
  208. }
  209. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
  210. IWDG_SetPrescaler(IWDG_Prescaler_256); /*设置为秒超时复位*/
  211. IWDG_SetReload(0xfff);
  212. while (IWDG_GetFlagStatus(IWDG_FLAG_PVU) == RESET)
  213. {
  214. ;
  215. }
  216. IWDG_Enable();
  217. }
  218. #endif
  219. //休眠设置
  220. void InsertLowpowerInit(void)
  221. {
  222. uint8_t ReadBackWatchdogFlag;
  223. ReadBackWatchdogFlag = ReadLowPowerFlag();
  224. if (ReadBackWatchdogFlag == LowPowerFlagValue)
  225. {// 进入深度休眠
  226. EnableInterrupt(); /*硬件初始化完成后开总中断*/
  227. Re_LowPower_SEEP();
  228. }
  229. else
  230. {
  231. WatchDog_Config();
  232. }
  233. }
  234. /******************************ADC通道配置**********************************/
  235. #define ADC1_DR_Address ((uint32_t)0x4001244C) /*!<ADC数据寄存器地址*/
  236. #define ADC_CHANN_INUSE_NUM 1 /*!<ADC通道使用数*/
  237. static uint16_t ADCValueBuf[ADC_CHANN_INUSE_NUM]; /*!<采集数据缓存*/
  238. static const uint8_t TempChnnaIndex[]={0};
  239. static const uint8_t Sel_ADC_Chann[ADC_CHANN_INUSE_NUM]=
  240. {
  241. ADC_Channel_6//,ADC_Channel_7,
  242. };/*!<温度采集通道*/
  243. /**
  244. * @brief ADC采集通道引脚配置
  245. * @param NONE
  246. * @retval NONE
  247. */
  248. uint8_t EnterLowPower = 0; // 进入普通休眠标志
  249. static void ADC_ChannelPin_Config(void)
  250. {
  251. GPIO_InitTypeDef GPIO_InitStructure;
  252. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; /*模拟输入*/
  253. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  254. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//|GPIO_Pin_7; /*PA7*/
  255. GPIO_Init(GPIOA, &GPIO_InitStructure);
  256. }
  257. /**
  258. * @brief ADC模块初始化
  259. * @param NONE
  260. * @retval NONE
  261. */
  262. void ADC_Mode_Init()
  263. {
  264. ADC_InitTypeDef ADC_InitStructure;
  265. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /*打开ADC时钟*/
  266. ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  267. ADC_InitStructure.ADC_ScanConvMode = ENABLE; /*通道扫描模式*/
  268. ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//ENABLE; /*连续转换模式*/
  269. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  270. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; /*数据右对齐*/
  271. ADC_InitStructure.ADC_NbrOfChannel = ADC_CHANN_INUSE_NUM; /*通道个数*/
  272. ADC_Init(ADC1, &ADC_InitStructure);
  273. ADC_RegularChannelConfig(ADC1, Sel_ADC_Chann[0], 1, ADC_SampleTime_239Cycles5);
  274. ADC_Cmd(ADC1, ENABLE); /*打开ADC*/
  275. ADC_StartCalibration(ADC1); /*启动ADC校准*/
  276. while(ADC_GetCalibrationStatus(ADC1)); /*等待校准完成*/
  277. ADC_SoftwareStartConvCmd(ADC1, ENABLE); /*开始ADC数据采集*/
  278. }
  279. /**
  280. * @brief ADC模块初始化
  281. * @param NONE
  282. * @retval NONE
  283. */
  284. static void ADC_Module_Init(void)
  285. {
  286. ADC_ChannelPin_Config();
  287. ADC_Mode_Init();
  288. }
  289. // 延时
  290. static void _Delay_Nms_My(uint16_t Nms)
  291. {
  292. uint16_t i=0,j=0;
  293. while(i<Nms)
  294. {
  295. i++;
  296. #if SYSTEMCLOCK_24M
  297. j=4800;/*系统时钟24M*/
  298. #else
  299. //j=1600;/*系统时钟8M*/
  300. j = 800;
  301. #endif
  302. while(j)
  303. {
  304. j--;
  305. }
  306. }
  307. }
  308. // 获取ADC采集数据
  309. u16 Get_Adc(u8 ch)
  310. {
  311. u16 dat = 0;
  312. _Delay_Nms_My(200);
  313. dat = ADC_GetConversionValue(ADC1);
  314. return dat; //返回最近一次ADC1规则组的转换结果
  315. }
  316. static RegisterGroup BQ940RegDat; /*!<寄存器数据*/
  317. static uint8_t BQ940_ChipFunctionConfig(void)
  318. {
  319. uint8_t res = TRUE;
  320. BQ940RegDat.SysCtrl1.Byte = 0;/*控制寄存器数据清零*/
  321. BQ940RegDat.SysCtrl2.Byte = 0;/*控制寄存器数据清零*/
  322. BQ940RegDat.SysCtrl1.Bits.ADC_EN = 0;/*不使能ADC*/
  323. BQ940RegDat.SysCtrl2.Bits.DSG_ON = 1; // 放电mos打开
  324. BQ940RegDat.SysCtrl2.Bits.CC_EN = 0;/*使能电流采集*/
  325. BQ940RegDat.SysCtrl1.Byte = BQ940_WriteRegDat(REG_SYS_CTRL1,BQ940RegDat.SysCtrl1.Byte);
  326. BQ940RegDat.SysCtrl2.Byte = BQ940_WriteRegDat(REG_SYS_CTRL2,BQ940RegDat.SysCtrl2.Byte);
  327. return res;
  328. }
  329. static uint8_t SleepCnt = 0;
  330. void Re_LowPower(void)
  331. {
  332. uint8_t i;
  333. uint8_t sleep_cnt = 0;
  334. uint8_t SaveInsertWdg;
  335. float adc_val = 0;
  336. NVIC_InitTypeDef NVIC_InitStructure;
  337. EXTI_InitTypeDef EXTI_InitStructure;
  338. #if INSERT_DOG_ENABLE/*使用内部看门狗*/
  339. SaveInsertWdg = SleepFlagClear;
  340. #endif
  341. ALL_GPIO_Sleep_Init(); // 进入休眠一脚配置
  342. ADC_Cmd(ADC1, DISABLE);
  343. DMA_Cmd(DMA1_Channel1, DISABLE);
  344. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, DISABLE); /*关闭ADC时钟*/
  345. while(1)
  346. {
  347. Wake_EXTI_Intit();
  348. SleepStruct.sleep_flag = 1;
  349. /* RTC唤醒配置 */
  350. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR , ENABLE);
  351. RTC_Alarm_NVIC_Config();
  352. RTC_Configuration(); /*配置RTC时钟*/
  353. RTC_Alarm_EXTI_Config(5); /*配置闹钟唤醒*/
  354. FeedInsertWatchDog();
  355. SysTick->CTRL = 0x00; // 需要关闭滴答定时器,否则中断会唤醒
  356. SysTick->VAL = 0x00;
  357. PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);// 进入休眠
  358. //------------------------------------通信中断关闭
  359. GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
  360. EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  361. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//EXTI_Mode_Event EXTI_Mode_Interrupt
  362. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  363. EXTI_InitStructure.EXTI_LineCmd = DISABLE;
  364. EXTI_Init(&EXTI_InitStructure);
  365. EXTI_ClearITPendingBit(EXTI_Line0);
  366. NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
  367. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  368. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  369. NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
  370. NVIC_Init(&NVIC_InitStructure);
  371. //------------------------------------通信中断关闭
  372. /* 休眠唤醒计数 */
  373. SleepCnt++;
  374. if(SleepCnt>84)// 根据看门狗定时唤醒时间定
  375. {// 计数唤醒
  376. NVIC_SystemReset();
  377. return;
  378. }
  379. ADC_Module_Init();
  380. adc_val = Get_Adc(0);
  381. adc_val = adc_val*0.8056640625; // 测得电压
  382. if( (adc_val > 800) // 检测到有电流,唤醒78
  383. ||(SleepStruct.sleep_flag == 0)
  384. )
  385. {// 电压大于400 = 0.4v
  386. NVIC_SystemReset();
  387. return;
  388. }
  389. }
  390. }
  391. /*
  392. *休眠模式:停止模式(普通休眠)
  393. */
  394. void LowPower(void)
  395. {
  396. uint8_t SaveInsertWdg;
  397. if(GetSystemRunTimer()>10000)
  398. {
  399. Cap_SaveSocDat(); // 数据保存
  400. }
  401. BQ940_Power_Manage(BQ940_SLEEP_CMD); // 940关闭
  402. #if INSERT_DOG_ENABLE
  403. WriteSleepFlag(SleepFlagValue);
  404. SaveInsertWdg = SleepFlagValue;
  405. SleepStruct.sleep_flag = 1;
  406. #endif
  407. Re_LowPower();
  408. }

STANDBY休眠模式

  1. /*
  2. * 深度休眠
  3. */
  4. void LowPower_SEEP(void)
  5. {
  6. uint8_t SaveInsertWdg;
  7. if(GetSystemRunTimer()>10000)
  8. {
  9. Cap_SaveSocDat(); // 数据保存
  10. }
  11. BQ940_Power_Manage(BQ940_SLEEP_CMD); // 关闭940
  12. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR , ENABLE);
  13. #if INSERT_DOG_ENABLE /*使用内部看门狗*/
  14. SaveInsertWdg = SleepFlagValue;
  15. WriteLowPowerFlag(SaveInsertWdg);
  16. #endif
  17. PWR_WakeUpPinCmd(ENABLE); /*使能WAKEUP脚唤醒*/
  18. _Delay_Nms(500);
  19. FeedWatchDog(); // 喂狗
  20. NVIC_SystemReset(); // 复位
  21. PWR_EnterSTANDBYMode(); /*进入待机模式*/
  22. }
  23. void Re_LowPower_SEEP(void)
  24. {
  25. WriteLowPowerFlag(LowPowerFlagClear); // 清除进入休眠标志位
  26. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR , ENABLE);
  27. PWR_WakeUpPinCmd(ENABLE); /*使能WAKEUP脚唤醒*/
  28. _Delay_Nms(100);
  29. PWR_EnterSTANDBYMode(); /*进入待机模式*/
  30. }
  31. //休眠设置
  32. void InsertLowpowerInit(void)
  33. {
  34. uint8_t ReadBackWatchdogFlag;
  35. ReadBackWatchdogFlag = ReadLowPowerFlag(); // 获取进入休眠标志位
  36. if (ReadBackWatchdogFlag == LowPowerFlagValue)
  37. {// 进入深度休眠
  38. EnableInterrupt(); /*硬件初始化完成后开总中断*/
  39. Re_LowPower_SEEP();
  40. }
  41. else
  42. {
  43. WatchDog_Config();
  44. }
  45. }