所有人回答的都不对,其实是忘了将1602第5脚rw设置为0了,而郭天祥的板子是直接接地,所以不用设置,但是别的板子却不能照搬了。所以程序应改为
sbit lcdrw=P3^6;
void write_com(uchar com)
{
lcdrs=0;
lcdrw=0;//加这一条
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
lcdrw=0;//加这一条
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init_1602()
{
LCD_1602_EN=0;
LCD_1602_RW=0; //这句话你忘写了,液晶数据也不进去的。。
write_1602_com(0x38); //设置16X2显示,5X7点阵,8位数据接口
write_1602_com(0x0e); //设置开显示,不显示光标
write_1602_com(0x06); //写一个字符后地址指针加1
write_1602_com(0x01); //显示清0,数据指针清0
write_1602_com(0x80); //设置初始显示为第一行第一列
}
应该不是判断忙碌或者不只是判断忙碌,这个语句应该是送了一串命令进去
//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;
}
我有 at89s52 的键盘 和1602初始化程序,你可以组装一下,
STC12C5A08AD单片机和at89s52 差不多。
#include<at89x52h>
#include<intrinsh>
sbit d_i=P1^0;
sbit r_w=P1^1;
sbit e=P1^2;
#define uchar unsigned char
#define nop _nop_()
void delay(unsigned int ttt) // 长延时,ttt为延时的mS数
{
unsigned int aaa;
while(ttt--) // 延时约1mS
{
aaa=89;
while(aaa--);
}
}
void wcom(uchar cmd);
void wddr(uchar dat);
void checkbusy();
void lcd_init();
void display();
void main()
{
delay(40);
lcd_init();
while(1){
display();}
}
void lcd_init()
{
delay(15);
wcom(0x01);
delay(5);
wcom(0x38);
delay(5);
wcom(0x38);
delay(10);
wcom(0x0c);
delay(5);
wcom(0x06);
delay(5);
wcom(0x01);
delay(5);
}
void wcom(uchar cmd)
{
checkbusy();
d_i=0;
r_w=0;
e=0;
P0=cmd;
nop;
nop;
nop;
nop;
e=1;
nop;
nop;
nop;
nop;e=0;
}
void wddr (uchar dat)
{
checkbusy();
d_i=1;
r_w=0;
e=0;
P0=dat;
nop;nop;
nop;nop;
e=1;
nop;nop;
nop;nop;
e=0;
}
void checkbusy ()
{ bit result;
d_i=0;
r_w=1;
P0=0xff;
e=0;
nop;nop;
e=1;
nop;
nop;nop;
nop;
do {delay(1);
result = (bit)(P0&0x80);}
while(result!=0);
e=0;}
void display()
{// wcom(0x80);
wddr(0x61);
delay(100);
}
/取键值函数/
uchar keyscan()
{
uchar hang=0x00,lie=0x01,i=0x00;
uchar lcode,hcode;
uchar keynumber=0xff;
P2=0xf0;
if((P2&0xf0)!=0xf0)
{
delay(15); //去抖动
if((P2&0xf0)!=0xf0)
{
hcode=0xfe; //从第一行开始查
while((hcode&0x10)!=0)
{ P2=hcode;
if((P2&0xf0)!=0xf0)
{
lcode=(P2&0xf0)|0x0f;
//nop;nop;nop;nop;
//shortdelay(2);
lcode=(~lcode)>>4;
while(lie!=lcode) //判断在第几列(通过移动到某一位,是否匹配)
{ lie=lie<<1; //变量移动后要赋值
i++;
}
lie=i;
keynumber=hang4+lie;// 键值
//shortdelay(5); //开始程序不健壮,加上延时就可以稳定显示了,现在去掉也没问题
// nop;nop;nop;nop;
return (keynumber);
//break; //当初以为返回值后,不能跳出故加上;现在也可去掉
}
else
{
hcode=(hcode<<1)|0x01;
hang++;//下一次扫描第hang行,行值
}
}
}
}
else return (keynumber);
}
你这个接线就不合理 引脚4、5、6是控制端竟然没接给你个图参考
再给你一个仿真中的接线图和程序 你自己试试看
程序:
RS EQU P37
RW EQU P36
E EQU P35
mov p3,#0ffh
MOV P1,#01H ;清除屏幕
ACALL ENABLE
MOV P1,#38H ;8位点阵
ACALL ENABLE
MOV P1,#0FH ;开显示
ACALL ENABLE
MOV P1,#06H ;移动光标
ACALL ENABLE
MOV P1,#80H ;显示位置
ACALL ENABLE
l3: mov p1,#01h
acall enable
mov dptr,#table1 ;送第一句话
call write1
call enable
MOV P1,#0C0H ;写入显示起始地址(第二行第一个位置)
ACALL ENABLE ;调用写入命令子程序
call write1
call enable
mov p1,#01h
call delay1
call delay1
call delay1
jmp l3
ENABLE: ;送命令
CLR RS
CLR RW
CLR E
ACALL DELAY
SETB E
RET
write1: ;送字符串
mov r1,#00h
a1:mov a,r1
movc a,@a+dptr
call write2
inc r1
cjne a,#00h,a1 ;以00H做字符串结束标志
ret
write2: ;送单个字符
mov p1,a
setb rs
clr rw
clr e
call delay
setb e
ret
delay: ;延时子程序
mov r7,#255
d1:mov r6,#255
d2:djnz r6,d2
djnz r7,d1
ret
delay1:
mov r7,#255
delay2:mov r6,#255
djnz r6,$
djnz r7,delay2
ret
table1: db 20h,20h,20h,57h,45h,4ch,43h,4fh,4dh,45h,20h,54h,4fh,20h,20h,20h,20h,00h
table2: db 20h,57h,57h,57h,2Eh,4dh,43h,55h,39h,39h,2Eh,43h,4Fh,4Dh,20h,20h,20h,00h
end
给你推荐一本书《MCS51单片机应用设计》哈尔滨工业大学出版社,里面很详细的
#include "reg52h"
#include "intrinsh"
unsigned char text1[] = "TTT TTTT add:qhd" ;
unsigned char text2[] = "TEL:13333333333 " ;
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P1^2;
void delay_ms(unsigned char t);
void init_1602(void);
void write_command(unsigned char cmd);
void write_data(unsigned char dat);
void dsp_onechar(unsigned char pos,unsigned char c);
void dsp_string(unsigned char line,unsigned char p,unsigned char length);
void main(void)
{
init_1602();
delay_ms(15);
dsp_string(0,text1,16);
dsp_string(1,text2,16);
while(1);
}
void delay_ms(unsigned char t)//毫秒延时大约延时1ms
{
unsigned char i;
while(t--)
{
for(i=0;i<112;i++);
}
}
void write_command(unsigned char cmd)//写指令函数(按规定RS=0,RW=0为写指令)
{
delay_ms(5); // check busy flag
EN = 0;
RS = 0;
RW = 0;
_nop_();
EN = 1;
P0 = cmd;
EN = 0;
}
void write_data(unsigned char dat)//往1602写数据
{
delay_ms(5); //busy flag check
EN = 0;
RS = 1;
RW = 0;
_nop_();
EN = 1;
P0 = dat;
EN = 0;
RS = 0;
}
void dsp_onechar(unsigned char pos,unsigned char c)//写一个字节函数
{
unsigned char p;
if (pos>=0x10)
p=pos+0xb0;
else
p=pos+0x80;
write_command(p);
write_data(c);
}
void dsp_string(unsigned char line,unsigned char p,unsigned char length)
{
unsigned char l,i;
l=line<<4;
for (i=0;i<length;i++)
dsp_onechar(l++,(p+i));
}
void init_1602(void)//1602初始化
{
delay_ms(15);
write_command(0x38);
write_command(0x38);
write_command(0x38);
write_command(0x06);
write_command(0x0c);
write_command(0x01);
}
以上就是关于我看郭天祥的十天学会51单片机里的程序,买了块TX的51开发板 编写1602液晶显示的程序全部的内容,包括:我看郭天祥的十天学会51单片机里的程序,买了块TX的51开发板 编写1602液晶显示的程序、这是在CC2530单片机下面写的一个1602的液晶显示程序,大家看看哪里出问题了啊,是不是IO口的初始化有问题。、关于单片机液晶1602的程序片段问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)