STM32F107+LWIP---如何检查tcp通讯断开?并重新连接
有没有用过数字式mems麦克风的,想问下SPI通讯的问题
stm32f103 CAN通讯多帧发送问题?
STM32F107作为USB主机模拟串口通讯不成功(采用PL2303芯片)
STM32F103C8 LIN通讯例程
求助,丐版J-Link通讯不上
ethernet可以与ethercat通讯吗?
LIS3DH SPI 通讯问题
STM32F373 CAN通讯问题CAN_FLAG_LEC
请教高手看看,STM32F1的多机通讯,地址匹配唤醒问题
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);//AIE,TIEÖжϽûÖ¹
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);//ʹÄܶ¨Ê±¼ÆÊýÆ÷£d¶¨Ê±Ê±ÖÓƵÂʵÄÑ¡ÔñΪ1hz
I2C_Write(PCF8563_TIME_COUNT_DOWN,0x3C);//µ¹¼ÆÊýÖÜÆÚΪ1s = 60/1HZ£¬£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿£¿
I2C_Write(PCF8563_MONTH_REGISTER,0x00);//Ô¼ĴæÆ÷×î¸ßλд0£¬±íʾ20ÊÀ¼Í
}
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_11\RTC_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ģʽ
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = PCF8563_ADDR_W;//I2CµÄ´ÓµØÖ·ºÍPCF8563µÄдµØÖ·ÊÇÒ»¸öµØÖ·Â𣿣¿£¿£¿£¿
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;//ʹÄÜÓ¦´ð
I2C_InitStructure.I2C_ClockSpeed = I2C_Speed; //400000hz
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;//Ó¦´ð7λµØÖ·
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);
}
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_11\RTC_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,具体型号?用的哪个版本的库?要控制的IIC外设是什么?
我只能告诉你,我在第一次用I2C读写EEPROM AT24CXX的时候,也遇到过类似这样的死循环出不来的问题,是有办法解决的。
解决方法大致有三种:
1、IO模拟I2C
2、操作完后,不能马上去读标志位,得延时一小会儿
3、while()循环中,增加一个超时退出自加变量
评分
查看全部评分
MCU:stm32f103rbt,库:CMSIS Cortex-M3 Core V1.30,IIC外设:PCF8563
评分
查看全部评分
评分
查看全部评分
我觉得我卡死在while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));里面是我写完7位地址后,没有应答,网上说是从设备慢的问题,但我不太清楚。您觉得是什么原因导致的呢?
嗯嗯是的,:
#define PCF8563_ADDR_W 0xA2
#define PCF8563_ADDR_R 0xA3
我想问一下,是不是得写一个用于I2C接收应答的中断服务函数呢?因为我是参照书上的例程写的,没有看到中断服务函数,网上的例子也很少。