完整版基于stm32的LCD12864显示+16个矩阵键盘程序.docx
《完整版基于stm32的LCD12864显示+16个矩阵键盘程序.docx》由会员分享,可在线阅读,更多相关《完整版基于stm32的LCD12864显示+16个矩阵键盘程序.docx(24页珍藏版)》请在冰豆网上搜索。
完整版基于stm32的LCD12864显示+16个矩阵键盘程序
#include"stm32f10x.h"
#defineread_busybit()GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_7)
#defineDATAOUTGPIOC->ODR//
unsignedchartable1[]={"65"};
unsignedchartable2[]={"大学"};
unsignedchartable3[]={"学院"};
unsignedchartable4[]={"电子信息"};
unsignedchartable5[]={"姓名"};
unsignedcharhy5;
unsignedchark=0;
voiddelay(unsignedintx)
{
while(x--);
}
//外设时钟使能
/*************************************************************************/
voidRCC_Configuration(void)//在208页这里是引脚时钟配置
{
/*使能外设时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|
RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|
RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
}
/*******************************************************************************
全部用到1602的引脚将在在配置
*******************************************************************************/
voidGPIO_Configuration(void)//1602引脚配置
{
GPIO_InitTypeDefGPIO_InitStructure;
/*LD1&LD2&LD3&LD4configration*/
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(GPIOE,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=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(GPIOA,&GPIO_InitStructure);
//矩阵键盘引脚输出配置
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_9;
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_13|GPIO_Pin_11|GPIO_Pin_7|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
voidkey()//按键检测
{
unsignedcharii,jj;
unsignedintkey[10][10];
u16ling[]={GPIO_Pin_7,GPIO_Pin_6,GPIO_Pin_11,GPIO_Pin_13};
u16heng[]={GPIO_Pin_0,GPIO_Pin_1,GPIO_Pin_2,GPIO_Pin_9};
GPIO_SetBits(GPIOB,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_9);
for(ii=0;ii<4;ii++)
{
GPIO_ResetBits(GPIOB,heng[ii]);
for(jj=0;jj<4;jj++)
{
delay(50000);
delay(50000);
//delay(50000);
if(GPIO_ReadInputDataBit(GPIOB,ling[jj])==0)
{
while(GPIO_ReadInputDataBit(GPIOB,ling[jj]==0));
key[ii][jj]=1;
}
else
{
key[ii][jj]=0;
//delay(50000);
}
}
GPIO_SetBits(GPIOB,heng[ii]);
}
if((key[0][0]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[0])==1)){hy5=1;}//****
if((key[0][1]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[0])==1)){hy5=2;}
if((key[0][2]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[0])==1)){hy5=3;}
if((key[0][3]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[0])==1)){hy5=4;}
if((key[1][0]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[1])==1)){hy5=5;}//***
if((key[1][1]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[1])==1)){hy5=6;}
if((key[1][2]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[1])==1)){hy5=7;}
if((key[1][3]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[1])==1)){hy5=8;}
if((key[2][0]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[2])==1)){hy5=9;}//*****
if((key[2][1]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[2])==1)){hy5=10;}
if((key[2][2]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[2])==1)){hy5=11;}
if((key[2][3]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[2])==1)){hy5=12;}
if((key[3][0]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[3])==1)){hy5=13;}//*****
if((key[3][1]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[3])==1)){hy5=14;}
if((key[3][2]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[3])==1)){hy5=15;}
if((key[3][3]==1)&&(GPIO_ReadInputDataBit(GPIOB,ling[3])==1)){hy5=16;}
//hy2=9;
//returnhy2;
//
}
voidLCD_DataIo_In(void)//数据线输入1602输入引脚配置
{
GPIO_InitTypeDefGPIO_InitStructure;
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_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitStructure);
}
voidLCD_DataIo_Out(void)//数据线输出1602引脚输出配置
{
GPIO_InitTypeDefGPIO_InitStructure;
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(GPIOE,&GPIO_InitStructure);
}
voidhy()
{
GPIO_ResetBits(GPIOE,GPIO_Pin_7);
GPIO_ResetBits(GPIOE,GPIO_Pin_6);
GPIO_ResetBits(GPIOE,GPIO_Pin_5);
GPIO_ResetBits(GPIOE,GPIO_Pin_4);
GPIO_ResetBits(GPIOE,GPIO_Pin_3);
GPIO_ResetBits(GPIOE,GPIO_Pin_2);
GPIO_ResetBits(GPIOE,GPIO_Pin_1);
GPIO_ResetBits(GPIOE,GPIO_Pin_0);
}
voidGPIO_Cofiguration_12864(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
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(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
}
voidLCD_12864_in(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
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_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
voidLCD_12864_out(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
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(GPIOC,&GPIO_InitStructure);
}
voidreadbusy_12864()
{
LCD_12864_in();
GPIO_ResetBits(GPIOD,GPIO_Pin_1);//rs
GPIO_SetBits(GPIOD,GPIO_Pin_2);//rw
GPIO_SetBits(GPIOD,GPIO_Pin_3);//en
while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_7));
GPIO_ResetBits(GPIOD,GPIO_Pin_3);
LCD_12864_out();
}
voidhy2()
{
GPIO_ResetBits(GPIOC,GPIO_Pin_0);
GPIO_ResetBits(GPIOC,GPIO_Pin_1);
GPIO_ResetBits(GPIOC,GPIO_Pin_2);
GPIO_ResetBits(GPIOC,GPIO_Pin_3);
GPIO_ResetBits(GPIOC,GPIO_Pin_4);
GPIO_ResetBits(GPIOC,GPIO_Pin_5);
GPIO_ResetBits(GPIOC,GPIO_Pin_6);
GPIO_ResetBits(GPIOC,GPIO_Pin_7);
}
/*******************************************************************************
*******************************************************************************/
voidNVIC_Configuration(void)
{
}
/*******************************************************************************
初始化时钟晶振72MHZ
*******************************************************************************/
voidSysClock_Init(void)//上半部分在196页
{
ErrorStatusHSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);//这个函数表示选择外部晶振作为时钟源
HSEStartUpStatus=RCC_WaitForHSEStartUp();//
if(HSEStartUpStatus==SUCCESS){//等待外部晶振起振
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//开打预取指缓存使能
FLASH_SetLatency(FLASH_Latency_2);//2延时周期。
设一个flash的等待状态。
时钟过快要在这里等待
RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置主时钟。
设置AHB时钟(HCLK)AHB时钟=系统时钟
RCC_PCLK2Config(RCC_HCLK_Div1);//设置高速的AHB时钟(PCLK2),APB2时钟=HCLK(RCC_HCLK_Div1)
RCC_PCLK1Config(RCC_HCLK_Div2);//设置低速的AHB时钟(PCLK1),AHB1时钟=HCLK/2
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//PLL的输入时钟=HSE时钟频率(RCC_PLLSource_HSE_Div1),PLL输入时钟X9(RCC_PLLMul_9)
//这里如果晶振是8MHZ时,RCC_PLLMul_9表示通过锁相环倍频9倍,此时晶振变为8MHZ*9=72MHZ,就变为72MHZ。
此时主时钟就是72MHZ(RCC_HCLKConfig(RCC_SYSCLK_Div1);)
//此时上面的RCC_PCLK2Config(RCC_HCLK_Div1)也为72MHZ,还有上面的RCC_PCLK1Config(RCC_HCLK_Div2);时钟就变为76MHZ/2=36MHZ
RCC_PLLCmd(ENABLE);//使能或者失能PLL在这里是使能
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET){//RCC_FLAG_PLLRDY是PLL就绪
;
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//选择PLL做为系统时钟(RCC_SYSCLKSource_PLLCLK)
while(RCC_GetSYSCLKSource()!
=0x08){//RCC_GetSYSCLKSource功能是返回用作系统时钟的时钟源,0x08表示PLL作为系统时钟
//;
}
}
}
/******************************************
*
*
*
****************************************/
/****************************************************
读忙函数
****************************************************/
//unsignedcharreadbusy()//读忙函数
voidreadbusy()
{
//unsignedcharresult;
LCD_DataIo_In();
//GPIO_SetBits(GPIOE,GPIO_Pin_7);
GPIO_ResetBits(GPIOA,GPIO_Pin_1);//rs=0
GPIO_SetBits(GPIOA,GPIO_Pin_2);//rw=1
GPIO_SetBits(GPIOA,GPIO_Pin_3);//en=1
//delay(5000);
while(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_7));
//result=read_busybit();
GPIO_ResetBits(GPIOA,GPIO_Pin_3);//en=0
//returnresult;
LCD_DataIo_Out();
}
voidwrite_12864_cmd(unsignedchara11)//写指令
{
//while(readbusy());
readbusy_12864();
hy2();
GPIO_ResetBits(GPIOD,GPIO_Pin_1);//rs=0
GPIO_ResetBits(GPIOD,GPIO_Pin_2);//rw=0
GPIO_ResetBits(GPIOD,GPIO_Pin_3);
//GPIO_ResetBits(GPIOA,GPIO_Pin_3);
delay(5000);
DATAOUT=a11;
GPIO_SetBits(GPIOD,GPIO_Pin_3);//1
delay(5000);
GPIO_ResetBits(GPIOD,GPIO_Pin_3);//0
}
voidwrite_12864_data(unsignedchardata11)//写数据
{
//unsignedintj=256;
//while((readbusy())&&(j--));
//while(readbusy());
readbusy_12864();
hy2();
delay(5000);
GPIO_ResetBits(GPIOD,GPIO_Pin_3);
GPIO_SetBits(GPIOD,GPIO_Pin_1);//rs=1
GPIO_ResetBits(GPIOD,GPIO_Pin_2);//rw=0
delay(5000