【笔记------LCD1602 SCM1602H-P9-Ver1.2】------ 4位数据驱动
角落里翻出来一块1602液晶屏,型号是SCM1602H-P9-Ver1.2,可能是普中开发板带的,不过是9针的:
引脚 | 定义 |
---|---|
Pin1 | Vss |
Pin2 | Vdd |
Pin3 | R/S |
Pin4 | EN |
Pin5 | D4 |
Pin6 | D5 |
Pin7 | D6 |
Pin8 | D7 |
Pin9 | BK |
点亮程序供参考。
lcd1602.h :
#ifndef _lcd1602_h
#define _lcd1602_h#include "../inc/header.h"#define LCD_1602_4b 0
#define LCD_1602_8b 1
#define LCD_MODE LCD_1602_4b#define LCD_D4 P10
#define LCD_D5 P11
#define LCD_D6 P12
#define LCD_D7 P13
#define LCD_RS P14 //cmd/dat-1/0
#define LCD_EN P15void Lcd1602_Init(void);
void Lcd1602_WriteString(unsigned char x, unsigned char y, unsigned char len, unsigned char *str);void Lcd1602_Test(void);
#endif
lcd1602.c :
#include "../inc/lcd1602.h"//如果没有使用读忙信号,必须通过延时保证写入完成,软件延时参数,测试使用stc15系列,35mhz,
//速度快的要适当调大延时,慢的可以把延时调小一点
#define DELAY_TIME_1602_WRITE 80/* --------------------------------------------------------------------------*/
/*** @Synopsis 软件延时 ** @Param t:延时参数*/
/* --------------------------------------------------------------------------*/
void Delay_1602(unsigned int t)
{while(t --);
}/* --------------------------------------------------------------------------*/
/*** @Synopsis 数据有效电平跳变*/
/* --------------------------------------------------------------------------*/
void Lcd1602_ENABLE(void)
{LCD_EN = 0;_nop_();_nop_();_nop_();
}/* --------------------------------------------------------------------------*/
/*** @Synopsis 数据无效电平跳变*/
/* --------------------------------------------------------------------------*/
void Lcd1602_DISENABLE(void)
{LCD_EN = 1;
}/* --------------------------------------------------------------------------*/
/*** @Synopsis 写一个字节数据** @Param dat*/
/* --------------------------------------------------------------------------*/
void Lcd1602_WriteByte_Dat(unsigned char dat)
{LCD_EN = 0;_nop_();LCD_RS = 1;Lcd1602_DISENABLE();_nop_();
#if LCD_MODE == LCD_1602_4bLCD_D7 = dat & 0x80;LCD_D6 = dat & 0x40;LCD_D5 = dat & 0x20;LCD_D4 = dat & 0x10;Lcd1602_ENABLE();Lcd1602_DISENABLE();LCD_D7 = dat & 0x08;LCD_D6 = dat & 0x04;LCD_D5 = dat & 0x02;LCD_D4 = dat & 0x01;Lcd1602_ENABLE();
#endif
//8bits数据总线
#if LCD_MODE == LCD_1602_8b
#endifDelay_1602(DELAY_TIME_1602_WRITE);
}/* --------------------------------------------------------------------------*/
/*** @Synopsis 写一个字节命令** @Param cmd*/
/* --------------------------------------------------------------------------*/
void Lcd1602_WriteByte_Cmd(unsigned char cmd)
{LCD_EN = 0;_nop_();LCD_RS = 0;Lcd1602_DISENABLE();_nop_();
#if LCD_MODE == LCD_1602_4bLCD_D7 = cmd & 0x80;LCD_D6 = cmd & 0x40;LCD_D5 = cmd & 0x20;LCD_D4 = cmd & 0x10;Lcd1602_ENABLE();Lcd1602_DISENABLE();LCD_D7 = cmd & 0x08;LCD_D6 = cmd & 0x04;LCD_D5 = cmd & 0x02;LCD_D4 = cmd & 0x01;Lcd1602_ENABLE();
#endif//8bits数据总线
#if LCD_MODE == LCD_1602_8b
#endifDelay_1602(DELAY_TIME_1602_WRITE);
}/* --------------------------------------------------------------------------*/
/*** @Synopsis 写字符串,只支持1602液晶自带的ascii字符** @Param x: 横坐标,0开始* @Param y: 纵坐标,0开始* @Param len: 数据长度* @Param str: 字符串指针*/
/* --------------------------------------------------------------------------*/
void Lcd1602_WriteString(unsigned char x, unsigned char y, unsigned char len, unsigned char *str)
{if(y == 0){Lcd1602_WriteByte_Cmd(0x80 + x);while(len --){Lcd1602_WriteByte_Dat(*(str++));}}else if(y == 1){Lcd1602_WriteByte_Cmd(0x80 + 0x40 + x);while(len --){Lcd1602_WriteByte_Dat(*(str++));}}
}/* --------------------------------------------------------------------------*/
/*** @Synopsis 1602液晶初始化*/
/* --------------------------------------------------------------------------*/
void Lcd1602_Init(void)
{P1M0 = 0x00;P1M1 = 0x00;LCD_RS = 0;LCD_EN = 0;LCD_D4 = 0;LCD_D5 = 0;LCD_D6 = 0;LCD_D7 = 0;Delay_1602(500);Lcd1602_WriteByte_Cmd(0x28); //设置4线驱动Delay_1602(500);Lcd1602_WriteByte_Cmd(0x0f); //开显示,配置光标Delay_1602(500);
}/* --------------------------------------------------------------------------*/
/*** @Synopsis 测试函数*/
/* --------------------------------------------------------------------------*/
void Lcd1602_Test(void)
{Lcd1602_WriteByte_Cmd(0x83);Lcd1602_WriteByte_Dat(0x30);Lcd1602_WriteByte_Dat(0x31);Lcd1602_WriteByte_Dat(0x32);Lcd1602_WriteByte_Dat(0x33);Lcd1602_WriteByte_Dat(0x34);Lcd1602_WriteByte_Dat(0x35);Lcd1602_WriteByte_Dat(0x36);Lcd1602_WriteByte_Dat(0x37);Lcd1602_WriteByte_Dat(0x38);Lcd1602_WriteByte_Dat(0x39);Lcd1602_WriteByte_Dat(0x30);Lcd1602_WriteByte_Cmd(0xc1);Lcd1602_WriteByte_Dat(0x31);Lcd1602_WriteByte_Dat(0x32);Lcd1602_WriteByte_Dat(0x33);Lcd1602_WriteByte_Dat(0x34);Lcd1602_WriteString(7, 1, 3, "ABC");
}
main.c :
头文件header.h只是把用到的头文件都包含了进去,比如stc15.h、intrins.h,不再列出
#include "../inc/header.h"void main(void)
{Lcd1602_Init();Lcd1602_Test();while (1) {}
}
注意:
- 这款液晶没有读信号,只能写,要通过适当延时来保证写完成,否则会导致丢失和乱码
- 液晶驱动可以参考HD4478U
- EN下降沿数据写入,BK是液晶背光,接电源负极
测试例程可以运行: