📄 st7920.h
字号:
/*--------------------------------------------------------------------------
ST7920.H
The user function is MSP430.
Copyright (c) 2008-2110 Medic information Engineering LAB
Designeer:kuangbin Liu
All rights reserved.
--------------------------------------------------------------------------*/
// 并行方式
#include <msp430x24x.h>
#ifndef __ST7920_H__
#define __ST7920_H__
#define uchar unsigned char
#define uint unsigned int
/* 控制信号*/
#define PORT_CONTROL_LCD_OUT (P2OUT)
#define PORT_CONTROL_LCD_DIR (P2DIR)
//定义控制总线//
/*#define Lcd_PSB (1<<0)
#define Lcd_RS (1<<1)
#define Lcd_RW (1<<2)
#define Lcd_E (1<<3)
*/
#define Lcd_PSB 0x01
#define Lcd_RS 0x02
#define Lcd_RW 0x04
#define Lcd_E 0x08
#define LCD_PSB() {PORT_CONTROL_LCD_OUT |= Lcd_PSB;}//并/串选择端口:H - 并行;L - 串行
#define Lcd_RS_HIGH() {PORT_CONTROL_LCD_OUT |= Lcd_RS;}// 指令/数据选择
#define Lcd_RS_LOW() {PORT_CONTROL_LCD_OUT &= ~(Lcd_RS);}
#define Lcd_RW_HIGH() {PORT_CONTROL_LCD_OUT |= Lcd_RW;}//读写选择位
#define Lcd_RW_LOW() {PORT_CONTROL_LCD_OUT &= ~(Lcd_RW);}
#define Lcd_E_HIGH() {PORT_CONTROL_LCD_OUT |=Lcd_E ;}//高电平有效
#define Lcd_E_LOW() {PORT_CONTROL_LCD_OUT &= ~(Lcd_E);}
#define LCD_DATA_BUS (P1OUT)//数据总线
#define LCD_DATA_READ (P1IN)//read file
/*****************************************************
函 数 名:void Delay_LCD(void)
功 能:5ms延时
入口参数:无
返 回 值:无
****************************************************/
void Delay_LCD(uint t)
{
uint i,j;
for(i=0;i<t;i++)
for(j=0;j<5;j++);
}
/*****************************************************
函 数 名:Check_Busy()
功 能:测试LCD忙
入口参数:
返 回 值:busy
****************************************************/
void Check_Busy()
{
P1DIR = 0x00;
Lcd_RS_LOW() ;
Lcd_RW_HIGH();
Lcd_E_HIGH();
while (LCD_DATA_READ&0x80)
P1DIR = 0xFF;
Lcd_E_LOW();
}
/*****************************************************
函 数 名:void Wait()
功 能:等待LCD到空闲
入口参数:
返 回 值:无
****************************************************/
/*void Wait()
{
while(Check_Busy());
} */
/*****************************************************
函 数 名:void WriteCommandLCM()
功 能:向LCM中写入指令
入口参数:WCLCM
返 回 值:无
****************************************************/
void WriteCommandLCM(uchar WCLCM)
{
//Check_Busy();
Lcd_RS_LOW() ; // 选择写入指令
Lcd_RW_LOW() ; // 写入
Lcd_E_HIGH() ; // 使能
LCD_DATA_BUS = WCLCM; // 写入指令
Delay_LCD(5);
Lcd_E_LOW() ;
Delay_LCD(5);
}
/*****************************************************
函 数 名:void WriteDataLCM()
功 能:向LCM1602中写入数据
说 明:将形参WDLCM中的数据写入LCM中
入口参数:WDLCM
返 回 值:无
*****************************************************/
void WriteDataLCM(uchar WDLCM)
{
// Check_Busy();
Lcd_RS_HIGH() ; // 选择写入数据
Lcd_RW_LOW(); // 写入
Delay_LCD(5);
Lcd_E_HIGH(); //
LCD_DATA_BUS = WDLCM; // 写入数据
Delay_LCD(5);
Lcd_E_LOW() ;
Delay_LCD(5);
}
/*****************************************************
函 数 名:void LCMInit()
功 能:初始化LCM
说 明:LCM在工作前先要对显示屏初始化,否则模块无法正常工作
入口参数:无
返 回 值:无
*****************************************************/
void LCMInit(void)
{
P1DIR = 0xFF;
P2DIR=0XFF;
Delay_LCD(20);
LCD_PSB() ; // 选择并行传输方式
Delay_LCD(20);
//LCD_RES = 0; // 复位,可不要
//Delay_LCD(10);
//LCD_RES = 1;
WriteCommandLCM(0x30); // 选择基本指令集
Delay_LCD(10);
WriteCommandLCM(0x0c); // 开显示(无游标、反白)
Delay_LCD(10);
WriteCommandLCM(0x01); // 清显示并设地址指针为00H
Delay_LCD(50);
WriteCommandLCM(0x06); //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位
}
/*****************************************************
函 数 名:void DisplayListChar()
功 能:向指定的地址写入字符串
入口参数:x-横坐标,y-纵坐标,s-字符串
返 回 值:无
*****************************************************/
void DisplayListChar(uchar x, uchar y, uchar *s)
{
uchar add; // 显示地址
switch (y) // 显示地址计数
{
case 0: add = x + 0x80; break; // 第一行的地址
case 1: add = x + 0x90; break; // 第二行的地址
case 2: add = x + 0x88; break; // 第三行的地址
case 3: add = x + 0x98; break; // 第四行的地址
default: break;
}
WriteCommandLCM(0x30); // 基本指令
WriteCommandLCM(add); // 写入地址
while (*s > 0) // 写入字符串
{
WriteDataLCM(*s);
s++;
Delay_LCD(50);
}
}
/*****************************************************
函 数 名:void DisplayPicture()
功 能:写入一幅128x64的图片
入口参数:img - 图片数组名
返 回 值:无
*****************************************************/
//显示图片(图片为公司名称)
/*绘图显示的步骤
1 关闭绘图显示功能
2 先将垂直的坐标(Y)写入绘图RAM地址
3 再将水平的位元组坐标(X)写入绘图RAM地址
4 将D15-D8写入RAM中
5 将D7-D0写入到RAM中
6 打开绘图显示功能*/
void DisplayPicture(uchar *img)
{ //WriteCommandLCM(0x01);
uint j = 0;
uchar x,y;
// -------------- 上半屏 ----------------
for (y=16; y<32; y++) // 垂直坐标32位
{
for (x=0; x<8; x++) // 水平坐标16字节
{
WriteCommandLCM(0x36); // 扩展指令
WriteCommandLCM(y+0x80); // 先写入垂直坐标
WriteCommandLCM(x+0x80); // 再写入水平坐标
WriteCommandLCM(0x30); // 基本指令
WriteDataLCM(img[j++]); // 连续写入16位数据
WriteDataLCM(img[j++]);
}
}
// --------------- 下半屏 ---------------
j =256; // 下半屏起始数据
for (y=0; y<32; y++) // 垂直坐标32位
{
for (x=0; x<8; x++) // 水平坐标16字节
{
WriteCommandLCM(0x36); // 扩展指令
WriteCommandLCM(y+0x80); // 先写入垂直坐标
WriteCommandLCM(x+0x88); // 再写入水平坐标
WriteCommandLCM(0x30); // 基本指令
WriteDataLCM(img[j++]); // 连续写入16位数据
WriteDataLCM(img[j++]);
}
}
}
void ClearGdram(void)
{
uchar x,y,i;
for(i=0;i<9;i=i+8)
for(y=0;y<=15;y++)
{
for(x=0;x<=7;x++)
{
WriteCommandLCM(0x34);
WriteCommandLCM(0x80+y);
//ReadBusy();
Delay_LCD(10);
WriteCommandLCM(0x80+x+i);
//ReadBusy();
Delay_LCD(10);
WriteDataLCM(0x00);
//ReadBusy();
Delay_LCD(10);
WriteDataLCM(0x00);
WriteCommandLCM(0x36);
}
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -