|
F2D78F81DA9D678EA7D762CC9FCE7BD3.jpg (328.27 KB, 下載次數: 49)
下載附件
2018-6-9 12:17 上傳
- /**************main.c**************/
- 這是主函數數碼管顯示部分代碼
- /* shijian[]={50, 59,23, 1, 1, 1,2018}
- 0 1 2 3 4 5 6
- 秒 分 時 日 月 周 年
- 應顯示:
- 2018 01 01 23 59 50
- 6 4 3 2 1 0
- */
- if(xianshi==1) //年月日
- {
- yi=shijian[6]/1000;er=shijian[6]%1000/100;san=shijian[6]/10;si=shijian[6]%10;
- wu=shijian[4]/10;liu=shijian[4]%10;qi=shijian[3]/10;ba=shijian[3]%10;
- }
- /*************ds1302.c*******************/
- ds1302代碼我把所有的數據全改成了uint
- #include<stc15f2k60s2.h>
- #include <ds1302.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uint shijian[]={50,59,23,1, 1, 1, 2018};
- /* 秒 分 時 日 月 周 年 */
- sbit SCK=P1^7;
- sbit SDA=P2^3; // I/O口
- sbit RST = P1^3; // DS1302復位
- void Write_Ds1302_Byte(uint temp) //寫入
- {
- uint i;
- for (i=0;i<8;i++) //開始傳送八位地址命令
- {
- SCK=0;
- SDA=temp&0x01; //數據從低位開始傳送
- temp>>=1;
- SCK=1; //數據在上升沿時,DS1302讀取數據
- }
- }
- void Write_Ds1302( uint address,uint dat )
- {
- RST=0; //將RST(CE)置低電平。
- _nop_();
- SCK=0;
- _nop_();
- RST=1;
- _nop_();
- Write_Ds1302_Byte(address); //調用上面
- Write_Ds1302_Byte(dat/10<<4|(dat%10));
- RST=0;
- }
- unsigned int Read_Ds1302 ( uint address )
- {
- uint i,temp=0x00,dat1,dat2;
- RST=0;
- _nop_();
- SCK=0;
- _nop_();
- RST=1;
- _nop_();
- Write_Ds1302_Byte(address);
- for (i=0;i<8;i++)
- {
- SCK=0;
- temp>>=1;
- if(SDA)
- temp|=0x80;
- SCK=1;
- }
- RST=0; //讀取穩定
- _nop_();
- RST=0;
- SCK=0;
- _nop_();
- SCK=1;
- _nop_();
- SDA=0;
- _nop_();
- SDA=1;
- _nop_();
-
- dat1=temp/16;
- dat2=temp%16;
- temp=dat1*10+dat2;
- return (temp);
- }
- void ds1302_init()
- {
- uint add,i;
- add=0x80;
- Write_Ds1302(0x8e,0x00); //禁止寫保護,關閉寫保護 wp=1000 111 0
- for(i=0;i<7;i++) //寫 0x80 0x82 0x84 0x86 0x88 0x8a 0x8c
- {
- Write_Ds1302(add,shijian[i]);
- add+=2;
- }
- Write_Ds1302(0x8e,0x80); //打開寫保護,使其不受外界影響
- }
- void ds1302_get()
- {
- uint add,i;
- add=0x81;
- Write_Ds1302(0x8e,0x00);//禁止寫保護,關閉寫保護 wp=1000 111 0
- for(i=0;i<7;i++) //讀 0x81 0x83 0x85 0x87 0x89 0x8b 0x8d
- {
- shijian[i]=Read_Ds1302(add);
- add+=2;
- }
- Write_Ds1302(0x8e,0x80);//打開寫保護,使其不受外界影響
- }
復制代碼
|
|