头文件
#ifndef __LCD1602_H
#define __LCD1602_H
#include "sys.h"
/*============================================================
液晶指令说明:
0x01-------------->清除屏幕,置AC为0,光标回位。
0x02-------------->DDRAM 地址为0,显示回原位,DDRAM内容不变.
0x03-------------->DDRAM 地址为0,显示回原位,DDRAM内容不变.
0x04-------------->设置光标移动方向减量方式,并指定显示不移动。
0x05-------------->设置光标移动方向减量方式,并指定显示移动。
0x06-------------->设置光标移动方向增量方式,并指定显示不移动。
0x07-------------->设置光标移动方向增量方式,并指定显示移动。
0x08-------------->设置显示关、光标关、光标所在字符不闪烁.
0x0c-------------->显示开
0x0e-------------->显示开,光标开
0x0f-------------->显示开,光标开,光标闪烁
0x10-------------->光标移位,左移
0x14-------------->光标移位,右移
0x18-------------->显示移位,左移
0x1c-------------->显示移位,右移
0x20-------------->4BIT模式,显示一行,5x7字体
0x24-------------->4BIT模式,显示一行,5x10字体
0x28-------------->4BIT模式,显示两行,5x7字体
0x2c-------------->4BIT模式,显示两行,5x10字体
0x30-------------->8BIT模式,显示一行,5x7字体
0x34-------------->8BIT模式,显示一行,5x10字体
0x38-------------->8BIT模式,显示两行,5x7字体
0x3c-------------->8BIT模式,显示两行,5x10字体
0x30-------------->8BIT模式,显示一行,5x7字体
0x30-------------->8BIT模式,显示一行,5x7字体
0x30-------------->8BIT模式,显示一行,5x7字体
0x30-------------->8BIT模式,显示一行,5x7字体
显示地址:
===============================================================
=0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 ................ 0x27=
=0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 ................ 0x67=
===============================================================
每行可以显示40个字符,可以看到的只有16个字符,可以通过指令使字符
整体移动来显示所有字符。
四线控制的方式:先送字节的高四位,再送低四位。
值得注意的是当使用的I/O口为高四位时数据先给另一个变量,变量再将
数据高四位送到I/O口,接着是将变量左移四位,再送到I/O口上去。
当使用的I/O口为低四位时数据先给另一个变量,变量右移四位后送到I/O
口上去,接着将数据给变量直接送入I/O口。
使用时注意一下。
============================================================*/
//定义MCU与LCD的接口
/*******************************
*D4---->PB8
*D5---->PB5
*D6---->PB6
*D7---->PB7
*RS---->PA8
*RW---->PA9
*EN---->PA10
LCD1602
VCC---->5V
GND---->GND
VO----->接对比度调节滑动变阻器
LED+ -->5v
LED- -->GND
*********************************/
#define RS GPIO_Pin_14
#define SET_RS() GPIO_SetBits(GPIOB,GPIO_Pin_14)
#define CLR_RS() GPIO_ResetBits(GPIOB,GPIO_Pin_14)
#define RW GPIO_Pin_12
#define SET_RW() GPIO_SetBits(GPIOB,GPIO_Pin_12)
#define CLR_RW() GPIO_ResetBits(GPIOB,GPIO_Pin_12)
#define EN GPIO_Pin_13
#define SET_EN() GPIO_SetBits(GPIOB,GPIO_Pin_13)
#define CLR_EN() GPIO_ResetBits(GPIOB,GPIO_Pin_13)
/*-----------------------------------------------------
函数声明
-------------------------------------------------------*/
void LCD_init (void);
void LCD_write_cmd (unsigned char cmd);
void LCD_write_data (unsigned char w_data);
void LCD_Write_half_byte (unsigned char half_byte);
void LCD_set_xy (unsigned char x, unsigned char y);
void LCD_write_string (unsigned char X,unsigned char Y,unsigned char *s);
void show_str(unsigned char *s);
void Lcd_Display_2Date(unsigned char line,unsigned char list,unsigned int Date);
void Lcd_Display_3Date(unsigned char line,unsigned char list,unsigned int Date);
void Lcd_Display_Three_point_Date(unsigned char line,unsigned char list,unsigned int Date);
void Lcd_ShowSring(unsigned char line,unsigned char list,unsigned char *p);
void Lcd_Display_22Date(unsigned char line,unsigned char list,unsigned int Date);
void Lcd_Display_4Date(unsigned char line,unsigned char list,unsigned int Date);
void Lcd_Display_5Date(unsigned char line,unsigned char list,unsigned int Date);
void Lcd_Display_6Date(unsigned char line,unsigned char list,unsigned int Date1,unsigned int Date2);
//void Move (unsigned char step,unsigned char dirction,unsigned int time);
//void Flash_lcd (unsigned int delay_t,unsigned int times);
void delay(vu32 cnt);
//==================================================
#endif
源文件
#include "LCD1602.h"
GPIO_InitTypeDef GPIO_InitStructure;
/* If processor works on high frequency delay has to be increased, it can be
increased by factor 2^N by this constant */
#define DELAY_2N 0
//==================================================
void LCD_init(void)
{
/*********************液晶使用的I/O口初始化**************************/
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7|GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
CLR_RW(); //读写位直接低电平,只写不读
/*********************液晶初始化**************************/
delay (15000);
CLR_RS();
LCD_Write_half_byte(0x3);
delay (15000);
LCD_Write_half_byte(0x3);
delay (15000);
LCD_Write_half_byte(0x3);
LCD_Write_half_byte(0x2);
LCD_write_cmd(0x28); // 4bit显示模式,2行,5x7字体
delay (20000);
LCD_write_cmd(0x08); // 显示关闭
delay (20000);
LCD_write_cmd(0x01); // 显示清屏
delay (20000);
LCD_write_cmd(0x06); // 显示光标移动设置
delay (20000);
LCD_write_cmd(0x0C); //显示开,光标关,
//LCD_write_cmd(0x0F); // 显示开,光标开,光标闪烁
delay (20000);
LCD_write_cmd(0x01); //清屏
}
/*--------------------------------------------------
函数说明:写命令到液晶
---------------------------------------------------*/
void LCD_write_cmd(unsigned char cmd)
{
CLR_RS();
LCD_Write_half_byte(cmd >> 4);
LCD_Write_half_byte(cmd);
delay (10000);
}
/*--------------------------------------------------
函数说明:写数据到液晶
---------------------------------------------------*/
void LCD_write_data(unsigned char w_data)
{
SET_RS();
LCD_Write_half_byte(w_data >> 4);
LCD_Write_half_byte(w_data);
delay (10000);
}
/*--------------------------------------------------
函数说明:写4bit到液晶
--------------------------------------------------*/
void LCD_Write_half_byte(unsigned char half_byte)
{
if (half_byte&0x01)
GPIO_SetBits(GPIOB,GPIO_Pin_8);
else
GPIO_ResetBits(GPIOB,GPIO_Pin_8);
if (half_byte&0x02)
GPIO_SetBits(GPIOB,GPIO_Pin_7);
else
GPIO_ResetBits(GPIOB,GPIO_Pin_7);
if (half_byte&0x04)
GPIO_SetBits(GPIOB,GPIO_Pin_6);
else
GPIO_ResetBits(GPIOB,GPIO_Pin_6);
if (half_byte&0x08)
GPIO_SetBits(GPIOB,GPIO_Pin_5);
else
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
SET_EN();
delay(2000);
CLR_EN();
delay(2000);
}
/*----------------------------------------------------
LCD_set_xy : 设置LCD显示的起始位置
输入参数:x、y : 显示字符串的位置,X:1-16,Y:1-2
-----------------------------------------------------*/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y==1)
{
address=0x80-1+x;
}
else
{
address=0xc0-1+x;
}
LCD_write_cmd(address);
}
/*---------------------------------------------------
LCD_write_string : 英文字符串显示函数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置
---------------------------------------------------*/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y );
while (*s)
{
LCD_write_data(*s);
s++;
}
}
void show_str(unsigned char *s)
{ while(*s != '\0')
{
LCD_write_data(*s);
s++;
}
}
void Lcd_Display_22Date(unsigned char line,unsigned char list,unsigned int Date)//用于DS1302显示
{
unsigned char ta[]={0,0,'\0'}; //????????
switch(line){
case 1: LCD_write_cmd(0x80+list);break; //???
case 2: LCD_write_cmd(0xC0+list);break; //???
default : break;
}
ta[0]=Date/16+48;
ta[1]=Date%16+48;
show_str(ta);
}
void Lcd_Display_2Date(unsigned char line,unsigned char list,unsigned int Date)
{
unsigned char i;
unsigned char ta[]={0,0,'\0'}; //????????
switch(line){
case 1: LCD_write_cmd(0x80+list);break; //???
case 2: LCD_write_cmd(0xC0+list);break; //???
default : break;
}
for(i=2;i>0;i--)
{ //??????????????ta
ta[i-1]=Date%10+48;
Date=Date/10;
}
show_str(ta);
}
void Lcd_Display_3Date(unsigned char line,unsigned char list,unsigned int Date)
{
unsigned char i;
unsigned char ta[]={0,0,0,'\0'}; //????????
switch(line){
case 1: LCD_write_cmd(0x80+list);break; //???
case 2: LCD_write_cmd(0xC0+list);break; //???
default : break;
}
for(i=3;i>0;i--)
{ //??????????????ta
ta[i-1]=Date%10+48;
Date=Date/10;
}
show_str(ta);
}
void Lcd_Display_Three_point_Date(unsigned char line,unsigned char list,unsigned int Date)
{
unsigned char i;
unsigned char ta[]={0,0,0,'\0'}; //????????
switch(line){
case 1: LCD_write_cmd(0x80+list);break; //???
case 2: LCD_write_cmd(0xC0+list);break; //???
default : break;
}
for(i=3;i>0;i--)
{ //??????????????ta
ta[i-1]=Date%10+48;
Date=Date/10;
}
LCD_write_data(ta[0]);
LCD_write_data(ta[1]);
LCD_write_data(0x2e);
LCD_write_data(ta[2]);
LCD_write_data('C');
}
//函数 Lcd_init(uchar com)
//功能 精确屏幕显示字符串
//参数 line 液晶屏的第几行 list 第几个位置开始写 (最多8个列) *p要写的字符串
void Lcd_ShowSring(unsigned char line,unsigned char list,unsigned char *p)
{
switch(line){
case 1: LCD_write_cmd(0x80+list);break; //第一行
case 2: LCD_write_cmd(0xC0+list);break; //第二行
default : break;
}
while(*p !='\0')
{
LCD_write_data(*p++);
}
}
//========================================================
void delay(vu32 cnt)
{
cnt <<= DELAY_2N;
while (cnt--);
}
//========================================================