注:
此貼僅供提供項目(以正點原子的框架撰寫)(已測試),不提供LCD1602的資料(資料可以網上搜索,很容易就找到),如有對代碼有任何問題可以評論,我會不定期回復!
此貼所提供文件僅供學習,謝謝。
單片機源程序如下:
- #include "lcd1602.h"
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOF, ENABLE); //時鐘使能
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_4;
- 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_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(GPIOF, &GPIO_InitStructure);
- }
-
- void LCD1602_Wait_Ready(void) //判忙
- {
- u8 sta;
- //DATAOUT(0xff);
- LCD_RS_Clr();
- LCD_RW_Set();
- do
- {
- LCD_EN_Set();
- delay_ms(5);
- sta = GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_7);
- LCD_EN_Clr();
- }while(sta & 0x80);
- }
- void LCD1602_Write_Cmd(uint8_t cmd) //寫入指令
- {
- LCD1602_Wait_Ready();
- LCD_RS_Clr();
- LCD_RW_Clr();
- DATAOUT(cmd);
- LCD_EN_Set();
- LCD_EN_Clr();
- }
-
- void LCD1602_Write_Dat(uint8_t dat) //寫入數據
- {
- LCD1602_Wait_Ready();
- LCD_RS_Set();
- LCD_RW_Clr();
- DATAOUT(dat);
- LCD_EN_Set();
- LCD_EN_Clr();
- }
- void LCD1602_ClearScreen(void) //清屏
- {
- LCD1602_Write_Cmd(Cmd_Clear);
- }
- void LCD1602_Set_Cursor(uint8_t x, uint8_t y) //設置光標,字符顯示的地址(x是列,y是行)
- {
- u8 addr; //地址變量
- if (y == 0)
- {
- addr = 0x00 + x;
- }
- else
- {
- addr = 0x40 + x;
- }
- LCD1602_Write_Cmd(addr | 0x80); //這里|上0x80是因為在寫入顯示地址時LCD的D7位必須恒定為1
- }
- void LCD1602_Show_Str(uint8_t x, uint8_t y, uint8_t *str)
- {
- LCD1602_Set_Cursor(x, y);
- while(*str != '\0')
- {
- LCD1602_Write_Dat(*str++);
- }
- }
- void LCD1602_Init(void)
- {
- GPIO_Configuration();
- LCD1602_Write_Cmd(0x38); //LCD初始化設置(下同)
- LCD1602_Write_Cmd(0x0c);
- LCD1602_Write_Cmd(0x06);
- LCD1602_Write_Cmd(0x01);
- }
復制代碼
代碼見附件
Roy_STM32F103_LCD1602.7z
(177.22 KB, 下載次數: 213)
2020-2-20 20:43 上傳
點擊文件名下載附件
|