新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > PIC16F877單片機驅動1602液晶屏的數字時鐘程序

        PIC16F877單片機驅動1602液晶屏的數字時鐘程序

        作者: 時間:2016-11-29 來源:網絡 收藏
        4MHz時鐘--PIC-KIT3。
        main.c:
        #include
        #include
        #include "Display.h"
        #include "main.h"
        __CONFIG(WDTDIS & LVPDIS & HS & PWRTDIS & BORDIS); //設置877配置位
        /************************定義顯示字符*****************************************/
        uchar Welcome_1[] = {" Welcome To Use "};
        //uchar Welcome_2[] = {" DSK-27 System !"};
        //uchar Power_On1[] = {" Power On "};
        //uchar Auto_Mode1[] = {" Automatic Mode "};
        uchar Time_Now[] = {"Time Now"};
        uchar Time_Now_buf[6] = {0x00,0x00,0x00,0x00,0x00,0x00}; //定義Time Now顯示緩沖單元
        uchar hour=8; //定義小時分鐘和秒變量
        uchar min=12;
        uchar sec=0;
        uchar count_10ms; //定義10ms計數器
        /*******************************************************************************
        * 函 數 名: 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++);
        }
        }
        /*********定時器1初始化**********/
        void Timer1_Init(void)
        {
        GIE = 1; //開總中斷
        PEIE = 1; //開外圍功能模塊中斷
        T1CKPS0=1;T1CKPS1=1; //分頻比為1:8
        TMR1CS = 0; //設置為定時功能
        TMR1IE = 1; //使能TMRI中斷
        TMR1ON = 1; //啟動定時器TMR1
        TMR1H = 0xfb; //設置計數值高位,定時時間為10ms
        TMR1L = 0x1e; //設置計數值高低位
        }
        /*******中斷服務函數,用于產生秒、分鐘和小時信號*******/
        void interrupt ISR(void)
        {
        if(TMR1IF==1)
        {
        TMR1IF=0;
        TMR1H = 0xfb;
        TMR1L = 0x1e;
        count_10ms++;
        if(count_10ms>=100)
        {
        count_10ms=0;
        sec++;
        if(sec==60)
        {
        sec=0;
        min++;
        if(min==60)
        {
        min=0;
        hour++;
        if(hour==24)
        {
        hour=0;min=0;sec=0; //若到24h,小時、分鐘和秒單元清零
        }
        }
        }
        }
        }
        Time_Now_buf[0] = hour/10; //小時十位數據--轉換并存儲時鐘數據
        Time_Now_buf[1] = hour%10; //小時個位數據
        Time_Now_buf[2] = min/10; //分鐘十位數據
        Time_Now_buf[3] = min%10; //分鐘個位數據
        Time_Now_buf[4] = sec/10; //秒鐘十位數據
        Time_Now_buf[5] = sec%10; //秒鐘個位數據
        }
        /******************************************************************************
        * 函 數 名: main()
        * 函數功能: LCD顯示字符
        * 入口參數: 無
        * 返 回: 無
        *******************************************************************************/
        void main()
        {
        uchar j=0;
        Port_1602_Init();
        INIT_1602();
        TRISA3=0; //RA3和RA5已經初始化為普通IO口,此處只需設定方向,可以作為后續輸出口使用
        TRISA5=0;
        Delay_Ms(200);
        Display_1602_string(0,0,16,Welcome_1);//顯示歡迎詞
        //Display_1602_string(0,1,16,Welcome_2);//顯示歡迎詞
        Delay_Ms(100);
        Timer1_Init();
        Clear_Display();
        Display_1602_string(4,0,8,Time_Now); //顯示當前時間xx:xx:xx
        while(1)
        {
        DispNum_XY_1602(4,1,Time_Now_buf[0]);
        DispNum_XY_1602(5,1,Time_Now_buf[1]);
        DispChar_XY_1602(6,1,:);
        DispNum_XY_1602(7,1,Time_Now_buf[2]);
        DispNum_XY_1602(8,1,Time_Now_buf[3]);
        DispChar_XY_1602(9,1,:);
        DispNum_XY_1602(10,1,Time_Now_buf[4]);
        DispNum_XY_1602(11,1,Time_Now_buf[5]);
        //Clear_Display();
        //Display_1602_string(3,0,10,Power_On1); //顯示Power On
        //Delay_Ms(1000);
        //Close_Disp();
        //Display_1602_string(0,0,15,Auto_Mode1); //Automatic Mode
        //Open_Disp();
        //DispChar_XY_1602(15,1,L);
        //Delay_Ms(1000);
        }
        }
        main.h:
        #ifndef __MAIN_H__
        #define __MAIN_H__
        #define uchar unsigned char
        #define uint unsigned int
        void Delay_US(uint delay); //12微秒延時
        void Delay_Ms(uint delay); //1毫秒延時
        void Delay_Sec(uint delay); //1秒延時
        void Timer1_Init(void);
        void interrupt ISR(void);
        #endif
        Display.c:
        #include
        #include "Display.h"
        #include "main.h"

        上一頁 1 2 下一頁

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 卢龙县| 乌鲁木齐县| 阿尔山市| 汾西县| 宜君县| 开原市| 永和县| 吉水县| 滁州市| 泰和县| 轮台县| 太保市| 固镇县| 博白县| 兖州市| 南京市| 迭部县| 綦江县| 澳门| 军事| 凤城市| 盈江县| 固始县| 曲阳县| 西充县| 景谷| 凤城市| 安庆市| 新泰市| 资阳市| 枣强县| 深州市| 常宁市| 光山县| 从化市| 多伦县| 股票| 石阡县| 沾化县| 宁阳县| 台中县|