/**************************************************
*
* LCD1602显示
* cpu:AVRmega16
*
* 描述:4线数据宽度,操作Lcd1602
* 在LCD1602屏幕上第一行显示
* 第二行显示 13652037001
* 如果没有显示请按复位键,并多试几次
* 硬件电路:MSP430F135核心实验板-I型
* 硬件连接:
*
* MSP430与LCD连接信息
* LCD1602,4位接口,即使用D4-D7数据口,D0-D3不接入MCU
* PIN1 --> 地
* PIN2 --> VCC(一定要接+5V)
* PIN3 --> 2K电阻-->地 (一定要接好,否则没有任何显示)
* PIN4 --> RS --> P5.0
* PIN5 --> R/W --> GND
* PIN6 --> EN --> P5.1
* PIN7 --> D0不接
* PIN8 --> D1不接
* PIN9 --> D2不接
* PIN10 --> D3不接
* PIN11 --> D4 --> P4.4
* PIN12 --> D5 --> P4.5
* PIN13 --> D6 --> P4.6
* PIN14 --> D7 --> P4.7
* PIN15 --> VCC(一定要接+5V,如果不要背光可以不接)
* PIN16 --> 地
* 调试器:MSP430FET全系列JTAG仿真器
* 调试软件: IAR Embedded Workbench Version: 3.41A 编译
**************************************************/
#include
#include
////管脚定义////////////////////////////////////////////////////////////////////
#define LCD_EN_PORT P5OUT //以下2个要设为同一个口
#define LCD_EN_DDR P5DIR
#define LCD_RS_PORT P5OUT //以下2个要设为同一个口
#define LCD_RS_DDR P5DIR
#define LCD_DATA_PORT P4OUT //以下3个要设为同一个口
#define LCD_DATA_DDR P4DIR //一定要用高4位
#define LCD_RS BIT0
#define LCD_EN BIT1
#define LCD_DATA BIT4|BIT5|BIT6|BIT7
////预定义函数//////////////////////////////////////////////////////////////////
void LCD_init(void);
void LCD_en_write(void);
void LCD_write_command(unsigned char command);
void LCD_write_data(unsigned char data);
void LCD_set_xy (unsigned char x, unsigned char y);
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data);
void delay_nus(unsigned int n);
void delay_nms(unsigned int n);
void main()
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog
LCD_init();
LCD_write_string(1,0,"goodmcu.cn");
LCD_write_string(2,1,"13652037001");
delay_nms(50);
LCD_write_command(0x01); //清屏
delay_nms(50);
LCD_write_char(2,1,'1');
delay_nms(50);
LCD_write_char(3,1,'3');
delay_nms(50);
LCD_write_char(4,1,'6');
delay_nms(50);
LCD_write_char(5,1,'5');
delay_nms(50);
LCD_write_char(6,1,'2');
delay_nms(50);
LCD_write_char(7,1,'0');
delay_nms(50);
LCD_write_char(8,1,'3');
delay_nms(50);
LCD_write_char(9,1,'7');
delay_nms(50);
LCD_write_char(10,1,'0');
delay_nms(50);
LCD_write_char(11,1,'0');
delay_nms(50);
LCD_write_char(12,1,'1');
}
//LCD液晶操作函数///////////////////////////////////////////////////////////////
//LCD1602液晶初始化
void LCD_init(void)
{
LCD_DATA_DDR|=LCD_DATA; //数据口方向为输出
LCD_EN_DDR|=LCD_EN; //设置EN方向为输出
LCD_RS_DDR|=LCD_RS; //设置RS方向为输出
LCD_write_command(0x28); //4位数据接口
delay_nus(40);
LCD_write_command(0x28); //4位显示
LCD_write_command(0x0c); //显示开
LCD_write_command(0x01); //清屏
delay_nms(2);
}
//液晶使能
void LCD_en_write(void)
{
LCD_EN_PORT|=LCD_EN;
delay_nus(1);
LCD_EN_PORT&=~LCD_EN;
}
//写指令
void LCD_write_command(unsigned char command)
{
delay_nus(16);
LCD_RS_PORT&=~LCD_RS; //RS=0
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=command&0xf0; //写高四位
LCD_en_write();
command=command<<4; //低四位移到高四位
LCD_DATA_PORT&=0x0f; //清高四位
LCD_DATA_PORT|=command&0xf0; //写低四位
LCD_en_write();
}
//写数据
void LCD_write_data(unsigned char data)
{
delay_nus(16);
LCD_RS_PORT|=LCD_RS; //RS=1
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=data&0xf0; //写高四位
LCD_en_write();
data=data<<4; //低四位移到高四位
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=data&0xf0; //写低四位
LCD_en_write();
}
//写地址函数
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else address = 0xc0 + x;
LCD_write_command( address);
}
//LCD在任意位置写字符串
//列x=0~15,行y=0,1
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 ++;
}
}
//LCD在任意位置写字符
//列x=0~15,行y=0,1
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data)
{
LCD_set_xy( X, Y ); //写地址
LCD_write_data( data);
}
//延时函数//////////////////////////////////////////////////////////////////////
//1us延时函数
void delay_1us(void)
{
asm("nop");
}
//N us延时函数
void delay_nus(unsigned int n)
{
unsigned int i;
for (i=0;i
delay_1us();
}
//1ms延时函数
void delay_1ms(void)
{
unsigned int i;
for (i=0;i<1140;i++);
}
//N ms延时函数
void delay_nms(unsigned int n)
{
unsigned int i=0;
for (i=0;i
delay_1ms();
}