|
我想根據(jù)制作一個可以調(diào)節(jié)的ds1302時鐘,再根據(jù)時鐘計算出太陽高度角和方位角來進行太陽追蹤
可是我的程序卻不能用按鍵將改好的時間寫入ds1302
有高手能看下哪里不正確需要改嗎
WFVZ8R~K7M{O%[0QKA(%N8H.png (103.16 KB, 下載次數(shù): 33)
下載附件
2020-4-6 12:26 上傳
單片機源程序如下:
- #include <REG52.h>
- #include <intrins.h>
- #include <math.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define PI 3.14159 //圓周率
- #define LAT 30.57 //緯度
- sbit LCD_SCLK = P0^0;
- sbit LCD_STD = P0^1;
- sbit LCD_CS = P0^2;
- sbit DS1302_CE = P3^1;
- sbit DS1302_CK = P3^4;
- sbit DS1302_IO = P3^5;
- extern void _nop_(void);
- uchar time[8] ;
- uchar set_buf[12] = {0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30};
- uchar angle[3];
- uchar time_500ms;
- uchar set_num = 0;
- uchar key_num;
- uchar count1,count2;
- bit flag_js;
- /* 發(fā)送一個字節(jié)到DS1302通信總線上 */
- void DS1302ByteWrite(uchar dat)
- {
- uchar mask;
-
- for (mask=0x01; mask!=0; mask<<=1) //低位在前,逐位移出
- {
- if ((mask&dat) != 0) //首先輸出該位數(shù)據(jù)
- DS1302_IO = 1;
- else
- DS1302_IO = 0;
- DS1302_CK = 1; //然后拉高時鐘
- DS1302_CK = 0; //再拉低時鐘,完成一個位的操作
- }
- DS1302_IO = 1; //最后確保釋放IO引腳
- }
- /* 由DS1302通信總線上讀取一個字節(jié) */
- unsigned char DS1302ByteRead()
- {
- uchar mask;
- uchar dat = 0;
-
- for (mask=0x01; mask!=0; mask<<=1) //低位在前,逐位讀取
- {
- if (DS1302_IO != 0) //首先讀取此時的IO引腳,并設(shè)置dat中的對應(yīng)位
- {
- dat |= mask;
- }
- DS1302_CK = 1; //然后拉高時鐘
- DS1302_CK = 0; //再拉低時鐘,完成一個位的操作
- }
- return dat; //最后返回讀到的字節(jié)數(shù)據(jù)
- }
- /* 用單次寫操作向某一寄存器寫入一個字節(jié),reg-寄存器地址,dat-待寫入字節(jié) */
- void DS1302SingleWrite(uchar reg, uchar dat)
- {
- DS1302_CE = 1; //使能片選信號
- DS1302ByteWrite((reg<<1)|0x80); //發(fā)送寫寄存器指令
- DS1302ByteWrite(dat); //寫入字節(jié)數(shù)據(jù)
- DS1302_CE = 0; //除能片選信號
- }
- /* 用單次讀操作從某一寄存器讀取一個字節(jié),reg-寄存器地址,返回值-讀到的字節(jié) */
- unsigned char DS1302SingleRead(uchar reg)
- {
- uchar dat;
-
- DS1302_CE = 1; //使能片選信號
- DS1302ByteWrite((reg<<1)|0x81); //發(fā)送讀寄存器指令
- dat = DS1302ByteRead(); //讀取字節(jié)數(shù)據(jù)
- DS1302_CE = 0; //除能片選信號
-
- return dat;
- }
- /* 用突發(fā)模式連續(xù)寫入8個寄存器數(shù)據(jù),dat-待寫入數(shù)據(jù)指針 */
- void DS1302BurstWrite(unsigned char *dat)
- {
- uchar i;
-
- DS1302_CE = 1;
- DS1302ByteWrite(0xBE); //發(fā)送突發(fā)寫寄存器指令
- for (i=0; i<8; i++) //連續(xù)寫入8字節(jié)數(shù)據(jù)
- {
- DS1302ByteWrite(dat[i]);
- }
- DS1302_CE = 0;
- }
- /* 用突發(fā)模式連續(xù)讀取8個寄存器的數(shù)據(jù),dat-讀取數(shù)據(jù)的接收指針 */
- void DS1302BurstRead(uchar *dat)
- {
- uchar i;
-
- DS1302_CE = 1;
- DS1302ByteWrite(0xBF); //發(fā)送突發(fā)讀寄存器指令
- for (i=0; i<8; i++) //連續(xù)讀取8個字節(jié)
- {
- dat[i] = DS1302ByteRead();
- }
- DS1302_CE = 0;
- }
- /* DS1302初始化,如發(fā)生掉電則重新設(shè)置初始時間 */
- void InitDS1302()
- {
- uchar dat;
- uchar code InitTime[] = { //2020年4月1日 星期二 12:30:00
- 0x00,0x30,0x12, 0x01, 0x04, 0x02, 0x20
- };
-
- DS1302_CE = 0; //初始化DS1302通信引腳
- DS1302_CK = 0;
- dat = DS1302SingleRead(0); //讀取秒寄存器
- if ((dat & 0x80) != 0) //由秒寄存器最高位CH的值判斷DS1302是否已停止
- {
- DS1302SingleWrite(7, 0x00); //撤銷寫保護以允許寫入數(shù)據(jù)
- DS1302BurstWrite(InitTime); //設(shè)置DS1302為默認的初始時間
- }
- }
- //***********************************************************
- //函數(shù):void key(void)
- //功能:鍵盤掃描、識別以及處理
- //***********************************************************
- void key(void)
- {
- uchar tmp,temp;
- tmp=P1|0x07;
- temp = tmp;
- temp &= 0x07;
- if(temp != key_num)
- {
- key_num = temp;
- switch(key_num)
- {
- case 0x06: if(flag_js == 0)//設(shè)定/退出
- {
- flag_js = 1;
- count1 = 1;
-
- }
- else if(flag_js == 1)
- {
- time[6] = (set_buf[11]<<4)+(set_buf[10]&0x0f);//年
-
- time[4] = (set_buf[9]<<4)+(set_buf[8]&0x0f);//月
- time[3] = (set_buf[7]<<4)+(set_buf[6]&0x0f);//日
- time[2] = (set_buf[5]<<4)+(set_buf[4]&0x0f);//時
- time[1] = (set_buf[3]<<4)+(set_buf[2]&0x0f);//分
- time[0] = (set_buf[1]<<4)+(set_buf[0]&0x0f); //秒
- DS1302BurstWrite(time);
- flag_js = 0;
- count2 = 1;
- }
- break;
- case 0x05: if(flag_js == 1)//移位
- {
- set_num ++;
- if(set_num > 11)
- set_num = 0;
- }
- break;
- case 0x03: if(flag_js == 1)//數(shù)字加一
- {
- switch(set_num)
- {
- case 0: if(set_buf[0] > 0x38) set_buf[0] = 0x30; //秒的個位
- else set_buf[0]++; break;
- case 1: if(set_buf[1] > 0x34) set_buf[1] = 0x30; //秒的十位
- else set_buf[1]++; break;
- case 2: if(set_buf[2] > 0x38) set_buf[2] = 0x30;
- else set_buf[2]++; break;
- case 3: if(set_buf[3] > 0x34) set_buf[3] = 0x30; //分
- else set_buf[3]++; break;
- case 4: if(set_buf[4] > 0x38) set_buf[4] = 0x30;
- else set_buf[4]++; break;
- case 5: if(set_buf[5] > 0x31) set_buf[5] = 0x30; //時
- else set_buf[5]++; break;
- case 6: if(set_buf[6] > 0x38) set_buf[5] = 0x30;
- else set_buf[6]++; break;
- case 7: if(set_buf[7] > 0x32) set_buf[5] = 0x30; //日
- else set_buf[7]++; break;
- case 8: if(set_buf[8] > 0x38) set_buf[5] = 0x30;
- else set_buf[8]++; break;
- case 9: if(set_buf[9] > 0x31) set_buf[5] = 0x30; //月
- else set_buf[9]++; break;
- case 10: if(set_buf[10] > 0x38) set_buf[5] = 0x30;
- else set_buf[10]++; break;
- case 11: if(set_buf[11] > 0x38) set_buf[5] = 0x30; //年
- else set_buf[11]++; break;
- default: break;
- }
- }
- break;
- default: break;
- }
- }
- }
復(fù)制代碼
|
|