我想根据制作一个可以调节的ds1302时钟,再根据时钟计算出太阳高度角和方位角来进行太阳追踪
可是我的程序却不能用按键将改好的时间写入ds1302
有高手能看下哪里不正确需要改吗
单片机源程序如下:
- #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;
- /* 发送一个字节到DS1302通信总线上 */
- void DS1302ByteWrite(uchar dat)
- {
- uchar mask;
-
- for (mask=0x01; mask!=0; mask<<=1) //低位在前,逐位移出
- {
- if ((mask&dat) != 0) //首先输出该位数据
- DS1302_IO = 1;
- else
- DS1302_IO = 0;
- DS1302_CK = 1; //然后拉高时钟
- DS1302_CK = 0; //再拉低时钟,完成一个位的操作
- }
- DS1302_IO = 1; //最后确保释放IO引脚
- }
- /* 由DS1302通信总线上读取一个字节 */
- unsigned char DS1302ByteRead()
- {
- uchar mask;
- uchar dat = 0;
-
- for (mask=0x01; mask!=0; mask<<=1) //低位在前,逐位读取
- {
- if (DS1302_IO != 0) //首先读取此时的IO引脚,并设置dat中的对应位
- {
- dat |= mask;
- }
- DS1302_CK = 1; //然后拉高时钟
- DS1302_CK = 0; //再拉低时钟,完成一个位的操作
- }
- return dat; //最后返回读到的字节数据
- }
- /* 用单次写操作向某一寄存器写入一个字节,reg-寄存器地址,dat-待写入字节 */
- void DS1302SingleWrite(uchar reg, uchar dat)
- {
- DS1302_CE = 1; //使能片选信号
- DS1302ByteWrite((reg<<1)|0x80); //发送写寄存器指令
- DS1302ByteWrite(dat); //写入字节数据
- DS1302_CE = 0; //除能片选信号
- }
- /* 用单次读操作从某一寄存器读取一个字节,reg-寄存器地址,返回值-读到的字节 */
- unsigned char DS1302SingleRead(uchar reg)
- {
- uchar dat;
-
- DS1302_CE = 1; //使能片选信号
- DS1302ByteWrite((reg<<1)|0x81); //发送读寄存器指令
- dat = DS1302ByteRead(); //读取字节数据
- DS1302_CE = 0; //除能片选信号
-
- return dat;
- }
- /* 用突发模式连续写入8个寄存器数据,dat-待写入数据指针 */
- void DS1302BurstWrite(unsigned char *dat)
- {
- uchar i;
-
- DS1302_CE = 1;
- DS1302ByteWrite(0xBE); //发送突发写寄存器指令
- for (i=0; i<8; i++) //连续写入8字节数据
- {
- DS1302ByteWrite(dat[i]);
- }
- DS1302_CE = 0;
- }
- /* 用突发模式连续读取8个寄存器的数据,dat-读取数据的接收指针 */
- void DS1302BurstRead(uchar *dat)
- {
- uchar i;
-
- DS1302_CE = 1;
- DS1302ByteWrite(0xBF); //发送突发读寄存器指令
- for (i=0; i<8; i++) //连续读取8个字节
- {
- dat[i] = DS1302ByteRead();
- }
- DS1302_CE = 0;
- }
- /* DS1302初始化,如发生掉电则重新设置初始时间 */
- 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); //撤销写保护以允许写入数据
- DS1302BurstWrite(InitTime); //设置DS1302为默认的初始时间
- }
- }
- //***********************************************************
- //函数: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)//设定/退出
- {
- 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)//数字加一
- {
- 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;
- }
- }
- }
复制代码
|