说明:
单片机是华邦的78E516B,液晶屏是T6963控制器的240×128点阵的,单片机总线方式访问。基本功能见各个函数的说明:
#include "W78E516.h"
#include
#include
#include "LCD.h"
//#include "matrix.h"
//#include "main.h"
//------------------------------------------------------------
// 函数: 无参数指令写入函数
// 入口参数:
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void LcdWriteCommand_withZero(unsigned char Command)
{
//Check_CDRW();
n_NOP();
LCD_COMMAND = Command;
}
//------------------------------------------------------------
// 函数: 1参数指令写入函数
// 入口参数:
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void LcdWriteCommand_withOne(unsigned char Par_data,unsigned char Command)
{
//Check_CDRW();
n_NOP();
LCD_DATA = Par_data;
//Check_CDRW();
n_NOP();
LCD_COMMAND = Command;
}
//------------------------------------------------------------
// 函数: 2参数指令写入函数
// 入口参数:
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void LcdWriteCommand_withTwo(unsigned char Par_data1,unsigned char Par_data2,unsigned char Command)
{
//Check_CDRW();
n_NOP();
LCD_DATA = Par_data1;
//Check_CDRW();
n_NOP();
LCD_DATA = Par_data2;
//Check_CDRW();
n_NOP();
LCD_COMMAND = Command;
}
//------------------------------------------------------------
// 函数: 检测LCD状态
// 入口参数:
// 返回值 :
// 函数说明:
//------------------------------------------------------------
//状态位ST1,ST0 判断函数,读写指令和读写数据
void Check_CDRW(void)
{
while((LCD_COMMAND & 0x03) != 0x03)
{
}
}
//状态位ST2,ST3 判断函数数据自动写
void CheckAutoW(void)
{
while((LCD_COMMAND & 0x08) != 0x08)
{
}
}
//状态位ST5 判断控制器可用
void Check_ERROR(void)
{
while((LCD_COMMAND & 0x20) != 0x20)
{
}
}
//------------------------------------------------------------
// 函数: 单数据写入函数
// 入口参数:
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void LcdWriteData(unsigned char Par_data)
{
//Check_CDRW();
n_NOP();
LCD_DATA = Par_data;
}
//------------------------------------------------------------
// 函数: LCD初始化函数
// 入口参数:
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void Lcd_Init(void)
{
LcdWriteCommand_withTwo(0x00,0x00,0x40); //设置文本显示区首地址
LcdWriteCommand_withTwo(0x1f,0x00,0x41); //设置文本显示区宽度
LcdWriteCommand_withTwo(0x00,0x08,0x42); //设置图形显示区首地址0x0800
LcdWriteCommand_withTwo(0x1f,0x00,0x43); //设置图形显示区宽度
LcdWriteCommand_withZero(0xA0); //设置光标形状 8x8方块
LcdWriteCommand_withZero(0x80); //显示方式设置 文本and图形(异或)
LcdWriteCommand_withZero(0x98); //显示开关设置 文本关,图形开,光标闪烁关
}
//------------------------------------------------------------
// 函数: 设定显示RAM地址函数
// 入口参数: X横坐标-对应第几列(0-30),Y纵坐标-对应第几行(0-128)
// 返回值 :
// 函数说明: 指定在液晶的什么位置进行操作
//------------------------------------------------------------
void LocateXY(unsigned char x,unsigned char y,unsigned char mode)
{
unsigned int temp;
temp = 0x1f*y + x;
if(mode) //mode=1为Graphic
{ //如果图形模式要加上图形区首地址0x0800
temp += 0x0800;
}
LcdWriteCommand_withTwo(temp&0xff,temp/256,0x24);
}
//------------------------------------------------------------
// 函数: 显示一个ASCII码函数
// 入口参数: X横坐标-对应第几列(0-30),Y纵坐标-对应第几行(0-128)
// 返回值 :
// 函数说明: (8X8,液晶内部字符集)
// Char_table参照数据手册,液晶内部字符表
//------------------------------------------------------------
void DisplayASCII(unsigned char x,unsigned char y,unsigned char Char_table)
{
LocateXY(x,y,TXT_Mode);
LcdWriteCommand_withOne(Char_table,0xC4);
}
//------------------------------------------------------------
// 函数: 指定位置显示一个中文字符(16×16)
// 入口参数: *WrodTable指向待写入汉字的字模数组
// X横坐标-对应第几列(0-30),Y纵坐标-对应第几行(0-16)
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void DisplayChineseWord(unsigned char *WrodTable,unsigned X,
unsigned char Y,unsigned char Background)
{
unsigned char j;
for(j=0;j<16;j++) //每个循环写两个字节数据(一行上的)
{
LocateXY(X,Y*8+j,Graphic_Mode); //指定待写位置
LcdWriteCommand_withZero(0xb0); //进入自动写,地址自动加1
//CheckAutoW();
n_NOP();
if(Background)
{
LcdWriteData(~WrodTable[2*j]);
}
else
{
LcdWriteData(WrodTable[2*j]);
}
//CheckAutoW();
n_NOP();
if(Background)
{
LcdWriteData(~WrodTable[2*j+1]);
}
else
{
LcdWriteData(WrodTable[2*j+1]);
}
LcdWriteCommand_withZero(0xB2); //退出自动写
}
}
//------------------------------------------------------------
// 函数: 指定位置显示一个英文字符(16×8)
// 入口参数: *WrodTable指向待写入汉字的字模数组
// X横坐标-对应第几列(0-30),Y纵坐标-对应第几行(0-16)
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void DisplayEnglishWord(unsigned char *WrodTable,unsigned X,unsigned char Y,unsigned char Background)
{
unsigned char i="0",j;
for(j=0;j<16;j++) //每个循环写一个字节数据
{
LocateXY(X,Y*8+j,Graphic_Mode); //指定待写位置
LcdWriteCommand_withZero(0xb0); //进入自动写,地址自动加1
//CheckAutoW();
n_NOP();
if(Background)
{
LcdWriteData(~WrodTable[j]);
}
else
{
LcdWriteData(WrodTable[j]);
}
//i += 1;
LcdWriteCommand_withZero(0xB2); //退出自动写
}
}
//------------------------------------------------------------
// 函数: 指定位置显示一个英文字符(8×8)
// 入口参数: *WrodTable指向待写入汉字的字模数组
// X横坐标-对应第几列(0-30),Y纵坐标-对应第几行(0-16)
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void DisplayRrow(unsigned char *WrodTable,unsigned X,unsigned char Y,unsigned char Background)
{
unsigned char j;
for(j=0;j<8;j++) //每个循环写一个字节数据
{
LocateXY(X,Y*8+j,Graphic_Mode); //指定待写位置
//LcdWriteCommand_withZero(0xb0); //进入自动写,地址自动加1
//CheckAutoW();
//n_NOP();
if(Background)
{
LcdWriteData(~WrodTable[j]);
}
else
{
LcdWriteData(WrodTable[j]);
}
//i += 1;
//LcdWriteCommand_withZero(0xb2); //退出自动写
n_NOP();
LcdWriteCommand_withOne(WrodTable[j],0xc4);
}
}
//------------------------------------------------------------
// 函数: LCD屏填充函数
// 入口参数: Fill_char填充的数
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void Lcd_Fill(unsigned char X_start,unsigned char X_end,
unsigned char Y_start,unsigned char Y_end,
unsigned char Fill_char)
{ unsigned char i,j;
for (i=0;i<=(Y_end-Y_start+1)*8;i++)
{
LocateXY(X_start,Y_start*8+i,Graphic_Mode); //指定待写位置
LcdWriteCommand_withZero(0xb0); //进入自动写,地址自动加1
for (j=0;j<=(X_end-X_start+1);j++)
{
//CheckAutoW();
n_NOP();
LcdWriteData(Fill_char);
}
LcdWriteCommand_withZero(0xB2); //退出自动写
}
}
//------------------------------------------------------------
// 函数: 指定位置显示一个中文字符(12×12)
// 入口参数: *WrodTable指向待写入汉字的字模数组
// X横坐标-对应第几列(0-30),Y纵坐标-对应第几行(0-16)
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void DisplayChineseWord12(unsigned char *WrodTable,unsigned X,
unsigned char Y,unsigned char Background)
{
unsigned char j;
for(j=0;j<12;j++) //每个循环写两个字节数据(一行上的)
{
LocateXY(X,Y*8+j,Graphic_Mode); //指定待写位置
LcdWriteCommand_withZero(0xb0); //进入自动写,地址自动加1
//CheckAutoW();
n_NOP();
if(Background)
{
LcdWriteData(~WrodTable[2*j]);
}
else
{
LcdWriteData(WrodTable[2*j]);
}
//CheckAutoW();
n_NOP();
if(Background)
{
LcdWriteData(~WrodTable[2*j+1]);
}
else
{
LcdWriteData(WrodTable[2*j+1]);
}
LcdWriteCommand_withZero(0xB2); //退出自动写
}
}
//------------------------------------------------------------
// 函数: 指定位置显示一个英文字符(12×6)
// 入口参数: *WrodTable指向待写入汉字的字模数组
// X横坐标-对应第几列(0-30),Y纵坐标-对应第几行(0-16)
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void DisplayEnglishWord12(unsigned char *WrodTable,unsigned X,
unsigned char Y,unsigned char Background)
{
unsigned char j;
for(j=0;j<12;j++) //每个循环写1个字节数据(一行上的)
{
LocateXY(X,Y*8+j,Graphic_Mode); //指定待写位置
//LcdWriteCommand_withZero(0xb0); //进入自动写,地址自动加1
//CheckAutoW();
n_NOP();
if(Background)
{
LcdWriteData(~WrodTable[j]);
}
else
{
LcdWriteData(WrodTable[j]);
}
//LcdWriteCommand_withZero(0xB2); //退出自动写
n_NOP();
LcdWriteCommand_withOne(WrodTable[j],0xc4);
}
}
//------------------------------------------------------------
// 函数: 延时子函数
// 入口参数: 延时常数
// 返回值 : 无
// 函数说明: 软件延时,不要求精确
//------------------------------------------------------------
void Delay(unsigned char m)
{
unsigned char i;
unsigned char j;
for( i = 0; i < m; i++)
{
for( j = 0; j < 200; j++)
{
}
}
}
//---------短延时------------------
void n_NOP(void)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
//------------------------------------------------------------
// 函数: LCD画水平的直线
// 入口参数: Fill_char填充的数
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void Lcd_Line_H(unsigned X_start,unsigned X_end,unsigned Y,unsigned char fill_char)
{
unsigned char j;
//for (i=X_start;i //{
LocateXY(X_start,Y*8,Graphic_Mode);
LcdWriteCommand_withZero(0xb0); //进入自动写,地址自动加1
for (j=0;j<=(X_end-X_start+1);j++)
{
//CheckAutoW();
n_NOP();
LcdWriteData(fill_char);
}
LcdWriteCommand_withZero(0xB2); //退出自动
//}
}
//------------------------------------------------------------
// 函数: LCD画垂直的直线
// 入口参数: Fill_char填充的数
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void Lcd_Line_L(unsigned X,unsigned Y_start,unsigned Y_end,unsigned char fill_char)
{
unsigned char i;
for (i=Y_start;i {
LocateXY(X,i,Graphic_Mode);
LcdWriteCommand_withOne(fill_char,0xc4);
}
}
//------------------------------------------------------------
// 函数: LCD画任意两点间的直线
// 入口参数:
// 返回值 :
// 函数说明:
//------------------------------------------------------------
void Lcd_Line(unsigned char X_start,unsigned Y_start,unsigned char X_end,unsigned char Y_end)
{
unsigned char temp,temp_data,addr;
unsigned char i,j,m;
if ((Y_end - Y_start)>=(X_end - X_start)*8)
{
temp = (Y_end - Y_start)/((X_end - X_start)*8);
temp_data = 0x80;
addr = X_start;
for (i=Y_start;i {
for (j=0;j {
LocateXY(addr,i,Graphic_Mode);
n_NOP();
LcdWriteCommand_withOne(temp_data,0xc4);
i += 1;
}
if (temp_data == 0x01)
{
addr += 1;
temp_data = 0x80;
}
else
{
temp_data >>= 1;
}
}
}
else
{
temp_data = 0x80;
temp = ((X_end - X_start)*8)/(Y_end - Y_start);
for (i=1;i {
temp_data |= temp_data>>1;
}
addr = Y_start;
for (i=X_start;i {
for (j=0;j {
LocateXY(i,addr,Graphic_Mode);
n_NOP();
LcdWriteCommand_withOne(temp_data,0xc4);
addr += 1;
if (temp_data < (0xff>>(8-temp)))
{
temp_data=0x80;
for (m=1;m {
temp_data |= temp_data>>1;
}
i = i+1;
}
else
{
temp_data >>= temp ;
}
}
}
}
}
LCD的头文件:
#ifndef __LCD_H__
#define __LCD_H__
//
// 240×128 ,T6963
//
//Register Set
#define Cursor_pointer 0x21 //00100001 X address Y address Cursor pointer set
#define Off_register 0x22 //00100010 Data 00H Off register
#define Address_pointer 0x24 //00100100 Low address High address Address pointer set
//Control Word set
#define Text_home_addr 0x40 //01000000 Low address High address Text home address set
#define Text_area 0x41 //01000001 Columns 00H Text area set
#define Graphic_home_addr 0x42 //01000010 Low address High address Graphic home address set
#define Graphic_area 0x43 //01000011 Columns 00H Graphic area set
//Mode set
#define Or_mode 0x80 //1000x000 - - "OR" mode
#define Exor_mode 0x81 //1000x001 - - "EXOR" mode
#define And_mode 0x83 //1000x011 - - "AND" mode
#define Text_attr_mode 0x84 //1000x100 - - "Text attribute" mode
#define In_CGROM_mode 0x80 //10000xxx - - Internal CGROM mode
#define Ex_CGROM_mode 0X88 //10001xxx - - External CGRAM mode
//Display Mode
#define Display_off 0x90 //10010000 - - Display off
#define CursorOn_BlinkOff 0x92 //1001xx10 - - Cursor on, blink off
#define CursorOn_BlinkOn 0x93 //1001xx11 - - Cursor on, blink on
#define TextOn_GraphicOff 0x94 //100101xx - - Text on, graphic off
#define TextOff_GraphicOn 0x98 //100110xx - - Text off, graphic on
#define TextOn_GraphicOn 0x9c //100111xx - - Text on, graphic on
//Cursor Pattern Select
#define Line_1_cursor 0xa0 //10100000 - - 1 line cursor
#define Line_2_cursor 0xa1 //10100001 - - 2 line cursor
#define Line_3_cursor 0xa2 //10100010 - - 3 line cursor
#define Line_4_cursor 0xa3 //10100011 - - 4 line cursor
#define Line_5_cursor 0xa4 //10100100 - - 5 line cursor
#define Line_6_cursor 0xa5 //10100101 - - 6 line cursor
#define Line_7_cursor 0xa6 //10100110 - - 7 line cursor
#define Line_8_cursor 0xa7 //10100111 - - 8 line cursor
//Data auto Read/write
#define Data_auto_write 0xb0 //10110000 - - Data auto write set
#define Data_auto_read 0xb1 //10110001 - - Data auto read set
#define Auto_reset 0xb2 //10110010 - - Auto reset
//Data read Write
#define DW_ADP_I 0xc0 //11000000 Data - Data write and ADP increment
#define DR_ADP_I 0xc1 //11000001 - - Data read and ADP increment
#define DW_ADP_D 0xc2 //11000010 Data - Data write and ADP decrement
#define DR_ADP_D 0xc3 //11000011 - - Data read and ADP decrement
#define DW_ADP_N 0xc4 //11000100 Data - Data write and ADP no variable
#define DR_ADP_N 0xc5 //11000101 - - Data read and ADP no variable
//Screen peek
#define Screen_peek 0xe0 //11100000 - - Screen peek
//Screen copy
#define Screen_copy 0xe8 //11101000 - - Screen copy
#define Bit_reset 0xf0 //11110XX - - Bit reset
#define Bit_set 0xf8 //11111XXX - - Bit set
#define Bit_0 0xf0 //1111X000 - - Bit 0( LSB )
#define Bit_1 0xf1 //1111X001 - - Bit 1
#define Bit_2 0xf2 //1111X010 - - Bit 2
#define Bit_3 0xf3 //1111X011 - - Bit 3
#define Bit_4 0xf4 //1111X100 - - Bit 4
#define Bit_5 0xf5 //1111X101 - - Bit 5
#define Bit_6 0xf6 //1111X110 - - Bit 6
#define Bit_7 0xf7 //1111X111 - - Bit 7( MSB )
#define LCD_DATA XBYTE[0X8000]
#define LCD_COMMAND XBYTE[0X8001]
#define TXT_Mode 0
#define Graphic_Mode 1
void CheckAutoW(void);
void n_NOP(void);
void LcdWriteCommand_withZero(unsigned char Command);
void LcdWriteCommand_withOne(unsigned char Par_data,unsigned char Command);
void LcdWriteCommand_withTwo(unsigned char Par_data1,unsigned char Par_data2,unsigned char Command);
void LcdWriteData(unsigned char Par_data);
void Lcd_Init(void);
void Check_CDRW(void);
void LocateXY(unsigned char x,unsigned char y,unsigned char mode);
void DisplayASCII(unsigned char x,unsigned char y,unsigned char Char_table);
void DisplayChineseWord(unsigned char *WrodTable,unsigned Y,unsigned char X,unsigned char Background);
void DisplayChineseWord12(unsigned char *WrodTable,unsigned X,unsigned char Y,unsigned char Background);
void DisplayEnglishWord(unsigned char *WrodTable,unsigned Y,unsigned char X,unsigned char Background);
void DisplayRrow(unsigned char *WrodTable,unsigned X,unsigned char Y,unsigned char Background);
void Lcd_Fill(unsigned char X_start,unsigned char X_end,unsigned char Y_start,unsigned char Y_end,unsigned char Fill_char);
void LCD_String(unsigned char *String,unsigned char X_add,unsigned char Y_add,unsigned char Back_ground);
void DisplayEnglishWord12(unsigned char *WrodTable,unsigned X,unsigned char Y,unsigned char Background);
void Lcd_Line_H(unsigned X_start,unsigned X_end,unsigned Y,unsigned char fill_char);
void Lcd_Line_L(unsigned X,unsigned Y_start,unsigned Y_end,unsigned char fill_char);
void Lcd_Line(unsigned char X_start,unsigned Y_start,unsigned char X_end,unsigned char Y_end);
#endif
文章评论(0条评论)
登录后参与讨论