Key.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include "includes.h"
  2. unsigned long g_ulKeyValue;
  3. unsigned char g_ucKeyFree=0;
  4. unsigned char g_ucKeyMode=0;
  5. unsigned char g_ucKeyPPTPress=0;
  6. unsigned char g_ucUKC=0;
  7. signed char Headsetsta=0;
  8. /********************************************************************************
  9. 并行数据读取
  10. //CE始终接地
  11. //PL 拉低 放入锁存器
  12. //CP为高 上升沿移位
  13. ********************************************************************************/
  14. unsigned short KeyScan(void)
  15. {
  16. unsigned char i;
  17. unsigned short RetDat=0;
  18. HC165_PL_LOW;
  19. HC165_PL_HIGH;
  20. for(i = 0; i < 16; i++)
  21. {
  22. RetDat <<= 1;
  23. if(HC165_DATA_READ == 1)
  24. {
  25. RetDat |= 0x01;
  26. }else RetDat &= 0xFFFE;
  27. HC165_CP_LOW;
  28. HC165_CP_HIGH;
  29. }
  30. HC165_CP_LOW;
  31. return RetDat;
  32. }
  33. void CheckPwrKeyOn(void)
  34. {//长按电源键直到电筒灯亮后释放
  35. unsigned int tick=0;
  36. if(KeyPwrStatus !=0)
  37. {
  38. POWER_ON_FAILED:
  39. SlwTrace(INF, "\r\nPwrOnFailed",1);
  40. DelayMs(10);
  41. PWR_EN_LOW;
  42. while(1);
  43. }
  44. while(KeyPwrStatus==0)
  45. {
  46. if(++tick >= 200000)
  47. {
  48. MODEM_LED2_HIGH;
  49. SlwTrace(INF, "\r\nPwrOnOk",1);
  50. // while(KeyPwrStatus==0){
  51. //// DelayMs(30);
  52. //// MODEM_LED2_LOW;
  53. //// DelayMs(30);
  54. //// MODEM_LED2_HIGH;
  55. // IWDG_ReloadCounter();
  56. // }
  57. //MODEM_LED2_LOW;
  58. PWR_EN_HIGH;//
  59. return;
  60. }
  61. }
  62. SlwTrace(INF,"\r\nPwr erro",1);
  63. goto POWER_ON_FAILED;
  64. }
  65. void OnOff_Init(void)
  66. {
  67. EXTI_InitTypeDef EXTI_InitStructure;
  68. GPIO_InitTypeDef GPIO_InitStructure;
  69. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO , ENABLE);
  70. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  71. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  72. GPIO_InitStructure.GPIO_Pin = ONOFF_CK_PIN;
  73. GPIO_Init(ONOFF_CK_PORT, &GPIO_InitStructure);
  74. GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource2);
  75. EXTI_InitStructure.EXTI_Line = EXTI_Line2;
  76. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  77. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  78. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  79. EXTI_Init(&EXTI_InitStructure);
  80. }
  81. void EXTI2_IRQHandler(void)
  82. {
  83. if(SET==EXTI_GetFlagStatus(EXTI_Line2))
  84. {//外部电源关了
  85. if(modemInitReady==1)
  86. {
  87. PWR_EN_HIGH;//模块已初始化成功的,需要软件关机,先锁好电源
  88. modemPwrOffNow=1;
  89. }
  90. EXTI_ClearFlag(EXTI_Line2);
  91. }
  92. }
  93. //检测关机
  94. void SoftPwrOffCheck(void)
  95. {
  96. static unsigned int seconds;
  97. if(!seconds) seconds=uSysTickGet()+500;//首次进入
  98. if(KeyPwrStatus==0)
  99. {
  100. if(uSysTickGet() > seconds)
  101. {
  102. SlwTrace(INF, "PwrOffOk",1);
  103. MODEM_LED1_HIGH;
  104. //SetBeepWithOS(878,100);
  105. // while(KeyPwrStatus==0) IWDG_ReloadCounter();
  106. GuiClearAll();
  107. GuiShowBmp(0,0,"BYEBYE.bmp");
  108. MODEM_PWREN_LOW;
  109. PWR_EN_LOW;
  110. MODEM_LED1_LOW;
  111. while(1);
  112. }
  113. }else seconds=uSysTickGet()+200;
  114. }
  115. void KeyInit(void)
  116. {
  117. GPIO_InitTypeDef GPIO_InitStructure;
  118. //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  119. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);//复用功能IO 时钟使能 这里必须先使能复用IO的时钟,再关闭JTAG功能
  120. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // JTAG-DP Disabled and SW-DP Enabled
  121. GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE);
  122. //KEY_POWER 电源检测几秒
  123. /*GPIO设置为浮空*/
  124. GPIO_InitStructure.GPIO_Pin = BEEP_PIN; //
  125. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;; //复用推挽输出
  126. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  127. GPIO_Init(BEEP_PORT, &GPIO_InitStructure);
  128. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  129. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  130. GPIO_InitStructure.GPIO_Pin=GPIO_InitStructure.GPIO_Pin=HC165_READ_PIN;;
  131. GPIO_Init(HC165_READ_PORT, &GPIO_InitStructure);
  132. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  133. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  134. GPIO_InitStructure.GPIO_Pin=GPIO_InitStructure.GPIO_Pin=HC165_PL_PIN;;
  135. GPIO_Init(HC165_PL_PORT, &GPIO_InitStructure);
  136. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  137. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  138. GPIO_InitStructure.GPIO_Pin=GPIO_InitStructure.GPIO_Pin=HC165_CP_PIN;;
  139. GPIO_Init(HC165_CP_PORT, &GPIO_InitStructure);
  140. //CheckPwrKeyOn();
  141. g_ucKeyMode=0;//连续触发模式
  142. HC165_PL_LOW;
  143. HC165_CP_LOW;
  144. }
  145. /************************************************************
  146. *GetKey
  147. 读键盘数据
  148. 建议10ms调用一次此函数,不同调用时间需要修改函数内的相关参数以达到比较好的效果
  149. 参数:
  150. g_ucKeyMode=1时,单次模式
  151. g_ucKeyMode=0时,持续模式
  152. 返回值:
  153. 0--按键无变化
  154. 1 --按键有按下:
  155. 单次模式下,当按键第一次按下才返回一次该值;
  156. 持续模式下,按着按键不放将间隔返回该值
  157. g_ulKeyValue保存当前按键值
  158. -1 --按键有释放.不管单次模式还是持续模式,当按键释放时只返回一次
  159. 释放后,g_ulKeyValue保存上次按下的按键值
  160. *************************************************************/
  161. int GetKey(void)
  162. {
  163. static unsigned char sucCt=0;//连续模式下响应频率控制;
  164. static unsigned char sucFlag=0;
  165. static unsigned char sucPress=0;
  166. static unsigned char sucLastPress=0;
  167. static unsigned short Key=0;
  168. static unsigned short Key1=0;
  169. static unsigned short Key2=0;
  170. static signed char LastHeadsetsta=0;
  171. //GetEncodeKey();
  172. //去抖动 0 1
  173. if(++sucFlag>2){ //1
  174. sucFlag=0;
  175. }
  176. if(sucFlag==0){
  177. Key1=KeyScan();
  178. }else{
  179. Key2=KeyScan();
  180. }
  181. if(Key1!=Key2)return 0;
  182. //if(Key1&~HEDESET_DET){ //0x01<<2
  183. if(Key1&(0x01<<2)){
  184. Headsetsta=0;
  185. }else {
  186. Headsetsta=1;
  187. }
  188. if(LastHeadsetsta!=Headsetsta&&sutUIstatus.Status!=UIS_NULL){
  189. LastHeadsetsta=Headsetsta;
  190. if(LastHeadsetsta){
  191. GuiShowBmp(120,0,"Ear.bmp");
  192. printf("耳机接入\r\n");
  193. SPK_MAIN_MUTE_HIGH;
  194. }else{
  195. printf("耳机拔出\r\n");
  196. //GuiShowBmp(120,1,"VBAT1.bmp");
  197. //GuiClearArea(120,0,16,12);
  198. //GuiClearRect(120,1,16,12);
  199. GuiShowStr(120,1," ",0x01,0);
  200. SPK_MAIN_MUTE_LOW;
  201. }
  202. SetSpkLvProcess(2);
  203. }
  204. Key1=Key1&HEDESET_DET;
  205. //按键状态发生变更
  206. if(Key!=Key1){
  207. Key=Key1;
  208. if(Key!=KEY_ALL_IDLE)
  209. {
  210. sucPress=1; //有按下
  211. printf("KEY=%x\r\n",Key);
  212. }
  213. else
  214. {
  215. sucPress=0; //无按下
  216. }
  217. }
  218. if(sucPress){//有按键按下
  219. if(g_ucKeyMode){ //单次模式
  220. if(sucLastPress!=sucPress){
  221. sucLastPress=sucPress;
  222. g_ulKeyValue=Key;
  223. return 1;
  224. }else{
  225. return 0;
  226. }
  227. }else{ //持续模式
  228. sucLastPress=sucPress;
  229. if(0==sucCt){
  230. g_ulKeyValue=Key1;
  231. sucCt=25;//10ms*100=1000ms 这个时间不准确,是个粗略时间
  232. return 1;
  233. }else{
  234. sucCt--;
  235. return 0;
  236. }
  237. }
  238. }else{
  239. sucCt=0;
  240. if(sucLastPress){
  241. sucLastPress=0;
  242. return -1; //释放
  243. }
  244. }
  245. return 0;
  246. }
  247. void KeyCount(void)
  248. {
  249. //if(g_ucDKC<255)g_ucDKC++;
  250. if(g_ucUKC<255)g_ucUKC++;
  251. }