借鑒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;
- }
- }
復制代碼
|