基于stm32f407的ch451l矩阵键盘控制源码(4*4,16个按键的矩阵键盘实际应用)

ch451l.c

#include "ch451l.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
Question    My = {
	
	0,        // Q_Start;
	0,         //My.Mode
	0,
};

int key_num = 20;

void ch451_write(u16 command)
{
   u8 i;
   load_0;   
   for(i=0;i<12;i++)           //д12λÃüÁî  
   {
      if(command&0x0001) din_1;
      else               din_0;
      dclk_0;                  //¸øÆäÒ»¸öÉÏÉýÑØ
      dclk_1;
      command>>=1;
   }
   load_1;                         //¼ÓÔؽøÈ¥
}



void ch451_init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_10|GPIO_Pin_9;	
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;     // ÆÕͨÊä³öģʽ
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;    //ÍÆÍìÊä³ö
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;      //ÉÏÀ­
	GPIO_Init(GPIOG, &GPIO_InitStructure);     //³õʼ»¯   GPIO
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;		
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;  
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;  	
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;      //ÉÏÀ­
	GPIO_Init(GPIOG, &GPIO_InitStructure);
	din_0;
  din_1;  
  ch451_write(0x0201);//оƬÄÚ²¿¸´Î»
  ch451_write(0x0403);	
}


u16 ch451_read(void)
{  
   u8 i,keycode=0;
   u16 command;
	 u8 t=0;
   command=0x07;            
   load_0;                
   for(i=0;i<4;i++)
   {
      if(command&0x0001) din_1;
      else               din_0;                //¶ÁÈ¡Ç°ËÄλ°´¼ü´úÂëÃüÁî
      dclk_0;
      dclk_1;
      command>>=1;
   }
   load_1;
   for(i=0;i<7;i++)
   {
      keycode<<=1;
      if(dout) keycode|=0X01;        //´®¿Ú½ÓÊÕÊý¾Ý
      dclk_1;
      dclk_0; 
   }
   
   switch(keycode)
   {
      case 67:t=1;break;
      case 66:t=2;break;
      case 65:t=3;break;
      case 64:t=4;break;
      case 75:t=5;break;
      case 74:t=6;break;
      case 73:t=7;break;
      case 72:t=8;break;
      case 83:t=9;break;
      case 82:t=10;break;
      case 81:t=11;break;
      case 80:t=12;break;
      case 91:t=13;break;
      case 90:t=14;break;
      case 89:t=15;break;
      case 88:t=16;break;
      default:t=0;break; 
   }
    return t; 	
}

ch451l.h

#ifndef __CH451L_H
#define __CH451L_H
#include "sys.h"

typedef struct
{
	u16    Q_Start;
	u16    Mode;         //ÎÊÌâÑ¡Ôñ±ê־λ
	u16    POS_Sec;
	u8     save_flag;    //±£´æ±ê־λ
	u8     flash_start;
  u8     change_mode;    //¸Ä±ä·þÎñº¯Êý
}Question;
extern  Question  My;



#define  dclk_0  GPIO_ResetBits(GPIOG,GPIO_Pin_11)
#define  dclk_1  GPIO_SetBits(GPIOG,GPIO_Pin_11)
#define  din_0   GPIO_ResetBits(GPIOG,GPIO_Pin_10)
#define  din_1   GPIO_SetBits(GPIOG,GPIO_Pin_10)
#define  load_0  GPIO_ResetBits(GPIOG,GPIO_Pin_9)
#define  load_1  GPIO_SetBits(GPIOG,GPIO_Pin_9)
#define  dout    GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_12)

void  ch451_write(u16 command);
void  ch451_init(void);
u16   ch451_read(void);
u8 get_val(u8 a);
u16 get_value(u8 mode);
void CH451_Get_val(void);



#endif



主函数中的具体调用:

    ch451_init();//初始化

 while(1){

    key=ch451_read();//读取键值

}

你可能感兴趣的:(stm32,矩阵,计算机外设)