|
#ifndef _LCD1602_H #define _LCD1602_H #include “LCD1602.H” //============================端口位清除寄存器==============================================// #define LCD1602_RS0 GPIOE-》BRR = 0x00000004 //低電平 PE.2 #define LCD1602_RW0 GPIOE-》BRR = 0x00000010 //低電平 PE.4 #define LCD1602_EN0 GPIOE-》BRR = 0x00000040 //低電平 PE.6 //============================端口位設(shè)置/清除寄存器=========================================// #define LCD1602_RS1 GPIOE-》BSRR = 0x00000004 //高電平 PE.2 #define LCD1602_RW1 GPIOE-》BSRR = 0x00000010 //高電平 PE.4 #define LCD1602_EN1 GPIOE-》BSRR = 0x00000040 //高電平 PE.4 #define DATA_OUT GPIOD-》ODR//數(shù)據(jù)端口 PB0-PB7 //==========================================================================================// //單片機(jī)系統(tǒng)時(shí)鐘在72MHZ下的延時(shí) void Delay_us(unsigned long CountLing) { signed char i; while(CountLing--) { i = 10; while(i--); } } //==========================================================================================// //LCD1602液晶讀忙狀態(tài) void LCD_FBUF(void) { LCD1602_RS0; //RS信號(hào)拉低 LCD1602_RW1; //RW信號(hào)拉高 LCD1602_EN1;//EN信號(hào)拉高 DATA_OUT = 0xFF; //數(shù)據(jù)端口 11111111全高電平 while((DATA_OUT & 0x80) == 0x80); //判斷數(shù)據(jù)端口PB7 是否一直高 LCD1602_RW1;//RW信號(hào)拉高 LCD1602_EN0; //EN信號(hào)拉低 } //==========================================================================================// //LCD1602寫指令子函數(shù) void Write_LCD1602_InstrucTIon_Com(unsigned char Com) { LCD_FBUF(); //讀忙狀態(tài) LCD1602_RS0; //RS信號(hào)拉低寫指令 LCD1602_RW0; //RW信號(hào)拉低 LCD1602_EN0; //EN信號(hào)拉低 DATA_OUT = Com; //數(shù)據(jù)端口指令輸入 Delay_us(340); //延時(shí)1ms LCD1602_EN1; //EN信號(hào)一個(gè)高脈沖 Delay_us(340); //延時(shí)1ms LCD1602_EN0; } //==========================================================================================// //LCD1602寫數(shù)據(jù)子函數(shù) void Write_LCD1602_InstrucTIon_Data(unsigned char Date) { LCD_FBUF(); //讀忙狀態(tài) LCD1602_RS1; //RS信號(hào)拉高寫數(shù)據(jù)指令 LCD1602_RW0; LCD1602_EN0; //EN信號(hào)拉低 DATA_OUT = Date; //數(shù)據(jù)端口數(shù)據(jù)輸入 Delay_us(340); //延時(shí)1ms LCD1602_EN1; //EN信號(hào)一個(gè)高脈沖 Delay_us(340); //延時(shí)1ms LCD1602_EN0; } //==========================================================================================// //LCD1602寫指令和寫數(shù)據(jù)子函數(shù) void Write_LCD1602_InstrucTIon_Com_Data(unsigned char CountLiey, unsigned char Date) { if(CountLiey == 0) //CountLiey == 0 寫指令 { Write_LCD1602_InstrucTIon_Com(Date); // 寫指令 } else //CountLiey !=0 寫數(shù)據(jù) { Write_LCD1602_Instruction_Data(Date); //不等于0寫數(shù)據(jù) } } //==========================================================================================// //LCD1602寫指令初始化子函數(shù) void Init_LCD1602(void) { Write_LCD1602_Instruction_Com(0x38); //設(shè)置16×2顯示,5×7點(diǎn)陣,8位數(shù)據(jù)接口 Delay_us(6); //延時(shí) 200us Write_LCD1602_Instruction_Com(0x38); //設(shè)置16×2顯示,5×7點(diǎn)陣,8位數(shù)據(jù)接口 Delay_us(6); //延時(shí) 200us Write_LCD1602_Instruction_Com(0x0C); //設(shè)置打開顯示 Delay_us(6); //延時(shí) 200us Write_LCD1602_Instruction_Com(0x06); //讀或?qū)懸粋(gè)字符后地址指針加1 Delay_us(6); Write_LCD1602_Instruction_Com(0x01); //顯示清屏 Delay_us(6); } #endif //========================================END==================================================// #include “stm32f10x_lib.h” #include “LCD1602.H” //#include “DS18B20.H” typedef volatile unsigned char uint8; typedef volatile unsigned int uint16; typedef volatile unsigned long uint32; //==========================================================================================// //配置GPIO端口 void GPIO_InitStructReadtempCmd(void) { GPIO_InitTypeDef GPIO_InitStruct; //GPIOC端口初始化 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 |GPIO_Pin_4 | GPIO_Pin_6; // LCD1602 RS-RW-EN腳 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD; //開漏輸出 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //輸出速率50MHZ GPIO_Init(GPIOE, &GPIO_InitStruct); //GPIOE端口初始化 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |//數(shù)據(jù)腳 PB0-PB7 GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD; //開漏輸出 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //輸出速率50MHZ GPIO_Init(GPIOD, &GPIO_InitStruct); //GPIOD端口初始化 } //==========================================================================================// //配置GPIO外設(shè)時(shí)鐘 void RCC_APB2PeriphReadtempyCmd(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); // 開啟GPIOD外設(shè)時(shí)鐘 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); // 開啟GPIOE外設(shè)時(shí)鐘 } //==========================================================================================// //函數(shù)主題 int main(void) { RCC_APB2PeriphReadtempyCmd();//外設(shè)時(shí)鐘初始化 //注意外設(shè)初始化一定要在GPIO端口配置初始化前面 GPIO_InitStructReadtempCmd(); //GPIO端口配置初始化 while(1) { Write_LCD1602_Instruction_Com_Data(0, 0x80+4); //0是寫指令 Write_LCD1602_Instruction_Com_Data(1, ‘A’ ); //1是寫數(shù)據(jù) } } |
|