|
DS1302时钟芯片,按照手册写的呀!为什么在单片机开发板上一直2081-29-29 29:29:29 ,我是用的LCD1602作为显示器,程序烧写进去以后一直是这样。其中的LCD1602显示程序没有问题,可以不用看,应该是DS1302的程序哪里出了问题,附件里我加了keil5的工程文件,大佬们有空帮我看一下,不胜感激。我网上查过资料,也有很多人遇到DS1302的这种问题,但没有合适的解决我的问题。extern unsigned char Real_Time[]; 已经设置为全局变量。
单片机源程序如下:
- #include <REGX51.H>
- #include "LCD1602.H"
- #include "DS1302.H"
- unsigned int Second,Minutes,Hour,Day,Month,Year,Week;
- unsigned char Weeks[7][3]={"Mon","Tue","Thu","Wed","Fri","Sat","Sun"};
- void main()
- {
- LCD_Init();
- LCD_ShowNum(1,1,20,2);//LCD1602显示数字,分别是,第一行,第一列,数字位数。
- LCD_ShowString(1,5,"- -");//显示字符串
- LCD_ShowString(2,3,": :");
- DS1302_Init();//初始化1302
- Set_Time();//设置给定的时间
-
- while(1)
- {
- Read_Time();//读取时间
- LCD_ShowNum(1,3,Real_Time[0],2);
- LCD_ShowNum(1,6,Real_Time[1],2);
- LCD_ShowNum(1,9,Real_Time[2],2);
- LCD_ShowNum(2,1,Real_Time[3],2);
- LCD_ShowNum(2,4,Real_Time[4],2);
- LCD_ShowNum(2,7,Real_Time[5],2);
- LCD_ShowString(1,13,"Sat");
- }
- }
- //一-------------------
- #include <REGX51.H>
- //写指令
- #define DS1302_Second 0x80
- #define DS1302_Minute 0x82
- #define DS1302_Hour 0x84
- #define DS1302_Day 0x86
- #define DS1302_Month 0x88
- #define DS1302_Year 0x8A
- #define DS1302_Week 0x8C
- #define DS1302_WP 0x8E
- //读指令
- #define DS1302R_Second 0x81
- #define DS1302R_Minute 0x83
- #define DS1302R_Hour 0x85
- #define DS1302R_Day 0x87
- #define DS1302R_Month 0x89
- #define DS1302R_Year 0x8B
- #define DS1302R_Week 0x8D
- sbit DS1302_SCLK=P3^6;
- sbit DS1302_CE=P3^5;
- sbit DS1302_IO=P3^4;
- unsigned char Time[7]={21,11,28,20,50,30,7};//设定的时间,依次为 年/月/日/时/分/秒/周
- unsigned char Real_Time[7];
- void DS1302_Init() //初始化
- {
- DS1302_CE=0;
- DS1302_SCLK=0;
- }
- DS1302_WriteByte(unsigned char cmd,datax)//DS1302写命令
- {
- unsigned int i;
- DS1302_CE=1;
- for(i=0;i<8;i++)
- {
- DS1302_IO=cmd&(0x01<<i);
- DS1302_SCLK=1;
- DS1302_SCLK=0;
- }
- for(i=0;i<8;i++)
- {
- DS1302_IO=datax & (0x01<<i);
- DS1302_SCLK=1;
- DS1302_SCLK=0;
- }
- DS1302_CE=0;
- }
- unsigned char DS1302_ReadByte(unsigned char cmd)//DS1302读命令
- {
- unsigned char data1=0x00;
- unsigned int i;
- DS1302_CE=1;
- for(i=0;i<8;i++)
- {
- DS1302_IO=cmd&(0x01<<i);
- DS1302_SCLK=0;
- DS1302_SCLK=1;
- }
- for(i=0;i<8;i++)
- {
- if(DS1302_IO==1){data1=DS1302_IO|(0x01<<i);}
- DS1302_SCLK=1;
- DS1302_SCLK=0;
- }
- DS1302_CE=0;
- DS1302_IO=0;
- return data1;
- }
- void Set_Time()
- {
- DS1302_WriteByte(DS1302_WP,0x00);//关闭写保护
- DS1302_WriteByte(DS1302_Year,21);//设置年
- DS1302_WriteByte(DS1302_Month,Time[1]);//设置年
- DS1302_WriteByte(DS1302_Day,Time[2]);//设置年
- DS1302_WriteByte(DS1302_Hour,Time[3]);//设置年
- DS1302_WriteByte(DS1302_Minute,Time[4]);//设置年
- DS1302_WriteByte(DS1302_Second,Time[5]);//设置年
- DS1302_WriteByte(DS1302_Week,Time[6]);//设置年
- }
- void Read_Time()
- {
- unsigned int i;
- Real_Time[0]=DS1302_ReadByte(DS1302R_Year);
- Real_Time[1]=DS1302_ReadByte(DS1302R_Month);
- Real_Time[2]=DS1302_ReadByte(DS1302R_Day);
- Real_Time[3]=DS1302_ReadByte(DS1302R_Hour);
- Real_Time[4]=DS1302_ReadByte(DS1302R_Minute);
- Real_Time[5]=DS1302_ReadByte(DS1302R_Second);
- Real_Time[6]=DS1302_ReadByte(DS1302R_Week);
- for(i=0;i<7;i++)
- {
- Time[i]=Time[i]/16*10+Time[i]%16;
- }
- }
复制代码 |
|