关于单片机液晶1602的程序片段问题

关于单片机液晶1602的程序片段问题,第1张

应该不是判断忙碌或者不只是判断忙碌,这个语句应该是送了一串命令进去

//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;

}

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); //设置初始显示为第一行第一列

}

这是我写的一个成功的,你仔细对比下就知道了!

#include<reg52h>

#include<intrinsh>

#define U8 unsigned char

#define U16 unsigned int

#define BOOL bit

U8 secondh,secondl,minuteh,minutel,hourh,hourl,second,minute,hour;

U8 monthl,monthh,month,day,datel,dateh,date,year,yearl,yearh;

/定义LCD功能管脚/

sbit LCDRS = P2^6 ; //控制LCD数据或命令的读写

sbit LCDRW = P2^5 ; //控制LCD读写信号

sbit LCDEN = P2^7 ; //使能LCD

//

/定义DS1302功能管脚/

sbit DSRST = P3^5 ; //DS1302复位管脚

sbit DSIO = P3^4 ; //控制DS1302数据传输

sbit DSCLK = P3^6 ; //DS1302时钟

//

/延时函数/

void delay(U16 time)

{

U16 time1,time2;

for(time1=time;time1>0;time1--)

for(time2=110;time2>0;time2--);

}

//

/检测LCD忙碌状态/

BOOL judge_lcd()

{

BOOL flag;

LCDEN = 0;

LCDRS = 0;

LCDRW = 1;

LCDEN = 1;

delay(1);

flag=(BOOL)(P0&0x80); //状态位第7位为1时禁止读写,为0时可读写

LCDEN = 0;

return flag;

}

//

/实现LCD写指令/

void write_lcd_command(U16 command)

{

while(judge_lcd()); //检测LCD是否为忙碌状态

P0 = command ;

LCDRS = 0 ;

LCDRW = 0 ;

LCDEN = 1 ;

delay(1);

LCDEN = 0 ;

}

//

/实现LCD写数据/

void write_lcd_date(U16 date)

{

while(judge_lcd()); //检测LCD是否为忙碌状态

P0 = date ;

LCDRS = 1 ;

LCDRW = 0 ;

LCDEN = 1 ;

delay(1);

LCDEN = 0 ;

}

//

/实现LCD写指令及数据/

void write_lcd_com_date(U16 addr,U16 date)

{

write_lcd_command(addr);

delay(1);

write_lcd_date(date);

}

//

/初始化LCD/

void init_lcd()

{

write_lcd_command(0x38); //设置16x2显示,5x7点阵,8位数据口

delay(1);

write_lcd_command(0x0c); //开显示,不显示光标,光标不闪烁

delay(1);

write_lcd_command(0x06); //读写一个字符后,地址自动加1,不移动屏幕

delay(1);

write_lcd_command(0x01); //清屏

delay(1);

}

//

/读DS1302/

U8 read_ds1302(U8 addr)

{

U8 i,temp=0x00;

DSRST=0;

DSIO=1;

DSCLK=0;

DSRST=1;

for (i=0;i<8;i++) //循环8次 写入地址数据

{

DSCLK=0;

DSIO=addr&0x01; //每次传输低字节

addr>>=1; //右移一位

DSCLK=1;

}

delay(1);

if(DSIO)

temp|=0x80; //每次传输低字节

DSCLK=0;

temp>>=1;

for (i=0;i<7;i++) //循环8次 读取数据

{

DSCLK=0;

if(DSIO)

temp|=0x80; //每次传输低字节

DSCLK=1;

temp>>=1; //右移一位

}

DSCLK=1;

DSRST=0;

DSIO=0;

return temp; //返回

}

//

/写DS1302/

void write_ds1302(U8 addr, U8 date)

{

U8 i;

DSRST=0;

DSCLK=0;

DSRST=1;

for (i=0;i<8;i++) //循环8次 写入地址数据

{

DSCLK=0;

_nop_();

DSIO=addr&0x01; //每次传输低字节

addr>>=1; //右移一位

DSCLK=1;

_nop_();

}

for (i=0;i<8;i++) //循环8次 写入数据

{

DSCLK=0;

_nop_();

DSIO=date&0x01; //每次传输低字节

date>>=1; //右移一位 DSCLK=1

DSCLK=1;

_nop_();

}

DSRST=0;

delay(1);

}

//

/初始化DS1302/

void init_ds1302()

{

//读秒

if((((second=read_ds1302(0x81))>>7)&&0x01)==1)

{

write_ds1302(0x8e,0x00); //关闭写保护

write_ds1302(0x80,0x00); //00秒

write_ds1302(0x82,0x54); //54分

write_ds1302(0x84,0x17); //17点

write_ds1302(0x86,0x14); //14日

write_ds1302(0x88,0x10); //10月

write_ds1302(0x8a,0x05); //星期五

write_ds1302(0x8c,0x11); //2011年

write_ds1302(0x8e,0x80); //允许写保护

}

}

//

/主函数/

void main()

{

init_lcd();

delay(2);

write_lcd_com_date(0x81,'2');

write_lcd_com_date(0x82,'0');

delay(1);

init_ds1302();

delay(1);

while(1)

{

second=read_ds1302(0x81);

secondl=second&0x0f;

secondh=second>>4;

minute=read_ds1302(0x83);

minutel=minute&0x0f;

minuteh=minute>>4;

hour=read_ds1302(0x85);

hourl=hour&0x0f;

hourh=hour>>4;

month=read_ds1302(0x89);

monthl=month&0x0f;

monthh=month>>4;

year=read_ds1302(0x8d);

yearl=year&0x0f;

yearh=year>>4;

date=read_ds1302(0x87);

datel=date&0x0f;

dateh=date>>4;

day=read_ds1302(0x8b);

day=day&0x0f;

delay(15);

write_lcd_com_date(0xca,0x30+secondl);

write_lcd_com_date(0xc9,0x30+secondh);

write_lcd_com_date(0xc8,':');

write_lcd_com_date(0xc7,0x30+minutel);

write_lcd_com_date(0xc6,0x30+minuteh);

write_lcd_com_date(0xc5,':');

write_lcd_com_date(0xc4,0x30+hourl);

write_lcd_com_date(0xc3,0x30+hourh);

write_lcd_com_date(0x84,0x30+yearl);

write_lcd_com_date(0x83,0x30+yearh);

write_lcd_com_date(0x85,'-');

write_lcd_com_date(0x87,0x30+monthl);

write_lcd_com_date(0x86,0x30+monthh);

write_lcd_com_date(0x88,'-');

write_lcd_com_date(0x8a,0x30+datel);

write_lcd_com_date(0x89,0x30+dateh);

write_lcd_com_date(0x8c,0x30+day);

}

}

以上就是关于关于单片机液晶1602的程序片段问题全部的内容,包括:关于单片机液晶1602的程序片段问题、这是在CC2530单片机下面写的一个1602的液晶显示程序,大家看看哪里出问题了啊,是不是IO口的初始化有问题。、单片机1602液晶显示的DS1302实时时钟帮我分析以下程序运行步骤,详细解释一下,尤其是Bcd转换等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/10109915.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存