新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 12864液晶(AVR_Mega128)的頭文件

        12864液晶(AVR_Mega128)的頭文件

        作者: 時間:2016-11-25 來源:網絡 收藏
        大家使用時把這段代碼保存成.h的頭文件,在主程序中包含這個頭文件就行
        根據你們液晶電路更改下面的端口宏定義即可
        //CPU:ATmega128; 時鐘頻率為16MHz
        //編譯環境為ICCAVR
        //頭文件
        #include
        #include
        #include<avrdef.h>
        //數據端口定義
        #define Dat_Port_Write PORTA
        #define Dat_Port_Read PINA
        //控制端口及相應的位
        #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
        //延時函數
        void Delay_Ms(unsigned int ms)
        {
        for(;ms>1;ms--);
        }
        //寫數據
        void Write_Data_LCM(unsigned char WDLCM)
        {
        Read_Status_LCM(); //檢測忙
        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; //輸出數據
        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(); //根據需要檢測忙
        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);
        }
        //讀狀態:檢測忙
        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); //三次顯示模式設置,不檢測忙信號
        Delay_Ms(1000);
        Write_Command_LCM(0x38);
        Delay_Ms(1000);
        Write_Command_LCM(0x38);
        Delay_Ms(1000);
        Write_Command_LCM(0x38); //顯示模式設置,開始要求每次檢測忙信號
        Write_Command_LCM(0x08); //關閉顯示
        Write_Command_LCM(0x01); //顯示清屏
        Write_Command_LCM(0x06); //顯示光標移動設置
        Write_Command_LCM(0x0C); //顯示開及光標設置
        }
        /


        評論


        技術專區

        關閉
        主站蜘蛛池模板: 南平市| 永济市| 靖边县| 沁水县| 同仁县| 博野县| 双柏县| 乡宁县| 太湖县| 青州市| 蒙山县| 焦作市| 呼伦贝尔市| 南汇区| 盘山县| 都匀市| 江油市| 同心县| 柳江县| 昭觉县| 尤溪县| 康平县| 英超| 丽江市| 安龙县| 济南市| 新竹县| 栾城县| 许昌县| 灵石县| 师宗县| 海城市| 黄梅县| 七台河市| 盐亭县| 黔东| 顺平县| 温州市| 南川市| 万源市| 三亚市|