新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > C51單片機模塊化編程萬年歷設計

        C51單片機模塊化編程萬年歷設計

        作者: 時間:2016-11-24 來源:網絡 收藏
        原文地址:歷設計作者:給力哈哈




        程序如下:
        main.h

        本文引用地址:http://www.104case.com/article/201611/320760.htm

        #ifndef __MAIN_H__
        #define __MAIN_H__

        #include
        #include
        #include
        #define uchar unsigned char
        #define uint unsigned int
        #define DQ P3_7
        #endif

        ds18b20.h

        #include "main.h"
        uint sec;
        uint min=41;
        uint hour=18;
        uint day=20;
        uint month=4;
        uint yearl=11;
        uint yearh=20;
        uint tcnt;
        uint cursor=0;
        uchar a=0xff;
        uchar code Seg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

        void delay(uint t)
        {
        uint i;
        while(t--)
        {for (i=0;i<125;i++);}
        }
        void Tdelay(unsigned int i)
        {
        while(i--);
        }
        void Kdelay(unsigned int z)
        {
        uchar i,j;
        for(i=0;i for(j=248;j>0;j--);
        }
        Init_DS18B20(void)
        {
        unsigned char x = 0;
        DQ = 1;
        Tdelay(200);
        DQ=0;
        Tdelay(80);
        DQ=1;
        Tdelay(200);
        Tdelay(200);
        }
        //讀一個字節
        ReadOneChar(void)
        {
        unsigned char i=0;
        unsigned char dat = 0;
        for (i=8;i>0;i--)
        {
        DQ = 0;
        dat>>=1;
        DQ = 1;
        if(DQ)
        dat|=0x80;
        Tdelay(4);
        }
        return(dat);
        }

        //寫一個字節
        WriteOneChar(unsigned char dat)
        {
        unsigned char i=0;
        for (i=8; i>0; i--)
        {
        DQ = 0;
        DQ = dat&0x01;
        Tdelay(5);
        DQ = 1;
        dat>>=1;
        }
        }

        //讀取溫度
        ReadTemperature(void)
        {
        unsigned char a=0;
        unsigned char b=0;
        unsigned int t=0;
        float tt=0;
        Init_DS18B20();
        WriteOneChar(0xCC);
        WriteOneChar(0x44);
        Init_DS18B20();
        WriteOneChar(0xCC);
        WriteOneChar(0xBE);
        a=ReadOneChar();
        b=ReadOneChar();
        t=b;
        t<<=8;
        t=t|a;
        tt=t*0.0625;
        t= tt*10+0.5;
        return(t);
        }

        void display(uchar L1,uchar L2,uchar L3,uchar L4,uchar L5,uchar L6,uchar L7,uchar L8,uchar L9,uchar L10,uchar L11,uchar L12,uchar L13,uchar L14,uchar L15,uchar L16)
        {
        P2=0x7F;P0=L1;delay(1); //yearh
        P2=0xBF;P0=L2;delay(1); //yearh
        if(cursor==6){P2=0xDF;P0=L3;delay(1);}else{P2=0xDF;P0=L3;delay(1);} //yearl
        if(cursor==6){P2=0xEF;P0=L4;delay(1);}else{P2=0xEF;P0=L4;delay(1);} //yearl
        if(cursor==5){P2=0xF7;P0=L5;delay(1);}else{P2=0xF7;P0=L5;delay(1);} //month
        if(cursor==5){P2=0xFB;P0=L6;delay(1);}else{P2=0xFB;P0=L6;delay(1);} //month
        if(cursor==4){P2=0xFD;P0=L7;delay(1);}else{P2=0xFD;P0=L7;delay(1);} //day
        if(cursor==4){P2=0xFE;P0=L8;delay(1);}else{P2=0xFE;P0=L8;delay(1);} //day
        P2=0xFF;
        if(cursor==3){P1=0x7F;P0=L9;delay(1);}else{P1=0x7F;P0=L9;delay(1);} //hour
        if(cursor==3){P1=0xBF;P0=L10;delay(1);}else{P1=0xBF;P0=L10;delay(1);} //hour
        if(cursor==2){P1=0xDF;P0=L11;delay(1);}else{P1=0xDF;P0=L11;delay(1);} //min
        if(cursor==2){P1=0xEF;P0=L12;delay(1);}else{P1=0xEF;P0=L12;delay(1);} //min
        if(cursor==1){P1=0xF7;P0=L13;delay(1);}else{P1=0xF7;P0=L13;delay(1);} //sec
        if(cursor==1){P1=0xFB;P0=L14;delay(1);}else{P1=0xFB;P0=L14;delay(1);} //sec
        P1=0xFD;P0=L15;delay(1); //temp
        P1=0xFE;P0=L16;delay(1); //temp
        P1=0xFF;
        }

        main.c

        #include "main.h"
        #include "ds18B20.h"
        void delay(uint t);
        void Tdelay(unsigned int i);
        void Kdelay(unsigned int z);
        Init_DS18B20(void);
        ReadOneChar(void);
        WriteOneChar(unsigned char dat);
        ReadTemperature(void);
        void display(uchar L1,uchar L2,uchar L3,uchar L4,uchar L5,uchar L6,uchar L7,uchar L8,uchar L9,uchar L10,uchar L11,uchar L12,uchar L13,uchar L14,uchar L15,uchar L16);
        main()
        {
        uint i;
        TMOD=0x02; //設置模式為定時器T0的模式2 (8位自動重裝計數初值的計數值)
        TH0=0x06; //設置計數器初值,靠TH0存儲重裝的計數值X0=256-250=6
        TL0=0x06;
        TR0=1; //啟動T0
        ET0=1; //開啟定時器T0中斷允許
        EA=1; //開啟中斷總控制
        while(1)
        {
        if(P3_0==0)
        {
        Kdelay(200);
        if(P3_0==0)
        {
        cursor++;
        if(cursor>=7){cursor=0;}
        }
        }
        if(P3_1==0)
        {
        Kdelay(200);
        if(P3_1==0)
        {
        if(cursor==1){sec++;if(sec==60)sec=0;}
        if(cursor==2){min++;if(min==60)min=0;}
        if(cursor==3){hour++;if(hour==24)hour=0;}
        if(cursor==4){day++;if(day==31)day=0;}
        if(cursor==5){month++;if(month==12)month=0;}
        if(cursor==6){yearl++;if(yearl==100)yearl=0;}
        if(cursor==7){yearh++;if(yearh==30)yearh=20;}
        }
        }
        if(P3_2==0)
        {
        Kdelay(200);
        if(P3_2==0)
        {
        if(cursor==1){sec--;}
        if(cursor==2){min--;}
        if(cursor==3){hour--;}
        if(cursor==4){day--;}
        if(cursor==5){month--;}
        if(cursor==6){yearl--;}
        if(cursor==7){yearh--;}
        }
        }
        i=ReadTemperature();
        display(Seg[yearh/10],Seg[yearh],Seg[yearl/10],Seg[yearl],Seg[month/10],Seg[month],Seg[day/10],Seg[day],Seg[hour/10],Seg[hour],Seg[min/10],Seg[min],Seg[sec/10],Seg[sec],Seg[i/100],Seg[i/10]);
        }
        }

        void t0(void)interrupt 1 using 0 //t0的中斷程序
        {
        tcnt++;
        if(tcnt==4000)//定時器的定時計數,4000次250us為1秒
        {
        tcnt=0;
        P3_3=~P3_3;
        a=~a;
        sec++;
        if(sec==60)
        {
        sec=0;
        min++;
        if(min==60)
        {
        min=0;
        hour++;
        if(hour==24)
        {
        hour=0;
        day++;
        if(month==2&&((yearl==0&&yearh%4==0)||(yearl!=0&&yearl%4==0))&&day==30)day=1;
        else if(month==2&&day==29)day=1;
        else if((month==4||month==6||month==9||month==11)&&day==31)day=1;
        else if(day==32)day=1;
        if(day==1)
        {
        month++;
        if(month==13)
        {
        month=1;
        yearl++;
        if(yearl==100)
        {
        yearl=0;
        yearh++;
        if(yearh==100)
        {
        yearh=20;
        }
        }
        }
        }
        }
        }
        }
        }
        }



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 三明市| 八宿县| 磴口县| 霍山县| 沛县| 克拉玛依市| 莒南县| 会宁县| 玉山县| 华坪县| 玛曲县| 石柱| 湟源县| 满洲里市| 红安县| 华安县| 长寿区| 鹿邑县| 阿城市| 星座| 景泰县| 武穴市| 界首市| 鸡泽县| 咸阳市| 金乡县| 林西县| 阳春市| 正蓝旗| 大埔县| 忻州市| 资中县| 彩票| 胶南市| 新丰县| 武威市| 密云县| 乌兰浩特市| 东兴市| 驻马店市| 石屏县|