|
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;
- }
- }
復制代碼 |
|