新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > PIC16F91x系列單片機驅動1602液晶屏程序

        PIC16F91x系列單片機驅動1602液晶屏程序

        作者: 時間:2016-11-29 來源:網絡 收藏
        IO口需要如下這樣配置,不用段式LCD功能,也必須配置COMCON0和LCDCON,
        /*-----------1602端口初始化-------------*/
        void Port_1602_Init(void)
        {
        PORTA=0x00;
        CMCON0=0xff;
        ANSEL = 0x00;
        TRISA=0X00;
        PORTC=0x00;
        TRISC=0x00;
        LCDCON=0x00;
        }
        完整程序如下:
        main.c如下:
        #include
        #include "Display.h"
        #include "main.h"
        #define uchar unsigned char
        #define uint unsigned int
        __CONFIG(WDTDIS & HS & PWRTDIS & BORDIS);//設置配置位
        /************************定義顯示字符*****************************************/
        uchar Wel[] = {"Welcome To"};
        uchar tab[] = {"Bei Jing"};
        /*******************************************************************************
        * 函 數 名: Delay_US(uint8 delay)
        * 函數功能: 微秒延時--12us
        * 入口參數: delay
        * 返 回: 無
        *******************************************************************************/
        void Delay_US(uint delay)
        {
        for(;delay;)
        {
        delay--;
        }
        }
        /*******************************************************************************
        * 函 數 名: Delay_MS(uint16 delay)
        * 函數功能: 毫秒延時--1ms
        * 入口參數: delay
        * 返 回: 無
        *******************************************************************************/
        void Delay_Ms(uint delay)
        {
        uint i;
        for(i=0;i
        Delay_US(83);
        }
        /*******************************************************************************
        * 函 數 名: Delay_Sec(uint16 delay)
        * 函數功能: 毫秒延時--1ms
        * 入口參數: delay
        * 返 回: 無
        *******************************************************************************/
        void Delay_Sec(uint delay)
        {
        uint i,j;
        for(i=0;i<20*delay;i++)
        {
        for(j=0;j<4536;j++);
        }
        }
        /******************************************************************************
        * 函 數 名: main()
        * 函數功能: LCD顯示字符
        * 入口參數: 無
        * 返 回: 無
        *******************************************************************************/
        void main()
        {
        Port_1602_Init();
        INIT_1602();
        Display_1602_string(3,0,10,Wel);
        Display_1602_string(4,1,8,tab);
        while(1)
        {
        }
        }
        main.h如下:
        #ifndef __MAIN_H__
        #define __MAIN_H__
        #define uchar unsigned char
        #define uint unsigned int
        #define LCD_RS RA0
        #define LCD_RW RA1
        #define LCD_EN RA2
        void Delay_US(uint delay); //12微秒延時
        void Delay_Ms(uint delay); //1毫秒延時
        void Delay_Sec(uint delay); //1秒延時
        #endif
        Display.c如下:
        #include
        #include "Display.h"
        #include "main.h"
        /*******************************************************************************
        * 函 數 名: uchar Chk_1602_busy(void)
        * 函數功能: 讀液晶忙通道數據
        * 入口參數: 無
        * 返 回: 無
        *******************************************************************************/
        uchar Chk_1602_busy(void)
        {
        uint gR_data;
        uint gwait_time=0xff; //設置忙超時數
        LCD_RS=0; //表示狀態
        LCD_RW=1; //選擇讀
        LCD_EN=1;
        TRISC = 0xFF; //接收口設為輸入口
        Delay_US(30);
        gR_data=PORTC;
        while(TESTBIT(gR_data,7)) //表示busy
        {
        --gwait_time;
        if(!gwait_time)
        { LCD_EN=0;TRISC = 0x00; return 0; }
        }
        LCD_EN=0;
        TRISC = 0x00; //恢復為輸出口
        return 1;
        }
        /******************************************************************************
        * 函 數 名: void Write_1602_command(uchar gcmd,uchar gvalue)
        * 函數功能: 寫指令
        * 入口參數: gcmd--指令 gvalue--是否查忙
        * 返 回: 無
        *******************************************************************************/
        void Write_1602_command(uchar gcmd,uchar gvalue)
        {
        if(gvalue) //寫命令時大部分情況下是在LCD空閑模式下寫
        {
        if(Chk_1602_busy())
        {
        LCD_RS=0; //選擇指令
        LCD_RW=0; //選擇寫
        PORTC=gcmd; //賦指令
        LCD_EN=1; //使能
        Delay_US(30);
        LCD_EN=0;
        }
        }
        else
        {
        LCD_RS=0; //選擇指令
        LCD_RW=0; //選擇寫
        PORTC=gcmd; //賦指令
        LCD_EN=1; //使能
        Delay_US(30);
        LCD_EN=0;
        }
        }

        上一頁 1 2 下一頁

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 乐平市| 江达县| 西城区| 合川市| 绥德县| 富平县| 昭苏县| 白河县| 河源市| 鱼台县| 通河县| 富平县| 嘉善县| 望都县| 电白县| 乌拉特中旗| 北流市| 富平县| 瑞安市| 岳阳市| 松桃| 鄂伦春自治旗| 东阳市| 靖安县| 巫溪县| 庆城县| 景谷| 离岛区| 石家庄市| 庆安县| 嘉禾县| 江陵县| 宁都县| 四平市| 和平区| 昭通市| 连平县| 阿拉善左旗| 涡阳县| 图片| 思茅市|