串行1602的四位數(shù)據(jù)線程序-avr單片機(jī)
1602Driver.h
/*********************LCD模塊1602底層驅(qū)動(dòng)庫(kù)***********************
* LCD模塊與單片機(jī)的硬件連接:
* /端口名稱(chēng) /端口符號(hào) /MCU端口 / LCD引腳號(hào)/
*
* RS數(shù)據(jù)/指令選擇線
* RS PD3 4
* R/W讀寫(xiě)選擇線
* R/W GND 5
* EN寫(xiě)使能線
* EN PD2 6
* 三態(tài)雙向數(shù)據(jù)總線
* D0-D3 NC 7-10
* D4 PD4 11
* D5 PD5 12
* D6 PD6 13
* D7 PD7 14
*
* 其他端口連接:
* 電源負(fù) VSS 1
* 電源正+4.5--+5.5V VDD 2
* 對(duì)比度調(diào)節(jié) VL GND 3
* 背光負(fù) BLK NC 15
* 背光正 BLA NC 16
* 說(shuō)明:采用節(jié)省端口的4線線連接方式,即將8位的數(shù)據(jù)分兩次傳輸;同
* 時(shí)將R/W接地RW(只用W不需要R,所以可以省一根線)
* 日期:2006/02/03
*****************************************************************/
/*****************定義MCU與LCD的接口******************/
#define LCD_EN_PORT PORTC //寫(xiě)使能。高有效,下降沿時(shí)鎖定數(shù)據(jù)
#define LCD_RS_PORT PORTC //指令/數(shù)據(jù)選擇
#define LCD_DATA_PORT PORTC //數(shù)據(jù)總線
#define LCD_EN 0x04 //PORTD2 out
#define LCD_RS 0x08 //PORTD3 out
#define LCD_DATA 0xf0 //PORTD4/5/6/7 out
#define Data_Write LCD_RS_PORT |= LCD_RS //寫(xiě)數(shù)據(jù)模式
#define Command_Write LCD_RS_PORT &= ~LCD_RS //寫(xiě)指令模式
#define Write_En LCD_EN_PORT |= LCD_EN //寫(xiě)使能
#define Write_Lock LCD_EN_PORT &= ~LCD_EN //鎖定數(shù)據(jù)
//#define Graph_Right_Run LCD_Write_Char(0x02,0) //畫(huà)面右移
const char CHR[16] = {0,1,2,3,4,5,6,7,8
,9,a,b,c,d,e,f};
unsigned char LCD_Buff[] = {0,0,0,0,0,0,0,0};
/*******************N us延時(shí)函數(shù)**********************/
void Delay_nus(unsigned int j)
{
unsigned int i="0";
for(i=j;i>0;i--)
{
asm("nop");
}
}
/********************N ms延時(shí)函數(shù)*********************/
void Delay_nms(unsigned int n)
{
unsigned int m = 0;
unsigned int k = 0;
for (m=n;m>0;m--)
{
for (k=1140;k>0;k--)
{
Delay_nus(1);
}
}
}
/******讀寫(xiě)使能(高電平有效,下降沿鎖定數(shù)據(jù) )********/
void Write_Enable(void)
{
Write_En;
Delay_nus(1);
Write_Lock;
}
/*---------------------------------------------------------------------
* LCD_Write_Char :英文單字符顯示函數(shù)
*
* 向LCD發(fā)送數(shù)據(jù)/指令。 Command="1" 發(fā)送顯示數(shù)據(jù)
* Command=0 發(fā)送指令數(shù)據(jù)
*
* 輸入?yún)?shù) :Command 數(shù)據(jù)/指令寄存器選擇
* Data 顯示內(nèi)容
*
-----------------------------------------------------------------------*/
void LCD_Write_Char(char Command,char Data)
{
unsigned Command_Temp,Data_Temp;
Command_Temp = Command;
Data_Temp = Data;
Delay_nms(1); //延時(shí)1ms,以避開(kāi)"寫(xiě)繁忙"時(shí)期
if (Command == 0) //數(shù)據(jù)輸入模式
{
Data_Write;
LCD_DATA_PORT &= 0x0f; //數(shù)據(jù)口清零
LCD_DATA_PORT |= Data_Temp&0xf0; //先送高4位
}
else //指令輸入模式
{
Command_Write;
LCD_DATA_PORT &= 0x0f;
LCD_DATA_PORT |= Command_Temp&0xf0;
}
Write_Enable();
評(píng)論