標(biāo)題:
stm32單片機(jī)驅(qū)動(dòng)1602顯示的源程序
[打印本頁(yè)]
作者:
a123455
時(shí)間:
2018-7-11 01:54
標(biāo)題:
stm32單片機(jī)驅(qū)動(dòng)1602顯示的源程序
注意供電電壓,測(cè)試時(shí)因?yàn)橐恢庇?.3V所以導(dǎo)致無(wú)顯示,浪費(fèi)大量時(shí)間。且對(duì)比度引腳接地
STM32單片機(jī)源程序如下:
#include "lcd1602.h"
#include "delay.h"
#define DELAY_2N 0
//==================================================
void LCD_init(void)
{
/*********************液晶使用的I/O口初始化**************************/
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2| GPIO_Pin_0| GPIO_Pin_3
| GPIO_Pin_4| GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7
| GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
LCD_RW(0); //讀寫(xiě)位直接低電平,只寫(xiě)不讀
/*********************液晶初始化**************************/
delay_us(340);
LCD_RS(0);
LCD_write_cmd(0x38); // 8bit顯示模式,2行,5x7字體
delay_ms(4);
LCD_write_cmd(0x08); // 顯示關(guān)閉
delay_ms(4);
LCD_write_cmd(0x01); // 顯示清屏
delay_ms(4);
LCD_write_cmd(0x06); // 顯示光標(biāo)移動(dòng)設(shè)置
delay_ms(4);
LCD_write_cmd(0x0c); // 顯示開(kāi),光標(biāo)開(kāi),光標(biāo)閃爍
delay_ms(4);
LCD_write_cmd(0x01); //清屏
delay_ms(4);
}
/*--------------------------------------------------
函數(shù)說(shuō)明:寫(xiě)命令到液晶
---------------------------------------------------*/
void LCD_write_cmd(unsigned char cmd)
{
LCD_RS(0);
LCD_Write_byte(cmd);
delay_us(340);
}
/*--------------------------------------------------
函數(shù)說(shuō)明:寫(xiě)數(shù)據(jù)到液晶
---------------------------------------------------*/
void LCD_write_data(unsigned char w_data)
{
LCD_RS(1);
LCD_Write_byte(w_data);
delay_us(340);
}
/*--------------------------------------------------
函數(shù)說(shuō)明:寫(xiě)4bit到液晶
--------------------------------------------------*/
void LCD_Write_byte(unsigned char num)
{
if (num&0x01)
data0(1);
else
data0(0);
if (num&0x02)
data1(1);
else
data1(0);
if (num&0x04)
data2(1);
else
data2(0);
if (num&0x08)
data3(1);
else
data3(0);
if (num&0x10)
data4(1);
else
data4(0);
if (num&0x20)
data5(1);
else
data5(0);
if (num&0x40)
data6(1);
else
data6(0);
if (num&0x80)
data7(1);
else
data7(0);
delay_us(340);
LCD_EN(1);
delay_us(340);
LCD_EN(0);
delay_us(340);
}
/*----------------------------------------------------
LCD_set_xy : 設(shè)置LCD顯示的起始位置
輸入?yún)?shù):x、y : 顯示字符串的位置,X:0-15,Y:0-1
-----------------------------------------------------*/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address = 0;
if (y==0)
{
address=0x80+x;
}
else
{
address=0xc0+x;
}
// y ? (address=0xc0+x): (address=0x80+x) ;
LCD_write_cmd(address);
}
/*---------------------------------------------------
LCD_write_string : 英文字符串顯示函數(shù)
輸入?yún)?shù):*s :英文字符串指針;
X、Y : 顯示字符串的位置
---------------------------------------------------*/
void LCD_write_string(unsigned char X,unsigned char Y, char *s)
{
LCD_set_xy(X,Y);
while (*s)
{
LCD_write_data(*s);
s++;
}
}
//=======================================================
void LCD_wstring(unsigned char X,unsigned char *s)
{
LCD_write_cmd(X);
while (*s)
{
LCD_write_data(*s);
s++;
}
}
//void delay(vu32 cnt)
//{
// cnt <<= DELAY_2N;
// while (cnt--);
//}
復(fù)制代碼
所有資料51hei提供下載:
lcd1602.rar
(301.67 KB, 下載次數(shù): 100)
2018-7-11 03:20 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
LXH121
時(shí)間:
2019-1-17 18:15
你好,請(qǐng)問(wèn)如何顯示一個(gè)變量的值
作者:
HuM1989
時(shí)間:
2019-6-2 20:55
挺好的額
作者:
lk3272
時(shí)間:
2019-11-5 15:41
下來(lái)看看
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1