#include \
#define read_busybit() GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_7 ) #define DATAOUT GPIOC->ODR// unsigned char table1[]={%unsigned char table2[]={\大学%unsigned char table3[]={\学院%unsigned char table4[]={\电子信息%unsigned char table5[]={\姓名\ unsigned char hy5;
unsigned char k=0;
void delay(unsigned int x) {
while(x--); } // 外设时钟使能
/*************************************************************************/ void RCC_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的引脚将在在配置
*******************************************************************************/
void GPIO_Configuration(void) // 1602引脚配置
{ GPIO_InitTypeDef GPIO_InitStructure; /* LD1 & LD2 & LD3 & LD4 configration */ 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); }
void key()//按键检测 {
unsigned char ii,jj; unsigned int key[10][10];
u16 ling[]={GPIO_Pin_7,GPIO_Pin_6,GPIO_Pin_11,GPIO_Pin_13}; u16 heng[]={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; return hy2; //
void LCD_DataIo_In(void)//数据线输入 1602 输入引脚配置 { GPIO_InitTypeDef GPIO_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); }
void LCD_DataIo_Out(void)//数据线输出 1602引脚输出配置 { GPIO_InitTypeDef GPIO_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); }
void hy() { 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); }
void GPIO_Cofiguration_12864(void) {
GPIO_InitTypeDef GPIO_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); }
void LCD_12864_in(void) {
GPIO_InitTypeDef GPIO_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); }
void LCD_12864_out(void) {
GPIO_InitTypeDef GPIO_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); }
void readbusy_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(); }
void hy2() {
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); }
/*******************************************************************************
*******************************************************************************/
void NVIC_Configuration(void) { }
/*******************************************************************************
初始化时钟晶振 72MHZ
*******************************************************************************/
void SysClock_Init(void) //上半部分在196页 { ErrorStatus HSEStartUpStatus;
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作为系统时钟 // ; } } }
/****************************************** *
* *
****************************************/
/**************************************************** 读忙函数
****************************************************/
//unsigned char readbusy() //读忙函数 void readbusy() {
//unsigned char result; 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 //return result; LCD_DataIo_Out();
}
void write_12864_cmd(unsigned char a11) //写指令 { //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 }
void write_12864_data(unsigned char data11) //写数据 { //unsigned int j=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 ); DATAOUT=data11; GPIO_SetBits(GPIOD,GPIO_Pin_3);//1 delay(5000 ); GPIO_ResetBits(GPIOD,GPIO_Pin_3); //0 }
void init_12864() {
// GPIO_ResetBits(GPIOD,GPIO_Pin_5);//这是12864的复位键 //delay(5000 ); GPIO_SetBits(GPIOD,GPIO_Pin_4); GPIO_SetBits(GPIOD,GPIO_Pin_5); write_12864_cmd(0x30); delay(5000 ); write_12864_cmd(0x30); delay(5000 ); write_12864_cmd(0x30); delay(5000 );
write_12864_cmd(0x06); delay(5000 );
write_12864_cmd(0x01); delay(5000 );
write_12864_cmd(0x0c); delay(5000 );
//write_12864_cmd(0x02); }
void weizhi_12864(unsigned char a1,unsigned char a2) { unsigned char x; if(a1==1) { a1=0x80; } else if(a1==2) { a1=0x90; } else if(a1==3) { a1=0x88; } else if(a1==4) { a1=0x98; } x=a1+a2; write_12864_cmd(x) ; }
void writeby(unsigned char a1,unsigned char a2) { readbusy(); hy(); GPIO_ResetBits(GPIOA,GPIO_Pin_1); //rs =0 GPIO_ResetBits(GPIOA,GPIO_Pin_2); GPIO_ResetBits(GPIOA,GPIO_Pin_3); if(a2==0) { GPIO_ResetBits(GPIOA,GPIO_Pin_1); //rs =0 GPIO_ResetBits(GPIOA,GPIO_Pin_2); //rw=0 } if(a2==1) { GPIO_SetBits(GPIOA,GPIO_Pin_1); //rs=1 GPIO_ResetBits(GPIOA,GPIO_Pin_2); //rw=0 } if(a1&0x80) { GPIO_SetBits(GPIOE,GPIO_Pin_7) ; } else { GPIO_ResetBits(GPIOE,GPIO_Pin_7); } if(a1&0x40) { GPIO_SetBits(GPIOE,GPIO_Pin_6) ; //等于1的意思 } else { GPIO_ResetBits(GPIOE,GPIO_Pin_6); } if(a1&0x20) { GPIO_SetBits(GPIOE,GPIO_Pin_5) ; } else { GPIO_ResetBits(GPIOE,GPIO_Pin_5); }
if(a1&0x10) {
GPIO_SetBits(GPIOE,GPIO_Pin_4) ; } else {
GPIO_ResetBits(GPIOE,GPIO_Pin_4); }
GPIO_SetBits(GPIOA,GPIO_Pin_3);
delay(5000 );
GPIO_ResetBits(GPIOA,GPIO_Pin_3); hy();
if((a1<<4)&0x80) {
GPIO_SetBits(GPIOE,GPIO_Pin_7) ; } else {
GPIO_ResetBits(GPIOE,GPIO_Pin_7); }
if((a1<<4)&0x40) {
GPIO_SetBits(GPIOE,GPIO_Pin_6) ; //等于1的意思 } else {
GPIO_ResetBits(GPIOE,GPIO_Pin_6); } if((a1<<4)&0x20) {
GPIO_SetBits(GPIOE,GPIO_Pin_5) ; } else {
GPIO_ResetBits(GPIOE,GPIO_Pin_5); } if((a1<<4)&0x10) {
GPIO_SetBits(GPIOE,GPIO_Pin_4) ; } else
{ GPIO_ResetBits(GPIOE,GPIO_Pin_4); } GPIO_SetBits(GPIOA,GPIO_Pin_3); delay(5000 ); GPIO_ResetBits(GPIOA,GPIO_Pin_3); }
/********************************************************
1602的定义
********************************************************/ void init() { writeby(0x28,0); writeby(0x28,0); writeby(0x28,0); writeby(0x01,0); writeby(0x02,0); writeby(0x0C,0); writeby(0x06,0); }
/******************************************************* MAIN 函数
*******************************************************/ int main(void) {
// unsigned char pp=0; SysClock_Init(); // 初始化系统时钟 72MHZ
RCC_Configuration(); // 使能外设 GPIO_Configuration(); // 配置引脚 GPIO_Cofiguration_12864(); init(); delay(20000 ); writeby(0xc0,0); delay(20000 );
writeby(0x30+1,1); delay(40000); k=0; while(table1[k]!=0) { writeby(table1[k],1); //Delay(2); delay(5000 ); k++; }
delay(10000 ); init_12864(); delay(20000 ); weizhi_12864(1,0); delay(10000 ); k=0;
while(table2[k]!='\\0') {
write_12864_data(table2[k]); delay(5000 ); k++; }
delay(10000 ); weizhi_12864(2,0); delay(10000 ); k=0;
while(table3[k]!='\\0') {
write_12864_data(table3[k]); delay(5000 ); k++; }
delay(10000 ); weizhi_12864(3,0); delay(10000 );
k=0;
while(table4[k]!='\\0') {
write_12864_data(table4[k]); delay(5000 ); k++; }
delay(10000 ); weizhi_12864(4,0); delay(10000 ); k=0;
while(table5[k]!='\\0') {
write_12864_data(table5[k]); delay(5000 ); k++; }
while(1) { //key();
key();//按键检测 //下面是lcd12864显示程序 writeby(0x80,0); writeby(0x30+hy5/10,1); writeby(0x30+hy5,1); delay(60000); delay(60000); delay(60000); } }
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库基于stm32的LCD12864显示+16个矩阵键盘程序在线全文阅读。
相关推荐: