?? lcd1602.c
字號:
//LCD1602文件
#include<reg52.h>
#include <stdio.h>
#include <INTRINS.H>
#include <Lcd_1602.h>
#include <Time_Delay.h>
#define LCD_DATA P0 //LCD1602 data transfer define
#define uint unsigned int
#define uchar unsigned char
/*只由主函數調用的 有
Init_Lcd()
LCD_write_str(uchar X,uchar Y,uchar *s)
LCD_value(unsigned char x,unsigned char y,float f)
*/
sbit LCD_RS = P2^5; //1602 control define
sbit RW = P2^6;
sbit LCD_E = P2^7;
/**********************************************************************
*****
#define LCD_SCREEN_ON 0x0C //顯示開
#define LCD_SCREEN_OFF 0x08 //顯示關
#define LCD_CURSOR_ON 0x0A //顯示光標
#define LCD_CURSOR_OFF 0x0c //無光標
#define LCD_C_FLASH_ON 0x0f //有光標,光標閃動
#define LCD_C_FLASH_OFF 0x0e //有光標,光標不閃動
//進入模式設置指令
#define LCD_AC_UP 0x06 //新數據后光標右移
#define LCD_AC_DOWN 0x04 //新數據后光標左移
#define LCD_S_MOVE_ON 0x05 // 畫面可平移
#define LCD_S_MOVE_OFF 0x04 //畫面不可平移
//設定顯示屏或光標移動方向指令
#define LCD_C_LEFT 0x10 //光標左移1格,且AC值減1
#define LCD_C_RIGHT 0x11 //光標右移1格,且AC值加1
#define LCD_CHAR_LEFT 0x18 //顯示器上字符全部左移一格,但光標不動
#define LCD_CHAR_RIGHT 0x1C //顯示器上字符全部右移一格,但光標不動
***********************************************************************
****/
//注 有主函數調用的函數都已作說明 其他函數一般不由主函數調用
/**********************************************************************
******
* 名 稱:Init_Lcd() 主函數調用
* 功 能:Lcd初始化
* 入口參數:無
* 出口參數:無
* 范 例: 在主函數中直接調用
***********************************************************************
*****/
void Init_Lcd() //LCD初始化
{
LCD_write_char(0x38,0);
Delay_ms(1);
LCD_write_char(0x38,0);
Delay_ms(1);
LCD_write_char(0x38,0);
Delay_ms(1);
LCD_write_char(0x0c,0);
Delay_ms(1);
LCD_write_char(0x06,0);
Delay_ms(1);
LCD_write_char(0x0c,0);
Delay_ms(1);
//
}
/**********************************************************************
******
* 名 稱:LCD_write_str(uchar X,uchar Y,uchar *s)主函數調用
* 功 能:在指定地址寫一個字符串 eg:Y=0,1,2,3,4,5,6,7,8,9,10...15。
X=0,1。
* 入口參數:X:橫坐標 Y:縱坐標 *s:字符串首地址
* 出口參數:無
* 范 例: LCD_write_str(1,1,uchar *s)
***********************************************************************
*****/
void LCD_write_str(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_write_char(0,' ');
LCD_set_xy( X, Y ); //寫地址
while (*s) // 寫顯示字符
{
LCD_write_char( 0, *s );
s ++;
}
}
/**********************************************************************
******
* 名 稱:LCD_set_xy( uchar x, uchar y ) the optic sign
flash?
* 功 能:指定一個地址
* 入口參數:X:橫坐標 Y:縱坐標
* 出口參數:無
* 范 例: LCD_set_xy(5,1)
*************CD_set_xy*************************************************
**************/
void LCD_set_xy( uchar x, uchar y ) //寫地址函數
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else
address = 0xc0 + x;
LCD_write_char( address, 0 );
}
/**********************************************************************
******
* 名 稱:LCD_en_write(void)
* 功 能:液晶使能
* 入口參數:無
* 出口參數:無
* 范 例: 直接調用
*************CD_set_xy*************************************************
**************/
void LCD_en_write(void) //液晶使能
{
_nop_();
LCD_E=1;//EN=1
_nop_();
LCD_E=0;//EN=0
}
//------------------------------------------------
/**********************************************************************
******
* 名 稱:LCD_write_char(uchar cd,uchar ab)
* 功 能:寫指令或數據 當寫ab時 應使cd=0 當cd不為0 則寫cd 且ab
的賦值無效
* 入口參數:cd:指令內容 ab:數據內容 指令常量已在上面定義 但一般不
用
* 出口參數:無
* 范 例: LCD_write_char( 0, ‘f’)
*************LCD_set_xy************************************************
***************/
void LCD_write_char(uchar cd,uchar ab) // 寫數據
{
Delay_us(20);
if(cd==0)
{
LCD_RS=1; //RS=1,寫顯示內容
LCD_byte(ab);
}
else
{
LCD_RS=0; //RS=0,寫命令
LCD_byte(cd);
}
}
/**********************************************************************
******
* 名 稱:LCD_byte(abc);
* 功 能:寫一個字符到 or called one byte to LCD中
* 入口參數:
* 出口參數:無
*************LCD_set_xy************************************************
***************/
void LCD_byte(unsigned char abc)
{
RW = 0;
LCD_E = 0;
LCD_DATA = abc;
LCD_en_write();
}
//在液晶中顯示浮點數函數
LCD_value(unsigned char x,unsigned char y,float f)
{
unsigned char str[15]; //不能定義為char* str,數組長
度一定要大于浮點數的總位數
sprintf(str,"%.1f",f); //1表示小數位數 小數太多 自動四舍
五入
LCD_write_str( x, y, str);
return 0;
}
//主函數文件
#include <reg52.h>
#include <intrins.h>
#include <Lcd_1602.h>
#include <Time_Delay.h>
#include"DHT11.h"
extern float F16T,F16RH; //全局變量聲明
void main ()
{
Init_Lcd();
LCD_write_str(0,1,"abc"); //液晶預顯示測試
LCD_value(0,0,34.345);
Delay_ms(2000);
Init_Lcd();
while(1)
{
getDHT11();
LCD_write_str(0,0,"T=");
LCD_value(3,0,F16T); LCD_write_str(8,0,"\"C"); //字符" 應用轉
義格式
LCD_write_str(0,1,"RH=");
LCD_value(4,1,F16RH); LCD_write_str(9,1,"%");
Delay_ms(500);
}
}
//延時函數文件
//以下為延時函數 this is fit to old C51 12MHz, 12 devide freqency
void Delay_ms(unsigned int n)//n毫秒延時
{
unsigned char j
while(n--)
{
for(j=0;j<125;j++);
}
}
void Delay_us(unsigned char n) //N us延時函數 精度 ±4us
{
n=n/2;
while(--n);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -