微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 硬件电路设计 > TI模拟硬件电路设计 > 求助使用TM4C123GH6PM的I2C读取ds1307时钟数据都是乱码?

求助使用TM4C123GH6PM的I2C读取ds1307时钟数据都是乱码?

时间:10-02 整理:3721RD 点击:

写地址也正常啊  就是为什么我设置完了之后读取数据结果跟正常时钟不一样啊   用的是UART的串口打印出来的我列出下面的读和写的程序并附图一张

void ds1307_write(void)
{
I2CWrite_DS1307(0x00,0x00);
I2CWrite_DS1307_bytes(0x01,0x49,0x20,0x02);
I2CWrite_DS1307_bytes(0x04,0x14,0x06,0x16);


}
void I2CWrite_DS1307(uint8_t Address,uint8_t Address2)
{
I2CMasterSlaveAddrSet(I2C0_BASE,DS1307_write_ADDRESS, false);
I2CMasterDataPut(I2C0_BASE, Address);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
while(I2CMasterBusy(I2C0_BASE));
I2CMasterDataPut(I2C0_BASE, Address2);
I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
while(I2CMasterBusy(I2C0_BASE));

}
void I2CWrite_DS1307_bytes(uint8_t AD1,uint8_t AD2,uint8_t AD3,uint8_t AD4)
{
I2CMasterSlaveAddrSet(I2C0_BASE,DS1307_write_ADDRESS, false);
I2CMasterDataPut(I2C0_BASE,AD1);
I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_START);
while(I2CMasterBusy(I2C0_BASE));
I2CMasterDataPut(I2C0_BASE,AD2);
I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C0_BASE));
I2CMasterDataPut(I2C0_BASE,AD3);
I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_CONT);
while(I2CMasterBusy(I2C0_BASE));
I2CMasterDataPut(I2C0_BASE,AD4);
I2CMasterControl(I2C0_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH);
while(I2CMasterBusy(I2C0_BASE));

}

下面是读的程序

void receive(void)
{
unsigned int xh, xl, yh, yl, zh, zl,bh;
char cmd[16];
int i,index;
uint8_t pui32DATARx[Number], MasterCMD;

// I2CWrite_DS1307(0x00,0x00);
// while(I2CMasterBusy(I2C0_BASE));

I2CMasterSlaveAddrSet(I2C0_BASE,DS1307_write_ADDRESS, true);

for(i=0;i<Number;i++)
{
SysCtlDelay(SysCtlClockGet()/(1000*15));
if(i==0)
{
MasterCMD=I2C_MASTER_CMD_BURST_RECEIVE_START;
}
else if(i==(Number-1))
{
MasterCMD=I2C_MASTER_CMD_BURST_RECEIVE_FINISH;
}
else
{
MasterCMD=I2C_MASTER_CMD_BURST_RECEIVE_CONT;
}
I2CMasterControl(I2C0_BASE, MasterCMD);

while(I2CMasterBusy(I2C0_BASE));

pui32DATARx[i]=I2CMasterDataGet(I2C0_BASE);

}

xh=pui32DATARx[0]&0x7f;
UARTprintf("year:%d ",xh);
bh=pui32DATARx[6]&0x3f;
UARTprintf("month:%d ",bh);
yl=pui32DATARx[5]&0x3f;
UARTprintf("data:%d ",yl);
yh=pui32DATARx[4]&0x07;
UARTprintf("day:%d ",yh);
zl=pui32DATARx[3]&0x7f;
UARTprintf("hour:%d ",zl);
zh=pui32DATARx[2]&0x7f;
UARTprintf("minte:%d ",zh);
xl=pui32DATARx[1]&0x7f;
UARTprintf("seconds:%d \n",xl);

建议转移到MCU论坛回复。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top