新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 簡易12684液晶和Atmega32的電子萬年歷

        簡易12684液晶和Atmega32的電子萬年歷

        作者: 時間:2013-12-12 來源:網絡 收藏


        /**********************************************/

        //從DS1302讀一個字節數據
        unsigned char DS1302_ReadByte(void)
        {
        unsigned char i,dat = 0; //dat存放讀出的數據,初始化為0
        PORTB = ~(1 PB1); //DS1302的I/O口上拉不使能,
        DDRB = ~(1 PB1); //DS1302的I/O口設置為輸入口,準備讀數據

        for(i = 0;i 8;i++) //讀8位,低位在前,右移
        {
        dat >>= 1; //讀出的數據右移一位
        PORTB |= (1 PB0); //DS1302的SCLK端口拉高
        Delayus(10); //
        PORTB = ~(1 PB0); //DS1302的SCLK端口拉低,產生下降沿,
        Delayus(10);
        if(PINB (1 PB1)) //讀數據端口狀態
        {
        dat |= 0x80; //如果數據端口位高,相應數據位置1
        }
        }
        DDRB |= (1 PB1); //最后將數據端口設置為輸出
        return dat; //返回讀出的數據
        }

        //向DS1302寫一個字節數據
        void DS1302_WriteByte(unsigned char dat)
        {
        unsigned char i;

        for(i = 0;i 8;i++) //寫8位,低位在前
        {
        PORTB = ~(1 PB0); //DS1302的SCLK置低
        if(dat 0x01) //寫數據位
        {
        PORTB |= (1 PB1); //如果該位為1,則I/O口置高
        }
        else
        {
        PORTB = ~(1 PB1); //如果該位為0,則I/O口置低
        }
        Delayus(10); //
        PORTB |= (1 PB0); //DS1302的SCLK置高,產生上升沿
        dat >>= 1; //數據右移1位
        }
        }

        //從DS1302的指定地址讀一個字節數據
        unsigned char DS1302_ReadData(unsigned addr)
        {
        unsigned char data;

        PORTB = ~(1 PB2); //拉低片選端
        PORTB = ~(1 PB0);//拉低時鐘端
        Delayus(10);
        PORTB |= (1 PB2);//拉高片選端
        Delayus(10);
        DS1302_WriteByte(addr);//寫入操作命令(地址)
        Delayus(10);
        data = DS1302_ReadByte();//讀出數據
        Delayus(10);
        PORTB = ~(1 PB0); //拉低時鐘端
        PORTB = ~(1 PB2); //拉低片選端

        return data;
        }

        //向DS1302的指定地址寫一個字節數據
        void DS1302_WriteData(unsigned char addr,unsigned data)
        {
        PORTB = ~(1 PB2); //拉低片選端
        PORTB = ~(1 PB0);//拉低時鐘端
        Delayus(10);
        PORTB |= (1 PB2);//拉高片選端
        Delayus(10);
        DS1302_WriteByte(addr);//寫入操作命令(地址)
        Delayus(10);
        PORTB = ~(1 PB0);//拉低時鐘端
        Delayus(10);
        DS1302_WriteByte(data);//寫入數據
        PORTB = ~(1 PB0); //拉低時鐘端

        Delayus(10);
        PORTB = ~(1 PB2); //拉低片選端
        }

        //對DS1302設置時間
        void DS1302_SetTime(unsigned char *time)
        {
        unsigned char i;
        unsigned char addr = 0x80;//寫入地址從秒寄存器開始

        DS1302_WriteData(WR_PROTECT | WR,UPROTECT);//控制命令,WP位為0,允許寫操作
        Delayms(5);
        for(i = 0;i 7;i++)
        {
        DS1302_WriteData(addr | WR,time[i]);// 秒 分 時 日 月 星期 年
        addr += 2;
        Delayms(1);
        }
        DS1302_WriteData(WR_PROTECT | WR,PROTECT);//控制命令,WP位為1,不允許寫操作
        }

        //從DS1302讀取時間
        void DS1302_GetTime(void)
        {
        unsigned char i;
        PORTB = ~(1 PB2);
        Delayus(10);
        PORTB |= (1 PB2);
        Delayus(10);
        DS1302_WriteByte(0xbf);
        for(i = 0;i 8;i++)
        {
        Get_Time[i] = DS1302_ReadByte();
        }
        PORTB = ~(1 PB2);
        PORTB = ~(1 PB0);
        }

        //DS1302是否工作檢測
        unsigned char DS1302_Check(void)
        {
        DS1302_WriteData(WR_PROTECT | WR,UPROTECT);
        DS1302_WriteData(RAMBASE | WR,0x31);

        if(DS1302_ReadData(RAMBASE | WR) == 0x31)
        {
        return TURE;
        }
        else
        {
        return FALSE;
        }
        }

        void DS1302_DisCharge(void)
        {
        DS1302_WriteData(CHARGE|WR,TC_DISABLED);
        Delayus(10);
        }

        unsigned char DS1302_Alarm(unsigned char hour,unsigned char min)
        {

        }

        void System_Beep(void)
        {

        }

        //DS1302初始化
        void DS1302_Init(void)
        {
        DS1302_WriteData(WR_PROTECT | WR,UPROTECT); //寫入寫允許命令
        DS1302_WriteData(SECOND | WR,CLK_START); //啟動振蕩器,DS1302開始工作
        DS1302_WriteData( WR_PROTECT | WR,PROTECT); //控制命令,WP位為1,不允許寫操作
        }


        //us級別的延時函數
        void Delayus(unsigned int lus)
        {
        while(lus--)
        {
        _delay_loop_2(3); //_delay_loop_2(1)是延時4個時鐘周期,參數為3則延時12
        //個時鐘周期,本實驗用12M晶體,則12個時鐘周期為12/12=1us
        }
        }

        //ms級別的延時函數
        void Delayms(unsigned int lms)
        {
        while(lms--)
        {
        Delayus(800); //延時1ms
        }
        }

        晶振相關文章:晶振原理

        上一頁 1 2 3 下一頁

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 神池县| 凤城市| 临桂县| 荣昌县| 东乌珠穆沁旗| 南投市| 鄢陵县| 泽州县| 靖江市| 白河县| 武夷山市| 垫江县| 驻马店市| 新兴县| 宜君县| 灵台县| 子洲县| 沾化县| 巨野县| 手游| 遂平县| 呼和浩特市| 烟台市| 武清区| 峨眉山市| 南昌县| 保康县| 东乌珠穆沁旗| 共和县| 林西县| 会同县| 工布江达县| 登封市| 微山县| 额敏县| 长子县| 闽侯县| 贵溪市| 安阳县| 友谊县| 集贤县|