借鉴ynzsc001用户提供的前期代码加以改善。亲测89,12,15系列都可以用。几个显示函数比较方便。分享给大家
单片机源程序如下:
- #include <reg52.h>
- #include "intrins.h"
- #define uchar unsigned char
- #define uint unsigned int
- sbit SCL = P3^0;
- sbit SDA = P3^1;
- uchar i;
- char ADDR = 0x4E; // PCF8574 T
- // char ADDR = 0x7e; // PCF8574 AT //如是后缀AT的用这个地址
- uchar code CGCODE[]={
- 0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,//年0x00
- 0x0F,0x09,0x0F,0x09,0x0F,0x09,0x13,0x00,//月0x01
- 0x0F,0x09,0x09,0x0f,0x09,0x09,0x0f,0x00,//日0x02
- 0x18,0x1B,0x04,0x04,0x04,0x04,0x04,0x03,//摄氏度0x03
- 0x1F,0x0A,0x0A,0x1F,0x0A,0x0A,0x0A,0x12,//开0x04
- 0x11,0x0A,0x1F,0x04,0x1F,0x04,0x0A,0x11,//关0x05
- 0x00,0x04,0x04,0x04,0x04,0x15,0x0E,0x04,//向下0x06
- 0x00,0x04,0x0E,0x15,0x04,0x04,0x04,0x04//向上0x07
- };
- //***************************** 延迟函数 ms ***********************************************
- void delay1(int y) //
- {
- ;
- while(y--)
- {
- unsigned char a,b,c;
- for(c=1;c>0;c--)
- for(b=142;b>0;b--)
- for(a=2;a>0;a--);
- }
- }
- //******************************** IIC开始********************************************
- void IIC_start(void)
- {
- SDA=1;
- _nop_();
- SCL=1;
- _nop_();_nop_(); _nop_();_nop_();_nop_();
- SDA=0;
- _nop_();_nop_();_nop_();_nop_();_nop_();
- SCL=0;
- }
- //********************************** IIC 写字节 ******************************************
- void IIC_writeByte(char temp)
- {
- char i;
- for(i=0;i<8;i++)
- {
- SDA=(bit)(temp & 0x80) ; //
- temp <<=1;
- _nop_();_nop_();
- SCL=1;
- _nop_();_nop_();_nop_();_nop_(); _nop_();
- SCL=0;
- }
- _nop_(); _nop_();_nop_();_nop_();
- SDA=1;
- _nop_(); _nop_(); _nop_();_nop_();
- SCL=1;
- _nop_();_nop_();_nop_();
- while(SDA);
- _nop_();
- SCL=0;
- }
- //******************************** 1602写命令 ********************************************
- void LCD_write_command(char comm)
- {
- char tmp;
- IIC_start();
- IIC_writeByte(ADDR);
- tmp = comm & 0xF0;
- tmp |= 0x0C;
- IIC_writeByte(tmp);
- delay1(20);
- tmp &= 0xFB; //Make EN = 0
- IIC_writeByte(tmp);
- tmp = (comm & 0x0F) << 4 ;
- tmp |= 0x0C; //RS = 0, RW = 0, EN = 1
- IIC_writeByte(tmp);
- delay1(20);
- tmp &= 0xFB; // Make EN = 0
- IIC_writeByte(tmp);
- }
- //******************************** 1602写数据 ********************************************
- void LCD_write_data(char data1)
- {
- char tmp;
- IIC_start();
- IIC_writeByte(ADDR);
- tmp = data1 & 0xF0;
- tmp |= 0x0D; //RS = 0, RW = 0, EN = 1
- IIC_writeByte(tmp);
- delay1(20);
- tmp &= 0xFB; //Make EN = 0
- IIC_writeByte(tmp);
- tmp = (data1 & 0x0F) << 4 ;
- tmp |= 0x0D; //RS = 0, RW = 0, EN = 1
- IIC_writeByte(tmp);
- delay1(20);
- tmp &= 0xFB ; // Make EN = 0
- IIC_writeByte(tmp);
- }
- //******************************** 1602初始化 ********************************************
- void UserDiy()
- {
- unsigned char i;
- LCD_write_command(0x40); //设置CGRAM地址,自定义字符共8个
- for(i=0;i<64;i++)
- {
- LCD_write_data(CGCODE[i]);
- }
- }
- void Init_Lcd(void)
- {
- LCD_write_command(0x33); //
- delay1(50) ;
- LCD_write_command(0x32); //
- delay1(50) ;
- LCD_write_command(0x28); //
- delay1(50) ;
- LCD_write_command(0x0C); //
- delay1(50) ;
- LCD_write_command(0x06); //
- delay1(50) ;
- LCD_write_command(0x01); //
- delay1(50) ;
- UserDiy();
- }
- /*****************次方函数***********************/
- int NumberPow(int x,int y)
- {
- unsigned char i;
- int Result=1;
- for(i=0;i<y;i++)
- {
- Result*=x;
- }
- return Result;
- }
- //************************设置第几行第几列***************************
- void SetHL(unsigned x,unsigned y)
- {
- if(x==1)
- LCD_write_command(0x80|(y-1));
- if(x==2)
- LCD_write_command(0x80|(y-1)+0x40);
- }
- //**************************第几行,第几列,显示数字,数字几位************************
- void LCDShowNumber(unsigned char x,unsigned char y,unsigned int Number,unsigned char NumberLength)
- {
- unsigned char i;
- SetHL(x,y);
- for(i=NumberLength;i>0;i--)
- {
- LCD_write_data('0'+Number/NumberPow(10,i-1)%10); /*加'0'是将数字转换成ASCLL码*/
- }
- }
- //*************************************** 第x行第x列写字串符 *************************************
- void LCDShowString(unsigned char x,unsigned char y,unsigned char String[]) /*unsigned char Char[]=unsigned char *Char*/
- {
- unsigned char i;
- SetHL(x,y);
- for(i=0;String[i]!='\0';i++) /*Char[]{"abc"}=Char[]{'a','b','c','\0'},'\0'为结束标志位*/
- {
- LCD_write_data(String[i]);
- }
- }
- /*第几行第几列显示一个字符*/
- void LCDShowChar(unsigned char x,unsigned char y,unsigned char Char)
- {
- SetHL(x,y);
- LCD_write_data(Char);
- }
- /********************************主函数****************************/
- void main()
- {
- Init_Lcd();
- LCDShowChar(1,1,0x00);
- LCDShowChar(1,2,0x01);
- LCDShowChar(1,3,0x02);
- LCDShowChar(1,4,0x03);
- LCDShowChar(1,5,0x04);
- LCDShowChar(1,6,0x05);
- LCDShowChar(1,7,0x06);
- LCDShowChar(1,8,0x07);
- LCDShowString(1,9,"abcdefgf");
- while(1)
- {
- for(i=1;i<100;i++)
- {
- LCDShowNumber(1,2,i,3);
- delay1(1000);
- }
- i=0;
- }
- }
复制代码
|