新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 12864液晶(AVR_Mega128)的頭文件

12864液晶(AVR_Mega128)的頭文件

作者: 時(shí)間:2016-11-25 來(lái)源:網(wǎng)絡(luò) 收藏
大家使用時(shí)把這段代碼保存成.h的頭文件,在主程序中包含這個(gè)頭文件就行
根據(jù)你們液晶電路更改下面的端口宏定義即可
//CPU:ATmega128; 時(shí)鐘頻率為16MHz
//編譯環(huán)境為ICCAVR
//頭文件
#include
#include
#include<avrdef.h>
//數(shù)據(jù)端口定義
#define Dat_Port_Write PORTA
#define Dat_Port_Read PINA
//控制端口及相應(yīng)的位
#define RS_RW_EN_Control PORTG
#define LCM_RS 0
#define LCM_RW 1
#define LCM_EN 2
#define uchar unsigned char
#define uint unsigned int
//延時(shí)函數(shù)
void Delay_Ms(unsigned int ms)
{
for(;ms>1;ms--);
}
//寫數(shù)據(jù)
void Write_Data_LCM(unsigned char WDLCM)
{
Read_Status_LCM(); //檢測(cè)忙
Delay_Ms(100);
RS_RW_EN_Control|=BIT(LCM_RS); //RS=1
Delay_Ms(100);
RS_RW_EN_Control&=~BIT(LCM_RW); //RW=0
Delay_Ms(100);
RS_RW_EN_Control|=BIT(LCM_EN); //EN=1
Delay_Ms(100);
Dat_Port_Write=WDLCM; //輸出數(shù)據(jù)
Delay_Ms(100);
RS_RW_EN_Control&=~BIT(LCM_EN); //EN=0
Delay_Ms(100);
}
//寫指令
void Write_Command_LCM(unsigned char WCLCM)
{
Read_Status_LCM(); //根據(jù)需要檢測(cè)忙
Delay_Ms(100);
RS_RW_EN_Control&=~BIT(LCM_RS); //RS=0
Delay_Ms(100);
RS_RW_EN_Control&=~BIT(LCM_RW); //RW=0
Delay_Ms(100);
RS_RW_EN_Control|=BIT(LCM_EN); //EN=1
Delay_Ms(100);
Dat_Port_Write=WCLCM; //輸出指令
Delay_Ms(100);
RS_RW_EN_Control&=~BIT(LCM_EN); //EN=0
Delay_Ms(100);
}
//讀狀態(tài):檢測(cè)忙
void Read_Status_LCM(void)
{
uchar temp;
uchar flag = 1;
while(flag==1)
{
DDRA=0x00; //端口A改為輸入
Dat_Port_Write = 0xff;
Delay_Ms(100);
RS_RW_EN_Control&=~BIT(LCM_RS); //RS=0
Delay_Ms(100);
RS_RW_EN_Control|=BIT(LCM_RW); //RW=1
Delay_Ms(100);
RS_RW_EN_Control|=BIT(LCM_EN); //EN=1
Delay_Ms(100);
temp = Dat_Port_Read; //讀端口A
Delay_Ms(100);
DDRA=0xff; //端口A改為
Delay_Ms(100);
RS_RW_EN_Control&=~BIT(LCM_EN); //EN=0
Delay_Ms(100);
if(temp>>7==0)
flag = 0;
}
}
//LCM初始化
void LCM_Init(void)
{
Write_Command_LCM(0x38); //三次顯示模式設(shè)置,不檢測(cè)忙信號(hào)
Delay_Ms(1000);
Write_Command_LCM(0x38);
Delay_Ms(1000);
Write_Command_LCM(0x38);
Delay_Ms(1000);
Write_Command_LCM(0x38); //顯示模式設(shè)置,開始要求每次檢測(cè)忙信號(hào)
Write_Command_LCM(0x08); //關(guān)閉顯示
Write_Command_LCM(0x01); //顯示清屏
Write_Command_LCM(0x06); //顯示光標(biāo)移動(dòng)設(shè)置
Write_Command_LCM(0x0C); //顯示開及光標(biāo)設(shè)置
}
/


關(guān)鍵詞: 12864液晶AVRMega128頭文

評(píng)論


技術(shù)專區(qū)

關(guān)閉