51单片机——LCD1602
1.LCD1602显示一个字符C
#include "reg52.h"
#include "intrins.h"
#define databuffer P0
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^4;void check_busy()
{char tmp = 0x80;databuffer = 0x80;while(tmp & 0x80){RS = 0;RW = 1;EN = 0;_nop_();EN = 1;_nop_();_nop_();tmp = databuffer;EN = 0;_nop_();}
}void Write_Cmd_Func(char cmd)
{check_busy();RS = 0;RW = 0;EN = 0;_nop_();databuffer = cmd;_nop_();EN = 1;_nop_();_nop_();EN = 0;_nop_();
}void Write_Data_Func(char dataShow)
{check_busy();RS = 1;RW = 0;EN = 0;_nop_();databuffer = dataShow;_nop_();EN = 1;_nop_();_nop_();EN = 0;_nop_();
}void Delay15ms()
{unsigned char i, j;i = 27;j = 226;do{while (--j);} while (--i);
}
void Delay5ms()
{unsigned char i, j;i = 9;j = 244;do{while (--j);} while (--i);
}void LCD1602_INIT()
{Delay15ms();
Write_Cmd_Func(0x38);
Delay5ms();
Write_Cmd_Func(0x38);
Write_Cmd_Func(0x08);
Write_Cmd_Func(0x01);
Write_Cmd_Func(0x06);
Write_Cmd_Func(0x0c);
}void main()
{char position = 0x80 + 0x05;char dataShow = 'C';LCD1602_INIT();Write_Cmd_Func(position);Write_Data_Func(dataShow);}
2.LCD1602显示一行
#include "reg52.h"
#include "intrins.h"
#define databuffer P0
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^4;void check_busy()
{char tmp = 0x80;databuffer = 0x80;while(tmp & 0x80){RS = 0;RW = 1;EN = 0;_nop_();EN = 1;_nop_();_nop_();tmp = databuffer;EN = 0;_nop_();}
}void Write_Cmd_Func(char cmd)
{check_busy();RS = 0;RW = 0;EN = 0;_nop_();databuffer = cmd;_nop_();EN = 1;_nop_();_nop_();EN = 0;_nop_();
}void Write_Data_Func(char dataShow)
{check_busy();RS = 1;RW = 0;EN = 0;_nop_();databuffer = dataShow;_nop_();EN = 1;_nop_();_nop_();EN = 0;_nop_();
}void Delay15ms()
{unsigned char i, j;i = 27;j = 226;do{while (--j);} while (--i);
}
void Delay5ms()
{unsigned char i, j;i = 9;j = 244;do{while (--j);} while (--i);
}void LCD1602_INIT()
{Delay15ms();
Write_Cmd_Func(0x38);
Delay5ms();
Write_Cmd_Func(0x38);
Write_Cmd_Func(0x08);
Write_Cmd_Func(0x01);
Write_Cmd_Func(0x06);
Write_Cmd_Func(0x0c);
}void LCD1602_showLine(char row, char col, char *string)
{switch(row){case 1:Write_Cmd_Func(0x80+col);while(*string){Write_Data_Func(*string);string++;}break;case 2:Write_Cmd_Func(0x80+0x40+col);while(*string){Write_Data_Func(*string);string++;}break;}
}void main()
{char position = 0x80 + 0x05;LCD1602_INIT();LCD1602_showLine(1,5,"NO.1");LCD1602_showLine(2,0,"clc handsome");}