ruoxing1 发表于 2018-2-28 17:10:09

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

求看看这个程序问题出在哪了,能二位一体动态数码管能显示,但是按矩阵键盘并不会显示我要的数字。


ruoxing1 发表于 2018-2-28 17:29:26

#include "stm32f10x.h"
#include"sys.h"
u8 KeyValue=0;
void Delay_Ms( u16 time );
u8 anjian(void);
u8 table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//Õý³£¹²Ñô¼«´úÂë
void GPIO_Configuration(void);

int main()
{

    GPIO_Configuration();
   
    while(1)
                {
                anjian();
                GPIO_ResetBits (GPIOC, GPIO_Pin_0);
                GPIOA->ODR=table;
                Delay_Ms(5);
                GPIO_SetBits(GPIOC, GPIO_Pin_0);
                GPIO_ResetBits (GPIOC, GPIO_Pin_1);
                GPIOA->ODR=table;
                Delay_Ms(5);
                GPIO_SetBits(GPIOC, GPIO_Pin_1);
                }
}

void GPIO_Configuration(void)    //GPIO¿ÚÅäÖÃ
{   
GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
//LED
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
        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;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        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);

}



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

u8 anjian(void)
{
       
        if((GPIO_ReadInputData(GPIOB)&0xff)!=0x0f)
        {
                Delay_Ms(10);
                if((GPIO_ReadInputData(GPIOB)&0xff)!=0x0f)
                {
                        GPIO_SetBits(GPIOB, GPIO_Pin_0);
                        GPIO_ResetBits(GPIOB, GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3);
                        switch(GPIO_ReadInputData(GPIOB)&0xff)
                        {
                        case 0x11: KeyValue = 1; break;
                        case 0x21: KeyValue = 5; break;
                        case 0x41: KeyValue = 9; break;
                        case 0x81: KeyValue = 13;break;
                        }
                        GPIO_SetBits(GPIOB, GPIO_Pin_1);
                        GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3);
                        switch(GPIO_ReadInputData(GPIOB)&0xff)
                        {
                        case 0x12: KeyValue = 2; break;
                        case 0x22: KeyValue = 6; break;
                        case 0x42: KeyValue = 10;break;
                        case 0x82: KeyValue = 14;break;
                        }
                        GPIO_SetBits(GPIOB, GPIO_Pin_2);
                        GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3);
                        switch(GPIO_ReadInputData(GPIOB)&0xff)
                        {
                        case 0x14: KeyValue = 3; break;
                        case 0x24: KeyValue = 7; break;
                        case 0x44: KeyValue = 11;break;
                        case 0x84: KeyValue = 15;break;
                        }
                        GPIO_SetBits(GPIOB, GPIO_Pin_3);
                        GPIO_ResetBits(GPIOB, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2);
                        switch(GPIO_ReadInputData(GPIOB)&0xff)
                        {
                        case 0x18: KeyValue = 4; break;
                        case 0x28: KeyValue = 8; break;
                        case 0x48: KeyValue = 12;break;
                        case 0x88: KeyValue = 16;break;
                        }
                        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);
                        while((GPIO_ReadInputData(GPIOB)&0xff)!=0x0f)
                        {
                        GPIO_ResetBits (GPIOC, GPIO_Pin_0);
                GPIOA->ODR=table;
                Delay_Ms(5);
                GPIO_SetBits(GPIOC, GPIO_Pin_0);
                GPIO_ResetBits (GPIOC, GPIO_Pin_1);
                GPIOA->ODR=table;
                Delay_Ms(5);
                GPIO_SetBits(GPIOC, GPIO_Pin_1);
                        };
                        return KeyValue;
                }
        }
        return 0;
}

        void Delay_Ms( u16 time )
{
                u16 i,j;
                for( i = 0; i < time; i++ )
                                for( j = 10000; j > 0; j-- );
}

す疯Ⅱ恒す 发表于 2018-2-28 17:21:54

发帖可以直接贴代码的,会好看很多。
另,矩阵键盘扫描不需要输出一行低电平么?输出前不需要配置为输出模式么?STM32与51还是有点区别的,51的不需要配置方向。

ruoxing1 发表于 2018-2-28 17:28:51

す疯Ⅱ恒す 发表于 2018-2-28 17:21
发帖可以直接贴代码的,会好看很多。
另,矩阵键盘扫描不需要输出一行低电平么?输出前不需要配置为输出模 ...

第一次发帖不知道能直接贴代码{:3_62:}。我图二里的
        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);
这一块不就是已经配置了一行低电平和输出模式了吗{:3_45:}

す疯Ⅱ恒す 发表于 2018-2-28 17:49:53

ruoxing1 发表于 2018-2-28 17:29


设置了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);

不吃肉的羊 发表于 2018-2-28 20:42:10

我就一个问题,你硬件仿真有没有有效检测到按键按下?

xself 发表于 2018-2-28 22:24:02

先不要管按键部分,先直接显示出来看正不正常,用排除法看下哪部分出问题。再细化。

wudianjun2001 发表于 2018-3-1 08:25:46

矩阵的话,输入输出要配置好,输入要带上拉
页: [1] 2
查看完整版本: 【萌新求助】我这个stm32矩阵4x4显示数字,按键没反应