#include <reg52.h> typedef unsigned int uint; typedef unsigned char uchar; void delay(uint z){ uint x,y; for(x=z;x>0;--x) for(y=110;y>0;--y); } uchar posit = 0x00; sbit lcd_en_port=P3^4; sbit lcd_rs_port=P3^5; sbit lcd_rw_port=P3^6; sbit dula=P2^6;//数码管的段选信号 sbit wela=P2^7; //数码管的位选信号 void lcd_busy_wait() /*LCD1602 忙等待*/ { lcd_rs_port = 0; lcd_rw_port = 1; lcd_en_port = 1; P0 = 0xff; while (P0&0x80); lcd_en_port = 0; } void lcd_command_write(uchar com){ lcd_rs_port=0; lcd_rw_port=0; lcd_en_port=0; P0=com; lcd_en_port=1; lcd_en_port=0; } void lcd_data_write(uchar _data){ lcd_rs_port=1; lcd_rw_port=0; lcd_en_port=0; P0=_data; lcd_en_port=1; lcd_en_port=0; } void lcd_char_write(uchar _posit,uchar _data){ uchar row,col,addr=0x00; row=(_posit>>4)&0x01; col=_posit&0x0f; addr=col; if(row){ addr+=0x40; } addr+=0x80; lcd_busy_wait(); lcd_command_write(addr); lcd_busy_wait(); lcd_data_write(_data); P0=0xff; } void lcd_char_show(uchar content){ lcd_char_write(posit++,content); if(posit>=0x20){ posit=0x00; } } void lcd_system_reset() /*LCD1602 初始化*/ { dula=1; wela=1; delay(15); lcd_command_write(0x38); delay(5); lcd_command_write(0x38); delay(5); lcd_command_write(0x38); //have to read lcd_busy_wait(); lcd_command_write(0x01); lcd_busy_wait(); lcd_command_write(0x06); lcd_busy_wait(); lcd_command_write(0x0c); P0 = 0xff; /*释放数据端口*/ } void ser_system_reset(){ TMOD=0x20; TH1=0xfd; TL1=0xfd; TR1=1; REN=1; SM0=0; SM1=1; EA=1; ES=1; } void ser() interrupt 4{ uchar _content; if(RI){ //EA=0; _content=SBUF; lcd_char_show(_content); RI=0; SBUF=_content; //while(!TI); TI=0; //EA=1; } } void main(){ uchar num; lcd_system_reset(); ser_system_reset(); while(1); }