热度 4
#
#include "io430.h"
#define uint unsigned int //定义数据类型替代码
#define uchar unsigned char
#define dat P1OUT //定义LCD1602的数据口为P1
#define CLR_RS P2OUT&=~BIT0 //置零P2.0位,即置零RS
#define SET_RS P2OUT|=BIT0 //置一P2.0位,即置一RS
#define CLR_RW P2OUT&=~BIT1 //置零P2.1位,即置零RW
#define SET_RW P2OUT|=BIT1 //置一P2.1位,即置一RW
#define CLR_EN P2OUT&=~BIT2 //置零P2.2位,即置零EN
#define SET_EN P2OUT|=BIT2 //置一P2.2位,即置一EN
uchar busy; //1602判忙标志
void delay_lms(int x); //延时程序,短延时
void delay_ls(int x); //延时程序,长延时
void display(void); //显示程序
void busy_1602(void); //液晶查忙程序
void init_1602(void); //液晶初始化
void shj_1602(uchar a); //液晶写数据程序
void zhl_1602(uchar a); //液晶写指令程序
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P1DIR=0xff;
P3DIR=0xff;
display();
}
void delay_lms(int x) //延时程序,短延时
{
while(x--)
for(int i=0;i<25;i++);
}
void delay_ls(int x) //延时程序,长延时
{
for(int i=0;i<8;i++)
{
while(x--)
for(int i=0;i<150;i++);
}
}
void display(void) //液晶显示程序
{
uchar i; //定义计数值
uchar table0[16]={"fuck the rules.."}; //定义LCD1602显示两行的字符
uchar table1[16]={"God bless you..."};
init_1602(); //初始化1602
zhl_1602(0x08); //关闭显示
zhl_1602(0x01); //显示清屏
for(i=0;i<16;i++) //发送数据第一行
{
shj_1602(table0);
}
zhl_1602(0x80+0x40); //显示换行
for (i=0;i<16;i++) //发送数据第二行
{
shj_1602(table1);
}
zhl_1602(0x0c); //显示开及光标设置
}
void busy_1602(void) //查询忙碌标志信号程序
{
do
{
CLR_EN; //EN置0
SET_RW; //RW置1 读信号线
CLR_RS; //RS置0 读取指令
SET_EN; //EN置1 读信息
busy=dat;
CLR_EN; //EN置0
delay_lms(10);
}
while(busy&&0x10==1);
}
void zhl_1602(uchar a) //写指令到LCM程序
{
busy_1602();
CLR_EN; //EN置0
CLR_RW; //RW置0 写信号线
CLR_RS; //RS置1 读取指令
SET_EN; //EN置1 读信息
dat=a;
CLR_EN; //EN置0
}
void shj_1602(uchar a) //写指令到LCM程序
{
busy_1602();
CLR_EN; //EN置0
CLR_RW; //RW置0 写信号线
SET_RS; //RS置1 读取数据
SET_EN; //EN置1 读信息
dat=a;
CLR_EN; //EN置0
}
void init_1602(void) //启动LCM程序
{
zhl_1602(0x38); //写指令
zhl_1602(0x0c); //显示开及光标设置
zhl_1602(0x06); //光标移动设置
}