Key.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. printf("KEY=%lx\r\n",Key);
  173. sucPress=0; //无按下
  174. g_ucDKC=0;
  175. }
  176. }
  177. if(sucPress){//有按键按下
  178. if(g_ucKeyMode){ //单次模式
  179. if(sucLastPress!=sucPress){
  180. sucLastPress=sucPress;
  181. g_ulKeyValue=Key;
  182. return 1;
  183. }else{
  184. return 0;
  185. }
  186. }else{ //持续模式
  187. sucLastPress=sucPress;
  188. if(0==sucCt){
  189. g_ulKeyValue=Key1;
  190. sucCt=100;//10ms*100=1000ms
  191. return 1;
  192. }else{
  193. sucCt--;
  194. return 0;
  195. }
  196. }
  197. }else{ //无按键按下
  198. sucCt=0;
  199. if(sucLastPress){
  200. sucLastPress=0;
  201. return -1;
  202. }
  203. }
  204. return 0;
  205. }
  206. void KeyCount(void)
  207. {
  208. if(g_ucDKC<255)g_ucDKC++;
  209. if(g_ucUKC<255)g_ucUKC++;
  210. }
  211. void OnOff_Init(void)
  212. {
  213. EXTI_InitTypeDef EXTI_InitStructure;
  214. GPIO_InitTypeDef GPIO_InitStructure;
  215. RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO , ENABLE);
  216. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  217. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  218. GPIO_InitStructure.GPIO_Pin = ONOFF_CK_PIN;
  219. GPIO_Init(ONOFF_CK_PORT, &GPIO_InitStructure);
  220. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
  221. EXTI_InitStructure.EXTI_Line = EXTI_Line9;
  222. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  223. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  224. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  225. EXTI_Init(&EXTI_InitStructure);
  226. }
  227. void EXTI9_5_IRQHandler(void)
  228. {
  229. if(SET==EXTI_GetFlagStatus(EXTI_Line9))
  230. {//外部电源关了
  231. if(modemInitReady==1)
  232. {
  233. PWR_EN_HIGH;//模块已初始化成功的,需要软件关机,先锁好电源
  234. modemPwrOffNow=1;
  235. }
  236. EXTI_ClearFlag(EXTI_Line9);
  237. }
  238. }
  239. /********************************************************************************
  240. * Function:EncodeKeyScanPort
  241. *******************************************************************************/
  242. unsigned char EncodeKeyScanPort(void)
  243. {
  244. unsigned char v;
  245. unsigned long k1,k2;
  246. k2=GPIOB->IDR;
  247. k2&=KEY_PANEL_UP_PIN;
  248. k1=GPIOA->IDR;
  249. k1&=KEY_PANEL_DOWN_PIN;
  250. k1=k1|k2;
  251. v=(unsigned char)k1&0xff;
  252. return v;
  253. }
  254. /********************************************************************************
  255. GetEncodeKey
  256. 返回0 --不变
  257. 1 --加1
  258. -1 --减1
  259. *******************************************************************************/
  260. int GetEncodeKey(void)
  261. {
  262. static unsigned char sucV[3]={0,0,0};
  263. static unsigned char si=0;
  264. static unsigned char lastEncode;
  265. unsigned char Encode;
  266. unsigned char flag;
  267. sucV[si]=EncodeKeyScanPort();
  268. Encode=sucV[0];
  269. if(0!=(Encode&KEY_PANEL_DOWN_PIN) && 0==(lastEncode&KEY_PANEL_DOWN_PIN)){ //上升沿
  270. if(Encode & KEY_PANEL_UP_PIN){
  271. g_iEncode=1;
  272. }else{
  273. g_iEncode=-1;
  274. }
  275. }else if(0==(Encode&KEY_PANEL_DOWN_PIN) && 0!=(lastEncode&KEY_PANEL_DOWN_PIN)){//下降沿
  276. if(Encode & KEY_PANEL_UP_PIN){
  277. g_iEncode=-1;
  278. }else{
  279. g_iEncode=1;
  280. }
  281. }
  282. lastEncode= Encode;
  283. return flag;
  284. }
  285. /********************************************************************************
  286. * End of Module
  287. *******************************************************************************/