詳細LCD1602串行或者并行數(shù)據(jù)傳輸51單片機程序(自編)
0.png (36.6 KB, 下載次數(shù): 91)
下載附件
2017-7-28 15:49 上傳
單片機串行源程序如下:
- #include "reg52.h"
- #include <intrins.h>
-
- //LCD1602接口定義
- #define LCD_DATA P1 //P1口(P14~P17)與LCD高四位(D4~D7)對應相接
- sbit LCD1602_RS=P1^2; //數(shù)據(jù)指令
- sbit LCD1602_EN=P1^3; //使能
-
- //延時函數(shù),12mhz 6t延遲時間10*255us
- void LCD_init(void);
- void LCD_en_write(void);
- void LCD_write_command(unsigned char command) ;
- void LCD_write_data(unsigned char Recdata);
- void LCD_set_xy (unsigned char x, unsigned char y);
- void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);
- void LCD_write_char(unsigned char X,unsigned char Y,unsigned char Recdata);
- void delay_nus(unsigned int n);
- void delay_nms(unsigned int n);
-
- //以下函數(shù)用于輸出字符串和數(shù)字
- int LCD_PutNum(unsigned long num,int XS,int pos);
- int LCD_PutStr(unsigned char *DData,int pos);
-
- //-------------------------1us延時函數(shù)---------------------------------
- void delay_1us(void)
- {
- _nop_();
- }
- //------------------------N us延時函數(shù)---------------------------------
- void delay_nus(unsigned int n)
- {
- unsigned int i=0;
- for (i=0;i<n;i++)
- delay_1us();
- }
- //-----------------------1ms延時函數(shù)----------------------------------
- void delay_1ms(void)
- {
- unsigned int i;
- for (i=0;i<1140;i++);
- }
- //-----------------------N ms延時函數(shù)---------------------------------
- void delay_nms(unsigned int n)
- {
- unsigned int i=0;
- for (i=0;i<n;i++)
- delay_1ms();
- }
- //------------------------液晶初始化-----------------------------------
- void LCD_init(void)
- {
- LCD_write_command(0x28);
- delay_nus(40);
- LCD_write_command(0x28);
- delay_nus(40);
- LCD_write_command(0x28);
- delay_nus(40);
-
- LCD_en_write();
- delay_nus(40);
- LCD_write_command(0x28); //4位顯示
- LCD_write_command(0x0c); //顯示開
- LCD_write_command(0x01); //清屏
- delay_nms(2);
- }
-
- //----------------------液晶使能函數(shù)-------------------------------------
- void LCD_en_write(void)
- { //EN由高電平跳變到低電平時液晶使能
- LCD1602_EN=1;
- delay_nus(1);
- LCD1602_EN=0;
- }
- //------------------------寫指令函數(shù)--------------------------------------
- void LCD_write_command(unsigned char command)
- {
- delay_nus(16);
- LCD1602_RS=0; //RS=0
- LCD_DATA &=0X0f; //清高四位
- LCD_DATA|=command&0xf0; //寫高四位
- LCD_en_write();
- command=command<<4; //低四位移到高四位
- LCD_DATA&=0x0f; //清高四位
- LCD_DATA|=command&0xf0; //寫低四位
- LCD_en_write();
- }
-
- //------------------------寫數(shù)據(jù)函數(shù)--------------------------------------
- void LCD_write_data(unsigned char Recdata)
- {
- delay_nus(16);
- LCD1602_RS=1; //RS=1
- LCD_DATA&=0X0f; //清高四位
- LCD_DATA|=Recdata&0xf0; //寫高四位
- LCD_en_write();
- Recdata=Recdata<<4; //低四位移到高四位
- LCD_DATA&=0X0f; //清高四位
- LCD_DATA|=Recdata&0xf0; //寫低四位
- LCD_en_write();
- }
-
- //-----------------------地址定位函數(shù)-------------------------------------
- void LCD_set_xy( unsigned char x, unsigned char y )
- {
- unsigned char address;
- if (y == 0) address = 0x80 + x;
- else address = 0xc0 + x;
- LCD_write_command(address);
- }
-
-
- //----------------------在某個地址處,寫一個字符----------------------------
- void LCD_write_char(unsigned char X,unsigned char Y,unsigned char Recdata) //列x=0~15,行y=0,1
- {
- LCD_set_xy(X, Y); //寫地址
- LCD_write_data(Recdata);
- }
- //----------------------------輸出字符串-------------------------------------
- int LCD_PutStr(unsigned char *DData,int pos) //pos表示字符顯示位置,0~31
- {
- unsigned char i;
- if(pos==-1)
- {
- LCD_write_command(0x01); //清屏
- delay_nms(2);
- pos=0;
- }
- while((*DData)!='\0')
- {
- switch(*DData)
- {
- case '\n': //如果是\n,則換行
- {
- if(pos<17)
- {
- for(i=pos;i<16;i++) LCD_write_char(i%16, i/16,' ');
- pos=16;
- }
- else
- {
- for(i=pos;i<32;i++) LCD_write_char(i%16, i/16,' ');
- pos=0;
- }
- break;
- }
-
- case '\b': //如果是\b,則退格
- {
- if(pos>0) pos--;
- LCD_write_char(pos%16, pos/16, ' ');
- break;
- }
-
- default:
- {
- if((*DData)<0x20) //小于0x20的顯示不了?
- {
- *DData=' ';
- }
-
- LCD_write_char(pos%16, pos/16,*DData);
- pos++;
- break;
- }
- }
- DData++;
- }
- return(pos);
- }
- //----------------------------輸出數(shù)字---------------------------------
- //這段程序寫法我的理解大致是這樣:num為去掉小數(shù)點之后的數(shù)字,XS為小數(shù)點
- //從左數(shù)第幾位,pos為顯示的位置
- //---------------------------------------------------------------------
- int LCD_PutNum(unsigned long num,int XS,int pos)
- {
- unsigned long tmp=0;
- unsigned char numbits=0; //總數(shù)字位數(shù)
- if(pos==-1)
- {
- LCD_write_command(0x01);
- delay_nms(2);
- pos=0;
- }
-
- if(num==0)
- {
- LCD_write_char(pos%16, pos/16, '0');
- pos++;
- }
- else
- {
- if(num<0)
- {
- LCD_write_char(pos%16, pos/16, '-');
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
主函數(shù)還沒有寫,大家可以按自己寫需求寫,程序存在問題還很多,請多多指教,一起進步:
LCD1602.zip
(61.29 KB, 下載次數(shù): 101)
2017-7-28 10:38 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|