Key.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. g_ucKeyMode=0;//连续触发模式
  69. RunMake(THIS_FILE_ID);
  70. }
  71. unsigned long g_ulEncode=0;
  72. /********************************************************************************
  73. * Function:KeyScanPort
  74. *******************************************************************************/
  75. unsigned long KeyScanPort(void)
  76. {
  77. unsigned long k1,k2;
  78. k1=GPIOB->IDR;
  79. k1&=KEY_ALL_PORTB;
  80. k1<<=16;
  81. k2=GPIOC->IDR;
  82. k2&=KEY_ALL_PORTC;
  83. return (k1|k2);
  84. }
  85. /***************************************
  86. *flag=1 按键按下 flag=-1按键释放
  87. ****************************************/
  88. void KeyToModem(int flag)
  89. {
  90. //char buf[400];
  91. // RunMake(THIS_FILE_ID);
  92. if(g_ulKeyValue==KEY_PPT){
  93. if(flag==1){
  94. MODEM_DTR_LOW;
  95. }else{
  96. MODEM_DTR_HIGH;
  97. }
  98. }
  99. //buf[0]=1;
  100. //if(buf[0])return;
  101. // if(g_ulKeyValue==KEY_UP){
  102. // if(flag==1){
  103. // MODEM_DSR_LOW;
  104. // SlwTrace(INF,"[UP+]\r\n");
  105. // }else{
  106. // MODEM_DSR_HIGH;
  107. // SlwTrace(INF,"[UP-]\r\n");
  108. // }
  109. // }
  110. //
  111. // if(g_ulKeyValue==KEY_DOWN){
  112. // if(flag==1){
  113. // MODEM_RING_LOW;
  114. // SlwTrace(DEBUG,"[DOWN+]\r\n");
  115. // }else{
  116. // MODEM_RING_HIGH;
  117. // SlwTrace(DEBUG,"[DOWN-]\r\n");
  118. // }
  119. // }
  120. }
  121. /************************************************************
  122. *GetKey
  123. 读键盘数据
  124. 建议10ms调用一次此函数,不同调用时间需要修改函数内的相关参数以达到比较好的效果
  125. 参数:
  126. g_ucKeyMode=1时,单次模式
  127. g_ucKeyMode=0时,持续模式
  128. 返回值:
  129. 0--按键无变化
  130. 1 --按键有按下:
  131. 单次模式下,当按键第一次按下才返回一次该值;
  132. 持续模式下,按着按键不放将间隔返回该值
  133. g_ulKeyValue保存当前按键值
  134. -1 --按键有释放.不管单次模式还是持续模式,当按键释放时只返回一次
  135. 释放后,g_ulKeyValue保存上次按下的按键值
  136. *************************************************************/
  137. int GetKey(void)
  138. {
  139. static unsigned char sucCt=0;//连续模式下响应频率控制;
  140. static unsigned char sucFlag=0;
  141. static unsigned char sucPress=0;
  142. static unsigned char sucLastPress=0;
  143. static unsigned long Key=0;
  144. static unsigned long Key1=0;
  145. static unsigned long Key2=0;
  146. //GetEncodeKey();
  147. //去抖动 0 1
  148. if(++sucFlag>2){ //1
  149. sucFlag=0;
  150. }
  151. if(sucFlag==0){
  152. Key1=KeyScanPort();
  153. }else{
  154. Key2=KeyScanPort();
  155. }
  156. if(Key1!=Key2)return 0;
  157. //按键状态发生变更
  158. if(Key!=Key1){
  159. Key=Key1;
  160. if(Key!=((KEY_ALL_PORTB<<16)| KEY_ALL_PORTC))
  161. {
  162. sucPress=1; //有按下
  163. g_ucUKC=0;
  164. printf("KEY=%lx\r\n",Key);
  165. }
  166. else
  167. {
  168. sucPress=0; //无按下
  169. g_ucDKC=0;
  170. }
  171. }
  172. if(sucPress){//有按键按下
  173. if(g_ucKeyMode){ //单次模式
  174. if(sucLastPress!=sucPress){
  175. sucLastPress=sucPress;
  176. g_ulKeyValue=Key;
  177. return 1;
  178. }else{
  179. return 0;
  180. }
  181. }else{ //持续模式
  182. sucLastPress=sucPress;
  183. if(0==sucCt){
  184. g_ulKeyValue=Key1;
  185. sucCt=100;//10ms*100=1000ms
  186. return 1;
  187. }else{
  188. sucCt--;
  189. return 0;
  190. }
  191. }
  192. }else{ //无按键按下
  193. sucCt=0;
  194. if(sucLastPress){
  195. sucLastPress=0;
  196. return -1;
  197. }
  198. }
  199. return 0;
  200. }
  201. void KeyCount(void)
  202. {
  203. if(g_ucDKC<255)g_ucDKC++;
  204. if(g_ucUKC<255)g_ucUKC++;
  205. }
  206. void EXTIX_Init(void)
  207. {
  208. GPIO_InitTypeDef GPIO_InitStructure;
  209. EXTI_InitTypeDef EXTI_InitStructure;
  210. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE);
  211. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_8;
  212. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  213. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  214. GPIO_Init(GPIOA, &GPIO_InitStructure);
  215. //编码旋钮开关
  216. GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource6);
  217. EXTI_InitStructure.EXTI_Line = EXTI_Line6;
  218. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  219. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
  220. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  221. EXTI_Init(&EXTI_InitStructure);
  222. //Modem DCD (PA使能控制输出)
  223. // GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource8);
  224. // EXTI_InitStructure.EXTI_Line = EXTI_Line8;
  225. // EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  226. // EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
  227. // EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  228. // EXTI_Init(&EXTI_InitStructure);
  229. // RunMake(THIS_FILE_ID);
  230. }
  231. void EXTI9_5_IRQHandler(void)
  232. {
  233. __IO unsigned long k1;
  234. __IO unsigned long k2;
  235. //Modem DCD (PA使能控制输出)
  236. // if(SET==EXTI_GetFlagStatus(EXTI_Line8)){
  237. // //PA8
  238. // k1=GPIOA->IDR;
  239. // if(k1 & MODEM_DCD_PIN){
  240. // if(sutPocStatus.FirstLogined){
  241. // sutPocStatus.Speaker=1;
  242. // SpeakerEnable();//只有登陆后才受DCD控制PA,规避模块开机播报"欢迎使用..."
  243. // }
  244. // SlwTrace(INF,"DCD Up");
  245. // }else{
  246. // SpeakerDisable(); //PA的关闭则完全受DCD控制,因为TTS播报完后DCD也会关闭
  247. // sutPocStatus.Speaker=0;
  248. // SlwTrace(INF,"DCD Down");
  249. // }
  250. // EXTI_ClearFlag(EXTI_Line8);
  251. // }
  252. //编码旋钮开关
  253. if(SET==EXTI_GetFlagStatus(EXTI_Line6)){
  254. //PA6
  255. k1=GPIOB->IDR;
  256. k2=GPIOA->IDR;
  257. if(k2&KEY_PANEL_DOWN_PIN){//上升沿
  258. if(k1&KEY_PANEL_UP_PIN){
  259. g_iEncode=-1;
  260. }else{
  261. g_iEncode=1;
  262. }
  263. }else{ //下降沿
  264. if(k1&KEY_PANEL_UP_PIN){
  265. g_iEncode=1;
  266. }else{
  267. g_iEncode=-1;
  268. }
  269. }
  270. EXTI_ClearFlag(EXTI_Line6);
  271. }
  272. //---------
  273. }
  274. /********************************************************************************
  275. * Function:EncodeKeyScanPort
  276. *******************************************************************************/
  277. unsigned char EncodeKeyScanPort(void)
  278. {
  279. unsigned char v;
  280. unsigned long k1,k2;
  281. k2=GPIOB->IDR;
  282. k2&=KEY_PANEL_UP_PIN;
  283. k1=GPIOA->IDR;
  284. k1&=KEY_PANEL_DOWN_PIN;
  285. k1=k1|k2;
  286. v=(unsigned char)k1&0xff;
  287. return v;
  288. }
  289. /********************************************************************************
  290. GetEncodeKey
  291. 返回0 --不变
  292. 1 --加1
  293. -1 --减1
  294. *******************************************************************************/
  295. int GetEncodeKey(void)
  296. {
  297. static unsigned char sucV[3]={0,0,0};
  298. static unsigned char si=0;
  299. static unsigned char lastEncode;
  300. unsigned char Encode;
  301. unsigned char flag;
  302. sucV[si]=EncodeKeyScanPort();
  303. Encode=sucV[0];
  304. if(0!=(Encode&KEY_PANEL_DOWN_PIN) && 0==(lastEncode&KEY_PANEL_DOWN_PIN)){ //上升沿
  305. if(Encode & KEY_PANEL_UP_PIN){
  306. g_iEncode=1;
  307. }else{
  308. g_iEncode=-1;
  309. }
  310. }else if(0==(Encode&KEY_PANEL_DOWN_PIN) && 0!=(lastEncode&KEY_PANEL_DOWN_PIN)){//下降沿
  311. if(Encode & KEY_PANEL_UP_PIN){
  312. g_iEncode=-1;
  313. }else{
  314. g_iEncode=1;
  315. }
  316. }
  317. lastEncode= Encode;
  318. return flag;
  319. }
  320. /********************************************************************************
  321. * End of Module
  322. *******************************************************************************/