动态显示(dynamicdisplay)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

动态显示(dynamic display)
One, about LCD1602:
Before writing the LCD1602 program, understand some of the very important information in the manual. If the information is not clear enough, the program may experience more or less problems.
1. pin:
1602 a total of 16 pins, but the main pin programming used only three, respectively: RS (data terminal, R/W (selected) to read and write E (select end), enable signal); programming focuses on the three pin expansion initialization, write command, write data.
The following three specific description of the pin:
RS is the register selection, the high level selects the data register, and the low level selects the instruction register.
R/W read and write select, high level read operation, low-level write operation.
The E terminal is the enable terminal, and the latter is connected with the timing.
In addition, the D0~D7 are 8 bit bidirectional data lines.
2. operation sequence:
Read and write timing: 1602 in the timing diagram. Timing
diagrams are important. Programming is setting up registers according to the sequence diagram to allow the LCD to work.
Two write timing:
When we want to write the instruction word and set the working mode of LCD1602: we need to set the RS to low level, RW to low level, then send the data to the data port D0~D7, and finally a high pulse of E pin writes the data.
When we want to write data words, on the 1602 to achieve the display: RS needs to be set to high, RW to low, and then send data to the data port D0~D7, and finally the E pin, a high pulse to write data.
The difference between writing instructions and writing data is simply that the level of the RS is different. The following is the timing diagram of the LCD1602:
RS R/W operation instructions
00 write instruction code D0~D7
01 read and output the D0~D7 status word
10 write data D0~D7
11 read data from D0~D7
Note: about E=H pulse - at the start, initialize E to 0, and then set E to 1, and then clear 0.
When you read the status word, note that the D7 bit, D7=1, prohibits read-write operations; D7=0 allows read and write operations;
Therefore, the read and write test must be carried out before each read and write operation of the controller. (after the read busy subroutine), before executing each instruction, make sure that the busy sign of the module is low, indicating that it is not busy, otherwise the command is invalid.
Do{P0=0xff;
RS=0;
RW=1;
EN=1;}while (P0&0x80);
3. instruction set: LCD_1602 initialization instructions:
0x38 settings, 16*2 display, 5*7 dot matrix, 8 bit data interface
0x01 screen, the cursor is reset to the address location 00H
0x0F displays, displays the cursor, and blinks the cursor
0x08 only displays
0x0e displays, displays the cursor, and the cursor does not
blink
0x0c open display without cursor display
0x06 address plus 1, when the data is written, the cursor moves right
The 0x02 address counter AC=0; (at this time the address is 0x80) the cursor goes to the origin, but the DDRAM interrupt remains unchanged
The 0x18 cursor moves left together with the display
Two 、 LCD1602 programming flow:
The LCD1602 can be written after the above information is understood. Here we divide the program into the following steps:
1. defines the LCD1602 pins, including RS, R/W, and E. Here the definition refers to these pins are connected to the microcontroller which I/O port.
Examples are as follows:
Sbit EN=P2^2;
Sbit RW=P2^1;
Sbit RS=P2^0;
2. display initialization, in this step to initialize and set the display mode and other operations, including the following steps:
Set display mode
delayed
Clear display cache
Set display mode
The recommended initialization process is as follows:
Delayed 15ms
Write instruction 38H
Delayed 5ms
Write instruction 38H
Delayed 5ms
Write instruction 38H
Delayed 5ms
Note: the 38H instruction described above can be omitted. 1~2 steps are omitted
Write instruction 38H
Write instruction 08H to close the display
Write instructions for 01H display screen
Write instruction 06H cursor movement settings
Write instruction 0cH displays open and cursor settings
3. sets the display address (where the characters are displayed).
4. write data to display characters.
Three, LCD1602 subroutine modules and the main program written:
According to the flow of the program written above, an example of each subroutine module and the main program is given.
1. header files, macro definitions, definitions, pins, etc.:
#include<reg52.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
Sbit EN=P2^2;
Sbit RW=P2^1;
Sbit RS=P2^0;
2.LCD1602 basic initialization subroutine:
Void, LCD1602 ()
{
EN=0;
RS=1;
RW=1;
P0=0xff; / / where P0 is connected with LCD D0~D7 I/O }
3. read busy subroutine:
Void, read_busy ()
{
P0=0xff;
RS=0;
EN=1;
While (P0&0x80); //P0 and 10000000 phases and D7 bits, if not 0, stop here
EN=0;
/ / if 0 jump into the next step; this statement is the role of detection of D7
If the busy wait} / / read out, not busy busy reading and writing instruction execution subroutine
4. write instructions and write data subroutines:
Void lcd_write (uchar, I, bit, J)
{
Read_busy ();
P0=i; / / i=0, i=1, and write data write instructions;
RS=j;
RW=0;
EN=1;
}
5. time delay subroutine:
Void delayms (uint C) / / other functions are to provide initialization subroutine delay in 1xc MS
{
Uint, a, b;
For (a=0; a<c; a++)
For (b=0; b<120; b++);
}
6.LCD1602 initialization subroutine:
Void (init) / / fully in accordance with the requirements of the initialization process, omitted a step write command 38H
{
Delay (15);
Write (0x38,0);
Delay (5);
Write (0x38,0);
Write (0x08,0);
Write (0x01,0);
Write (0x06,0);
Write (0x0c, 0);
}
7. main program:
In addition to the main program in the initialization procedure is written by themselves according to the display subroutine, you need not write function can display subroutine of various types, not detail here, as the following example shows a character string display and display subroutine.
Void, main ()
{
LCD1602 ();
Init ();
Lcd_write (0xc1,0); / / display a character
Lcd_write (0x41,1); / / display a character
While (1);
}
Task 1: the program that displays the letter "A" at the first character of the second line of the LCD module:
Task 2: dynamic display of a string of characters
#include "reg51.h""
#include "Intrins.h""
#include "string.h""
#define uchar unsigned char
Sbit rs = P2^0; / / determine the connection of hardware
Sbit RW = P2^1;
Sbit e = P2^2;
Void, LCD_write ();
Void, busy_wait ();
Void delayms (uchar MS);
"Uchar str[] = {" abc//defghijklm "}; Void, main ()
{
Uchar i=0;
P0 = 0x01;
LCD_write ();
P0 = 0x38;
LCD_write ();
P0 = 0x0f;
LCD_write ();
P0 = 0x06;
LCD_write ();
Do{
For (I = 0; i<strlen (STR); i++) {
P0 = 0xc0+i;
LCD_write ();
P0 = str[i];
Rs = 1;
RW = 0;
E = 0;
Busy_wait ();
E = 1;
Delayms (200); / / delay
P0 = 0x01; / / screen
LCD_write ();
}
}while (1);
Return;
}
Write control command function * / / * Void, LCD_write () {
Rs = 0;
RW = 0;
E = 0;
Busy_wait ();
E = 1;
Return;
}
To determine whether the LCD / * * / busy subroutine Void, busy_wait () {
Do{P0 = 0xff;
Rs = 0;
RW = 1;
E = 0;
_nop_ ();
E = 1;
} while ((P0&0x80) = = 0);
/ / E = 0;
Return;
}
Void Delayms (uchar MS)
{
Uchar i;
While (ms--) for (i=0; i<120; i++);
}
Task 3 (omitted): display two lines of characters on the LCD, the first behavior is "", second "GO:0", and the value in the second line will be added.
/ * LCD1602.h*/ header file
#ifndef LCD_CHAR_1602_2011_4_9
#define LCD_CHAR_1602_2011_4_9
#include "REG51.H""
#include <intrins.h>
*********************************************************
//PortDefinitions
Sbit LcdRs = P2^0;
Sbit LcdRw = P2^1;
Sbit LcdEn = P2^2;
SFR DBPort = 0x80; //P0=0x80, P1=0x90, P2=0xA0, P3=0xB0. data ports
*********************************************************** ****
Internal / / wait function
Unsigned char LCD_Wait (void)
{
LcdRs=0;
LcdRw=1;
_nop_ ();
LcdEn=1;
_nop_ ();
而(dbport & 0x80);
lcden = 0;
返回dbport;
}
*********************************************************** *
/ /向液晶写入命令或数据
#定义lcd_command 0 / /命令
#定义lcd_data 1 / /数据
#定义lcd_clear_screen 0x01 /清屏
#定义lcd_homing 0x02 /光标返回原点
无效lcd_write(点式、无符号字符输入)
{
lcden = 0;
lcdrs =风格;
lcdrw = 0;_nop_();
dbport =输入;_nop_();/ /注意顺序
lcden = 1;//注意顺序_nop_();
lcden = 0;_nop_();
lcd_wait();
}
*********************************************************** *
/ /设置显示模式
#定义lcd_show寄存器/显示开
#定义lcd_hide 0x00 /显示关
#定义lcd_cursor 0x02 /显示光标
#定义lcd_no_cursor 0x00 /无光标
#定义lcd_flash 0x01 /光标闪动
#定义lcd_no_flash 0x00 /光标不闪动
无效lcd_setdisplay(无符号字符显示模式)
{
lcd_write(lcd_command,0x08 |显示模式);
}
*********************************************************** *
/ /设置输入模式
#定义lcd_ac_up 0x02
#定义lcd_ac_down 0x00 /默认
#定义lcd_move 0x01 /画面可平移
#定义lcd_no_move 0x00 /默认
无效lcd_setinput(unsigned char InputMode)
{
lcd_write(lcd_command,0x04 | InputMode);
}
*****************************************************
/ /初始化液晶
无效lcd_initial()
{
lcden = 0;
lcd_write(lcd_command,0X38);/ / 8位数据端口,2行显示,5×7点阵
lcd_write(lcd_command,0X38);
lcd_setdisplay(lcd_show | lcd_no_cursor);/ /开启显示,无光标
lcd_write(lcd_command,lcd_clear_screen);/ /清屏
lcd_setinput(lcd_ac_up | lcd_no_move);/ /交流递增,画面不动
}
/ / ******************************************************
无效gotoxy(unsigned char unsigned char X,Y)
{
如果(y=0)
lcd_write(lcd_command,0x80 | X);
如果(y=1)
lcd_write(lcd_command,0x80 |(x-0x40));
}
空白打印(无符号字符)
{
当(*)!=“0”)
{
lcd_write(lcd_data,* STR);
STR;
}
}
/ /
********************************************************* # endif
/ * * / C源文件LCD1602。

#包括“LCD1602。


unsigned char tempbuffer [ 10 ];
虚空IntToStr(unsigned int,unsigned char * str字符串n){
无符号字符a [ 5 ];字符i,j;
一个[ 0 ] =(t / 10000)% 10;/ /取得整数值到数组
a = 1 =(t / 1000)% 10;
a = 2 =(t / 100)% 10;
a = 3 =(t / 10)% 10;
a = 4 =(t / 1)% 10;
为(i = 0;i < 5;i++)/转成ASCII码
the [i] = a [i] + '0';
for (i = 0; the [i] = = '0' & & i < = 3; i + +);
for (j = 5 - n; j < i; j + +) / / 填充空格
{* str = '"+ str +;};
for (; i < 5; i + +)
{* str = a [i]; str + +;} / / 加入有效的数字
str = '0' * i;
}
void delay1ms (unsigned int count) {
unsigned int i, j;
for (i = 0; i < count; i + +)
for (j = 0,; j < 120; j + +);
}
main ()
{
unsigned int count = 0;
_ initial (lcd);
gotoxy (0,0);
print ("");
gotoxy (0,1);
print ("go");
while (1)
{
inttostr (count + +, & tempbuffer [0], 5);
gotoxy (3.1);
print (& tempbuffer [0]);
delay1ms (100);
}
}
注意: 如果设置完并下载了程序液晶却不显示, 则请用螺丝刀调节1602 液晶对比度电位器, 直到看清显示为止.
任务4: (略) 动态显示逐渐增加的数字 (0 ~ 65535)
# include "reg51.h"
# include "intrins.h"
# include "string.h"
# define uchar unsigned char
# uint unsigned int define
sbit p2 ^ rs = 0; / / 确定具体硬件的连接方式sbit rw = p2 ^ 1;
sbit e = p2 ^ 2;
uint counter;
uchar str [] = {0,0,0,0,0,0};
_ void write (lcd);
_ busy wait (void);
void delayms (uchar ms);
void process ();
写入控制命令的函数 / * * /
lcd _ void write () {
rs = 0;
rw = 0;
e = 0;
_ busy wait ();
e = 1;
return;
}
判断液晶显示器是否忙的子程序 / * * / _ busy wait (void) {
do 0xff {p0 =;
rs = 0;
rw = 1;
e = 0;
_ _ (nop);
e = 1;
(0x80 &} while (p0). = 0);
/ / e = 0;
return;
}
void process () {
str [0] = [0 / 10000 + "';
str [1] (counter / 1000) = 10 + 0 '%'; str [2] = ([10 / 100) + 0 '%';
str [3] (counter / 10) = 10 + 0 '%'; str [4] = [0 +% 10 '';
if (str [0] = = '0')
{str [0] = '';
if (str = = [1], '0') [1], {str = ''; if (str = = [2], '0') [2], {str = ''; if (str = = [3], '0') [3], {str = ''; }
}
}
}
}
void delayms (uchar ms)
{
uchar);
while (ms -) for (i = 0; i < 120; i + +); }
void main ()
{
uchar (i = 0;
p0 = 0x01;
_ write (lcd);
p0 = 0x38;
_ write (lcd);
p0 = 0x0c;
_ write (lcd);
/ / p0 = 0x06;
_ write (lcd);
do {
p0 = 0x01; / / 清屏
_ write (lcd);
counter + +;
if (counter counter = 0 = = 0); (process);
for (i = 0; i < 5; i + +) {
0xc0 p0 = + i;
_ write (lcd);
p0 = str [i];
rs = 1;
rw = 0;
e = 0;
_ busy wait ();
e = 1;
delayms (255); / / 延时
while (1);}
}
任务4: 数字时钟 (二) 液晶显示时 - 分 - 秒。

相关文档
最新文档