野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8125|回复: 3

关于4X4矩阵键盘的问题,main函数实在不会写,基本都是只给键盘的算法。

[复制链接]
发表于 2018-4-14 05:57:12 | 显示全部楼层 |阅读模式
感觉找遍了全网,也没找到main函数控制led或者是relay继电器的语句,c语言实在是没有精通,各位大大可以帮忙看下这个是什么情况么,就是没法点亮我的板载led,我用的是f429.下面是我的程序。第一段main.c

  1. #include "stm32f4xx.h" //寄存器外设地址映射
  2. #include "bsp_keyboard.h"
  3. #include "bsp_led.h"

  4. int main(void)
  5. {
  6.   int KeyNumber;
  7.         LED_GPIO_Config();
  8.   KeyBoard_GPIO_Config();
  9.         Key_Scan();
  10.         while(1)
  11.         {       
  12.                 KeyNumber = Key_Scan();
  13.           switch(KeyNumber)
  14.                 {
  15.                         case 1 :
  16.                                 LED_G_ON;                  
  17.                 }

  18.         }
  19.        
  20. }
复制代码
第二段bsp_keyboard.c
  1. #include "bsp_keyboard.h"
  2. #include "stm32f4xx_gpio.h"
  3. #include "stm32f4xx_rcc.h"

  4. void Delay_ms(int xms)        
  5. {
  6.   uint32_t i,j;
  7.         for(i=xms;i>0;i--)
  8.           for(j=72000;j>0;j--);
  9. }
  10. void KeyBoard_GPIO_Config(void)
  11. {
  12.         GPIO_InitTypeDef  GPIO_InitStructure;
  13.         RCC_AHB1PeriphClockCmd(C1_GPIO_CLK|
  14.                                      R1_GPIO_CLK,ENABLE);
  15.              
  16.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  17.         GPIO_InitStructure.GPIO_OType =GPIO_OType_PP;       
  18.         GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
  19.               GPIO_InitStructure.GPIO_Pin  = C1_PIN |C2_PIN |C3_PIN |C4_PIN ;
  20.         GPIO_Init(GPIOC,&GPIO_InitStructure);
  21.         
  22.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  23.               GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  24.         GPIO_InitStructure.GPIO_Pin  = R1_PIN|R2_PIN|R3_PIN|R4_PIN;//DD
  25.         GPIO_Init(GPIOA,&GPIO_InitStructure);               
  26. }

  27. int Key_Scan(void)
  28. {
  29.         uint8_t KeyVal;         
  30.         GPIO_Write(GPIOC,((GPIOC->ODR & 0xf0ff) | 0x0f00));
  31.         if((GPIOA->IDR & 0xf000)==0x0000)  
  32.                 return -1;
  33.         else
  34.         {        
  35.             Delay_ms(5);   
  36.             if((GPIOA->IDR & 0xf000)==0x0000)
  37.             return -1;
  38.         }

  39.         GPIO_Write(GPIOC,((GPIOC->ODR & 0xf0ff) | 0x0100));        
  40.                 switch(GPIOA->IDR & 0xf000)
  41.                         {
  42.                                 case 0x1000: KeyVal=15; break;
  43.                                 case 0x2000: KeyVal=11;        break;
  44.                                 case 0x4000: KeyVal=7;        break;
  45.                                 case 0x8000: KeyVal=3;        break;
  46.                         }
  47.         GPIO_Write(GPIOC,((GPIOC->ODR & 0xf0ff) | 0x0200));        
  48.                 switch(GPIOA->IDR & 0xf000)                     
  49.                 {
  50.                         case 0x1000: KeyVal=14;        break;
  51.                         case 0x2000: KeyVal=10;        break;
  52.                         case 0x4000: KeyVal=6;        break;
  53.                         case 0x8000: KeyVal=2;        break;
  54.                 }               
  55.         GPIO_Write(GPIOC,((GPIOC->ODR & 0xf0ff) | 0x0400));        
  56.                 switch(GPIOA->IDR & 0xf000)                       
  57.                 {
  58.                         case 0x1000: KeyVal=13;        break;
  59.                         case 0x2000: KeyVal=9;        break;
  60.                         case 0x4000: KeyVal=5;        break;
  61.                         case 0x8000: KeyVal=1;        break;
  62.                 }                    

  63.         GPIO_Write(GPIOC,((GPIOC->ODR & 0xf0ff) | 0x0800));        
  64.                 switch(GPIOA->IDR & 0xf000)                       
  65.                 {
  66.                         case 0x1000: KeyVal=12;        break;
  67.                         case 0x2000: KeyVal=8;        break;
  68.                         case 0x4000: KeyVal=4;        break;
  69.                         case 0x8000: KeyVal=0;        break;
  70.                 }                                                                 
  71.                
  72.         return KeyVal;               
  73. }
复制代码
第三段 bsp_keyboard.h
  1. #ifndef __BSP_KEYBOARD_H
  2. #define __BSP_KEYBOARD_H

  3. #include "stm32f4xx.h"
  4. #include "stm32f4xx_gpio.h"
  5. #include "stm32f4xx_rcc.h"
  6. //引脚定义
  7. /*******************************************************/
  8. #define C1_PIN                  GPIO_Pin_8      
  9. #define C1_GPIO_PORT            GPIOC                     
  10. #define C1_GPIO_CLK             RCC_AHB1Periph_GPIOC

  11. #define C2_PIN                  GPIO_Pin_9                 
  12. #define C2_GPIO_PORT            GPIOC               
  13. #define C2_GPIO_CLK             RCC_AHB1Periph_GPIOC

  14. #define C3_PIN                  GPIO_Pin_10     
  15. #define C3_GPIO_PORT            GPIOC
  16. #define C3_GPIO_CLK             RCC_AHB1Periph_GPIOC

  17. #define C4_PIN                  GPIO_Pin_11
  18. #define C4_GPIO_PORT            GPIOC
  19. #define C4_GPIO_CLK             RCC_AHB1Periph_GPIOC

  20. #define R1_PIN                  GPIO_Pin_1      
  21. #define R1_GPIO_PORT            GPI0A               
  22. #define R1_GPIO_CLK             RCC_AHB1Periph_GPIOA

  23. #define R2_PIN                  GPIO_Pin_2               
  24. #define R2_GPIO_PORT            GPIOA         
  25. #define R2_GPIO_CLK             RCC_AHB1Periph_GPIOA

  26. #define R3_PIN                  GPIO_Pin_3      
  27. #define R3_GPIO_PORT            GPIOA              
  28. #define R3_GPIO_CLK             RCC_AHB1Periph_GPIOA

  29. #define R4_PIN                  GPIO_Pin_4     
  30. #define R4_GPIO_PORT            GPIOA              
  31. #define R4_GPIO_CLK             RCC_AHB1Periph_GPIOA



  32. /*******************************************************/



  33. int Key_Scan(void);
  34. void KeyBoard_GPIO_Config(void);

  35. #endif /*__BSP_KEYBOARD_H */

复制代码
led控制是用的秉火的源码,已经通过板载的K1和K2成功控制。


回复

使用道具 举报

 楼主| 发表于 2018-4-14 06:00:33 | 显示全部楼层
经过程序的仿真,我只能看到我的行列端口分别打开了moder的输出模式和输入模式。
但是我就是找不到原因,我的矩阵键盘也是可以正常使用的,从昨天晚上搞到了现在也没弄明白。跪求答案。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-14 06:12:53 | 显示全部楼层
我主要是想通过键盘来控制八路继电器。再就是觉得矩阵键盘可拓性很强,想学习一下。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-14 12:26:21 | 显示全部楼层
哇为什么我的帖子看不到
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-4 09:50 , Processed in 0.047934 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表