首页 > 代码库 > STM32 4x4矩阵键盘

STM32 4x4矩阵键盘

可同时识别两个键值,并分别显示,用了自己的12864显示,有些注释的地方有乱码请忽略。基于STM32f103vct6.

主函数:

#include "Rect_key.h"#include "12864.h"int main(){    u16 keynum=0;    delay_init();    lcd_gpio_init();    lcd_init();    Rect_key_Init();    lcd_6x8(0,0,"STM32 4x4 KEY Test :");    while(1)    {        keynum=Read_one_key_value();        lcd_6x8_num(50,2,keynum);         Read_more_key_value();    }}

12864显示函数:

#ifndef _12864_H#define _12864_H#include "sys.h"#include "delay.h"#define rs PBout(0)#define reset PBout(1)#define cs PBout(2)#define d6 PBout(6)#define d7 PBout(7)extern void lcd_gpio_init(void);extern void lcd_init(void);    extern void lcd_clear(void);extern void lcd_clear_y(u8 y);extern void wr_cmd(u8 data1);extern void wr_data(u8 data1);extern void lcd_pos(u8 x,u8 y);extern void lcd_6x8(u8 x, u8 y,u8 ch[]);extern void lcd_6x8_num(u8 x,u8 y,u16 num);#endif
#include "12864.h"const u8 F6X8[][6]={            { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },   // sp    { 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 },   // !    { 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 },   // "    { 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 },   // #    { 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 },   // $    { 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 },   // %    { 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 },   // &    { 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 },   //    { 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 },   // (    { 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 },   // )    { 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 },   // *    { 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 },   // +    { 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 },   // ,    { 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 },   // -    { 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 },   // .    { 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 },   // /    { 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E },   // 0    { 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 },   // 1    { 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 },   // 2    { 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 },   // 3    { 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 },   // 4    { 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 },   // 5    { 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 },   // 6    { 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 },   // 7    { 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 },   // 8    { 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E },   // 9    { 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 },   // :    { 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 },   // ;    { 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 },   // <    { 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 },   // =    { 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 },   // >    { 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 },   // ?    { 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E },   // @    { 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C },   // A    { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 },   // B    { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 },   // C    { 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C },   // D    { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 },   // E    { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 },   // F    { 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A },   // G    { 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F },   // H    { 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 },   // I    { 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 },   // J    { 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 },   // K    { 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 },   // L    { 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F },   // M    { 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F },   // N    { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E },   // O    { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 },   // P    { 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E },   // Q    { 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 },   // R    { 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 },   // S    { 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 },   // T    { 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F },   // U    { 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F },   // V    { 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F },   // W    { 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 },   // X    { 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 },   // Y    { 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 },   // Z    { 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 },   // [    { 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55 },   // 55    { 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 },   // ]    { 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 },   // ^    { 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 },   // _    { 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 },   //    { 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 },   // a    { 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 },   // b    { 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 },   // c    { 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F },   // d    { 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 },   // e    { 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 },   // f    { 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C },   // g    { 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 },   // h    { 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 },   // i    { 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 },   // j    { 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 },   // k    { 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 },   // l    { 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 },   // m    { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 },   // n    { 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 },   // o    { 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 },   // p    { 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC },   // q    { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 },   // r    { 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 },   // s    { 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 },   // t    { 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C },   // u    { 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C },   // v    { 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C },   // w    { 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 },   // x    { 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C },   // y    { 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 },   // z    { 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 }    // horiz lines};void lcd_gpio_init(void){    GPIO_InitTypeDef  GPIO_InitStructure;    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);             GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_6|GPIO_Pin_7;                     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;          //ÍÆÍìÊä³ö    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;             GPIO_Init(GPIOB, &GPIO_InitStructure);                         //GPIO_ResetBits(GPIOB,GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);    }void wr_cmd(u8 data1){    u8 i;    cs=0;    rs=0;                   //Ö¸ÁîÐźŠ   for(i=0;i<8;i++)    {        if((data1<<i)&0x80)             d7=1;        else             d7=0;        d6=0;        d6=1;    }}void wr_data(u8 data1){    u8 i;    //cs=0;    rs=1;              //Êý¾Ý    for(i=0;i<8;i++)    {        if((data1<<i)&0x80)             d7=1;          else             d7=0;        d6=0;        d6=1;    }}void lcd_pos(u8 x,u8 y){    wr_cmd(0xb0+y);   //ÉèÖÃÒ³µØÖ·    wr_cmd(((x&0xf0)>>4)|0x10);  //ÁеØÖ·¸ßËÄλÉèÖà    wr_cmd((x&0x0f)|0x01);       //ÁеØÖ·µÍËÄλÉèÖà    wr_cmd(0xa0);                //ÁеØÖ·ÔöÁ¿´Ó×óµ½ÓÒ    //wr_cmd(0xe0);}void lcd_init(void)                             {           u8 x,y;    d6=1;    reset=0;               delay_ms(10);    reset=1;      //¸´Î»Íê±Ï                      delay_ms(10);    wr_cmd(0xe2); //Èí¸´Î»    delay_ms(5);    wr_cmd(0x2c); //Éýѹ²½¾Û     delay_ms(5);    wr_cmd(0x2e); //Éýѹ²½¾Û     delay_ms(5);    wr_cmd(0x2f); //Éýѹ²½¾Û     delay_ms(5);    wr_cmd(0xc8); //ÐÐɨÃè˳Ðò£º´ÓÉϵ½Ï    wr_cmd(0xa0); //ÁÐɨÃè˳Ðò£º´Ó×óµ½ÓÒ    wr_cmd(0x40); //ÆðʼÐУºµÚÒ»ÐпªÊ¼            wr_cmd(0xaf); //¿ªÏÔʾ    for(y=0;y<8;y++)    {        wr_cmd(0xb0+y);        wr_cmd(0x01);        wr_cmd(0x10);        for(x=0;x<128;x++)            wr_data(0x00);    }           }void lcd_clear(void){    u8 x,y;    for(y=0;y<8;y++)    {        wr_cmd(0xb0+y);        wr_cmd(0x01);        wr_cmd(0x10);        for(x=0;x<128;x++)            wr_data(0x00);    }              }void lcd_clear_y(u8 y){    u8 x;    wr_cmd(0xb0+y);    wr_cmd(0x01);    wr_cmd(0x10);    for(x=0;x<128;x++)        wr_data(0x00);     }void lcd_6x8(u8 x, u8 y,u8 ch[])   //ÏÔʾ×Ö·û´®{    u8 c=0,i=0,j=0;          while (ch[j]!=\0)    {            c =ch[j]-32;        if(x>126){x=0;y++;}        lcd_pos(x,y);            for(i=0;i<6;i++)             wr_data(F6X8[c][i]);          x+=6;        j++;    }}void lcd_6x8_num(u8 x,u8 y,u16 num)   //ÏÔʾÊý×Ö±äÁ¿{    u8 ok=0,c=0,i=0,j=6;    int n[7]={0};     if(num==0)    {        lcd_6x8(x,y,"0  ");//        lcd_pos(x,y);//        for(j=0;j<7;j++)//            wr_data(F6X8[16][j]);    }            while(num!=0)    {        n[j]=num%10;   //n[6]¸öλ        num=num/10;        j--;    }    for (j=0;j<7;j++)    {            if(n[j]==0&&ok==0);  //num=0        else        {            ok=1;            c =n[j]+16;            if(x>126)            {                x=0;                y++;            }            lcd_pos(x,y);                for(i=0;i<6;i++)            {                 wr_data(F6X8[c][i]);            }                   x+=6;        }                 }           }

接下来是4x4矩阵键盘函数:

#include "12864.h"#define row0 PDout(0)#define row1 PDout(1)#define row2 PDout(2)#define row3 PDout(3)#define col0 PDout(4)#define col1 PDout(5)#define col2 PDout(6)#define col3 PDout(7)/*¿ª·¢°å 4x4¾ØÕó¼üÅÌ     PD4 PD5 PD6 PD7PD0   1   2   3   4PD1   5   6   7   8PD2   9   10   11  12PD3   13  14   15  16*/extern void Rect_key_Init(void);extern u16 Read_one_key_value(void);extern void Read_more_key_value(void);#endif
#include "Rect_key.h"static u8 key_output[4]={GPIO_Pin_0,GPIO_Pin_1,GPIO_Pin_2,GPIO_Pin_3};static u8 key_input[4]={GPIO_Pin_4,GPIO_Pin_5,GPIO_Pin_6,GPIO_Pin_7};u16 keyvalue[4][4];void Rect_key_Init() {    GPIO_InitTypeDef  GPIO_InitStructure;    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;                     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;              GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;             GPIO_Init(GPIOD, &GPIO_InitStructure);    GPIO_SetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;                     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;  //             GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;             GPIO_Init(GPIOD, &GPIO_InitStructure);    }u16 Read_one_key_value(void){    u16 row,col,temp;    u16 KeyValue = 0;    if((GPIO_ReadInputData(GPIOD) | 0x000f) != 0x000f)    {        delay_ms(10);        if((GPIO_ReadInputData(GPIOD) | 0x000f) != 0x000f)        {            GPIO_SetBits(GPIOD,GPIO_Pin_0);            GPIO_ResetBits(GPIOD,GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 );            row = GPIOD->IDR & 0x000f;              col = GPIOD->IDR & 0x00f0;            switch(row | col)            {                case 0x0011:KeyValue = http://www.mamicode.com/1; break;                case 0x0021:KeyValue = http://www.mamicode.com/2; break;                case 0x0041:KeyValue = http://www.mamicode.com/3; break;                case 0x0081:KeyValue = http://www.mamicode.com/4; break;                default: break;            }            GPIO_SetBits(GPIOD,GPIO_Pin_1);            GPIO_ResetBits(GPIOD,GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3 );            row = GPIOD->IDR & 0x000f;              col = GPIOD->IDR & 0x00f0;            switch(row | col)            {                case 0x0012:KeyValue = http://www.mamicode.com/5; break;                case 0x0022:KeyValue = http://www.mamicode.com/6; break;                case 0x0042:KeyValue = http://www.mamicode.com/7; break;                case 0x0082:KeyValue = http://www.mamicode.com/8; break;                default: break;            }            GPIO_SetBits(GPIOD,GPIO_Pin_2);            GPIO_ResetBits(GPIOD,GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3 );            row = GPIOD->IDR & 0x000f;              col = GPIOD->IDR & 0x00f0;            switch(row | col)            {                case 0x0014:KeyValue = http://www.mamicode.com/9; break;                case 0x0024:KeyValue = http://www.mamicode.com/10; break;                case 0x0044:KeyValue = http://www.mamicode.com/11; break;                case 0x0084:KeyValue = http://www.mamicode.com/12; break;                default: break;            }            GPIO_SetBits(GPIOD,GPIO_Pin_3);            GPIO_ResetBits(GPIOD,GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 );            row = GPIOD->IDR & 0x000f;              col = GPIOD->IDR & 0x00f0;            switch(row | col)            {                case 0x0018:KeyValue = http://www.mamicode.com/13; break;                case 0x0028:KeyValue = http://www.mamicode.com/14; break;                case 0x0048:KeyValue = http://www.mamicode.com/15; break;                case 0x0088:KeyValue = http://www.mamicode.com/16; break;                default: break;            }            return KeyValue;        }    }    else return KeyValue;}void Read_more_key_value(void){    u8 i,j;    for(i = 0; i < 4; i++)       {        GPIO_ResetBits(GPIOD,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3);        GPIO_SetBits(GPIOD, key_output[i]);           for(j = 0; j < 4; j++)          {            if(GPIO_ReadInputDataBit(GPIOD, key_input[j]) == 1)                keyvalue[i][j] = i*4+(j+1);             else                keyvalue[i][j] = 0;    }    lcd_6x8_num(10,4,keyvalue[0][0]);    lcd_6x8_num(30,4,keyvalue[0][1]);    lcd_6x8_num(50,4,keyvalue[0][2]);    lcd_6x8_num(70,4,keyvalue[0][3]);    lcd_6x8_num(10,5,keyvalue[1][0]);    lcd_6x8_num(30,5,keyvalue[1][1]);    lcd_6x8_num(50,5,keyvalue[1][2]);    lcd_6x8_num(70,5,keyvalue[1][3]);    lcd_6x8_num(10,6,keyvalue[2][0]);    lcd_6x8_num(30,6,keyvalue[2][1]);    lcd_6x8_num(50,6,keyvalue[2][2]);    lcd_6x8_num(70,6,keyvalue[2][3]);    lcd_6x8_num(10,7,keyvalue[3][0]);    lcd_6x8_num(30,7,keyvalue[3][1]);    lcd_6x8_num(50,7,keyvalue[3][2]);    lcd_6x8_num(70,7,keyvalue[3][3]);        GPIO_SetBits(GPIOD, key_output[i]);        }    }    

其他函数可在STM32库中找。

显示效果图:

STM32 4x4矩阵键盘