c语言at89c52单片机与1602液晶显示器第一行显示200115009,第二行显示FENGJINFU。a:从屏幕右侧移入,遇到屏幕左侧停止;b:从屏幕左侧移入,遇到屏幕右侧停止;c:这二行内容居中;d:第一行靠左显示,第二行靠右显示。
时间: 2024-02-19 13:57:52 浏览: 74
对于第一个问题,你可以使用下面的代码来实现:
```c
#include <reg52.h>
#include <stdio.h>
#define LCD_RS P2_0
#define LCD_RW P2_1
#define LCD_EN P2_2
#define LCD_DATA P0
void delay(unsigned int t)
{
unsigned int i, j;
for (i = 0; i < t; i++)
{
for (j = 0; j < 125; j++);
}
}
void LCD_Init()
{
delay(50);
LCD_RS = 0;
LCD_RW = 0;
LCD_DATA = 0x38;
LCD_EN = 1;
delay(5);
LCD_EN = 0;
delay(5);
LCD_DATA = 0x0C;
LCD_EN = 1;
delay(5);
LCD_EN = 0;
delay(5);
LCD_DATA = 0x01;
LCD_EN = 1;
delay(5);
LCD_EN = 0;
delay(5);
LCD_DATA = 0x06;
LCD_EN = 1;
delay(5);
LCD_EN = 0;
}
void LCD_Write(unsigned char dat, unsigned char rs)
{
LCD_RS = rs;
LCD_RW = 0;
LCD_DATA = dat;
LCD_EN = 1;
delay(5);
LCD_EN = 0;
}
void LCD_Clear()
{
LCD_Write(0x01, 0);
}
void LCD_SetCursor(unsigned char x, unsigned char y)
{
unsigned char addr;
if (y == 0)
{
addr = 0x80 + x;
}
else
{
addr = 0xC0 + x;
}
LCD_Write(addr, 0);
}
void LCD_DisplayString(unsigned char x, unsigned char y, unsigned char *s)
{
LCD_SetCursor(x, y);
while (*s)
{
LCD_Write(*s++, 1);
}
}
void LCD_DisplayStringRight(unsigned char x, unsigned char y, unsigned char *s)
{
unsigned char len = 0;
while (*s)
{
len++;
s++;
}
LCD_SetCursor(x - len, y);
while (*--s)
{
LCD_Write(*s, 1);
}
}
void LCD_DisplayStringCenter(unsigned char y, unsigned char *s)
{
unsigned char len = 0;
unsigned char *p = s;
while (*p)
{
len++;
p++;
}
LCD_SetCursor((16 - len) / 2, y);
while (*s)
{
LCD_Write(*s++, 1);
}
}
void main()
{
unsigned char str1[] = "200115009";
unsigned char str2[] = "FENGJINFU";
LCD_Init();
LCD_DisplayString(0, 0, str1);
LCD_DisplayStringCenter(1, str2);
//LCD_DisplayStringRight(16, 1, str2); // Uncomment this line to display the second line on the right.
while (1);
}
```
这个代码使用了AT89C52单片机和1602液晶显示器来显示两行文本。其中,`LCD_DisplayString`函数可以在指定的位置上显示一个字符串,`LCD_DisplayStringRight`函数可以在指定的位置上向右对齐显示一个字符串,`LCD_DisplayStringCenter`函数可以在指定的位置上居中显示一个字符串。你可以根据需要注释掉不需要的函数调用来实现你想要的效果。
注意:这个代码中使用的是汇编指令 `MOVX` 来访问外部RAM,因此需要在工程设置中打开 "Use Extended Instructions" 选项才能编译。
阅读全文