普中科技的单片机开发实验仪—矩阵键盘 代码

2025-10-22 08:18:01

1、主函数

#include "stm32f10x.h"

#include "delay.h"

#include "gpio.h"

#include "key.h"

#include "led.h"

#include "Globel_Var.h"

// 此表为 LED 的字模             0   1 2 3    4 5 6 7 8 9   a   b    c d   e f

unsigned char  LED7Code[] = {~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71};

int main ()

{

    delay_init();

    LED_Init();

while(1)

   {

         keydown(); //调用按键判断检测程序

         GPIO_Write(GPIOA,LED7Code[dis_buf%16]&0x7f);

   }

}

2、gpio.c  独立键盘引脚电平设置

#include "gpio.h"

void GPIOB1_Configuration(void)

{

    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); 

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |  GPIO_Pin_9 |  GPIO_Pin_10 |  GPIO_Pin_11 ; 

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

    GPIO_Init(GPIOB, &GPIO_InitStructure);  

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 |  GPIO_Pin_13 |  GPIO_Pin_14 |  GPIO_Pin_15 ;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

    GPIO_Init(GPIOB, &GPIO_InitStructure); 

    GPIO_Write(GPIOB,0XF0FF); 

}

void GPIOB2_Configuration(void)

{

    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |  GPIO_Pin_9 |  GPIO_Pin_10 |  GPIO_Pin_11 ; 

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

    GPIO_Init(GPIOB, &GPIO_InitStructure);  

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 |  GPIO_Pin_13 |  GPIO_Pin_14 |  GPIO_Pin_15 ; 

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

    GPIO_Init(GPIOB, &GPIO_InitStructure);  

    GPIO_Write(GPIOB,0X0FFF);

}

3、key.c

#include "key.h"

#include "delay.h"

#include "gpio.h"

#include "Globel_Var.h"

void  keyscan(void)

 {

    temp = 0;

    GPIOB1_Configuration();

    delay_ms(100); 

    temp=(GPIO_ReadInputData(GPIOB))>>8&0xF0; 

    temp=~((temp>>4)|0xF0);  

    if(temp==1)  

        key=1;   

    else if(temp==2)  

        key=2; //第2个按键值

    else if(temp==4)  

        key=3;   //第3个按键值

    else if(temp==8) 

        key=4;    

    else

        key=16;    

    GPIOB2_Configuration();

     delay_ms(100); //延时

    temp=(GPIO_ReadInputData(GPIOB)>>8)&0x0F;

    temp=~(temp|0xF0);

if(temp==1)    

        key=key+0;

    else if(temp==2)

        key=key+4;

    else if(temp==4)  

        key=key+8;

    else if(temp==8) 

        key=key+12;

    else

        key=16;     

     dis_buf = key;  

     dis_buf = dis_buf & 0x0f; 

 }

 void  keydown(void)

 {  int retval;

    GPIO_InitTypeDef GPIO_InitStructure;

     RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |  GPIO_Pin_9 |  GPIO_Pin_10 |  GPIO_Pin_11 ; 

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

    GPIO_Init(GPIOB, &GPIO_InitStructure);  

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 |  GPIO_Pin_13 |  GPIO_Pin_14 |  GPIO_Pin_15 ; 

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

    GPIO_Init(GPIOB, &GPIO_InitStructure); 

    GPIO_Write(GPIOB,0XF0FF);

retval =  GPIO_ReadInputData(GPIOB);

if(retval>>8!=0xF0)  

  keyscan();  /    }

 }

4、Globel_Var.c

#include "Globel_Var.h"

uchar  key;       

uchar  temp;

uchar  dis_buf;

Globel_Var.h

#ifndef __GLOBAL_VAR_H

#define __GLOBAL_VAR_H

#define uchar unsigned char //宏的定义变量类型 uchar 代替 unsigned char

#define uint  unsigned int //宏的定义变量类型 uint  代替 unsigned int

extern uchar  key;       

extern uchar  temp;

extern uchar  dis_buf; 

#endif

之所以写了这个,是为了防止重复定义的问题,你也可以去掉,在main函数中定义。

5、led.c

#include "led.h"

void LED_Init(void)//初始化

{

    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);

 // GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);  //关闭调试 端口重新映射  使用仿真器调试时,不能用此语

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; 

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  

   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 

   GPIO_Init(GPIOA, &GPIO_InitStructure);  

   GPIO_Write(GPIOA,0XFFFF); 

}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢