大学生
最后登录1970-1-1
在线时间 小时
注册时间2017-4-20
|
本帖最后由 北海被害 于 2017-7-2 20:17 编辑
有用硬件iic读取过ds1307的前辈吗,我写的程序都等不到应答,启动信号没有应答,确定接口没接错,因为可以读写时钟模块上的eeprom,用软件可以得到应答,但是数据的值是有问题的。怎么搞/**********************************************************/
#define DS1307_I2Cx I2C1
#define DS1307_I2C_APBxClock_FUN RCC_APB1PeriphClockCmd
#define DS1307_I2C_CLK RCC_APB1Periph_I2C1
#define DS1307_I2C_GPIO_APBxClock_FUN RCC_APB2PeriphClockCmd
#define DS1307_I2C_GPIO_CLK RCC_APB2Periph_GPIOB
#define DS1307_I2C_SCL_PORT GPIOB
#define DS1307_I2C_SCL_PIN GPIO_Pin_6
#define DS1307_I2C_SDA_PORT GPIOB
#define DS1307_I2C_SDA_PIN GPIO_Pin_7
/* 这个地址只要与STM32外挂的I2C器件地址不一样即可 */
#define I2Cx_OWN_ADDRESS7 0X0A
/*外设地址*/
#define DS1307_ADDRESS 0xd0
/* STM32 I2C 快速模式 */
#define I2C_Speed 400000 //*
/************************************************************/
static u8 ds1307_Read(u8 ReadAddr)
{
u8 temp;
I2CTimeout = I2CT_LONG_TIMEOUT;
//*((u8 *)0x4001080c) |=0x80; //干什么用的?
// while(I2C_GetFlagStatus(DS1307_I2Cx, I2C_FLAG_BUSY))
// {
// if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(9);
// }
/* Send START condition */
I2C_GenerateSTART(DS1307_I2Cx, ENABLE);
//*((u8 *)0x4001080c) &=~0x80;
I2CTimeout = I2CT_FLAG_TIMEOUT;
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(DS1307_I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(10);
}
/* Send DS1307 address for write */
I2C_Send7bitAddress(DS1307_I2Cx, DS1307_ADDRESS, I2C_Direction_Transmitter);
I2CTimeout = I2CT_FLAG_TIMEOUT;
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(DS1307_I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(11);
}
// /* Clear EV6 by setting again the PE bit */
// I2C_Cmd(DS1307_I2Cx, ENABLE);
/* Send the DS1307's internal address to write to */
I2C_SendData(DS1307_I2Cx, ReadAddr);
I2CTimeout = I2CT_FLAG_TIMEOUT;
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(DS1307_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(12);
}
/* Send STRAT condition a second time */
I2C_GenerateSTART(DS1307_I2Cx, ENABLE);
I2CTimeout = I2CT_FLAG_TIMEOUT;
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(DS1307_I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(13);
}
/* Send DS1307 address for read */
I2C_Send7bitAddress(DS1307_I2Cx, DS1307_ADDRESS, I2C_Direction_Receiver);
I2CTimeout = I2CT_FLAG_TIMEOUT;
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(DS1307_I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback(14);
}
/*接收数据*/
temp = I2C_ReceiveData(DS1307_I2Cx);
return temp;
}
|
|