📄 fun.c
字号:
#include <reg51.h>
#include <intrins.h>
#include "lcd1602.h"
void delay (uchar times) //delay times ms
{
uchar i = 0;
while (times --)
{
for (i = 0; i < 120; i++);
}
}
bit bBusy()
{
bit result;
RS = 0; RW = 1; // read the AC and BF
EN = 1; // generate a high level to read
_nop_(); _nop_();
_nop_(); _nop_();
result = (bit)(P0 & 0x80); //-----important------ *the four higher bits*
// the lower four bits has been ignored //
EN = 0;
return result;
}
void wr_byte(bit bcmd, uchar cmd)
{
uchar Hcmd, Lcmd;
Hcmd = cmd & 0xF0;
Lcmd = (cmd << 4) & 0xF0;
while (bBusy()); // wait if the lcd is busy
if (bcmd) // if it's cmd, RS = 0;
RS = 0;
else // if it's data, RS = 1;
RS = 1;
RW = 0; // set R/W = 0
// EN = 0; // this step is ignorable
_nop_(); _nop_();
_nop_(); _nop_();
P0 = Hcmd; // prepare the higher four bits of the valid data
EN = 1; // set E = 1
_nop_(); _nop_();
_nop_(); _nop_();
EN = 0; // set E = 0, generate a falling edge to write
_nop_(); _nop_();
_nop_(); _nop_();
P0 = Lcmd; // prepare the lower four bits of the valid data
EN = 1; // set E = 1
_nop_(); _nop_();
_nop_(); _nop_();
EN = 0; // set E = 0, generate a falling edge to write
_nop_();
}
void ini_lcd1602(void)
{
delay (10);
/* Function Set */
wr_byte (1, 0x28); // 2 lines; 5*7 font; 4 bits
delay (5);
wr_byte (1, 0x28); // repeat the Function Set
delay (5);
/* --Display on/off-- */
wr_byte (1, 0x0c); // Display: on; Cursor: on; Blink: on;
/* --Display clear-- */
wr_byte (1, 0x01); // display clear;
delay (5);
/* --Entry mode set-- */ // I/D: incremental; SH: 0;
wr_byte (1, 0x06);
}
void wr_byte_ram (bit bDDram, uchar addr, uchar ch)
{
if (bDDram) // write data to DDRAM
wr_byte (1, addr | 0x80);
else // write data to CGRAM
wr_byte (1, addr | 0x40);
wr_byte (0, ch);
}
void wr_length_ram (bit bDDram, uchar addr, uchar *pch, uchar len)
{
uchar i = 0;
if (bDDram) // write data to DDRAM
wr_byte (1, addr | 0x80);
else // write data to CGRAM
wr_byte (1, addr | 0x40);
for (; i < len; i++)
{
wr_byte (0, *pch); // the AC will increase/decrease automatically
pch ++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -