compiling ds1302.c.....\library\ds1302.c(5): warning C258: 'w_rtc_address': mspace on parameter ignored
..\library\ds1302.c(5): error C244: 'w_rtc_address': can't initialize, bad type or class..\library\ds1302.c - 1 Error(s), 1 Warning(s).
#include "ds1302.h"
//秒的起始位为0x80,一个单元一个8位的字节,偶数为写操作,奇数为读操作
//不使用指针,编译器会忽略mspace(用于内存分配的自定义空间)
code unsigned char w_rtc_address[]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年
code unsigned char r_rtc_address[]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
unsigned int set[];
unsigned char rtc[];
unsigned long Now_Clock=0; //输入现在的时间--时分秒
unsigned int Now_week=0; //周
unsigned long Now_Calendar=0; //年月日
//写入字节函数
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{ //写入数据时,上升沿有效
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
//写入地址字节与数据字节
void Write_Ds1302( unsigned char address,unsigned char dat )
{
RST=0;
_nop_(); //延迟几微秒
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
Write_Ds1302_Byte(dat);
RST=0;
}
//先写入地址字节,再读取数据
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
Write_Ds1302_Byte(address);
for (i=0;i<8;i++)
{ //读取数据,下降沿读取有效
SCK=0;
temp>>=1;
SCK=1;
if(SDA)
temp|=0x80;
}
RST=0;
_nop_();
RST=0;
SCK=0;
_nop_();
SCK=1;
_nop_();
SDA=0;
_nop_();
SDA=1;
_nop_();
return (temp);
}
//读取时钟信息
unsigned char* ReadRTC()
{
unsigned char i, *p;
unsigned char tmp[7];
p = (unsigned char *)r_rtc_address; //地址传递
for(i=0;i<7;i++){
tmp[i]=Read_Ds1302(*p); //读取秒、分、时三个寄存器数据
p++;
}
rtc[0] = (tmp[2] >> 4); //时的十位,temp[2]读取的是85H的内容
rtc[1] = (tmp[2] & 0x0F); //时的个位
rtc[2] = (tmp[1] >> 4); //分
rtc[3] = (tmp[1] & 0x0F);
rtc[4] = (tmp[0] >> 4); //秒
rtc[5] = (tmp[0] & 0x0F);
rtc[6] = (tmp[5] >> 4); //周
rtc[7] = (tmp[5] & 0x0F);
rtc[8] = (tmp[6] >> 4); //年
rtc[9] = (tmp[6] & 0x0F);
rtc[10] = (tmp[4] >> 4); //月
rtc[11] = (tmp[4] & 0x0F);
rtc[12] = (tmp[3] >> 4); //日
rtc[13] = (tmp[3] & 0x0F);
return rtc;
}
//输入的时间做处理
void set_clock()
{
set[0] = Now_Clock/100000; //时分秒
set[1] = Now_Clock/10000%10;
set[2] = Now_Clock/1000%10;
set[3] = Now_Clock/100%10;
set[4] = Now_Clock/10%10;
set[5] = Now_Clock%10;
set[6] = Now_week/10; //周
set[7] = Now_week%10;
set[8] = Now_Calendar/100000; //年月日
set[9] = Now_Calendar/10000%10;
set[10] = Now_Calendar/1000%10;
set[11] = Now_Calendar/100%10;
set[12] = Now_Calendar/10%10;
set[13] = Now_Calendar%10;
}
//设置初始时钟时间
void SetRTC()
{
set_clock();
Write_Ds1302(0x8E,0X00); //WP为0,打开写保护关
Write_Ds1302(w_rtc_address[0], (set[4]<<4) | (set[5])); //秒
Write_Ds1302(w_rtc_address[1], (set[2]<<4) | (set[3])); //分
Write_Ds1302(w_rtc_address[2], (set[0]<<4) | (set[1])); //时
Write_Ds1302(w_rtc_address[5], (set[6]<<4) | (set[7])); //周
Write_Ds1302(w_rtc_address[3], (set[12]<<4) | (set[13])); //日
Write_Ds1302(w_rtc_address[4], (set[10]<<4) | (set[11])); //月
Write_Ds1302(w_rtc_address[6], (set[8]<<4) | (set[9])); //年
Write_Ds1302(0x8E,0x80);//WP为1,不能写
}
unsigned char* select(unsigned char state1,unsigned char state2)
{
unsigned char *p_rtc;
p_rtc = ReadRTC();
switch(state1)
{
case 1:
switch(state2)
{
case 1:
p_rtc[4] = 0;
p_rtc[5] = 0;
break;
case 2:
p_rtc[3]++;
if(p_rtc[3]>=10)
{
p_rtc[3]=0;
p_rtc[2]++;
if (p_rtc[2]>=6)
{
p_rtc[2] = 0;
}
}
break;
case 3:
p_rtc[1]++;
if(p_rtc[1]>=10)
{
p_rtc[1]=0;
p_rtc[1]++;
}
if(p_rtc[1]>4&&p_rtc[0]>2)
{
p_rtc[0] = 0;
p_rtc[1] = 0;
}
break;
default:break;
}break;
case 2:
switch(state2)
{
case 1:
p_rtc[6] = 0;p_rtc[7]++;
if(p_rtc[7]>7) p_rtc[7] = 1;
break;
default:break;
}break;
case 3:
switch(state2)
{
case 1:
p_rtc[13]++;
if(p_rtc[13]>=10)
{
p_rtc[13]=0;
p_rtc[12]++;
}
if(p_rtc[13]>1&&p_rtc[12]>3&&
(p_rtc[10]==0&&(p_rtc[11]==1||p_rtc[11]==3||p_rtc[11]==5||p_rtc[11]==7||p_rtc[11]==8))
&&(p_rtc[10]==1&&p_rtc[11]==0||p_rtc[11]==2)) //腊月
{
p_rtc[13] = 0;
p_rtc[12] = 0;
}
if(p_rtc[13]>0&&p_rtc[12]>3&&
(p_rtc[10]==0&&(p_rtc[11]==4||p_rtc[11]==6||p_rtc[11]==9))
&&(p_rtc[10]==1&&p_rtc[11]==1))
{
p_rtc[13] = 0;
p_rtc[12] = 0;
}
if(p_rtc[13]>8&&p_rtc[12]>2&&(p_rtc[10]==0&&p_rtc[11]==2)) //2月
{
p_rtc[13] = 0;
p_rtc[12] = 0;
}
break;
case 2:
p_rtc[11]++;
if(p_rtc[11]>=10)
{
p_rtc[11]=0;
p_rtc[10]++;
}
if(p_rtc[11]>2&&p_rtc[10]>1)
{
p_rtc[11] = 0;
p_rtc[10] = 0;
}
break;
case 3:
p_rtc[9]++;
if(p_rtc[9]>=10)
{
p_rtc[9]=0;
p_rtc[8]++;
}
if(p_rtc[9]>9&&p_rtc[0]>9)
{
p_rtc[9] = 0;
p_rtc[8] = 0;
}
break;
default:break;
}break;
default:break;
}
return p_rtc;
}
void add_rtc(unsigned char state1,unsigned char state2)
{
unsigned char *p_rtc;
Write_Ds1302(0x8E,0X00); //WP为0,打开写保护关
p_rtc = select(unsigned char state1,unsigned char state2);
Write_Ds1302(w_rtc_address[0], (p_rtc[4]<<4) | (p_rtc[5])); //秒
Write_Ds1302(w_rtc_address[1], (p_rtc[2]<<4) | (p_rtc[3])); //分
Write_Ds1302(w_rtc_address[2], (p_rtc[0]<<4) | (p_rtc[1])); //时
Write_Ds1302(w_rtc_address[5], (p_rtc[6]<<4) | (p_rtc[7])); //周
Write_Ds1302(w_rtc_address[3], (p_rtc[12]<<4) | (p_rtc[13])); //日
Write_Ds1302(w_rtc_address[4], (p_rtc[10]<<4) | (p_rtc[11])); //月
Write_Ds1302(w_rtc_address[6], (p_rtc[8]<<4) | (p_rtc[9])); //年
Write_Ds1302(0x8E,0x80);//WP为1,不能写
}
|