LCD1602调试工具
生活随笔
收集整理的這篇文章主要介紹了
LCD1602调试工具
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
LCD作為顯示屏是單片機工作返回數據最直觀的體現。
下面將以模塊化編程的思想調用LCD進行顯示。
如下代碼就是LCD的模塊,顯示時調用即可;
#include <REGX52.H>sbit LCD_RS=P2^6; sbit LCD_RW=P2^5; sbit LCD_EN=P2^7; #define LCD_DataPort P0void LCD_Delay() {unsigned char i, j;i = 2;j = 239;do{while (--j);} while (--i); }void LCD_WriteCommand(unsigned char Command) {LCD_RS=0;LCD_RW=0;LCD_DataPort=Command;LCD_EN=1;LCD_Delay();LCD_EN=0;LCD_Delay(); }void LCD_WriteData(unsigned char Data) {LCD_RS=1;LCD_RW=0;LCD_DataPort=Data;LCD_EN=1;LCD_Delay();LCD_EN=0;LCD_Delay(); }void LCD_SetCursor(unsigned char Line,unsigned char Column) {if(Line==1){LCD_WriteCommand(0x80|(Column-1));}else if(Line==2){LCD_WriteCommand(0x80|(Column-1+0x40));} }void LCD_Init() {LCD_WriteCommand(0x38);LCD_WriteCommand(0x0c);LCD_WriteCommand(0x06);LCD_WriteCommand(0x01); }void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char) {LCD_SetCursor(Line,Column);LCD_WriteData(Char); }void LCD_ShowString(unsigned char Line,unsigned char Column,char *String) {unsigned char i;LCD_SetCursor(Line,Column);for(i=0;String[i]!='\0';i++){LCD_WriteData(String[i]);} }int LCD_Pow(int X,int Y) {unsigned char i;int Result=1;for(i=0;i<Y;i++){Result*=X;}return Result; }void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length) {unsigned char i;LCD_SetCursor(Line,Column);for(i=Length;i>0;i--){LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');} }void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length) {unsigned char i;unsigned int Number1;LCD_SetCursor(Line,Column);if(Number>=0){LCD_WriteData('+');Number1=Number;}else{LCD_WriteData('-');Number1=-Number;}for(i=Length;i>0;i--){LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');} }void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length) {unsigned char i,SingleNumber;LCD_SetCursor(Line,Column);for(i=Length;i>0;i--){SingleNumber=Number/LCD_Pow(16,i-1)%16;if(SingleNumber<10){LCD_WriteData(SingleNumber+'0');}else{LCD_WriteData(SingleNumber-10+'A');}} }void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length) {unsigned char i;LCD_SetCursor(Line,Column);for(i=Length;i>0;i--){LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');} }當然考慮到顯示屏更新顯示時速度較快,人眼不能識別出來,所以還有一個延遲函數,用于消隱(這里就不書寫了,前一章有)以下是主函數調用的方法:LCD1602的.h文件
#ifndef __LCD1602_H__ #define __LCD1602_H__void LCD_Init(); void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char);// (1,1,'A')顯示一個字符;一行:一列:字符 void LCD_ShowString(unsigned char Line,unsigned char Column,char *String);//(1,3,"Hello")顯示字符串;若超出那一排則會缺失; void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);//(1,9,123,3)顯示十進制數字;最后的“3”是指顯示數字的長度(因為定義類型為0~65535所以范圍是1~5),本例子若最后一位為“2”則不顯示最高位即為“23”; void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);//(1,13,-66,2)顯示有符號十進制數字;符號不算在“2”中,但符號顯示是從13位開始; void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);//(2,1,0xA8,2)顯示十六進制數字; void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);//(2,4,0xAA,8)顯示二進制數字;#endif主函數:
#include <REGX52.H> #include "LCD1602.h" #include "Delay.h"int Result=0;void main() {LCD_INit();LCD_ShowChar(1,1,'A');LCD_ShowString(1,3,"Hello");LCD_ShowNum(1,9,123,3);LCD_ShowSignedNum(1,13,-66,2);LCD_ShowHexNum(2,1,0xA8,2);LCD_ShowBinNum(2,4,0xAA,8);while(1){Result=Result+1;LCD_ShowNum(2,14,Result,3);Delay(1000);} }總結
以上是生活随笔為你收集整理的LCD1602调试工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: G17
- 下一篇: 东软mysql期末题库_东软大三上学期实