|
S35390A通过IIC读写,读取的时间与写入的时间不一致,读出来的数据只有年是对的,不知道问题出在哪里,请大佬帮忙分析
写入时间为:S35390A[6]={0x16,0x0C,0x17,0x0E,0x24,0x00}; //22-12-23 14:36:00
读出时间为:
以下是单片机程序部分:
- #include <stdio.h>
- #include <reg52.h>
- #include <35390.h>
- //#include <S-35390A.h>
- void InitUART(void)//使用定时器1作为串口波特率发生器
- {
- TH1 = 0xFD; //晶振11.0592mhz 波特率设为9600
- TL1 = TH1;
- TMOD |= 0x20; //定时器1方式2
- SCON = 0x50; //串口接收使能
- ES = 1; //串口中断使能
- TR1 = 1; //定时器1使能
- TI = 1; //发送中断标记位,必须设置
- }
- //毫秒延时函数
- void delay_ms(unsigned int t)
- {
- unsigned char a,b;
- while(t--)
- {
- for(b=102;b>0;b--)
- for(a=3;a>0;a--);
- }
- }
- void main()
- {
- InitUART(); //初始化串口
- EA = 1; //开总中断
- Write_S35390A();//初始化时间
- while(1)
- {
- Read_S35390A();//读取时间
- printf("S35390A[0]=%02BX\r\nS35390A[1]=%02BX\r\nS35390A[2]=%02BX\r\n",S35390A[0],S35390A[1],S35390A[2]);
- printf("S35390A[3]=%02BX\r\nS35390A[4]=%02BX\r\nS35390A[5]=%02BX\r\n",S35390A[3],S35390A[4],S35390A[5]);
- }
- }
- 35390.h
- #define Uchar unsigned char
- #define Uint unsigned int
- unsigned char S35390A[6]={0x16,0x0C,0x17,0x0E,0x24,0x00};// 22-12-23 14:36:00
- sbit IIC_SCL = P2^3;
- sbit IIC_SDA = P2^2;
- void delayms(Uint n)
- {
- Uint i,j;
- for(i=n;i>0;i--)
- {
- for(j=750;j>0;j--)
- {
- }
- }
- }
- /*****************************************
- I2c Wait
- Wait for some time to get proper I2C timing
- ******************************************/
- void IICWait(void)
- {
- unsigned int i=10;//i=5;
- while(i--);
- }
- /*****************************************
- i2c start
- condition SDA 1-0 while SCL=1
- ******************************************/
- void IICStart(void)
- {
- IIC_SDA=1;
- IIC_SCL=1;
- IICWait();
- IIC_SDA=0;
- IICWait();
- IIC_SCL=0;
- IICWait();
- }
- /*****************************************
- I2c sotp
- condition SDA=0-1 while SCL=1
- ******************************************/
- void IICStop(void)
- {
- IIC_SDA=0;
- IICWait();
- IIC_SCL=1;
- IICWait();
- IIC_SDA=1;
- IICWait();
- }
- /*****************************************
- 35390 Init
- Initialize I2C interface
- Release I2c BUS
- ****************************************
- void S35390_Init(void)
- {
- IIC_SDA=1;
- IIC_SCL=1;
- IIC_Wait();
- }
- /*****************************************
- I2c SentByte
- master transfer data to slave and return acknowledge bit
- don't include<intrins.h>
- ******************************************/
- bit IICSentByte(unsigned char bytedata)
- {
- unsigned char i;
- bit ack;
- for(i=0;i<8;i++)
- {
- if(bytedata & 0x80)
- IIC_SDA=1;
- else
- IIC_SDA=0;
- bytedata<<=1;
- IICWait();
- IIC_SCL=1;
- IICWait();
- IIC_SCL=0;
- IICWait();
- }
- IIC_SDA=1; //释放数据总线,等待应答
- IICWait();
- IIC_SCL=1; //等待数据线的ACK(时钟信号一般上升沿有效)
- IICWait();
- ack=IIC_SDA;
- IIC_SCL=0;
- IICWait();
- return ack;
- }
- /*****************************************
- I2c ReceiveByte
- slave trransfer data to master
- ******************************************/
- unsigned char IICReceiveByte(void)
- {
- unsigned char i;
- unsigned char bytedata=0;
- IIC_SCL=0;
- IICWait();
- for(i=0;i<8;i++)
- {
- IIC_SCL=1;
- IICWait();
- bytedata=bytedata<<1;
- if(IIC_SDA==1)
- {
- bytedata = bytedata|0x01;
- }
- IICWait();
- IIC_SCL=0;
- IICWait();
- }
- return bytedata;
- }
- /*****************************************
- I2c SendAcknowledge
- Master send acknowledge bit to slave
- acknowledge="0",non-acknowledge="1"
- ******************************************/
- void SendAck(bit ack)
- {
- IIC_SDA=ack;
- IIC_SCL=1;
- IICWait();
- IIC_SCL=0;
- }
- unsigned char Exchange_highlow(unsigned char car)
- {
- unsigned i,c=0;
- for(i=0;i<8;i++)
- {c=c*2+(car-2*(car>>1));car=car>>1;}
- return c;
- }
- void Write_S35390A(void)
- {
- unsigned char car;
- IICStart(); //Send start signal
- IICSentByte(0x60); //Send identifer IIC address,状态寄存器1
- IICSentByte(0xe0); //reset 0xe0
- IICStop();
- delayms(10); // Delay a period of time to write
- IICStart(); //Send start signal
- IICSentByte(0x61); //Send identifer IIC address,状态寄存器1
- IICStop();
- car=IICReceiveByte();
- if(car)
- {
- car^=0xff;
- }
- IICStart(); //Send start signal
- IICSentByte(0x68); //Send identifer IIC address,INT1
- IICSentByte(0x08); //16Hz
- IICStop();
- IICStart(); //Send start signal
- IICSentByte(0x62); //Send identifer IIC address,状态寄存器2
- IICSentByte(0x80); //INT1频率输出
- IICStop();
- IICStart(); //Send start signal
- IICSentByte(0x6C); //Send identifer IIC address,时钟较正
- IICSentByte(0x86); //较正值
- IICStop();
- // Send repeated start signal
- S35390A[0]=Exchange_highlow(S35390A[0]);
- S35390A[1]=Exchange_highlow(S35390A[1]);
- S35390A[2]=Exchange_highlow(S35390A[2]);
- S35390A[3]=Exchange_highlow(S35390A[3]);
- S35390A[4]=Exchange_highlow(S35390A[4]);
- IICStart(); // Send start signal
- IICSentByte(0x64); // Send identifer IIC address
- IICSentByte(S35390A[0]); //year
- IICSentByte(S35390A[1]); //month
- IICSentByte(S35390A[2]); //day
- IICSentByte(0); //week;
- IICSentByte(S35390A[3]); //hour
- IICSentByte(S35390A[4]); //min
- IICSentByte(0); //second
- IICStop();
- }
- void Read_S35390A(void)
- {
- unsigned char car;
- IICStart(); // Send start signal
- IICSentByte(0x65); // Send identifer IIC address
- // Send repeated start signal
- car=IICReceiveByte();S35390A[0]=Exchange_highlow(car); //year
- car=IICReceiveByte();S35390A[1]=Exchange_highlow(car); //mon
- car=IICReceiveByte();S35390A[2]=Exchange_highlow(car); //day
- car=IICReceiveByte(); //week;
- car=IICReceiveByte();S35390A[3]=Exchange_highlow(car); //hour
- *(&S35390A[3])=*(&S35390A[3])&0x3f; //???
- car=IICReceiveByte();S35390A[4]=Exchange_highlow(car); //min
- car=IICReceiveByte();S35390A[5]=Exchange_highlow(car); //second
- IICStop();
- }
复制代码
|
|