Key.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*******************************************************************************
  2. * File Name: Key.c
  3. * Function Describe:device for the Keys
  4. * Relate Module:
  5. * Explain: Hardware version is HS110C
  6. * Writer: ShiLiangWen
  7. * Date: 2015.1.26
  8. *******************************************************************************/
  9. #define THIS_FILE_ID 14
  10. //--------------------------------------------------------------------------------
  11. #include "stm32f10x.h"
  12. #include "Key.h"
  13. #include <includes.h>
  14. //key value variable
  15. unsigned long g_ulKeyValue;
  16. unsigned char g_ucDKC=0;
  17. unsigned char g_ucUKC=0;
  18. unsigned char g_ucKeyFree=0;
  19. unsigned char g_ucKeyMode=0;
  20. unsigned char g_ucKeyPPTPress=0;
  21. unsigned char g_ucEncode[20];
  22. ////////////////////////// function declare //////////////////////////////
  23. unsigned long KeyScanPort(void);
  24. ////////////////////////// end declare //////////////////////////////
  25. /********************************************************************************
  26. KeyInit
  27. *******************************************************************************/
  28. void CheckPwrKeyOn(void)
  29. {//长按电源键直到电筒灯亮后释放
  30. unsigned int tick=0;
  31. if(KeyPwrStatus !=0)
  32. {
  33. POWER_ON_FAILED:
  34. SlwTrace(INF, "\r\nPwrOnFailed",1);
  35. printf("KeyPwrStatus===%016x\r\n",KeyPwrStatus);
  36. DelayMs(10);
  37. PWR_EN_LOW;
  38. while(1);
  39. }
  40. while(KeyPwrStatus==0)
  41. {
  42. if(++tick >= 2000000)//0
  43. {
  44. MODEM_LED2_HIGH;
  45. SlwTrace(INF, "\r\nPwrOnOk",1);
  46. PWR_EN_HIGH;//
  47. return;
  48. }
  49. }
  50. SlwTrace(INF,"\r\nPwr erro",1);
  51. goto POWER_ON_FAILED;
  52. }
  53. //检测关机
  54. void SoftPwrOffCheck(void)
  55. {
  56. static unsigned int seconds=0;
  57. static unsigned char KeyPowerCnt=0;
  58. if(KeyPwrStatus==0)
  59. {
  60. KeyPowerCnt++;
  61. printf("KeyPowerCnt++===%d\r\n",KeyPowerCnt);
  62. if(KeyPowerCnt>2)
  63. {
  64. // if(g_uGtTaskEn||BOOTDebug){//写号模式直接关机
  65. // os_dly_wait(20);
  66. // PWR_EN_LOW;
  67. // }else {
  68. // UISetNextStatus(UIS_POWEROFF);
  69. // }
  70. SlwTrace(INF, "PwrOffOk",1);
  71. ShowMessageBox("正在关机!");
  72. MODEN_POWER_EN_HIGH;
  73. IWDG_ReloadCounter();
  74. os_dly_wait(100);
  75. MODEN_POWER_EN_LOW;
  76. // IWDG_ReloadCounter();
  77. // os_dly_wait(220);
  78. PWR_EN_LOW;
  79. while(1);
  80. // GuiShowBmp(0,0,"BYEBYE.bmp");
  81. // PWR_EN_LOW;
  82. // while(1);
  83. }
  84. }else KeyPowerCnt=0;
  85. }
  86. void KeyInit(void)
  87. {
  88. GPIO_InitTypeDef GPIO_InitStructure;
  89. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//复用功能IO 时钟使能 这里必须先使能复用IO的时钟,再关闭JTAG功能
  90. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // JTAG-DP Disabled and SW-DP Enabled
  91. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);
  92. PWR_BackupAccessCmd( ENABLE );
  93. RCC_LSEConfig( RCC_LSE_OFF );
  94. BKP_TamperPinCmd(DISABLE);
  95. /*GPIO先全部设置为上拉输入*/
  96. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  97. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  98. //PPT
  99. GPIO_InitStructure.GPIO_Pin=KEY_PPT_PIN;
  100. GPIO_Init(KEY_PPT_PORT, &GPIO_InitStructure);
  101. //PANEL_UP
  102. GPIO_InitStructure.GPIO_Pin=KEY_PANEL_UP_PIN;
  103. GPIO_Init(KEY_PANEL_UP_PORT, &GPIO_InitStructure);
  104. //PANEL_DOWN
  105. GPIO_InitStructure.GPIO_Pin=KEY_PANEL_DOWN_PIN;
  106. GPIO_Init(KEY_PANEL_DOWN_PORT, &GPIO_InitStructure);
  107. //PANEL_OK
  108. GPIO_InitStructure.GPIO_Pin=KEY_SOS_OK_PIN;
  109. GPIO_Init(KEY_SOS_OK_PORT, &GPIO_InitStructure);
  110. //PANEL_EXIT
  111. GPIO_InitStructure.GPIO_Pin=KEY_PANEL_EXIT_PIN;
  112. GPIO_Init(KEY_PANEL_EXIT_PORT, &GPIO_InitStructure);
  113. //PANEL_MENU
  114. GPIO_InitStructure.GPIO_Pin=KEY_PANEL_MENU_PIN;
  115. GPIO_Init(KEY_PANEL_MENU_PORT, &GPIO_InitStructure);
  116. //KEY_POWER
  117. GPIO_InitStructure.GPIO_Pin=KEY_FUNC_P1_PIN;
  118. GPIO_Init(KEY_FUNC_P1_PORT, &GPIO_InitStructure);
  119. //INFO
  120. GPIO_InitStructure.GPIO_Pin=KEY_INFO_PIN;
  121. GPIO_Init(KEY_INFO_PORT, &GPIO_InitStructure);
  122. //FUNC
  123. GPIO_InitStructure.GPIO_Pin=KEY_FUNC_P2_PIN;
  124. GPIO_Init(KEY_FUNC_P2_PORT, &GPIO_InitStructure);
  125. // //PPT_MIC
  126. // GPIO_InitStructure.GPIO_Pin=KEY_PPT_MIC_PIN;
  127. // GPIO_Init(KEY_PPT_MIC_PORT, &GPIO_InitStructure);
  128. //LED_BACK_LIGHT
  129. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  130. GPIO_InitStructure.GPIO_Pin=KEY_LED_PIN;
  131. GPIO_Init(KEY_LED_PORT, &GPIO_InitStructure);
  132. //chargechek
  133. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  134. GPIO_InitStructure.GPIO_Pin=CHARGEChek_PIN;
  135. GPIO_Init(CHARGEChek_PORT,&GPIO_InitStructure);
  136. //ear check
  137. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  138. GPIO_InitStructure.GPIO_Pin=EAR_CHEK_PIN;
  139. GPIO_Init(EAR_CHEK_PORT,&GPIO_InitStructure);
  140. //CHANGELED
  141. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  142. GPIO_InitStructure.GPIO_Pin=CHARGELED_PIN;
  143. GPIO_Init(CHARGELED_PORT, &GPIO_InitStructure);
  144. CheckPwrKeyOn();
  145. g_ucKeyMode=0;//连续触发模式
  146. RunMake(THIS_FILE_ID);
  147. }
  148. unsigned long g_ulEncode=0;
  149. /********************************************************************************
  150. * Function:KeyScanPort
  151. *******************************************************************************/
  152. unsigned long KeyScanPort(void)
  153. {
  154. unsigned long k1,k2;
  155. k1=GPIOB->IDR;
  156. k1&=KEY_ALL_PORTB;
  157. k1<<=16;
  158. k2=GPIOC->IDR;
  159. k2&=KEY_ALL_PORTC;
  160. return (k1|k2);
  161. }
  162. /************************************************************
  163. *GetKey
  164. 读键盘数据
  165. 建议10ms调用一次此函数,不同调用时间需要修改函数内的相关参数以达到比较好的效果
  166. 参数:
  167. g_ucKeyMode=1时,单次模式
  168. g_ucKeyMode=0时,持续模式
  169. 返回值:
  170. 0--按键无变化
  171. 1 --按键有按下:
  172. 单次模式下,当按键第一次按下才返回一次该值;
  173. 持续模式下,按着按键不放将间隔返回该值
  174. g_ulKeyValue保存当前按键值
  175. -1 --按键有释放.不管单次模式还是持续模式,当按键释放时只返回一次
  176. 释放后,g_ulKeyValue保存上次按下的按键值
  177. *************************************************************/
  178. int GetKey(void)
  179. {
  180. static unsigned char sucCt=0;//连续模式下响应频率控制;
  181. static unsigned char sucFlag=0;
  182. static unsigned char sucPress=0;
  183. static unsigned char sucLastPress=0;
  184. static unsigned long Key=0;
  185. static unsigned long Key1=0;
  186. static unsigned long Key2=0;
  187. static unsigned long Key3=0;
  188. static unsigned long Key4=0;
  189. static unsigned int KeyOstime=0;
  190. if(os_time_get() < KeyOstime) return 0;
  191. KeyOstime = os_time_get()+1;//1
  192. //去抖动 0 1
  193. if(++sucFlag>4){ //1
  194. sucFlag=0;
  195. }
  196. if(sucFlag==0){
  197. Key1=KeyScanPort();
  198. }else if(sucFlag==1){
  199. Key2=KeyScanPort();
  200. }else if(sucFlag==2){
  201. Key3=KeyScanPort();
  202. }else {
  203. Key4=KeyScanPort();
  204. }
  205. if(Key1!=Key2||Key2!=Key3||Key3!=Key4)return 0;
  206. //按键状态发生变更
  207. if(Key!=Key1){
  208. Key=Key1;
  209. if(Key!=((KEY_ALL_PORTB<<16)| KEY_ALL_PORTC))
  210. {
  211. sucPress=1; //有按下
  212. g_ucUKC=0;
  213. printf("KEY=%lx\r\n",Key);
  214. }
  215. else
  216. {
  217. sucPress=0; //无按下
  218. g_ucDKC=0;
  219. }
  220. }
  221. if(sucPress){//有按键按下
  222. if(g_ucKeyMode){ //单次模式
  223. if(sucLastPress!=sucPress){
  224. sucLastPress=sucPress;
  225. g_ulKeyValue=Key;
  226. return 1;
  227. }else{
  228. return 0;
  229. }
  230. }else{ //持续模式
  231. sucLastPress=sucPress;
  232. if(0==sucCt){
  233. g_ulKeyValue=Key1;
  234. sucCt=80;//100;//10ms*100=1000ms
  235. return 1;
  236. }else{
  237. sucCt--;
  238. return 0;
  239. }
  240. }
  241. }else{ //无按键按下
  242. sucCt=0;
  243. if(sucLastPress){
  244. sucLastPress=0;
  245. return -1;
  246. }
  247. }
  248. return 0;
  249. }
  250. void KeyCount(void)
  251. {
  252. if(g_ucDKC<255)g_ucDKC++;
  253. if(g_ucUKC<255)g_ucUKC++;
  254. }
  255. void OnOff_Init(void)
  256. {
  257. EXTI_InitTypeDef EXTI_InitStructure;
  258. GPIO_InitTypeDef GPIO_InitStructure;
  259. RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO , ENABLE);
  260. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;//
  261. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  262. GPIO_InitStructure.GPIO_Pin = ONOFF_CK_PIN;
  263. GPIO_Init(ONOFF_CK_PORT, &GPIO_InitStructure);
  264. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
  265. EXTI_InitStructure.EXTI_Line = EXTI_Line9;
  266. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  267. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  268. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  269. EXTI_Init(&EXTI_InitStructure);
  270. }
  271. void OnOff_Init2(void)
  272. {
  273. EXTI_InitTypeDef EXTI_InitStructure;
  274. GPIO_InitTypeDef GPIO_InitStructure;
  275. RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO , ENABLE);
  276. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;//
  277. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  278. GPIO_InitStructure.GPIO_Pin = ONOFF_CK_PIN;
  279. GPIO_Init(ONOFF_CK_PORT, &GPIO_InitStructure);
  280. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
  281. EXTI_InitStructure.EXTI_Line = EXTI_Line9;
  282. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  283. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  284. EXTI_InitStructure.EXTI_LineCmd = DISABLE;
  285. EXTI_Init(&EXTI_InitStructure);
  286. }
  287. void EXTI9_5_IRQHandler(void)
  288. {
  289. if(SET==EXTI_GetFlagStatus(EXTI_Line9))
  290. {//外部电源关了
  291. // if(modemInitReady==1)
  292. // {
  293. PWR_EN_HIGH;//模块已初始化成功的,需要软件关机,先锁好电源
  294. modemPwrOffNow=1;
  295. // }
  296. EXTI_ClearFlag(EXTI_Line9);
  297. }
  298. }
  299. void Charge_Init(void)
  300. {
  301. GPIO_InitTypeDef GPIO_InitStructure;
  302. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  303. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  304. GPIO_InitStructure.GPIO_Pin = ONOFF_CK_PIN;
  305. GPIO_Init(ONOFF_CK_PORT, &GPIO_InitStructure);
  306. GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource9);
  307. }
  308. /********************************************************************************
  309. * End of Module
  310. *******************************************************************************/