#ifndef LCD1602_H_
#define LCD1602_H_
typedef unsigned char uchar;
typedef unsigned int uint;
uchar LCD_Check_busy();
void LCD_Write_Com(unsigned char com);
void LCD_Write_data(unsigned int data);
void LCD_Clear();
void LCDSetCursor(uchar x,uchar y);
void LCDShowStr(uchar x, uchar y,uchar *str);
void LCDOpenCursor();
void LcdCloseCursor();
void LCD_Init();
void Init_Port_LCD();
void Init_Clock();
#define RS BIT0
#define RW BIT1
#define E BIT2
#define data_dir P1DIR
#define data_out P1OUT //|= 0xf0
#define data_in P1IN
#define RS_1 P1OUT |= RS //rs = 1
#define RS_0 P1OUT &= ~RS//rs = 0
#define RW_1 P1OUT |= RW;
#define RW_0 P1OUT &= ~RW;
#define E_1 P1OUT |= E //e = 1
#define E_0 P1OUT &= ~E //e = 0
void delay_1us(uint i)
{
asm("nop");
}
void delay_us(uint i)
{
uint j;
for(j=0;j<i;j++)
delay_1us(j);
}
//??????ms
void delay_1ms()
{
uint i;
for(i=0;i<1140;i++);
}
void delay_ms(uint i)
{
uint j;
for(j=0;j<i;j++)
delay_1ms(j);
}
void Init_Clock()
{
BCSCTL1 = RSEL2 + RSEL1+ RSEL0;
BCSCTL2 = SELM1 + SELS;
}
void Init_Port_LCD()
{
P1SEL |= 0x00;
P1DIR |= 0xf7;
}
uchar LCD_Check_busy()
{
char busy = 0;
RS_0;
RW_1;
E_1;
delay_ms(50);
data_dir &= ~BIT7;
busy = data_in & BIT7;
data_dir |= BIT7;
E_0;
return busy;
}
void LCD_Write_Com(unsigned char com)
{
//while(LCD_Check_busy());
RS_0;
RW_0;
data_out &= 0x0f;
data_out |= com & 0xf0;
E_1;
delay_ms(50);
E_0;
com = com << 4;
data_out &= 0x0f;
data_out |= com & 0xf0;
E_1;
delay_ms(50);
E_0;
}
void LCD_Write_data(unsigned int data)
{
//while(LCD_Check_busy());
RS_1;
RW_0;
data_out &= 0x0f;
data_out |= data & 0xf0;
E_1;
delay_ms(50);
E_0;
data <<= 4;
data_out &= 0x0f;
data_out |= data & 0xf0;
E_1;
delay_ms(5);
E_0;
}
void LCD_Clear()
{
LCD_Write_Com(0x01);
delay_ms(50);
}
void LCDSetCursor(uchar x,uchar y)
{
uchar addr;
if(y == 0)
addr = 0x00 + x;
else
addr = 0x40 +x;
LCD_Write_Com(addr | 0x80);
}
void LCDShowStr(uchar x, uchar y,uchar *str)
{
LCDSetCursor(x,y);
while (*str != ' ')
LCD_Write_data(*str++);
}
void LCDOpenCursor()
{
LCD_Write_Com(0x0F);
}
void LcdCloseCursor()
{
LCD_Write_Com(0x0C);
}
void LCD_Init()
{
delay_ms(500);
E_0 ;//E_1
//RS_1;
delay_ms(500);
LCD_Write_Com(0x28);
delay_ms(50);
LCD_Write_Com(0x28);
delay_ms(50);
LCD_Write_Com(0x28);
delay_ms(50);
//E_1;
//delay_us(10);
//E_0;
delay_ms(50);
LCD_Write_Com(0x28);
delay_ms(500);
LCD_Write_Com(0x0c);
LCD_Write_Com(0x06);
delay_us(40);
LCD_Write_Com(0x01);
delay_ms(5);
LCD_Write_Com(0x80);
}
#endif
#include <msp430.h>
#include "lcd1602.h"
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog
timer
// P1SEL |= 0X00;
//P1DIR |= BIT0;
P1DIR |=(BIT0+BIT1+BIT2+BIT4+BIT5+BIT6+BIT7);
P1OUT &= ~(BIT0|BIT1|BIT2|BIT4|BIT5|BIT6|BIT7);
Init_Clock();
LCD_Init();
LCDShowStr(0,0,"C X H");
LCDShowStr(0,1,"hello word!");
// delay_ms(10000);
//LCDShowStr(0,0,"he he");
//LCDShowStr(0,0,"C X H");
//delay_ms(10000);
}
一开始的时候 是可以显示字符 但是拔电再上电就会乱码 然后我就做了一系列的尝试 在多次写入代码之后显示屏对比度变暗 而且没有字符显示 我以为是判忙函数问题 就把判忙函数注释掉了 问题也没有解决 后来换了快ldc4602 同样 一开始可以显示字符 后面对比度变低 不显示
有偿代做
一周热门 更多>