#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int //定义在DotMatrix.c中的点阵、数字、符号等编码
extern uchar code Word_String[][32];
extern uchar code Keyboard_Chars[][16];
extern uchar code KeyPosTable[]; //KeyPad.c中的键盘扫描函数
extern uchar KeyScan(); //定义在LCD_12864.c中的相关液晶显示函数
void LCD_Initialize();
void Display_A_Char(uchar,uchar,uchar *);
void Display_A_WORD(uchar,uchar,uchar *);
void Display_A_WORD_String(uchar,uchar,uchar,uchar *); //键盘开启标志,其值由外部中断0控制
bit KeyPressDown=0;
uchar T_Count=0;
sbit SPK=P3^7;
//蜂鸣器发声//
void Beep()
{
uchar i,x=20;
while(x--)
{
for(i=0; i<120; i++); SPK=~SPK;
}}
//主程序//
void main()
{
uchar i;
LCD_Initialize(); //初始化LCD
for(i=0; i<7; i++) //从第一页开始,左边距16点,显示7个16*16点阵的中文提示信息
Display_A_WORD_String(1,16*(i+1),1,Word_String[i]);
P1=0x0F;
IE=0x83; //允许外部0和定时器0中断
IT0=1; //设为下降沿中断方式,外部中断0用于启停键盘扫描处理
TH0=(65536-50000)/256; //50ms定时
TL0=(65536-50000)%256;
while(1)
{ //如果有按键按下则处理按键
if(KeyPressDown==1)
{
Beep();
KeyPressDown=0;
Display_A_Char(4,55,Keyboard_Chars[KeyPosTable[KeyScan()]]);
TR0=0;
}}}
//外部中断0控制消抖延时//
void EX0_INT() interrupt 0
{
TR0=1; //开启定时器0,延时300ms消抖
}
//定时器0用于消抖并确认有键按下,启动主程序中的按键扫描 //
void T0_INT() interrupt 1
{
if(++T_Count==6) //50*6=300ms 延时抖动
{
T_Count=0;
KeyPressDown=1;
}
TH0=(65536-50000)/256; //50ms定时
TL0=(65536-50000)%256;
}
///LCD_12864.c //
//名称:12864LCD显示驱动软件//
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_DB_PORT P0 //液晶DB0~DB7
#define LCD_START_ROW 0xC0 //起始行
#define LCD_PAGE 0xB8 //页指令
#define LCD_COL 0x40 //列指令
//液晶引脚定义
sbit DI=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit CS1=P2^3;
sbit CS2=P2^4;
sbit RST=P2^5;
//检查LCD是否忙 //
bit LCD_Check_Busy()
{
LCD_DB_PORT=0xFF;
RW=1;_nop_();DI=0;
E=1; _nop_(); E=0;
return (bit)(P0&0x80);
}
//向LCD发送命令//
void LCD_Write_Command(uchar c)
{
while(LCD_Check_Busy());
LCD_DB_PORT=0xFF; RW=0; _nop_(); DI=0;
LCD_DB_PORT=c; E=1;_nop_(); E=0;
}
//向LCD发送数据//
void LCD_Write_Data(uchar d)
{
while(LCD_Check_Busy());
LCD_DB_PORT=0xFF; RW=0; _nop_(); DI=1;
LCD_DB_PORT=d; E=1; _nop_(); E=0;
}
//初始化LCD//
void LCD_Initialize()
{
CS1=1; CS2=1;
LCD_Write_Command(0x38); //8位形式,2行字符
LCD_Write_Command(0x0F); //开显示
LCD_Write_Command(0x01); //清屏
LCD_Write_Command(0x06); //画面不动光标右移
LCD_Write_Command(LCD_START_ROW); //设置起始行
}
//通用显示函数,//
//
//从第P页第L列开始显示W字节数据,数据在r所指向的缓冲
//每字节8位是垂直显示的,高位在下,低位在上
//每个8*128的矩形区域为一页(每页分左半页与右半页)
//整个LCD又由64*64的左半屏和右半屏构成
//
void Common_Show(uchar P,uchar L,uchar W,uchar *r)
{
uchar i;
if(L<64) //显示在左半屏或者左右半屏
{
CS1=1; CS2=0;
LCD_Write_Command(LCD_PAGE+P);//这里在指导书里有个“i9:”
LCD_Write_Command(LCD_COL+L);
if(L+W<64) //全部显示在左半屏
{
for(i=0; i<W; i++) LCD_Write_Data(r[i]);
}
else //如果越界则跨越左右半屏显示
{
for(i=0; i<64-L; i++) LCD_Write_Data(r[i]); //左半屏显示
CS1=0; CS2=1; //右半屏显示
LCD_Write_Command(LCD_PAGE+P);
LCD_Write_Command(LCD_COL);
for(i=64-L; i<W; i++) LCD_Write_Data(r[i]);
}}
else //全部显示在右半屏
{
CS1=0; CS2=1;
LCD_Write_Command( LCD_PAGE+P);
LCD_Write_Command( LCD_COL+L-64);
for( i=0; i<W;i++) LCD_Write_Data(r[i]);
}}
//显示一个8*16的点阵字符(字符上半部分与下半部分分别处在相邻两页中)
void Display_A_Char(uchar P,uchar L,uchar *M)
{
Common_Show(P,L,8,M);
Common_Show(P+1,L,8,M+8);
}
//显示一个16*16点阵汉子(汉字上半部分与下半部分分别处在相邻两页中)
void Display_A_WORD(uchar P,uchar L,uchar *M)
{
Common_Show(P,L,16,M); //显示汉字上半部分
Common_Show(P+1,L,16,M+16); //显示汉字下半部分
}
//显示一串16*16点阵汉字//
void Display_A_WORD_String(uchar P,uchar L,uchar C,uchar *M)
{
uchar i;
for(i=0; i<C; i++)
Display_A_WORD(P,L+i*16,M+i*32);
}
// CotMAtrix.c//
//相关的汉字与数字的点阵编码//
#define uchar unsigned char
#define uint unsigned int
//中文提示(16*16点阵)
uchar code Word_String[][32]=
{
/*-- 文字: 键 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
{0x40,0x30,0xEF,0x24,0x24,0x80,0xE4,0x9C,0x10,0x54,0x54,0xFF,0x54,0x7C,0x10,0x00,
0x01,0x01,0x7F,0x21,0x51,0x26,0x18,0x27,0x44,0x45,0x45,0x5F,0x45,0x45,0x44,0x00},
/*-- 文字: 盘 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
{0x20,0x20,0x20,0xFC,0x24,0x26,0xA5,0x2C,0x34,0x24,0x24,0xFC,0x20,0x20,0x20,0x00,
0x40,0x42,0x7D,0x44,0x44,0x7C,0x44,0x45,0x44,0x7D,0x46,0x45,0x7C,0x40,0x40,0x00},
/*-- 文字: 矩 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
{0x90,0x88,0x87,0xFC,0x84,0x84,0x80,0xFE,0x22,0x22,0x22,0x22,0x22,0xE2,0x02,0x00,
0x80,0x60,0x18,0x07,0x08,0x30,0x00,0x7F,0x44,0x44,0x44,0x44,0x44,0x47,0x40,0x00},
/*-- 文字: 阵 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
{0x00,0xFE,0x02,0x22,0xDA,0x06,0x08,0xC8,0xB8,0x8F,0xE8,0x88,0x88,0x88,0x08,0x00,
0x00,0xFF,0x08,0x10,0x08,0x07,0x08,0x08,0x08,0x08,0xFF,0x08,0x08,0x08,0x08,0x00},
/*-- 文字: 扫 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
{0x10,0x10,0x10,0xFF,0x10,0x90,0x04,0x84,0x84,0x84,0x84,0x84,0x84,0xFC,0x00,0x00,
0x04,0x44,0x82,0x7F,0x01,0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x7F,0x00,0x00},
/*-- 文字: 描 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
{0x10,0x10,0x10,0xFF,0x10,0x90,0x04,0xC4,0x5F,0x44,0xC4,0x44,0x5F,0xC4,0x04,0x00,
0x04,0x44,0x82,0x7F,0x01,0x00,0x00,0xFF,0x44,0x44,0x7F,0x44,0x44,0xFF,0x00,0x00},
}; //键盘数字与符号点阵(8*16)点阵
uchar code Keyboard_Chars[][16]=
{
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00},//0
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00},//1
{0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00},//2
{0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},//3
{0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00},//4
{0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00},//5
{0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00},//6
{0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00},//7
{0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00},//8
{0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00},//9
{0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00},///
{0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00},//*
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x1F,0x01,0x01,0x01,0x01},//-
{0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00},//+
{0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00},//=
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}//Space
};
//Keyboard_Chars中数字与符号编码与键盘键位对照表
uchar code KeyPosTable[]=
{
7,8,9,10,
4,5,6,11,
1,2,3,12,
15,0,14,13
};
//KeyPad.c//
//键盘扫描程序//
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
//扫描键盘时发送到0~3列上的扫描码
uchar KeyboardScanCode[4]={0xEF,0xDF,0xBF,0x7F};
//扫描键盘并返回按键在键盘上的位置序号//
uchar KeyScan()
{