#include "msp430g2553.h"
#define uint unsigned int
#define uchar unsigned char
/*****************************************
RS--->>P2.0
RW--->>P2.1
E---->>P2.2
PSB-->>P2.3
PAUSE>>P2.4
RES-->>P2.5
******************************************/
#define DataPort P1OUT //MCU P2<------> LCM
/*
void DelayMs(uint x)
{
uint a,b;
for(a=0;a<x;a++)
for(b=0;b<1000;b++);
}
*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
void DelayUs(uint x)
{
uint a,b;
for(a=0;a<x;a++)
for(b=0;b<2;b++);
}
//检测忙位
void Check_Busy()
{
P2OUT&=0xFE; //RS=0
P2OUT |=0x02; //RW=1
P2OUT |=0x04; //E=1
DataPort=0xFF; //
P1DIR=0x00; //数据端口设为输入状态
while((P2IN&0x80)==0x80);//忙则等待
P1DIR=0xFF; //数据端口还原为输出状态
P2OUT&=0xFB;
}
/*---------------------写命令------------------------*/
void Write_Cmd(unsigned char Cmd)
{
Check_Busy();
P2OUT&=0xFE; //RS=0;
P2OUT&=0xFD; //RW=0;
P2OUT |=0x04; //E=1;
DataPort=Cmd;
DelayUs(5);
P2OUT&=0xFB; //E=0;
DelayUs(5);
}
/*------------------------------------------------
写数据
------------------------------------------------*/
void Write_Data(unsigned char Data)
{
Check_Busy();
P2OUT |=0x01; //RS=1;
P2OUT&=0xFD; //RW=0;
P2OUT |=0x04; //E=1;
DataPort=Data;
DelayUs(5);
P2OUT&=0xFB; //E=0;
DelayUs(5);
}
void Init_ST7920()
{
DelayMs(40); //大于40MS的延时程序
P2OUT |=0x08; //PSB=1; 设置为8BIT并口工作模式
DelayMs(1); //延时
P2OUT&=0xDF; //RES=0; 复位
DelayMs(1); //延时
P2OUT |=0x20; //RES=1; 复位置高
DelayMs(10);
Write_Cmd(0x30); //选择基本指令集
DelayUs(50); //延时大于100us
Write_Cmd(0x30); //选择8bit数据流
DelayUs(20); //延时大于37us
Write_Cmd(0x0c); //开显示(无游标、不反白)
DelayUs(50); //延时大于100us
Write_Cmd(0x01); //清除显示,并且设定地址指针为00H
DelayMs(15); //延时大于10ms
Write_Cmd(0x06); //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位,光标从右向左加1位移动
DelayUs(50); //延时大于100us
}
/*------------------------------------------------
显示字符串
x:横坐标值,范围0~8
y:纵坐标值,范围1~4
------------------------------------------------*/
void LCD_PutString(unsigned char x,unsigned char y,char *s)
{
switch(y)
{
case 1: Write_Cmd(0x80+x);break;
case 2: Write_Cmd(0x90+x);break;
case 3: Write_Cmd(0x88+x);break;
case 4: Write_Cmd(0x98+x);break;
default:break;
}
while(*s>0)
{
Write_Data(*s);
s++;
DelayUs(100); //
}
}
int main(void)
{
char* string1="12864LCD Demo";
char* string2="Demo for Msp430";
char* string3="Launch pad G2553";
char* string4="电控》微电》哈哈》";
WDTCTL = WDTPW + WDTHOLD;//关闭看门狗
P1DIR=0xFF;//配置P1为输出,作为数据端口
P2DIR=0xFF;//配置P2为输出,作为控制端口
Init_ST7920();
while(1)
{
LCD_PutString(0,1,string1);
DelayUs(20);
LCD_PutString(0,2,string2);
DelayUs(20);
LCD_PutString(0,3,string3);
DelayUs(20);
LCD_PutString(0,4,string4);
DelayUs(20);
}
}
/********************************
上边是并行,下边是串行
*******************************/
#include "msp430g2553.h"
#define CS1 P1OUT |=0x01
#define CS0 P1OUT &=0x01
#define SID1 P1OUT |=0x02
#define SID0 P1OUT &=~0x02
#define SCLK1 P1OUT |=0x04
#define SCLK0 P1OUT &=~0x04
void DelayUs2x(unsigned char t)
{
while(--t);
}
void delay_1ms(unsigned char t)
{
while(t--)
{
//大致延时1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
void sendbyte(unsigned char zdata)
{
unsigned int i;
for(i=0; i<8; i++)
{
if((zdata << i) & 0x80)
{
SID1;
}
else
{
SID0;
}
SCLK0;
SCLK1;
}
}
void write_com(unsigned char cmdcode)
{
CS1;
sendbyte(0xf8); //1 1 1 1 RS RW 0 写操作RW=0 11111000写数据 11111010写指令
sendbyte(cmdcode & 0xf0);
sendbyte((cmdcode << 4) & 0xf0);
delay_1ms(1);
CS0;
}
void write_data(unsigned char Dispdata)
{
CS1;
sendbyte(0xfa);
sendbyte(Dispdata & 0xf0);
sendbyte((Dispdata << 4) & 0xf0);
delay_1ms(1);
CS0;
}
void Put_String(unsigned int x,unsigned int y,unsigned char* s)
{
switch(y)
{
case 1: write_com(0x80+x);break;
case 2: write_com(0x90+x);break;
case 3: write_com(0x88+x);break;
case 4: write_com(0x98+x);break;
default:break;
}
while(*s>0)
{
write_data(*s);
s++;
DelayUs2x(50);
}
}
void lcdinit()
{
delay_1ms(200);
write_com(0x30); //功能设定:基本指令集
delay_1ms(20);
write_com(0x0c); //显示状态:整体显示,游标关
delay_1ms(20);
write_com(0x01); //清空显示
delay_1ms(200);
}
int main( void )
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
P1DIR=0xFF;
P1OUT=0x00;
lcdinit();
while(1)
{
Put_String(1,1,"speed");
}
}