应该不是判断忙碌或者不只是判断忙碌,这个语句应该是送了一串命令进去
//HD44780 LCD DRIVER
#include <AT89X52H>
#include <ctypeh>
//LCD Commands
#define LCD_CLS 1 //Clears entire display and sets DDRAM address 0
#define LCD_HOME 2 //Sets DDRAM address 0 in address counter
#define LCD_SETMODE 4 //Sets cursor move direction and specifies display shift
#define LCD_SETVISIBLE 8 //Sets entire display (D) on/off,cursor on/off (C), and blinking of cursor position character (B)
#define LCD_SHIFT 16 //Moves cursor and shifts display without changing DDRAM contents
#define LCD_SETFUNCTION 32 //Sets interface data length (DL), number of display lines (N), and character font (F)
#define LCD_SETCGADDR 64 //Sets CGRAM addressCGRAM data is sent and received after this setting
#define LCD_SETDDADDR 128 //Sets DDRAM address DDRAM data is sent and received after this setting
#define TURE 1
#define FORSE 0
#define TMH 0xf8 //delay timeh 2ms
#define TML 0xCD //delay timel
//------------------------------------------
#define MAX_DISPLAY_CHAR 2
//-----------------------------------------
unsigned char code text[6]="hellow";
static unsigned char data counter,a;
void wcchar(unsigned char d); //write a command char
void wdchar(unsigned char i); //write a data char
char wtbusy(); //wait busy sign
void clrscr(void); //clear screen
void initime0(void); //initial time0
void delay(unsigned char i); //delay
//-------------------------------
unsigned char crc8(unsigned char pData,unsigned char count);
unsigned char pData[]={0x33,0x12,0x34,0x56,0x78,0x33,0x12,0x23,0x45,0x56};
unsigned char data crc;
static char outputbuffer[MAX_DISPLAY_CHAR];
char calc_decascii(char num);
//-------------------------------
void ISR_Timer0(void) interrupt 1
{
TL0=TML;
TH0=TMH;
counter--;
}
void ISR_Int0(void) interrupt 0
{
wdchar(text[a++]); //display "hellow"
if (a>=6)
{a=0;wdchar(' ');}
}
//main program display "hellow"
main(void)
{
unsigned char data k;
initime0();
wcchar(0x38); //initial lcd
wcchar(LCD_SETVISIBLE+6); //set lcd visible
clrscr(); //clear lcd
while(1)
{
crc8(&pData,10);
calc_decascii(crc);
wdchar (outputbuffer[1]);
wdchar (outputbuffer[2]);
wdchar (' ');
for(k=0;k<=6;k++)
{
// wdchar(text[k]); //display "hellow"
P1_2=0;
delay(55); //delay
P1_2=1;
delay(55); //delay
}
}
}
//sub function || display a char
void wdchar(unsigned char i) //write a char
{
unsigned char xdata j;
P1_0=1;
P1_1=0;
j=0x8000;
j=i;
while (wtbusy())
{}
}
void wcchar(unsigned char d) //write a command char
{
unsigned char xdata j;
P1_0=0;
P1_1=0;
j=0x8000;
j=d;
while (wtbusy())
{}
}
char wtbusy() //wait busy sign
{
unsigned char xdata j;
P1_0=0;
P1_1=1;
j=0x8000;
if(j&0x80)
{
return TURE;
}
else
{
return FORSE;
}
}
//clear screen
void clrscr()
{
wcchar(LCD_CLS);
}
//initial time0
void initime0()
{
TL0=TML;
TH0=TMH;
TR0=TURE;
IE=0x93; //Enable T0 and Serial Port
}
//sub function time delay
void delay(unsigned char i)
{
counter=i;
while (counter)
{}
}
unsigned char crc8(unsigned char pData,unsigned char count)
{
// unsigned char crc;
crc = 0;
while (count-- > 0)
{
crc ^= pData++;
}
return crc;
}
char calc_decascii(char num)
// A rather messy function to convert a floating
// point number into an ASCII string
{ long data temp = num;
char data arrayptr = &outputbuffer[MAX_DISPLAY_CHAR];
long data divisor = 10;
long data result;
char data remainder,asciival;
int data i;
// If the result of the calculation is zero
// insert a zero in the buffer and finish
if (!temp)
{ arrayptr = 48;
goto done;
}
// Handle Negative Numbers
if (temp < 0)
{ outputbuffer[0] = '-';
temp -= 2temp;
}
for (i=0 ; i < sizeof(outputbuffer) ; i++)
{ remainder = temp % divisor;
result = temp / divisor;
// If we run off the end of the number insert a space into
// the buffer
if ((!remainder) && (!result))
{ arrayptr = ' ';}
// We're in business - store the digit offsetting
// by 48 decimal to account for the ascii value
else
{ asciival = remainder + 48;
arrayptr = asciival;
}
temp /= 10;
// Save a place for a negative sign
if (arrayptr != &outputbuffer[1]) arrayptr--;
}
done: return outputbuffer;
}
程序肯定是有问题了,现让液晶显示出来内容,这样就说明1602的驱动是对的,
#include<stch>#define uchar unsigned char
#define uint unsigned int
uchar code table[]="SHUZILEIDA"; //让液晶显示这两行字
uchar code table1[]="IIS80COM!";
sbit lcdrs=P1^5; //数据/命令选择端sbit lcdrw=P1^6;
sbit lcden=P1^7; //液晶使能信号
sbit duan=P1^0; // 数码管段控制sbit wei=P1^1; // 数码管位控制
sbit rst=P3^4;//DS1302复位端,低电平关闭 uchar num;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) //命令{
lcdrs=0; //选择写命令
P0=com;
delay(5);
lcden=1; //使能信号
delay(5);
lcden=0; //关闭使能
}
void write_data(uchar date) //数据{
lcdrs=1; //选择写数据
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init(){
lcden=0;
write_com(0x38); //00111000 设置162显示,57点阵 8为数据接口
write_com(0x0c); //00001100 设置开显示,不显示光标
write_com(0x06); // 00000110 写一个字符后地址指针加 1
write_com(0x01); //00000001 显示清0,数据指针清零 / /
}
void main(){
P0=0XFF;
wei=0;
P0=0X00;
duan=0; //关闭数码管
lcdrw=0; //lcd写 *** 作
init();
write_com(0x80+0x10+0x03); // 数据行位置
for(num=0;num<10;num++) {
write_data(table[num]);
delay(20);
}
write_com(0x80+0x40);// 数据行位置 for(num=0;num<10;num++)
{
write_data(table1[num]);
delay(20);
}
/ for(num=0;num<16;num++) //屏幕左移
{
write_com(0x18);
delay(2000);
} /
while(1);
}
这是为你量身制作的,希望你能喜欢,光标根据按键在数字下面闪烁,一定符合你的要求,给你截了一张效果图
#include<reg52h>
#include<intrinsh>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uchar code table[]="1602";
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
sbit rs=P2^0;
sbit rw=P2^1;
sbit en=P2^2;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
rs=0;
rw=0;
en=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}
void write_dat(uchar dat)
{
rs=1;
rw=0;
en=0;
P0=dat;
delay(5);
en=1;
delay(5);
en=0;
}
void init()
{
uchar i;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x86);
for(i=0;i<4;i++)
{
write_dat(table[i]);
delay(5);
}
}
void main()
{
init();
while(1)
{
if(key1==0)
{
delay(10);
if(key1==0)
{
while(key1==0);
write_com(0x86);
write_com(0x0f);
}
}
if(key2==0)
{
delay(10);
if(key2==0)
{
while(key2==0);
write_com(0x87);
write_com(0x0f);
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
while(key3==0);
write_com(0x88);
write_com(0x0f);
}
}
if(key4==0)
{
delay(10);
if(key4==0)
{
while(key4==0);
write_com(0x89);
write_com(0x0f);
}
}
}
}
1、首先我们打开KeilμVision编译器,新建一个工程,然后保存在硬盘上的位置。
2、其次导入51单片机的头文件以及LCD1602的头文件。创建一个延时函数,可以传入想要具体延时的时长,其内部实现是由一个二重循环,两个循环的次数相乘积。
3、最后最后在主函数中首先执行LCD1602的初始化函数,首先创建一个无限循环,然后添加两个字符串,这里以两行显示百度经验的网址为例,再进行延时以及使用清屏函数进行刷新。
#include "reg52h"
sbit LED=P1^0;//LED锁存器
sbit SEG=P1^1;//数码管段选锁存器
sbit DIG=P1^2;//数码管位选锁存器
unsigned char t;
sbit LCDRS=P2^7;//数据指令控制
sbit LCDRW=P2^6;//读写控制
sbit LCDEN=P2^5;//液晶屏使能控制
//延时函数,延时n1ms
void delay(unsigned char n)
{
int i,j;
for (i=0; i<n; i++)
for (j=0; j<100; j++)
;
}
//写命令
void Write_Cmd(unsigned char C)
{
LCDEN=1;//使能端,由高电平跳变成低电平时,液晶屏模块执行命令
LCDRS=0;//指令寄存器选择
P0=C;
delay(5);
LCDEN=0;
}
//写数据
void Write_Data(unsigned char D)
{
LCDEN=1;//使能端,由高电平跳变成低电平时,液晶屏模块执行命令
LCDRS=1;//选择数据寄存器
P0=D;
delay(5);
LCDEN=0;
}
//LCD初始化
void LCD_Init()
{
LCDRS=0;//指令寄存器选择
Write_Cmd(0x01);//清屏
Write_Cmd(0x38);//功能设置8位双行显示57点阵
Write_Cmd(0x0F);//开关显示设置
Write_Cmd(0x06);//输入方式设置,光标从左向右移动,内容不移动
}
void main()
{
unsigned char Code1[]="I LOVE MCU!";
unsigned char m;
//LED,数码管锁存器片选设置
LED=0;
DUAN=0;
WEI=0;
LCDRW=0;//写选择
LCD_Init();
Write_Cmd(0x80+0x01);//写入显示缓冲区起始地址为1行2列
for(m=0;m<11;m++)
{
Write_Data(Code1[m]);
delay(3);
}
while (1)
;
}//end of main
以上就是关于关于单片机液晶1602的程序片段问题全部的内容,包括:关于单片机液晶1602的程序片段问题、单片机1602液晶用烧写程序烧入程序后,只显示16个方格,如图所示,怎么处理啊、用于1602液晶屏的C语言程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)