2019-07-14 13:37发布
TOPCB 发表于 2019-3-18 17:46 楼主最好贴一下整个过程,从开始到最后的I2C通讯过程。
zhouxk 发表于 2019-3-18 17:53 本帖最后由 toofree 于 2018-2-27 11:55 编辑 好好整理一下问题吧。
TOPCB 发表于 2019-3-18 18:43 407的我测试过可以的,过程和你的一样,103以前我用的是模拟I2C,硬件好像有点问题。不知道后来大家都是使用硬件还是模拟了。大家一块讨论一下吧。
最多设置5个标签!
void RTC_Init(void)
{
I2C_Initial();
PCF8563_Init();
}
void PCF8563_Init(void)
{
I2C_Write(PCF8563_CONTRL_STATE_1,0x00);
I2C_Write(PCF8563_CONTRL_STATE_2,0x00);
I2C_Write(PCF8563_SENCOND_AE_REGISTER,0x80);
I2C_Write(PCF8563_HOUR_AE_REGISTER,0x80);
I2C_Write(PCF8563_DAY_AE_REGISTER,0x80);
I2C_Write(PCF8563_WEEK_AE_REGISTER,0x80);
I2C_Write(PCF8563_TIME_CONTRL,0x82);
I2C_Write(PCF8563_TIME_COUNT_DOWN,0x3C);
I2C_Write(PCF8563_MONTH_REGISTER,0x00);
}
void I2C_Initial(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//------------RTC_SDA->PB_11RTC_SCL->PB_10----------------------------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//------------I2C_INIT--------------------------------------------
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = PCF8563_ADDR_W;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_ClockSpeed = I2C_Speed; //400000hz
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1,&I2C_InitStructure);
//---------NVIC_INIT----------------------------------------------------
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
I2C_Cmd(I2C1,ENABLE);
}
void I2C_Write(u8 WriteAddr,u8 Dat)
{
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,PCF8563_ADDR_W,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,WriteAddr);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1,Dat);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1,ENABLE);
}
u8 I2C_Read(u8 ReadAddr)
{
u8 a;
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,PCF8563_ADDR_W,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,ReadAddr);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,PCF8563_ADDR_R,I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
a = I2C_ReceiveData(I2C1);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED))
I2C_AcknowledgeConfig(I2C1,DISABLE);
I2C_GenerateSTOP(I2C1,ENABLE);
I2C_AcknowledgeConfig(I2C1,ENABLE);
return a;
}
void I2C_Write_nByte(u8 WriteAddr,u8* Dat, u8 NumByte)
{
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,PCF8563_ADDR_W,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,WriteAddr);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
while(NumByte--)
{
I2C_SendData(I2C1,*Dat);
Dat++;
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
I2C_GenerateSTOP(I2C1,ENABLE);
}
void I2C_Read_nByte(u8 ReadAddr,u8* Buffer,u8 NumByte)
{
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,PCF8563_ADDR_W,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,ReadAddr);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,PCF8563_ADDR_R,I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
while(NumByte)
{
if(NumByte == 1)
{
I2C_AcknowledgeConfig(I2C1,DISABLE);
I2C_GenerateSTOP(I2C1,ENABLE);
}
if(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED))
{
*Buffer = I2C_ReceiveData(I2C1);
Buffer++;
NumByte--;
}
}
I2C_AcknowledgeConfig(I2C1,ENABLE);
}
MCU:stm32f103rbt,库:CMSIS Cortex-M3 Core V1.30,IIC外设:PCF8563
我觉得我卡死在while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));里面是我写完7位地址后,没有应答,网上说是从设备慢的问题,但我不太清楚。您觉得是什么原因导致的呢?
一周热门 更多>