c51中定义联合体和结构体
扫描二维码
随时随地手机看文章
联合体
union {
unsigned char Ch; //"无符号数
unsigned char CHR[4]; //"无符号数组
unsigned long I; //"无符号整型数
long L; //"有符号长整型数
float F; //"浮点数
}EEP;
float shu;
EEP.F =shu; //
结构体
struct realti
{
uchar Second;
uchar Minute;
uchar Hour;
uchar Day;
uchar Month;
uchar Week;
uchar Year;
uchar kzz; //连续写必须8个字节,否则写不进去
};
struct realti RealTime; //struct realti RealTime[3];
或:
struct realti
{
uchar Second;
uchar Minute;
uchar Hour;
uchar Day;
uchar Month;
uchar Week;
uchar Year;
uchar kzz; //连续写必须8个字节,否则写不进去
}RealTime; //RealTime[3];
或:
struct{
uchar Second;
uchar Minute;
uchar Hour;
uchar Day;
uchar Month;
uchar Week;
uchar Year;
uchar kzz; //连续写必须8个字节,否则写不进去
}RealTime; // RealTime[3];
用法:
RealTime.Second =0X30;
RealTime.Minute =0X11;
RealTime.Hour =0X15;
RealTime.Day =0X06;
RealTime.Month =0X03;
RealTime.Week =0X05;
RealTime.Year =0X09;
RealTime.kzz =0X00;
DS1302Write_Time(&RealTime.Second); //向DS1302写入时钟数据(多字节方式)
DS1302Read_Time(&RealTime.Second); //读取DS1302时钟数据RealTime.Second首地址
void DS1302Read_Time(uchar *pSecDa) //读取DS1302时钟数据
{
uchar i;
DS1302_Open();//打开DS1302
DS1302_Write(0xbf); /* 0xbf:时钟多字节读命令 */
for (i=0; i<8; i++) //连续写必须8个字节,否则写不进去
{
*pSecDa = DS1302_Read(); /* 读1Byte数据 */
pSecDa++;
}
DS1302_Close();//关闭DS1302
}
void DS1302Write_Time(uchar *pSecDa) //向DS1302写入时钟数据(多字节方式)
{
uchar i;
DS1302Byte_Write(0x8e,0x00); /* 控制命令,WP=0,写 允许*/
DS1302_Open();//打开DS1302
DS1302_Write(0xbe); /* 0xbe:时钟多字节写命令 */
for (i=0; i<8; i++) /*8Byte = 7Byte 时钟数据 + 1Byte 控制*/
{
DS1302_Write(*pSecDa);/* 写1Byte数据*/
pSecDa++;
}
DS1302_Close();//关闭DS1302
}