用keil写一个lcd1602按键切换屏幕,第一屏幕上显示“xingming”,第二屏幕上显示“xuehao”
时间: 2024-05-03 12:20:22 浏览: 52
第一个问题的代码如下:
```
#include <reg51.h>
#include <intrins.h>
#define LCD_DB P0
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
void delay_ms(unsigned int t)
{
unsigned int i, j;
for(i=0; i<t; i++)
for(j=0; j<123; j++);
}
void lcd_write_command(unsigned char command)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_DB = command;
LCD_EN = 1;
_nop_();
_nop_();
LCD_EN = 0;
}
void lcd_write_data(unsigned char data)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_DB = data;
LCD_EN = 1;
_nop_();
_nop_();
LCD_EN = 0;
}
void lcd_init()
{
lcd_write_command(0x38);
delay_ms(5);
lcd_write_command(0x38);
delay_ms(5);
lcd_write_command(0x38);
delay_ms(5);
lcd_write_command(0x38);
delay_ms(5);
lcd_write_command(0x08);
delay_ms(5);
lcd_write_command(0x01);
delay_ms(5);
lcd_write_command(0x06);
delay_ms(5);
lcd_write_command(0x0c);
delay_ms(5);
}
void display_name()
{
lcd_write_command(0x80);
lcd_write_data('x');
lcd_write_data('i');
lcd_write_data('n');
lcd_write_data('g');
lcd_write_data('m');
lcd_write_data('i');
lcd_write_data('n');
lcd_write_data('g');
}
void display_number()
{
lcd_write_command(0x80);
lcd_write_data('x');
lcd_write_data('u');
lcd_write_data('e');
lcd_write_data('h');
lcd_write_data('a');
lcd_write_data('o');
}
void main()
{
unsigned char key = 0;
lcd_init();
display_name();
while(1)
{
key = P1;
if(key == 0xfe)
{
display_name();
}
if(key == 0xfd)
{
display_number();
}
}
}
```
在这个代码中,我通过 `P1` 来读取按键的值,如果按下了第一个按键,则显示“xingming”;如果按下了第二个按键,则显示“xuehao”。
阅读全文