新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 實驗7 利用51單片機的定時器設計一個時鐘

        實驗7 利用51單片機的定時器設計一個時鐘

        作者: 時間:2016-11-19 來源:網絡 收藏
        1.利用實驗板和配件,設計一個時鐘,時間顯示在LCD1602上,并按秒更新,能夠在實驗板上設計3個按鍵調整時,分,秒。其功能為:功能選擇鍵,數值增大和數值減小鍵。利用板上AT24C02設計實現斷電保護顯示數據的功能。
        =============Clock.h=============
        #ifndef __CLOCK_H__
        #define __CLOCK_H__


        //========全局變量區============================================
        unsignedcharT_High_50ms=(65536-45872)/256;
        unsignedintT_Low_50ms=(65536-45872)%256;
        unsignedcharCount,Count_T1,Count_1s;//Count用來記錄每50ms的計數,Count_T1用來記

        //========全局變量區結束============================================
        #endif

        =============I2C.H=============
        #ifndef __I2C_H__
        #define __I2C_H__
        #include
        sbit sda=P2^0;
        sbit scl=P2^1;
        //========函數區============================================
        voidnop();
        voidinitI2C();
        externvoidI2C_start();//開始信號
        externvoidI2C_stop();//停止
        externvoidI2C_respons();//應答
        externvoidI2C_write_byte(unsignedchardate);
        externunsignedcharI2C_read_byte();
        externvoidI2C_write_address(unsignedcharaddress,unsignedchardate);
        externunsignedcharI2C_read_address(unsignedcharaddress);
        //========函數區結束============================================
        #endif

        =============OrphanKey.h=============
        #ifndef __ORPHANKEY_H__
        #define __ORPHANKEY_H__

        sbit FUNCTION_KEY=P3^2;//功能鍵--按下暫停,再按開始
        sbit Key_ADD=P3^3;//加1
        sbit KEY_MINUS=P3^4;//減1
        constbit PRESSED=0;//按下
        bit SUSPEND=0;//0的時候運行,1的時候暫停

        #endif

        =============I2C.C=============
        #include "head/I2C.h"
        //=========全局變量區============================================

        #define uchar unsigned char
        #define uint unsigned int

        bit I2C_write=0;//寫24C02的標志;

        unsignedcharsec,tcnt;

        //=========全局變量區結束============================================
        voidnop()
        {;;}

        /***********************************************************
        I2C的初始化
        ***********************************************************/
        voidinitI2C(){
        sda=1;
        nop();
        scl=1;
        nop();

        }
        voidI2C_start()//開始信號
        {
        sda=1;
        nop();
        scl=1;
        nop();
        sda=0;
        nop();
        }
        voidI2C_stop()//停止
        {
        sda=0;
        nop();
        scl=1;
        nop();
        sda=1;
        nop();
        }
        voidI2C_respons()//應答
        {
        uchar i;
        scl=1;
        nop();
        while((sda==1)&&(i<250))i++;
        scl=0;
        nop();
        }

        voidI2C_write_byte(uchar date)
        {
        uchar i,temp;
        temp=date;
        for(i=0;i<8;i++)
        {
        temp=temp<<1;
        scl=0;
        nop();
        sda=CY;
        nop();
        scl=1;
        nop();
        }
        scl=0;
        nop();
        sda=1;
        nop();
        }
        uchar I2C_read_byte()
        {
        uchar i,k;
        scl=0;
        nop();
        sda=1;
        nop();
        for(i=0;i<8;i++)
        {
        scl=1;
        nop();
        k=(k<<1)sda;
        scl=0;
        nop();
        }
        returnk;
        }
        voidI2C_write_address(uchar address,uchar date)
        {
        I2C_start();
        I2C_write_byte(0xa0);
        I2C_respons();
        I2C_write_byte(address);
        I2C_respons();
        I2C_write_byte(date);
        I2C_respons();
        I2C_stop();
        }
        uchar I2C_read_address(uchar address)
        {
        uchar date;
        I2C_start();
        I2C_write_byte(0xa0);
        I2C_respons();
        I2C_write_byte(address);
        I2C_respons();
        I2C_start();
        I2C_write_byte(0xa1);
        I2C_respons();
        date=I2C_read_byte();
        I2C_stop();
        returndate;
        }

        =============lab8_1.c=================
        #include
        #include "head/Clock.h"
        #include "head/OrphanKey.h"
        #include "head/I2C.H"
        /*
        1.利用實驗板和配件,設計一個時鐘,時間顯示在LCD1602上,并按秒更新,能夠在實驗板上設計3個按鍵調整時,分,秒。其功能為:功能選擇鍵,數值增大和數值減小鍵。利用板上AT24C02設計實現斷電保護顯示數據的功能。
        */

        /*
        步驟:
        1、設計一個中斷,用來計時
        2、設計一個字符生成函數,用來生成所需的時間
        3、獨立按鍵監測功能,用來監測按下了什么鍵
        4、一個外部中斷
        5、一個斷點保護的功能,其實就是通過I2C寫入,讀取數值
        */

        //=========全局變量區============================================
        #define uchar unsigned char
        #define uint unsigned int
        uchar code table[]="12:23:12";
        uchar code table1[]="I am a boy!";
        sbit lcden=P2^7;//液晶使能端
        sbit lcdrs=P2^6;//數據或命令控制(0代表命令,1代表數據)
        sbit LCDWR=P2^5;//讀寫控制(0代表寫,1代表讀)
        uchar num;
        uchar FIRST_LINE=0x80;
        uchar SECOND_LINE=0xc0;
        uchar Current_Time[9];
        uchar Hour,Minute,Second=0;//時、分、秒
        sbit beer=P1^4;//蜂鳴器
        // sbit led1=P1^3;

        //=========全局變量區結束============================================


        //=========函數區============================================

        /***********************************************************
        延時函數
        參數說明:
        z代表要延時z ms
        ***********************************************************/
        voiddelay(uint z)
        {
        uint x,y;
        for(x=z;x>0;x--)
        for(y=110;y>0;y--);
        }
        /***********************************************************
        寫命令或數據
        參數說明:
        isRs=1代表寫數據,isRs=0代表寫命令
        ***********************************************************/
        voidwrite(uchar mdata,uchar isRs)
        {
        lcdrs=isRs;
        P0=mdata;
        delay(5);
        lcden=1;
        delay(5);
        lcden=0;
        }
        /***********************************************************
        寫命令
        參數說明:
        data代表要寫的命令
        ***********************************************************/
        voidwrite_command(uchar com)
        {
        write(com,0);
        }
        /***********************************************************
        寫數據
        參數說明:
        data代表要寫的數據
        ***********************************************************/
        voidwrite_data(uchar mdata)
        {
        write(mdata,1);
        }

        /***********************************************************
        初始化時鐘
        ***********************************************************/
        voidInitTime(){
        //時間的轉換
        Current_Time[0]=Hour/10+0;
        Current_Time[1]=Hour%10+0;
        Current_Time[2]=:;
        Current_Time[3]=Minute/10+0;
        Current_Time[4]=Minute%10+0;
        Current_Time[5]=:;
        Current_Time[6]=Second/10+0;
        Current_Time[7]=Second%10+0;
        Current_Time[8]= 主站蜘蛛池模板: 体育| 吉安县| 武汉市| 镇远县| 从江县| 晋江市| 隆尧县| 乐安县| 通渭县| 阳城县| 阿荣旗| 金湖县| 定南县| 都昌县| 安远县| 梅河口市| 绵阳市| 怀化市| 菏泽市| 微山县| 南丰县| 鄂伦春自治旗| 鄯善县| 广安市| 保康县| 微山县| 武山县| 曲靖市| 北辰区| 三门峡市| 昌黎县| 平昌县| 怀柔区| 古田县| 宜良县| 达孜县| 卢龙县| 阿拉善右旗| 神农架林区| 鄂温| 盐山县|