新闻中心

EEPW首页>嵌入式系统>设计应用> 自制51单片机常用头文件(st7920并行方式)

自制51单片机常用头文件(st7920并行方式)

作者: 时间:2016-11-10 来源:网络 收藏
/*--------------------------------------------------------------------------

ST7920.H

本文引用地址://m.amcfsurvey.com/article/201611/317350.htm

The user function is C51.
Copyright (c) 1988-2004 Keil Elektronik GmbH sum zhaojun
All rights reserved.
--------------------------------------------------------------------------*/
//并行方式
#ifndef __ST7920_H__
#define __ST7920_H__

#define uchar unsigned char
#define uint unsigned int

sbit LCD_RS = P3^2; // 指令/数据选择
sbit LCD_RW = P3^3; // 并行读写选择信号
sbit LCD_EN = P3^4; // 并行使能信号
sbit LCD_PSB = P3^5; // 并/串选择端口:H - 并行;L - 串行
sbit LCD_RES = P3^6;// LCD复位,LCD模块自带复位电路。可不接

#define LCD_DATA P1 // MCU P1<------>LCM
/*****************************************************
函 数 名:void Delay_LCD(void)
功 能:5ms延时
入口参数:无
返 回 值:无
****************************************************/
void Delay_LCD(uint t)
{
uint i,j;

for (i=0; i {
for (j=0; j<10; j++)
{
;
}
}
}
/*****************************************************
函 数 名:void WriteCommandLCM()
功 能:向LCM中写入指令
入口参数:WCLCM
返 回 值:无
****************************************************/
void WriteCommandLCM(uchar WCLCM)
{
LCD_RS = 0; // 选择写入指令
LCD_RW = 0; // 写入
LCD_EN = 1; // 使能

LCD_DATA = WCLCM; // 写入指令
LCD_EN = 0;
Delay_LCD(5);
}
/*****************************************************
函 数 名:void WriteDataLCM()
功 能:向LCM1602中写入数据
说 明:将形参WDLCM中的数据写入LCM中
入口参数:WDLCM
返 回 值:无
*****************************************************/
void WriteDataLCM(uchar WDLCM)
{
LCD_RS = 1; // 选择写入数据
LCD_RW = 0; // 写入
LCD_EN = 1; //

LCD_DATA = WDLCM; // 写入数据
LCD_EN = 0;
Delay_LCD(5);
}
/*****************************************************
函 数 名:void LCMInit()
功 能:初始化LCM
说 明:LCM在工作前先要对显示屏初始化,否则模块无法正常工作
入口参数:无
返 回 值:无
*****************************************************/
void LCMInit(void)
{
Delay_LCD(20);
LCD_PSB = 1; // 选择并行传输方式
Delay_LCD(20);

LCD_RES = 0; // 复位,可不要
Delay_LCD(1);
LCD_RES = 1;

WriteCommandLCM(0x30); // 选择基本指令集
Delay_LCD(10);
WriteCommandLCM(0x0c); // 开显示(无游标、反白)
Delay_LCD(10);
WriteCommandLCM(0x01); // 清显示并设地址指针为00H
Delay_LCD(50);
}
/*****************************************************
函 数 名:void DisplayListChar()
功 能:向指点的地址写入字符串
入口参数:x-横坐标,y-纵坐标,s-字符串
返 回 值:无
*****************************************************/
void DisplayListChar(uchar x, uchar y, uchar code *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 - 图片数组名
返 回 值:无
*****************************************************/
//显示图片(图片为公司名称)
void DisplayPicture(uchar code *img)
{
uint j = 0;
uchar x,y;
// -------------- 上半屏 ----------------
for (y=0; 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 = 512; // 下半屏起始数据

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++]);
}
}
}

#endif



评论


技术专区

关闭