你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【萌新求助】我这个stm32矩阵4x4显示数字,按键没反应

[复制链接]
ruoxing1 提问时间:2018-2-28 17:10 /
求看看这个程序问题出在哪了,能二位一体动态数码管能显示,但是按矩阵键盘并不会显示我要的数字。


1.jpg
2.jpg
3.jpg
4.jpg
收藏 评论12 发布时间:2018-2-28 17:10

举报

12个回答
ruoxing1 回答时间:2018-2-28 17:29:26
  1. #include "stm32f10x.h"
  2. #include"sys.h"
  3. u8 KeyValue=0;
  4. void Delay_Ms( u16 time );
  5. u8 anjian(void);
  6. u8 table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};  //Õý³£¹²Ñô¼«´úÂë
  7. void GPIO_Configuration(void);

  8. int main()
  9. {  
  10.   
  11.     GPIO_Configuration();  
  12.    
  13.     while(1)
  14.                 {
  15.                 anjian();
  16.                 GPIO_ResetBits (GPIOC, GPIO_Pin_0);
  17.                 GPIOA->ODR=table[KeyValue%10];  
  18.                 Delay_Ms(5);
  19.                 GPIO_SetBits(GPIOC, GPIO_Pin_0);
  20.                 GPIO_ResetBits (GPIOC, GPIO_Pin_1);
  21.                 GPIOA->ODR=table[KeyValue/10];  
  22.                 Delay_Ms(5);
  23.                 GPIO_SetBits(GPIOC, GPIO_Pin_1);
  24.                 }
  25. }

  26. void GPIO_Configuration(void)    //GPIO¿ÚÅäÖÃ
  27. {   
  28.   GPIO_InitTypeDef GPIO_InitStructure;
  29.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  30.   //LED
  31.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  32.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  33.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  34.   GPIO_Init(GPIOC, &GPIO_InitStructure);  
  35.        
  36.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  37.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  38.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  39.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  40.         GPIO_Init(GPIOA,&GPIO_InitStructure);
  41.        
  42.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  43.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  44.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  45.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  46.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  47.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  48.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  49.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  50.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  51.         GPIO_SetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
  52.         GPIO_ResetBits(GPIOB, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
  53.   
  54. }



  55. void delay_ms(u16 time)       //ÑÓʱº¯Êý½èÓñðÈ˵ĸÄÌì×ÐϸÀí½â´ó¸Å1000Ϊ1s
  56. {      
  57.   u16 i=0;      
  58.   while(time--)      
  59.   {      
  60.     i=12000;      
  61.     while(i--);      
  62.   }      
  63. }

  64. u8 anjian(void)
  65. {
  66.        
  67.         if((GPIO_ReadInputData(GPIOB)&0xff)!=0x0f)
  68.         {
  69.                 Delay_Ms(10);
  70.                 if((GPIO_ReadInputData(GPIOB)&0xff)!=0x0f)
  71.                 {
  72.                         GPIO_SetBits(GPIOB, GPIO_Pin_0);
  73.                         GPIO_ResetBits(GPIOB, GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
  74.                         switch(GPIO_ReadInputData(GPIOB)&0xff)
  75.                         {
  76.                         case 0x11: KeyValue = 1; break;
  77.                         case 0x21: KeyValue = 5; break;
  78.                         case 0x41: KeyValue = 9; break;
  79.                         case 0x81: KeyValue = 13;break;
  80.                         }
  81.                         GPIO_SetBits(GPIOB, GPIO_Pin_1);
  82.                         GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3);
  83.                         switch(GPIO_ReadInputData(GPIOB)&0xff)
  84.                         {
  85.                         case 0x12: KeyValue = 2; break;
  86.                         case 0x22: KeyValue = 6; break;
  87.                         case 0x42: KeyValue = 10;break;
  88.                         case 0x82: KeyValue = 14;break;
  89.                         }
  90.                         GPIO_SetBits(GPIOB, GPIO_Pin_2);
  91.                         GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3);
  92.                         switch(GPIO_ReadInputData(GPIOB)&0xff)
  93.                         {
  94.                         case 0x14: KeyValue = 3; break;
  95.                         case 0x24: KeyValue = 7; break;
  96.                         case 0x44: KeyValue = 11;break;
  97.                         case 0x84: KeyValue = 15;break;
  98.                         }
  99.                         GPIO_SetBits(GPIOB, GPIO_Pin_3);
  100.                         GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2);
  101.                         switch(GPIO_ReadInputData(GPIOB)&0xff)
  102.                         {
  103.                         case 0x18: KeyValue = 4; break;
  104.                         case 0x28: KeyValue = 8; break;
  105.                         case 0x48: KeyValue = 12;break;
  106.                         case 0x88: KeyValue = 16;break;
  107.                         }
  108.                         GPIO_SetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
  109.                         GPIO_ResetBits(GPIOB, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7);
  110.                         while((GPIO_ReadInputData(GPIOB)&0xff)!=0x0f)
  111.                         {
  112.                         GPIO_ResetBits (GPIOC, GPIO_Pin_0);
  113.                 GPIOA->ODR=table[KeyValue%10];  
  114.                 Delay_Ms(5);
  115.                 GPIO_SetBits(GPIOC, GPIO_Pin_0);
  116.                 GPIO_ResetBits (GPIOC, GPIO_Pin_1);
  117.                 GPIOA->ODR=table[KeyValue/10];  
  118.                 Delay_Ms(5);
  119.                 GPIO_SetBits(GPIOC, GPIO_Pin_1);
  120.                         };
  121.                         return KeyValue;
  122.                 }
  123.         }
  124.         return 0;
  125. }

  126.         void Delay_Ms( u16 time )
  127. {
  128.                 u16 i,j;
  129.                 for( i = 0; i < time; i++ )
  130.                                 for( j = 10000; j > 0; j-- );
  131. }
复制代码
す疯Ⅱ恒す 回答时间:2018-2-28 17:21:54
发帖可以直接贴代码的,会好看很多。
另,矩阵键盘扫描不需要输出一行低电平么?输出前不需要配置为输出模式么?STM32与51还是有点区别的,51的不需要配置方向。
ruoxing1 回答时间:2018-2-28 17:28:51
す疯Ⅱ恒す 发表于 2018-2-28 17:21
发帖可以直接贴代码的,会好看很多。
另,矩阵键盘扫描不需要输出一行低电平么?输出前不需要配置为输出模 ...

第一次发帖不知道能直接贴代码。我图二里的
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
        GPIO_SetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
        GPIO_ResetBits(GPIOB, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
这一块不就是已经配置了一行低电平和输出模式了吗
す疯Ⅱ恒す 回答时间:2018-2-28 17:49:53

设置了PB0-3为输出低,读PB4-7;如果调换过来的时候,比如想读PB0-3,那也应该先配置为输入才可以读。
ruoxing1 回答时间:2018-2-28 18:17:50
す疯Ⅱ恒す 发表于 2018-2-28 17:49
设置了PB0-3为输出低,读PB4-7;如果调换过来的时候,比如想读PB0-3,那也应该先配置为输入才可以读。 ...

我刚刚试着改了半天,还是不行,主要我才开始弄stm32,这些理论话我都不咋看的懂,大神能不能说一下我这该怎么改,改成功了,我才能配合着刚刚你说的那些话一起消化理解
wenyangzeng 回答时间:2018-2-28 19:23:25
读按键值时必须把GPIO口设成输入状态:

GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;               
        GPIO_Init(GPIOB, &GPIO_InitStructure);

评分

参与人数 1蝴蝶豆 +3 收起 理由
zero99 + 3

查看全部评分

不吃肉的羊 回答时间:2018-2-28 20:42:10
我就一个问题,你硬件仿真有没有有效检测到按键按下?
xself 回答时间:2018-2-28 22:24:02
先不要管按键部分,先直接显示出来看正不正常,用排除法看下哪部分出问题。再细化。

评分

参与人数 1蝴蝶豆 +2 收起 理由
zero99 + 2

查看全部评分

wudianjun2001 回答时间:2018-3-1 08:25:46
矩阵的话,输入输出要配置好,输入要带上拉

评分

参与人数 1蝴蝶豆 +2 收起 理由
zero99 + 2

查看全部评分

12下一页

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
微信公众号二维码 微信公众号
手机版二维码 手机版