新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 自制51單片機常用頭文件(st7920串行方式)

自制51單片機常用頭文件(st7920串行方式)

作者: 時間:2016-11-10 來源:網(wǎng)絡(luò) 收藏
/*--------------------------------------------------------------------------

ST7920.H

本文引用地址:http://butianyuan.cn/article/201611/317351.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 uint unsigned int
#define uchar unsigned char

//引腳定義
#define LCD_CS P3_2// 片選 高電平有效 單片LCD使用時可固定高電平
#define LCD_SID P3_3// 數(shù)據(jù)
#define LCD_CLK P3_4// 時鐘
#define LCD_PSB P3_5// 低電平時表示用串口驅(qū)動,可固定低電平
#define LCD_RST P3_6// LCD復(fù)位,LCD模塊自帶復(fù)位電路。可不接
//#define LCD_BACK P2_6 // LCD背光控制

/*****************************************************
函 數(shù) 名:void Delay_LCD(uint Number)
功 能:LCD延時
說 明:在送出一個指令前不檢查BF標志,則在前一個指令和這個指令中間必須延遲一段較長的時間
入口參數(shù):Number
返 回 值:無
*****************************************************/
void Delay_LCD(uint Number)
{
uint i,j;

for (i=0; i {
for(j=0; j<10; j++)
{
;
}
}
}

/*****************************************************
函 數(shù) 名:void Send_byte(uchar Byte)
功 能:發(fā)送一個字節(jié)
入口參數(shù):Byte
返 回 值:無
*****************************************************/
void Send_byte(uchar Byte)
{
uchar i;

for (i=0; i<8; i++)
{
LCD_SID = Byte&0x80; // 取出最高位 寫入串行數(shù)據(jù)
LCD_CLK = 1; // 串行同步時鐘信號
LCD_CLK = 0;
Byte <<= 1; // 左移
}
}
/*****************************************************
函 數(shù) 名:void WriteCommandLCM()
功 能:向LCM中寫入指令
入口參數(shù):WCLCM
返 回 值:無
****************************************************/
void WriteCommandLCM(uchar WCLCM)
{
uchar Start_data,Hdata,Ldata;

Start_data = 0xf8; // 寫指令 11111000

Hdata = WCLCM&0xf0; // 取高四位 DDDD0000
Ldata = (WCLCM << 4) & 0xf0; // 取低四位 0000DDDD

Send_byte(Start_data); // 發(fā)送起始信號 第1字節(jié)-格式:1111ABC
Delay_LCD(5); // 延時是必須的

Send_byte(Hdata); // 發(fā)送高四位 第2字節(jié)-格式:DDDD0000
Delay_LCD(1); // 延時是必須的

Send_byte(Ldata); // 發(fā)送低四位 第3字節(jié)-格式:0000DDDD
Delay_LCD(1); // 延時是必須的
}
/*****************************************************
函 數(shù) 名:void WriteDataLCM()
功 能:向LCM1602中寫入數(shù)據(jù)
說 明:將形參WDLCM中的數(shù)據(jù)寫入LCM中
入口參數(shù):WDLCM
返 回 值:無
*****************************************************/
void WriteDataLCM(uchar WDLCM)
{
uchar Start_data,Hdata,Ldata;

Start_data = 0xfa; // 寫數(shù)據(jù) 11111010

Hdata = WDLCM & 0xf0; // 取高四位 DDDD0000
Ldata = (WDLCM << 4) & 0xf0; // 取低四位 0000DDDD

Send_byte(Start_data); // 發(fā)送起始信號 第1字節(jié)-格式:1111ABC
Delay_LCD(5); // 延時是必須的

Send_byte(Hdata); // 發(fā)送高四位 第2字節(jié)-格式:DDDD0000
Delay_LCD(1); // 延時是必須的

Send_byte(Ldata); // 發(fā)送低四位 第3字節(jié)-格式:0000DDDD
Delay_LCD(1); // 延時是必須的
}

/*****************************************************
函 數(shù) 名:void Lcdinit(void)
功 能:LCD初始化
入口參數(shù):無
返 回 值:無
*****************************************************/
void Lcdinit(void)
{
Delay_LCD(10); // 啟動等待,等LCM講入工作狀態(tài)
LCD_PSB = 0; // 串口驅(qū)動模式
LCD_RST = 0;

Delay_LCD(1);
LCD_RST = 1; // 復(fù)位LCD
LCD_CS = 1; // 片選

WriteCommandLCM(0x30); // 8 位介面,基本指令集
WriteCommandLCM(0x0c); // 顯示打開,光標關(guān),反白關(guān)
WriteCommandLCM(0x01); // 清屏,將DDRAM的地址計數(shù)器歸零
}

/*****************************************************
函 數(shù) 名:void DisplayListChar()
功 能:向指點的地址寫入字符串
入口參數(shù):x-橫坐標,y-縱坐標,s-字符串
返 回 值:無
*****************************************************/
void DisplayListChar(uchar x, uchar y, uchar code *s)
{
uchar add; // 顯示地址

switch (y) // 顯示地址計數(shù)
{
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); // 8位介面,基本指令集
WriteCommandLCM(add); // 寫入地址

while (*s > 0) // 寫入字符串
{
WriteDataLCM(*s);
s++;
Delay_LCD(50);
}
}

/*****************************************************
函 數(shù) 名:void DisplayPicture(uint code *img)
功 能:在LCD上顯示圖形
說 明:在LCD上顯示圖形時,由于ST7920的特殊性應(yīng)分兩屏控制
入口參數(shù):*img
返 回 值:無
*****************************************************/
//圖形方式
void DisplayPicture(uint code *img)
{
uchar Line,Row; // Line為行地址;Row為列地址
uint regist = 0; // 圖形地址寄存器

WriteCommandLCM(0x36); // 圖形方式,擴展指令
// -------------- 上半屏 ----------------
for (Line=0; Line<32; Line++)
{
WriteCommandLCM(0x80 + Line); // 寫入行地址
WriteCommandLCM(0x80); // 寫入列

for (Row=0; Row<16; Row++)
{
WriteDataLCM(img[regist++]); // 寫入圖片數(shù)據(jù)
}
}
// --------------- 下半屏 ---------------
regist=512; // 下半屏起始數(shù)據(jù)

for (Line=0; Line<32; Line++)
{
WriteCommandLCM(0x80 + Line);// 寫入行地址
WriteCommandLCM(0x88);// 寫入列

for (Row=0; Row<16; Row++)
{
WriteDataLCM(img[regist++]); // 寫入圖片數(shù)據(jù)
}
}

WriteCommandLCM(0x30); // 基本指令
}

/*****************************************************
函 數(shù) 名:void main(void)
功 能:主函數(shù)
入口參數(shù):無
返 回 值:無
*****************************************************/
void main(void)
{

Lcdinit(); // 初始化LCD

while(1)
{

DisplayListChar(0,0,"zhaojun");
}

}

#endif



評論


技術(shù)專區(qū)

關(guān)閉