新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 字符型液晶1602檢測獨(dú)立鍵盤

字符型液晶1602檢測獨(dú)立鍵盤

作者: 時間:2016-11-20 來源:網(wǎng)絡(luò) 收藏
文件名:define.h

描述:定義宏

本文引用地址:http://m.butianyuan.cn/article/201611/318914.htm

#define uchar unsigned char
#define uint unsigned int
#define flase 0
#define true 1
#define RS 5
#define RW 6
#define EN 7


//文件名:key.h
//描述:聲明關(guān)于鍵盤的函數(shù)

#include"define.h"
uchar get_key0(void);
uchar get_key1(void);
uchar get_key2(void);
uchar get_key3(void);


//文件名:key.c
//描述:定義關(guān)于鍵盤的函數(shù)

#include
#include
#include"define.h"
#inclued"key.h"


//函數(shù)名:get_key0
//輸入:無
//輸出:有按鍵動作:true;無按鍵動作:flase
//描述:按鍵狀態(tài)的檢測

uchar get_key0(void)
{
static uchar key_flag=0;
if(key_flag==0)
{
if((PIND&0X01)==0X00)
{
key_flag=1;
}
}
else if(key_flag==1)
{
if((PIND&0X01)==0X01)
{
key_flag=0;
return(true);
}
}
return(flase);
}

uchar get_key1(void)
{
static uchar key_flag=0;
if(key_flag==0)
{
if((PIND&0X02)==0X00)
{
key_flag=1;
}
}
else if(key_flag==1)
{
if((PIND&0X02)==0X02)
{
key_flag=0;
return(true);
}
}
return(flase);
}

uchar get_key2(void)
{
static uchar key_flag=0;
if(key_flag==0)
{
if((PIND&0X04)==0X00)
{
key_flag=1;
}
}
else if(key_flag==1)
{
if((PIND&0X04)==0X04)
{
key_flag=0;
return(true);
}
}
return(flase);
}

uchar get_key3(void)
{
static uchar key_flag=0;
if(key_flag==0)
{
if((PIND&0X08)==0X00)
{
key_flag=1;
}
}
else if(key_flag==1)
{
if((PIND&0X08)==0X08)
{
key_flag=0;
return(true);
}
}
return(flase);
}


//文件名:LCD.h
//描述:聲明關(guān)于液晶1602的函數(shù)

#include"define.h"
void LCD_Reset(void);
void LCD_Show_Char(unsigned char x,uchar y,uchar data);
void LCD_Show_String(uchar x,uchar y,uchar * ptr);


//文件名:LCD.c
//描述:定義1602的底層驅(qū)動函數(shù)和上層應(yīng)用函數(shù)

#include
#include
#include"define.h"
#include"LCD.h"

#define RS_0 PORTA &=~BIT(RS)
#define RS_1 PORTA|=BIT(RS)
#define RW_0 PORTA&=~BIT(RW)
#define RW_1 PORTA|=BIT(RW)
#define EN_0 PORTA&=~BIT(EN)
#define EN_1 PORTA|=BIT(EN)

void Set_Data(uchar data);
void LCD_Write_Command(uchar command,uchar state);
void LCD_Write_Data(uchar data,uchar state);
uchar LCD_Read_Data(uchar state);
uchar LCD_Busy(void);
void delay_ms(uint ms);

//函數(shù)名;Set_Data
//輸入:數(shù)據(jù)data
//輸出:無
//描述:送數(shù)據(jù)data至B口


void Set_Data(uchar data)
{
PORTB=data;
}

//函數(shù)名;LCD_Busy
//輸入:無
//輸出:0=LCD空閑;0X80=LCD忙碌
//描述:LCD忙碌檢測函數(shù)

uchar LCD_Busy(void)
{
uchar state;
DDRB=0X00;
EN_0;
RS_0;
RW_1;
EN_1;
NOP();
state=PINB;
EN_0;
DDRB=0XFF;
return(state&0X80);
}

//函數(shù)名;LCD_Write_Command
//輸入:LCD寫入指令command,state=1時檢測LCD是否忙碌
//輸出:無
//描述:向LCD控制寄存器寫入指令

void LCD_Write_Command(uchar command ,uchar state)
{
if(state==1)
{
while(LCD_Busy()!=0X00){;}
}
EN_0;
RS_0;
RW_0;
Set_Data(command);
EN_1;
NOP();
NOP();
EN_0;
}

//函數(shù)名;LCD_Write_Data
//輸入:LCD寫入數(shù)據(jù)data,state=1時檢測LCD是否忙碌
//輸出:無
//描述:向LCD數(shù)據(jù)寄存器寫入數(shù)據(jù)

void LCD_Write_Data(uchar data,uchar state)
{
if(state==1)
{
while(LCD_Busy()!=0X00){;}
}
EN_0;
RS_1;
RW_0;
Set_Data(data);
EN_1;
NOP();
NOP();
EN_0;
}

//函數(shù)名;LCD_Read_Data
//輸入:state=1時檢測LCD是否忙碌
//輸出:讀出的數(shù)據(jù)
//描述:讀LCD數(shù)據(jù)寄存器的數(shù)據(jù)


uchar LCD_Read_Data(uchar state)
{
uchar data;
if(state==1)
{
while(LCD_Busy()!=0X00){;}
}
DDRB=0X00;
EN_0;
RS_1;
RW_1;
EN_1;
data=PINB;
NOP();
NOP();
EN_0;
DDRB=0XFF;
return(data);
}

//函數(shù)名;LCD_Reset
//輸入:無
//輸出:無
//描述:LCD軟復(fù)位

void LCD_Reset(void)
{
delay_ms(15);
LCD_Write_Command(0X38,0);
delay_ms(5);
LCD_Write_Command(0X38,0);
delay_ms(5);
LCD_Write_Command(0X38,0);
LCD_Write_Command(0X38,1);
LCD_Write_Command(0X08,1);
LCD_Write_Command(0X01,1);
LCD_Write_Command(0X06,1);
LCD_Write_Command(0X0c,1);
}

//函數(shù)名;LCD_Show_Char
//輸入:x-LCD行(0--1);y-LCD列(0--15);data-顯示的字符
//輸出:無
//描述:在LCD的特定位置顯示一個字符

void LCD_Show_Char(uchar x,uchar y,uchar data)
{
uchar address;
if((x<2)&&(y<16))
{
address=0x40*x+y;
LCD_Write_Command(0X80|address,1);
LCD_Write_Data(data,1);
}
}

//函數(shù)名;LCD_Show_String
//輸入:x-LCD行(0--1);y-LCD列(0--15);ptr-字符串首地址
//輸出:無
//描述:在LCD的特定位置連續(xù)顯示一個字符串

void LCD_Show_String(uchar x,uchar y,uchar* ptr)
{
uchar x_address=x;
uchar y_address=y;
while(*ptr!=