📄 lcd1602.c
字号:
//描述:LCD1602驱动程序
//编写:微控设计网 DC版主 日期:2007.9.
//编译环境:IAR EW430 V3.42A
//验证平台: www.microcontrol.cn MC430F14+开发板 V1.0
//声明:此程序只供微控用户使用.在未得到DC版主的许可请勿将资料外公开和用于商业用途.
//******************************************************************************
# include <msp430x14x.h>
# include "LCD1602.h"
//**************************************************************
//延时函数
void delay_nms(unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++) _NOP();
}
//**************************************************************
//正常读写操作之前必须检测LCD控制器状态:E=1 RS=0 RW=1;DB7: 0 LCD控制器空闲,1 LCD控制器忙。
//检测忙信号,等待LCD空闲函数
void wait_enable(void)
{
MCU_BUS_DIR_IN; //设置BUS口为输入
LVC4254_5Vto3V;
RS0; //RS=0
RW1; //RW=1
_NOP();
E1; //E=1
while(BUSIN & busy);//等待LCD_DB7为0
E0; //重设E=0
MCU_BUS_DIR_OUT; //设置BUS口为输出
LVC4254_3Vto5V;
}
//**************************************************************
//写指令函数: E=高脉冲 RS=0 RW=0
//command为指令,wait_en指定是否要检测LCD忙信号
void lcd_write_command(unsigned char command,unsigned char wait_en)
{
if(wait_en)
wait_enable();//若wait_en为1,则要检测LCD忙信号,等待其空闲
RS0;//RS=0
RW0;//RW=0
E0;//E=0,下面给LCD一个高脉冲
_NOP();
E1;//E=1
BUSOUT=command;
E0;//重设E=0
}
//**************************************************************
void LCD1602_INIT(void)
{
LVC4254_DIR0; //MCU对LVC4254 DIR控制端IO方向设为0
LVC4254_3Vto5V; //LVC4254方向为3V转5V,DIR=0
MCU_BUS_DIR_OUT; //MCU总线方向为输出
LCDCTL_RSRWE_DIR; //MCU对LCD 控制组端IO方向设为
LCDCTL_CS1CS2_DIR; //
//-------------------------------------------------
delay_nms(15);
lcd_write_command(0x38,0);//显示模式设置三次(此时不管lcd空闲与否)
delay_nms(5);
lcd_write_command(0x38,0);
delay_nms(5);
lcd_write_command(0x38,0);
delay_nms(5);
lcd_write_command(0x38,1);//显示模式设置(从此之后均需lcd空闲)
lcd_write_command(0x08,1);//显示关闭
lcd_write_command(0x01,1);//显示清屏
lcd_write_command(0x06,1);//显示光标移动设置
lcd_write_command(0x0c,1);//显示开及光标设置
}
//**************************************************************
//写数据函数: E =高脉冲 RS=1 RW=0
void lcd_write_data(unsigned char char_data)
{
wait_enable(); //等待LCD空闲
RS1; //RS=1
RW0; //RW=0
E0; //E=0,下面给LCD一个高脉冲
_NOP();
E1; //E=1
BUSOUT = char_data;
E0; //重设E=0
}
//**************************************************************
//指定位置显示一个字符:第一行位置0~15,第二行16~31
//显示一个字符函数
//参数position指定位置0~31,char_data为要显示的字符
void display_a_char(unsigned char position,unsigned char char_data)
{
unsigned char position_tem;
if(position>=0x10) //是否大于16?
position_tem=position+0xb0; //位置值+176
else
position_tem=position+0x80; //位置值+128
lcd_write_command(position_tem,1);//设置指定位置,且检测忙标志
lcd_write_data(char_data); //字一个字符
}
//**************************************************************
//指定一行显示连续字符串:0显示在第一行,1显示在第二行,注字符串不能长于16个字符
//显示一行连续字符串函数
//参数col指定行,*ptr指字符串数组的首指针
void display_a_string(unsigned char col,unsigned char *ptr)
{
unsigned char col_tem,i;
col_tem=col<<4;//若col为1(即在LCD第二行显示字符串),先把col左移4位,使显示字符的首位置改到第二行首位,即位置16
for(i=0;i<16;i++)
display_a_char(col_tem++,*(ptr+i));
}
//*****************************************************************************
void display_char (unsigned char L,unsigned char SP,unsigned char Dat)
{ unsigned char Position_Add;
if(L==1)
Position_Add = SP+128;
else
Position_Add = SP+192;
lcd_write_command(Position_Add,1);
lcd_write_data(Dat);
}
//*****************************************************************************
//指定一行显示字符串
//line:显示行数.数据范围1-2 StartPosition:显示行中显示开始位置,数据范围0-15
//*ptr 字符串数组地址
//[1]程序在超出每行的显示数量16个位置后,将退出.
//[2]若发送完字符串,则退出.
void Display_stringLine(unsigned char line,unsigned char StartPosition,unsigned char *ptr)
{ unsigned char temp,i;
if((line==2)||(line==1)) //校验行数是否正确
{ if(StartPosition <=15) //校验参数在每行显示的位置值是否正确
{ temp=16-StartPosition; //计算只能显示多少个字符
for(i=0;i<temp;i++) //发送字符串
{display_char(line,StartPosition,ptr[i]);//发送字符
StartPosition++; //加1,下一位字符位置
if(ptr[i+1]=='\0') //是否到了字符串结尾
break;
}
}
}
}
//******************************************************************************
/* MC430F14+板上IO测试用
while(1)
{
BUSOUT = 0xff;
RS1;RW1;E1;CS1_1;CS2_1;
_NOP();
BUSOUT = 0x00;
RS0;RW0;E0;CS1_0;CS2_0;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -