仿真电路图:
部分源程序:
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar keycode, DDram_value = 0xc0;
sbit rs = P2 ^ 0;
sbit rw = P2 ^ 1;
sbit e = P2 ^ 2;
sbit speaker = P2 ^ 3;
uchar speakerCnt = 0;
uchar code table[] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x20};
uchar code table_designer[] = "Calling........";
void lcd_delay();
void delay(uint n);
void lcd_init(void);
void lcd_busy(void);
void lcd_wr_con(uchar c);
void lcd_wr_data(uchar d);
uchar checkkey(void);
uchar keyscan(void);
void main()
{
uchar num;
lcd_init();
while (1)
{
keycode = keyscan();
if ((keycode >= 0) && (keycode <= 9))
{
lcd_wr_con(0x06);
lcd_wr_con(DDram_value);
lcd_wr_data(table[keycode]);
DDram_value++;
}
else if (keycode == 0x0a)
{
lcd_wr_con(0x04);
DDram_value--;
if (DDram_value <= 0xc0)
{
DDram_value = 0xc0;
}
else if (DDram_value >= 0xcf)
{
DDram_value = 0xcf;
}
lcd_wr_con(DDram_value);
lcd_wr_data(table[10]);
}
else if (keycode == 0x0b)
{
lcd_wr_con(0x01); //清屏
DDram_value = 0xc0;
speakerCnt = 0;
}
else if (keycode == 0x0c)
{
uchar sound;
lcd_wr_con(0x06);
lcd_wr_con(0x80);
for (num = 0; num <= 14; num++)
{
lcd_wr_data(table_designer[num]);
}
speakerCnt = 50;
}
if (speakerCnt >= 1)
{
speaker = 0; //发出按键响声
delay(2000);
speaker = 1;
delay(2000);
speakerCnt--;
}
}
}
void lcd_delay() //液晶屏显示延时函数
{
uchar y;
for (y = 0; y < 0xff; y++)
{
;
}
}
void lcd_init(void) //液晶屏初始化函数,向液晶写入各种命令
{
lcd_wr_con(0x01); //清屏
lcd_wr_con(0x38); //两行显示,5×7点阵,8位数据接口
lcd_wr_con(0x0c); //开整体显示,光标关,无闪烁
lcd_wr_con(0x06); //
}
void lcd_busy(void) //判断液晶显示器是否忙的函数
{
P0 = 0xff;
rs = 0;
rw = 1;
e = 1;
e = 0;
while (P0 & 0x80)
{
e = 0;
e = 1;
}
lcd_delay();
}
void lcd_wr_con(uchar c) //向液晶显示器写命令
{
lcd_busy();
e = 0;
rs = 0;
rw = 0;
e = 1;
P0 = c;
e = 0;
lcd_delay();
}
void lcd_wr_data(uchar d) //向液晶显示器写数据
{
lcd_busy();
e = 0;
rs = 1;
rw = 0;
e = 1;
P0 = d;
e = 0;
lcd_delay();
}
void delay(uint n) //延时函数
{
uchar i;
uint j;
for (i = 50; i > 0; i--)
for (j = n; j > 0; j--)
;
}
. 链接:https://pan.baidu.com/s/151GzI8zUpqg6niN5Z-DPmQ 提取码:1234 .
|