Key.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. int g_iEncode=0;
  22. unsigned char g_ucEncode[20];
  23. ////////////////////////// function declare //////////////////////////////
  24. unsigned long KeyScanPort(void);
  25. ////////////////////////// end declare //////////////////////////////
  26. /********************************************************************************
  27. KeyInit
  28. *******************************************************************************/
  29. void KeyInit(void)
  30. {
  31. GPIO_InitTypeDef GPIO_InitStructure;
  32. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//复用功能IO 时钟使能 这里必须先使能复用IO的时钟,再关闭JTAG功能
  33. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // JTAG-DP Disabled and SW-DP Enabled
  34. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);
  35. /*GPIO先全部设置为上拉输入*/
  36. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  37. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  38. //PPT
  39. GPIO_InitStructure.GPIO_Pin=KEY_PPT_PIN;
  40. GPIO_Init(KEY_PPT_PORT, &GPIO_InitStructure);
  41. //PANEL_UP
  42. GPIO_InitStructure.GPIO_Pin=KEY_PANEL_UP_PIN;
  43. GPIO_Init(KEY_PANEL_UP_PORT, &GPIO_InitStructure);
  44. //PANEL_DOWN
  45. GPIO_InitStructure.GPIO_Pin=KEY_PANEL_DOWN_PIN;
  46. GPIO_Init(KEY_PANEL_DOWN_PORT, &GPIO_InitStructure);
  47. //PANEL_OK
  48. GPIO_InitStructure.GPIO_Pin=KEY_PANEL_OK_PIN;
  49. GPIO_Init(KEY_PANEL_OK_PORT, &GPIO_InitStructure);
  50. //PANEL_EXIT
  51. GPIO_InitStructure.GPIO_Pin=KEY_PANEL_EXIT_PIN;
  52. GPIO_Init(KEY_PANEL_EXIT_PORT, &GPIO_InitStructure);
  53. //PANEL_MENU
  54. GPIO_InitStructure.GPIO_Pin=KEY_PANEL_MENU_PIN;
  55. GPIO_Init(KEY_PANEL_MENU_PORT, &GPIO_InitStructure);
  56. //KEY_POWER
  57. GPIO_InitStructure.GPIO_Pin=KEY_POWER_PIN;
  58. GPIO_Init(KEY_POWER_PORT, &GPIO_InitStructure);
  59. //INFO
  60. GPIO_InitStructure.GPIO_Pin=KEY_INFO_PIN;
  61. GPIO_Init(KEY_INFO_PORT, &GPIO_InitStructure);
  62. //FUNC
  63. GPIO_InitStructure.GPIO_Pin=KEY_FUNC_PIN;
  64. GPIO_Init(KEY_FUNC_PORT, &GPIO_InitStructure);
  65. //PPT_MIC
  66. GPIO_InitStructure.GPIO_Pin=KEY_PPT_MIC_PIN;
  67. GPIO_Init(KEY_PPT_MIC_PORT, &GPIO_InitStructure);
  68. //LED_BACK_LIGHT
  69. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  70. GPIO_InitStructure.GPIO_Pin=KEY_LED_PIN;
  71. GPIO_Init(KEY_LED_PORT, &GPIO_InitStructure);
  72. g_ucKeyMode=0;//连续触发模式
  73. RunMake(THIS_FILE_ID);
  74. }
  75. unsigned long g_ulEncode=0;
  76. /********************************************************************************
  77. * Function:KeyScanPort
  78. *******************************************************************************/
  79. unsigned long KeyScanPort(void)
  80. {
  81. unsigned long k1,k2;
  82. k1=GPIOB->IDR;
  83. k1&=KEY_ALL_PORTB;
  84. k1<<=16;
  85. k2=GPIOC->IDR;
  86. k2&=KEY_ALL_PORTC;
  87. return (k1|k2);
  88. }
  89. /***************************************
  90. *flag=1 按键按下 flag=-1按键释放
  91. ****************************************/
  92. void KeyToModem(int flag)
  93. {
  94. //char buf[400];
  95. // RunMake(THIS_FILE_ID);
  96. if(g_ulKeyValue==KEY_PPT){
  97. if(flag==1){
  98. MODEM_DTR_LOW;
  99. }else{
  100. MODEM_DTR_HIGH;
  101. }
  102. }
  103. //buf[0]=1;
  104. //if(buf[0])return;
  105. // if(g_ulKeyValue==KEY_UP){
  106. // if(flag==1){
  107. // MODEM_DSR_LOW;
  108. // SlwTrace(INF,"[UP+]\r\n");
  109. // }else{
  110. // MODEM_DSR_HIGH;
  111. // SlwTrace(INF,"[UP-]\r\n");
  112. // }
  113. // }
  114. //
  115. // if(g_ulKeyValue==KEY_DOWN){
  116. // if(flag==1){
  117. // MODEM_RING_LOW;
  118. // SlwTrace(DEBUG,"[DOWN+]\r\n");
  119. // }else{
  120. // MODEM_RING_HIGH;
  121. // SlwTrace(DEBUG,"[DOWN-]\r\n");
  122. // }
  123. // }
  124. }
  125. /************************************************************
  126. *GetKey
  127. 读键盘数据
  128. 建议10ms调用一次此函数,不同调用时间需要修改函数内的相关参数以达到比较好的效果
  129. 参数:
  130. g_ucKeyMode=1时,单次模式
  131. g_ucKeyMode=0时,持续模式
  132. 返回值:
  133. 0--按键无变化
  134. 1 --按键有按下:
  135. 单次模式下,当按键第一次按下才返回一次该值;
  136. 持续模式下,按着按键不放将间隔返回该值
  137. g_ulKeyValue保存当前按键值
  138. -1 --按键有释放.不管单次模式还是持续模式,当按键释放时只返回一次
  139. 释放后,g_ulKeyValue保存上次按下的按键值
  140. *************************************************************/
  141. int GetKey(void)
  142. {
  143. static unsigned char sucCt=0;//连续模式下响应频率控制;
  144. static unsigned char sucFlag=0;
  145. static unsigned char sucPress=0;
  146. static unsigned char sucLastPress=0;
  147. static unsigned long Key=0;
  148. static unsigned long Key1=0;
  149. static unsigned long Key2=0;
  150. //GetEncodeKey();
  151. //去抖动 0 1
  152. if(++sucFlag>2){ //1
  153. sucFlag=0;
  154. }
  155. if(sucFlag==0){
  156. Key1=KeyScanPort();
  157. }else{
  158. Key2=KeyScanPort();
  159. }
  160. if(Key1!=Key2)return 0;
  161. //按键状态发生变更
  162. if(Key!=Key1){
  163. Key=Key1;
  164. if(Key!=((KEY_ALL_PORTB<<16)| KEY_ALL_PORTC))
  165. {
  166. sucPress=1; //有按下
  167. g_ucUKC=0;
  168. //printf("KEY=%lx\r\n",Key);
  169. }
  170. else
  171. {
  172. sucPress=0; //无按下
  173. g_ucDKC=0;
  174. }
  175. }
  176. if(sucPress){//有按键按下
  177. if(g_ucKeyMode){ //单次模式
  178. if(sucLastPress!=sucPress){
  179. sucLastPress=sucPress;
  180. g_ulKeyValue=Key;
  181. return 1;
  182. }else{
  183. return 0;
  184. }
  185. }else{ //持续模式
  186. sucLastPress=sucPress;
  187. if(0==sucCt){
  188. g_ulKeyValue=Key1;
  189. sucCt=100;//10ms*100=1000ms
  190. return 1;
  191. }else{
  192. sucCt--;
  193. return 0;
  194. }
  195. }
  196. }else{ //无按键按下
  197. sucCt=0;
  198. if(sucLastPress){
  199. sucLastPress=0;
  200. return -1;
  201. }
  202. }
  203. return 0;
  204. }
  205. void KeyCount(void)
  206. {
  207. if(g_ucDKC<255)g_ucDKC++;
  208. if(g_ucUKC<255)g_ucUKC++;
  209. }
  210. void OnOff_Init(void)
  211. {
  212. EXTI_InitTypeDef EXTI_InitStructure;
  213. GPIO_InitTypeDef GPIO_InitStructure;
  214. RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO , ENABLE);
  215. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  216. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  217. GPIO_InitStructure.GPIO_Pin = ONOFF_CK_PIN;
  218. GPIO_Init(ONOFF_CK_PORT, &GPIO_InitStructure);
  219. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
  220. EXTI_InitStructure.EXTI_Line = EXTI_Line9;
  221. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  222. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  223. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  224. EXTI_Init(&EXTI_InitStructure);
  225. }
  226. void EXTI9_5_IRQHandler(void)
  227. {
  228. if(SET==EXTI_GetFlagStatus(EXTI_Line9))
  229. {//外部电源关了
  230. if(modemInitReady==1)
  231. {
  232. PWR_EN_HIGH;//模块已初始化成功的,需要软件关机,先锁好电源
  233. modemPwrOffNow=1;
  234. }
  235. EXTI_ClearFlag(EXTI_Line9);
  236. }
  237. }
  238. /********************************************************************************
  239. * Function:EncodeKeyScanPort
  240. *******************************************************************************/
  241. unsigned char EncodeKeyScanPort(void)
  242. {
  243. unsigned char v;
  244. unsigned long k1,k2;
  245. k2=GPIOB->IDR;
  246. k2&=KEY_PANEL_UP_PIN;
  247. k1=GPIOA->IDR;
  248. k1&=KEY_PANEL_DOWN_PIN;
  249. k1=k1|k2;
  250. v=(unsigned char)k1&0xff;
  251. return v;
  252. }
  253. /********************************************************************************
  254. GetEncodeKey
  255. 返回0 --不变
  256. 1 --加1
  257. -1 --减1
  258. *******************************************************************************/
  259. int GetEncodeKey(void)
  260. {
  261. static unsigned char sucV[3]={0,0,0};
  262. static unsigned char si=0;
  263. static unsigned char lastEncode;
  264. unsigned char Encode;
  265. unsigned char flag;
  266. sucV[si]=EncodeKeyScanPort();
  267. Encode=sucV[0];
  268. if(0!=(Encode&KEY_PANEL_DOWN_PIN) && 0==(lastEncode&KEY_PANEL_DOWN_PIN)){ //上升沿
  269. if(Encode & KEY_PANEL_UP_PIN){
  270. g_iEncode=1;
  271. }else{
  272. g_iEncode=-1;
  273. }
  274. }else if(0==(Encode&KEY_PANEL_DOWN_PIN) && 0!=(lastEncode&KEY_PANEL_DOWN_PIN)){//下降沿
  275. if(Encode & KEY_PANEL_UP_PIN){
  276. g_iEncode=-1;
  277. }else{
  278. g_iEncode=1;
  279. }
  280. }
  281. lastEncode= Encode;
  282. return flag;
  283. }
  284. /********************************************************************************
  285. * End of Module
  286. *******************************************************************************/