msp430g2553在LCD1602显示源程序
[复制链接]
单片机源程序如下:
#include<msp430.h> /* 必要操作:正确连接1602 */
#define LCD_OUT P1OUT //定义LCD1602的数据口为P1
#define CLR_RS P2OUT&=~BIT0; //置零P2.3位,也就是置零RS
#define SET_RS P2OUT|=BIT0; //置壹P2.3位,也就是置壹RS
#define CLR_RW P2OUT&=~BIT1; //置零P2.4位,也就是置零RW
#define SET_RW P2OUT|=BIT1; //置壹P2.4位,也就是置壹RW
#define CLR_EN P2OUT&=~BIT2; //置零P2.5位,也就是置零EN
#define SET_EN P2OUT|=BIT2; //置壹P2.5位,也就是置壹EN
unsigned char busy; //1602判忙标志
void busy_1602(void) //查询忙碌标志信号程序
{
do
{
CLR_EN;
SET_RW;
CLR_RS;
busy=LCD_OUT;
SET_EN;
_delay_cycles(1000);
CLR_EN;
_delay_cycles(1000);
}
while(busy&&0x10==1);
}
void LCD_Write_com(unsigned char com) //写指令到LCM程序
{
busy_1602();
CLR_EN;
CLR_RW;
CLR_RS;
LCD_OUT=com;
_nop();
SET_EN;
_nop();
CLR_EN;
}
void lcd_write_data(unsigned char data) //写数据到LCM程序
{
busy_1602();
CLR_EN;
CLR_RW;
SET_RS;
LCD_OUT=data;
_nop();
SET_EN;
_nop();
CLR_EN;
}
void init_1602(void) //启动LCM程序
{
LCD_Write_com(0x38);
LCD_Write_com(0x0c);
LCD_Write_com(0x06);
}
void lcd_clear(void) //清屏函数
{
LCD_Write_com(0x01);
_nop();
_nop();
_nop();
}
void lcd_write_string(unsigned char x,unsigned char y,unsigned char *s) //字符串显示函数
{
if (y==0)
{
LCD_Write_com(0x80 + x);
}
else
{
LCD_Write_com(0xC0 + x);
}
while(*s)
{
lcd_write_data(*s);
s++;
}
}
/* //字符显示函数 */
void lcd_write_char(unsigned char x,unsigned char y, unsigned char Data)
{
if(y==0)
{
LCD_Write_com(0x80 + x);
}
else
{
LCD_Write_com(0xC0 + x);
}
lcd_write_data(Data);
}
void main(void) //主程序开始
{
WDTCTL=WDTPW+WDTHOLD;
P1DIR=0xFF;
P2DIR=0xFF;
init_1602();
while(1)
{
lcd_write_string(0,0,"WARRNING!!!");
}
}
|