Key.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. //void CheckPwrKeyOn(void)
  8. //{//长按电源键直到电筒灯亮后释放
  9. // unsigned int tick=0;
  10. // if(KeyPwrStatus !=0)
  11. // {
  12. // POWER_ON_FAILED:
  13. // SlwTrace(INF, "\r\nPwrOnFailed",1);
  14. // DelayMs(10);
  15. // PWR_EN_LOW;
  16. // while(1);
  17. // }
  18. //
  19. // while(KeyPwrStatus==0)
  20. // {
  21. // if(++tick >= 200000)
  22. // {
  23. // MODEM_LED2_HIGH;
  24. // SlwTrace(INF, "\r\nPwrOnOk",1);
  25. //// while(KeyPwrStatus==0){
  26. ////// DelayMs(30);
  27. ////// MODEM_LED2_LOW;
  28. ////// DelayMs(30);
  29. ////// MODEM_LED2_HIGH;
  30. //// IWDG_ReloadCounter();
  31. //// }
  32. // //MODEM_LED2_LOW;
  33. //
  34. // PWR_EN_HIGH;//
  35. // return;
  36. // }
  37. // }
  38. // SlwTrace(INF,"\r\nPwr erro",1);
  39. // goto POWER_ON_FAILED;
  40. //}
  41. //检测关机
  42. void SoftPwrOffCheck(void)
  43. {
  44. // static unsigned int seconds;
  45. //
  46. // if(!seconds) seconds=uSysTickGet()+500;//首次进入
  47. //
  48. // if(KeyPwrStatus==0)
  49. // {
  50. //
  51. // if(uSysTickGet() > seconds)
  52. // {
  53. // SlwTrace(INF, "PwrOffOk",1);
  54. //// MODEM_LED1_HIGH;
  55. // if(g_ucTMMode==1)while(1);
  56. //
  57. // UISetNextStatus(UIS_POWEROFF_WAIT);
  58. //// GuiClearAll();
  59. //// GuiShowBmp(0,0,"BYEBYE.bmp");
  60. //// MODEM_PWREN_LOW;
  61. //// PWR_EN_LOW;
  62. //// MODEM_LED1_LOW;
  63. //// while(1);
  64. // }
  65. // }else seconds=uSysTickGet()+100;//200
  66. //
  67. }
  68. void KeyInit(void)
  69. {
  70. GPIO_InitTypeDef GPIO_InitStructure;
  71. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |RCC_APB2Periph_GPIOB,ENABLE);//复用功能IO 时钟使能 这里必须先使能复用IO的时钟,再关闭JTAG功能
  72. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // JTAG-DP Disabled and SW-DP Enabled
  73. /*GPIO先全部设置为上拉输入*/
  74. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  75. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  76. //PPT
  77. GPIO_InitStructure.GPIO_Pin=KEY_PPT_PIN;
  78. GPIO_Init(KEY_PPT_PORT, &GPIO_InitStructure);
  79. //MIC PTT
  80. GPIO_InitStructure.GPIO_Pin=KEY_MICPTT_PIN;
  81. GPIO_Init(KEY_MICPTT_PORT, &GPIO_InitStructure);
  82. // INFO VOL+
  83. GPIO_InitStructure.GPIO_Pin=KEY_INFO_PIN;
  84. GPIO_Init(KEY_INFO_PORT, &GPIO_InitStructure);
  85. // FUNC VOL-
  86. GPIO_InitStructure.GPIO_Pin=KEY_FUNC_PIN;
  87. GPIO_Init(KEY_FUNC_PORT, &GPIO_InitStructure);
  88. //HEADSET_PIN 耳机检测
  89. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  90. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  91. GPIO_InitStructure.GPIO_Pin=HEADSET_PIN;
  92. GPIO_Init(HEADSET_PORT, &GPIO_InitStructure);
  93. //CheckPwrKeyOn();
  94. g_ucKeyMode=0;//连续触发模式
  95. }
  96. static unsigned long KeyScanPort(void)
  97. {
  98. unsigned long k1,k2;
  99. k1=GPIOB->IDR;
  100. k1&=KEY_ALL_PORTB;
  101. k1<<=16;
  102. return (k1);
  103. }
  104. /************************************************************
  105. *GetKey
  106. 读键盘数据
  107. 建议10ms调用一次此函数,不同调用时间需要修改函数内的相关参数以达到比较好的效果
  108. 参数:
  109. g_ucKeyMode=1时,单次模式
  110. g_ucKeyMode=0时,持续模式
  111. 返回值:
  112. 0--按键无变化
  113. 1 --按键有按下:
  114. 单次模式下,当按键第一次按下才返回一次该值;
  115. 持续模式下,按着按键不放将间隔返回该值
  116. g_ulKeyValue保存当前按键值
  117. -1 --按键有释放.不管单次模式还是持续模式,当按键释放时只返回一次
  118. 释放后,g_ulKeyValue保存上次按下的按键值
  119. *************************************************************/
  120. unsigned char PttKeyFlag;
  121. int GetKey(void)
  122. {
  123. static unsigned char sucCt=0;//连续模式下响应频率控制;
  124. static unsigned char sucFlag=0;
  125. static unsigned char sucPress=0;
  126. static unsigned char sucLastPress=0;
  127. static unsigned long Key=0;
  128. static unsigned long Key1=0;
  129. static unsigned long Key2=0;
  130. //去抖动 0 1
  131. if(++sucFlag>2){ //1
  132. sucFlag=0;
  133. }
  134. if(sucFlag==0){
  135. Key1=KeyScanPort();
  136. }else{
  137. Key2=KeyScanPort();
  138. }
  139. if(Key1!=Key2)return 0;
  140. //按键状态发生变更
  141. if(Key!=Key1){
  142. Key=Key1;
  143. if(Key!=(KEY_ALL_PORTB<<16))
  144. {
  145. sucPress=1; //有按下
  146. printf("KEY=%lx\r\n",Key);
  147. }
  148. else
  149. {
  150. sucPress=0; //无按下
  151. }
  152. }
  153. if(sucPress){//有按键按下
  154. if(g_ucKeyMode){ //单次模式
  155. if(sucLastPress!=sucPress){
  156. sucLastPress=sucPress;
  157. g_ulKeyValue=Key;
  158. if(g_ulKeyValue==KEY_PPT)PttKeyFlag=1;
  159. // printf("##############\r\n");
  160. return 1;
  161. }else{
  162. return 0;
  163. }
  164. }else{ //持续模式
  165. sucLastPress=sucPress;
  166. if(0==sucCt){
  167. g_ulKeyValue=Key1;
  168. if(g_ulKeyValue==KEY_PPT)PttKeyFlag=1;
  169. sucCt=100;//10ms*100=1000ms
  170. // printf("************\r\n");
  171. return 1;
  172. }else{
  173. sucCt--;
  174. return 0;
  175. }
  176. }
  177. }else{ //无按键按下
  178. sucCt=0;
  179. if(sucLastPress){
  180. sucLastPress=0;
  181. return -1;
  182. }
  183. }
  184. return 0;
  185. }
  186. void KeyCount(void)
  187. {
  188. //if(g_ucDKC<255)g_ucDKC++;
  189. if(g_ucUKC<255)g_ucUKC++;
  190. }
  191. int GetKeyVBAT(void)
  192. {
  193. static unsigned char sucCt=0;//连续模式下响应频率控制;
  194. static unsigned char sucFlag=0;
  195. static unsigned char sucPress=0;
  196. static unsigned char sucLastPress=0;
  197. static unsigned long Key=0;
  198. static unsigned long Key1=0;
  199. static unsigned long Key2=0;
  200. //去抖动 0 1
  201. if(++sucFlag>2){ //1
  202. sucFlag=0;
  203. }
  204. if(sucFlag==0){
  205. Key1=GetKeyVbat();
  206. }else{
  207. Key2=GetKeyVbat();
  208. }
  209. if(Key1!=Key2)return 0;
  210. //按键状态发生变更
  211. if(Key!=Key1){
  212. Key=Key1;
  213. if(Key<290)
  214. {
  215. sucPress=1; //有按下
  216. printf("KEY=%d.+++++++++\r\n",Key);
  217. }
  218. else
  219. {
  220. sucPress=0; //无按下
  221. }
  222. }
  223. if(sucPress){//有按键按下
  224. if(g_ucKeyMode){ //单次模式
  225. if(sucLastPress!=sucPress){
  226. sucLastPress=sucPress;
  227. if(Key==0){
  228. g_ulKeyValue=KEY_PANEL_MENU;
  229. }
  230. else if(Key<40){
  231. g_ulKeyValue=KEY_UP_DOWN_IP;
  232. }
  233. else if(Key<60){
  234. g_ulKeyValue=KEY_PANEL_UP;
  235. }else if(Key<120){
  236. g_ulKeyValue=KEY_PANEL_DOWN;
  237. }
  238. else if(Key<160){
  239. g_ulKeyValue=KEY_PANEL_EXIT;
  240. }else if(Key<210){
  241. g_ulKeyValue=KEY_PANEL_P1;
  242. }else
  243. g_ulKeyValue=KEY_PANEL_P2;
  244. return 1;
  245. }else{
  246. return 0;
  247. }
  248. }else{ //持续模式
  249. sucLastPress=sucPress;
  250. // printf("KEYVbat=%d............\r\n",Key);
  251. if(0==sucCt){
  252. if(Key==0){
  253. g_ulKeyValue=KEY_PANEL_MENU;
  254. }
  255. else if(Key<40){
  256. g_ulKeyValue=KEY_UP_DOWN_IP;
  257. }
  258. else if(Key<60){
  259. g_ulKeyValue=KEY_PANEL_UP;
  260. }else if(Key<120){
  261. g_ulKeyValue=KEY_PANEL_DOWN;
  262. }
  263. else if(Key<160){
  264. g_ulKeyValue=KEY_PANEL_EXIT;
  265. }else if(Key<210){
  266. g_ulKeyValue=KEY_PANEL_P1;
  267. }else
  268. g_ulKeyValue=KEY_PANEL_P2;
  269. sucCt=100;//10ms*100=1000ms
  270. return 1;
  271. }else{
  272. sucCt--;
  273. return 0;
  274. }
  275. }
  276. }else{ //无按键按下
  277. sucCt=0;
  278. if(sucLastPress){
  279. sucLastPress=0;
  280. return -1;
  281. }
  282. }
  283. return 0;
  284. }
  285. void OnOff_Init(void)
  286. {
  287. EXTI_InitTypeDef EXTI_InitStructure;
  288. GPIO_InitTypeDef GPIO_InitStructure;
  289. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO , ENABLE);
  290. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  291. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  292. GPIO_InitStructure.GPIO_Pin = ONOFF_CK_PIN;
  293. GPIO_Init(ONOFF_CK_PORT, &GPIO_InitStructure);
  294. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource11);
  295. EXTI_InitStructure.EXTI_Line = EXTI_Line11;
  296. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  297. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  298. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  299. EXTI_Init(&EXTI_InitStructure);
  300. }
  301. void EXTI15_10_IRQHandler(void)
  302. {
  303. if(SET==EXTI_GetFlagStatus(EXTI_Line11))
  304. {//外部电源关了
  305. printf("Power down----\r\n");
  306. if(modemInitReady==1)
  307. {
  308. PWR_EN_HIGH;//模块已初始化成功的,需要软件关机,先锁好电源
  309. modemPwrOffNow=1;
  310. }
  311. EXTI_ClearFlag(EXTI_Line11);
  312. }
  313. }