STM32F0 系列產(chǎn)品基于超低功耗的 ARM Cortex-M0 處理器內(nèi)核,整合增強(qiáng)的技術(shù)和功能,瞄準(zhǔn)超低成本預(yù)算的應(yīng)用。該系列微控制器縮短了采用 8 位和 16 位微控制器的設(shè)備與采用 32 位微控制器的設(shè)備之間的性能差距,能夠在經(jīng)濟(jì)型用戶終端產(chǎn)品上實現(xiàn)先進(jìn)且復(fù)雜的功能。本文為大家介紹stm32矩陣鍵盤原理圖及程序
stm32矩陣鍵盤原理圖
stm32矩陣鍵盤程序介紹
主要實現(xiàn):掃描矩陣鍵盤,將檢測到的數(shù)據(jù)通過spi 通信發(fā)送到數(shù)碼管顯示。
主要步驟:
1:初始化時鐘
void RCC_Configuration(void)
{
//----------使用外部RC晶振-----------
RCC_DeInit(); //初始化為缺省值
RCC_HSEConfig(RCC_HSE_ON); //使能外部的高速時鐘
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); //等待外部高速時鐘使能就緒
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Enable Prefetch Buffer
FLASH_SetLatency(FLASH_Latency_2); //Flash 2 wait state
RCC_HCLKConfig(RCC_SYSCLK_Div1); //HCLK = SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); //PCLK2 = HCLK
RCC_PCLK1Config(RCC_HCLK_Div2); //PCLK1 = HCLK/2
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //PLLCLK = 8MHZ * 9 =72MHZ
RCC_PLLCmd(ENABLE); //Enable PLLCLK
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //Wait till PLLCLK is ready
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Select PLL as system clock
while(RCC_GetSYSCLKSource()!=0x08); //Wait till PLL is used as system clock source
}
2:配置管腳
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);//開啟GPIOD外設(shè)時鐘
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;//D0~D3
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉輸入
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//D4~D7
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽輸出
GPIO_Init(GPIOD, &GPIO_InitStructure);
//初始化管腳電平
GPIO_SetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2| GPIO_Pin_3);
GPIO_ResetBits(GPIOD, GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_SPI1, ENABLE); //開啟SPI1和GPIOA外設(shè)時鐘
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//設(shè)置SPI的四個引腳模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //設(shè)置GPIO A1管腳 用于鎖存74HC595輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽輸出
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI數(shù)據(jù)模式 雙線雙向全雙工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //SPI工作模式 主模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //設(shè)置SPI數(shù)據(jù)大小
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //設(shè)置時鐘的極性
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //設(shè)置時鐘的相位
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard; //NSS腳硬件置位
SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_64;//預(yù)分頻值
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;// 數(shù)據(jù)從高位傳輸
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值
SPI_I2S_DeInit(SPI1); //將外設(shè)SPI1寄存器重設(shè)為缺省值;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);//使能SPI1外設(shè)
}
3:編寫矩陣鍵盤掃描函數(shù)KEY.c
u8 shu=16;
void KeyScan(void)
{
u8 i;
if((GPIO_ReadInputData(GPIOD) & 0x0f) != 0x0f )
{
Delay_MS(20);
if((GPIO_ReadInputData(GPIOD) & 0x0f) != 0x0f )
{
GPIO_SetBits(GPIOD, GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_6);
GPIO_ResetBits(GPIOD, GPIO_Pin_7);
switch(GPIO_ReadInputData(GPIOD) & 0x0f)
{
case 0x07: shu=0; break;
case 0x0b: shu=1; break;
case 0x0d: shu=2; break;
case 0x0e: shu=3; break;
}
GPIO_SetBits(GPIOD, GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_7);
GPIO_ResetBits(GPIOD, GPIO_Pin_6);
switch(GPIO_ReadInputData(GPIOD) & 0x0f)
{
case 0x07: shu=4; break;
case 0x0b: shu=5; break;
case 0x0d: shu=6; break;
case 0x0e: shu=7; break;
}
GPIO_SetBits(GPIOD, GPIO_Pin_4 | GPIO_Pin_6| GPIO_Pin_7);
GPIO_ResetBits(GPIOD, GPIO_Pin_5);
switch(GPIO_ReadInputData(GPIOD) & 0x0f)
{
case 0x07: shu=8; break;
case 0x0b: shu=9; break;
case 0x0d: shu=10; break;
case 0x0e: shu=11; break;
}
GPIO_SetBits(GPIOD, GPIO_Pin_7 | GPIO_Pin_5| GPIO_Pin_6);
GPIO_ResetBits(GPIOD, GPIO_Pin_4);
switch(GPIO_ReadInputData(GPIOD) & 0x0f)
{
case 0x07: shu=12; break;
case 0x0b: shu=13; break;
case 0x0d: shu=14; break;
case 0x0e: shu=15; break;
}
GPIO_SetBits(GPIOD, GPIO_Pin_0 | GPIO_Pin_1| GPIO_Pin_2| GPIO_Pin_3);
GPIO_ResetBits(GPIOD, GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7);
while((i《50)&&((GPIO_ReadInputData(GPIOD) & 0x0f) != 0x0f))
{
i++;
Delay_MS(10);
}
}
}
}
4: SPI傳送數(shù)據(jù)函數(shù)
void Display_Data(u8 data)
{
u8 i=data;
PAOut(1)=0;
SPI_I2S_SendData(SPI1,DSY_CODE[i]);
Delay_MS(2);
PAOut(1)=1;
Delay_MS(1000);
}
5:主函數(shù)
#include“stm32f10x_lib.h”
#include
#include“Exboard.h”
u8 DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff};
int main(void)
{ //u8 i;
RCC_Configuration();
GPIO_Configuration();
//EXTI_Configuration();
//NVIC_Configuration();
while(1)
{
KeyScan();
Display_Data(shu);
}
}
附錄:Exboard.h
#ifndef _EXBOARD_H
#define _EXBOARD_H
#endif
#include“stm32f10x_lib.h”
#define GPIOA_IDR (GPIOA_BASE+0x08)
#define GPIOA_ODR (GPIOA_BASE+0x0c)
#define GPIOB_IDR (GPIOB_BASE+0x08)
#define GPIOB_ODR (GPIOB_BASE+0x0c)
#define GPIOC_IDR (GPIOC_BASE+0x08)
#define GPIOC_ODR (GPIOC_BASE+0x0c)
#define GPIOD_IDR (GPIOD_BASE+0x08)
#define GPIOD_ODR (GPIOD_BASE+0x0c)
#define GPIOE_IDR (GPIOE_BASE+0x08)
#define GPIOE_ODR (GPIOE_BASE+0x0c)
#define GPIOF_IDR (GPIOF_BASE+0x08)
#define GPIOF_ODR (GPIOF_BASE+0x0c)
#define GPIOG_IDR (GPIOG_BASE+0x08)
#define GPIOG_ODR (GPIOG_BASE+0x0c)
#define BitBang(Addr,BitNum) *((volatile unsigned long*)(((Addr&0xf0000000)+ 0x2000000)+(((Addr&0xfffff)《《5)+(BitNum《《2))))
#define PAIn(n) BitBang(GPIOA_IDR,n)
#define PAOut(n) BitBang(GPIOA_ODR,n)
#define PBIn(n) BitBang(GPIOB_IDR,n)
#define PBOut(n) BitBang(GPIOB_ODR,n)
#define PCIn(n) BitBang(GPIOC_IDR,n)
#define PCOut(n) BitBang(GPIOC_ODR,n)
#define PDIn(n) BitBang(GPIOD_IDR,n)
#define PDOut(n) BitBang(GPIOD_ODR,n)
#define PEIn(n) BitBang(GPIOE_IDR,n)
#define PEOut(n) BitBang(GPIOE_ODR,n)
#define PFIn(n) BitBang(GPIOF_IDR,n)
#define PFOut(n) BitBang(GPIOF_ODR,n)
#define PGIn(n) BitBang(GPIOG_IDR,n)
#define PGOut(n) BitBang(GPIOG_ODR,n)
#define KEY1 PEIn(0)
#define LED1 PDOut(13)
#define KEY2 PCIn(13)
#define LED2 PGOut(14)
void Delay_MS(u16 dly);
/
void RCC_Configuration(void);
void GPIO_Configuration(void);
//void EXTI_Configuration(void);
//void NVIC_Configuration(void);
extern void KeyScan(void);
void Display_Data(u8 data);
extern u8 shu;
-
STM32
+關(guān)注
關(guān)注
2270文章
10900瀏覽量
356001 -
矩陣鍵盤
+關(guān)注
關(guān)注
7文章
206瀏覽量
31471
發(fā)布評論請先 登錄
相關(guān)推薦
評論