华维科技 单片机程序 单片机开发板 单片机学习板 单片机编程  

 

 

 

 

 

 

                        51 AVR PIC 三星 义隆 合泰 十速 松翰 现代
                                        1602液晶驱动程序
           日期: 2008-06-27              发表人: 比尔盖茨熊           文章来源:本站原创,如有转载请注明出处
//1602.c

#include<reg51.h>
#include "1602lcd.h"

#define DB0_7 P2

sbit RS=P0^5;
sbit RW=P0^6;
sbit E=P0^7;
sbit BF=P2^7;

void Delay2ms(void)
{
uint i,j;
for(i=0;i<2;i++)
for(j=0;j<120;j++)
;
}

void Read_busy(void)
{
RS=0;
RW=1;
while(1)
{
E=1;
if(BF==0)break;
E=0;
}
}

void Send_Cmd(uchar CmdDat)
{
Read_busy();
DB0_7=CmdDat;
RS=0;
RW=0;
E=1;
Delay2ms();
E=0;

}
void Send_Data(uchar DisDat)
{
Read_busy();
DB0_7=DisDat;
RS=1;
RW=0;
E=1;
Delay2ms();
E=0;
}

void ShowMessage(uchar x,uchar y,uchar Num,uchar *ptr)
{
uchar i;
if(y==1)
{
Send_Cmd(0x80+x);
}
else
{
Send_Cmd(0xC0+x);
}
for(i=0;i<Num;i++)
{
Send_Data(ptr[i]);
}
}

void Init1602(void)
{
Delay2ms();//Wait >30ms
Delay2ms();
Delay2ms();
Send_Cmd(0x30);//Function Set
Delay2ms();//Wait >39us
Send_Cmd(0x30);//Function Set
Delay2ms();//Wait >39us
Send_Cmd(0x30);//Function Set
Send_Cmd(0x38);//Set Dot 5*8 2line
Send_Cmd(0x08);//Display OFF
Send_Cmd(0x01);//Display Clear
Send_Cmd(0x0C); //Entry Mode Set
Delay2ms();//Wait >39us
}

//main.c
#include <reg51.h>
#include "1602lcd.h"

code const uchar String[]={
"Say you, say me Say it for always That s the way it should be"
};

void Delayms(uint ms);

void main(void)
{
uchar i;
Init1602();
while(1)
{
for(i=0;i<100;i++)
{
ShowMessage(0,1,16,(String+i));
Delayms(1000);
}
}
}


void Delayms(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<120;j++)
;
}
 

//1602.h

#ifndef _1602LCD_H_
#define _1602LCD_H_

#define uchar unsigned char
#define uint unsigned int

extern void Delay2ms(uint ms);
extern void Read_busy(void);
extern void Send_Cmd(uchar CmdDat);
extern void Send_Data(uchar DisDat);
extern void ShowMessage(uchar x,uchar y,uchar Num,uchar *ptr);
extern void Init1602(void);

#endif

该程序测试通过

 

                                     返回首页

 

                                          版权所有@华维科技
                                        建议浏览分辨率1024*768