|
S35390A通過IIC讀寫,讀取的時(shí)間與寫入的時(shí)間不一致,讀出來的數(shù)據(jù)只有年是對(duì)的,不知道問題出在哪里,請(qǐng)大佬幫忙分析
寫入時(shí)間為:S35390A[6]={0x16,0x0C,0x17,0x0E,0x24,0x00}; //22-12-23 14:36:00
讀出時(shí)間為:
images.png (21.32 KB, 下載次數(shù): 38)
下載附件
2021-12-23 14:30 上傳
以下是單片機(jī)程序部分:
- #include <stdio.h>
- #include <reg52.h>
- #include <35390.h>
- //#include <S-35390A.h>
- void InitUART(void)//使用定時(shí)器1作為串口波特率發(fā)生器
- {
- TH1 = 0xFD; //晶振11.0592mhz 波特率設(shè)為9600
- TL1 = TH1;
- TMOD |= 0x20; //定時(shí)器1方式2
- SCON = 0x50; //串口接收使能
- ES = 1; //串口中斷使能
- TR1 = 1; //定時(shí)器1使能
- TI = 1; //發(fā)送中斷標(biāo)記位,必須設(shè)置
- }
- //毫秒延時(shí)函數(shù)
- 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();//初始化時(shí)間
- while(1)
- {
- Read_S35390A();//讀取時(shí)間
- 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; //釋放數(shù)據(jù)總線,等待應(yīng)答
- IICWait();
- IIC_SCL=1; //等待數(shù)據(jù)線的ACK(時(shí)鐘信號(hào)一般上升沿有效)
- 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,狀態(tài)寄存器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,狀態(tài)寄存器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,狀態(tài)寄存器2
- IICSentByte(0x80); //INT1頻率輸出
- IICStop();
- IICStart(); //Send start signal
- IICSentByte(0x6C); //Send identifer IIC address,時(shí)鐘較正
- 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();
- }
復(fù)制代碼
|
|