新聞中心

        DS1302控制代碼

        作者: 時間:2016-12-01 來源:網絡 收藏
        兩天的成果,
        /*************************************/
        #include
        #define uint unsigned int
        #define uchar unsigned char
        uchar year,month,day,week,hours,minutes,seconds;
        uchar flag1302,cnt;
        /*************************************
        控制按鍵
        *************************************/
        sbit K1=P1^0;
        sbit K2=P1^2;
        sbit K3=P1^4;
        /*************************************
        控制LCD液晶顯示
        *************************************/
        sbit lcdwrite=P2^5;
        sbit lcddatecommand=P2^6;
        sbit lcde=P2^7;
        /*************************************
        控制DS1302時鐘
        *************************************/
        sbit SCK=P3^6;
        sbit RST=P3^5;
        sbit SDA=P3^4;
        /*************************************
        延時子函數
        *************************************/
        void delay(uint z)
        {
        uint x,y;
        for(x=z;x>0;x--)
        {
        for(y=0;y<=112;y++)
        {
        }
        }
        }
        /*************************************
        LCD寫命令函數
        *************************************/
        void write_command(uchar command)
        {
        lcddatecommand=0; //開啟寫命令
        lcdwrite=0; //只寫不讀
        P0=command;
        delay(1);
        lcde=1; //形成使能E高脈沖
        delay(1);
        lcde=0; //使能E拉低
        }
        /*************************************
        LCD寫數據函數
        *************************************/
        void write_date(uchar date)
        {
        lcddatecommand=1; //開啟寫數據
        lcdwrite=0; //只寫不讀
        P0=date;
        delay(1);
        lcde=1; //形成使能E高脈沖
        delay(1);
        lcde=0; //使能E拉低
        }
        /*************************************
        LCD屏幕初始化
        *************************************/
        void lcdInit()
        {
        lcde=0;
        write_command(0x38);//設置16*2顯示,5*7點陣,8位數據接口
        write_command(0x0c);//設置開顯示,不顯示光標
        write_command(0x06);// 寫一個字符后地址指針加1
        write_command(0x01);//顯示清0,數據指針清0
        }
        /*************************************
        DS1302寫字節(jié)函數
        *************************************/
        void write_1302byte(uchar date)
        {
        uchar i;
        for(i=0;i<8;i++)
        {
        SCK=0;
        SDA=date&0x01;
        date=date>>1;
        SCK=1;//將電平置為已知狀態(tài)
        }
        }
        /*************************************
        DS1302讀字節(jié)函數
        *************************************/
        uchar read_1302byte(uchar address)
        {
        uchar i,temp;
        temp=0x00;
        RST=0;
        delay(1);
        SCK=0;
        delay(1);
        RST=1;
        delay(1);
        write_1302byte(address);
        for(i=0;i<8;i++)
        {
        if(SDA==1)
        {
        temp=temp|0x80; //每次傳輸低字節(jié)
        }
        temp=temp>>1;
        SCK=1;
        delay(1);
        SCK=0;
        delay(1);
        }
        return temp;
        }
        /*************************************
        往DS1302中寫數據
        *************************************/
        void write_1302(uchar address,uchar date)
        {
        RST=0;
        delay(1);
        SCK=0;
        delay(1);
        RST=1;
        delay(1);
        write_1302byte(address);
        write_1302byte(date);
        RST=0;
        }
        /*************************************
        往DS1302中讀取數據
        *************************************/
        uchar read_1302(uchar address)
        {
        uchar date;
        RST=0;
        SCK=0;
        RST=1;
        write_1302byte(address);
        date=read_1302byte(address);
        SCK=1;
        RST=0;
        return date;
        }
        /*************************************
        初始化時間
        *************************************/
        void Init_1302()
        {
        write_1302(0x8e,0x00); //寫入不保護指令
        write_1302(0x80,(14/10)<<4|(14%10));//14秒鐘
        write_1302(0x82,(7/10)<<4|(7%10)); //7分鐘
        write_1302(0x84,(3/10)<<4|(3%10)); //3小時
        write_1302(0x86,(6/10)<<4|(6%10)); //6日
        write_1302(0x88,(6/10)<<4|(6%10)); //6月
        write_1302(0x8A,(4/10)<<4|(4%10)); //周四
        write_1302(0x8C,(13/10)<<4|(13%10));//13年
        write_1302(0x90,0xa5); //打開充電功能,選擇2K電阻充電方式
        write_1302(0x8e,0x80); //寫保護
        }
        /*************************************
        時鐘顯示
        *************************************/
        void DisplayHours(uchar hours)
        {
        uchar shi,ge;
        shi=hours/10;
        ge=hours%10;
        write_command(0x06);
        write_command(0x80+0x40);
        write_date(shi+48);
        write_date(ge+48);
        write_date(:);
        }
        /*************************************
        分鐘顯示
        *************************************/
        void DisplayMinutes(uchar minutes)
        {
        uchar shi,ge;
        shi=minutes/10;
        ge=minutes%10;
        write_command(0x06);
        write_command(0x80+0x43);
        write_date(shi+48);
        write_date(ge+48);
        write_date(:);
        }
        上一頁 1 2 3 下一頁

        關鍵詞: DS1302控制代

        評論


        技術專區(qū)

        關閉
        主站蜘蛛池模板: 南阳市| 横山县| 博乐市| 邻水| 郸城县| 嵩明县| 敦化市| 馆陶县| 三亚市| 喜德县| 石河子市| 通州市| 宜昌市| 隆回县| 保靖县| 澄江县| 石屏县| 阿鲁科尔沁旗| 潼南县| 柘城县| 隆安县| 广饶县| 孟州市| 高邮市| 鄂托克前旗| 平谷区| 泾源县| 思茅市| 佛山市| 恭城| 筠连县| 泰州市| 贵州省| 白沙| 光山县| 凤山县| 马公市| 乐亭县| 厦门市| 莲花县| 北碚区|