DHT12 i2c读取 死循环
现在选用的STM32L52去读取DHT12通过I2C1PB6 PB7 去读取,老是进入死循环,读取不了数据楼主程序可以公开吗?贴程序看下吧,这么说看不出什么原因
void I2C_IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
// GPIOB->AFR = 0x04;
// GPIOB->AFR = 0x04;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init( GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;//I2C_Mode_SMBusHost;//I2C_Mode_I2C; //I2C¶¨Òå
I2C_InitStructure.I2C_DutyCycle =I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0xa0; //É豸×Ô¼ºµÄµØÖ· ----×÷Ϊ´ÓÉ豸ʱÓÐÓÃ
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 300000;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1,ENABLE);
I2C_AcknowledgeConfig(I2C1, ENABLE);
}
//*********¶ÁÈ¡Ò»¸öÊý¾Ý***************
void I2C1_ReadByte( uchar ReadAddr, uchar *buf, uchar len)
{
uchar i;
I2C_AcknowledgeConfig( I2C1, ENABLE); //ʹÄÜÓ¦´ð-----ÔÀ´Ã»ÓÐ
while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BUSY)); //µÈ´ýI2C²»Ã¦ wait i2c bus is not busyµÈ´ý×ÜÏ߸´Î»
I2C_GenerateSTART( I2C1, ENABLE); //²úÉúÆðʼÐźÅsend start condition·¢ËÍÒ»¸ö¿ªÊ¼Î»
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_MODE_SELECT)) ; //EV5test on EV5 and clear itµÈ´ýEV5
I2C_Send7bitAddress( I2C1, ReadAddr, I2C_Direction_Transmitter); //·¢Ë͵ØÖ·send ADXL address for write ·¢ËÍÒ»¸öαдָÁî
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) ; //EV6test on EV6 and clear it µÈ´ýEV6
I2C_SendData( I2C1, 0);//ReadAddr); //·¢ËͶÁµØÖ· REG_HUMIDITY_HIGH
//while( I2C_GetFlagStatus( I2C1, I2C_FLAG_BTF) == RESET) ;
while( !I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)) ; //ÁíÍâÒ»ÖÖд·¨
I2C_Send7bitAddress( I2C1, ReadAddr+1, I2C_Direction_Receiver); //send ADXL address for read ·¢ËÍÒ»¸öαдָÁî
while( I2C_GetFlagStatus(I2C1, I2C_FLAG_ADDR) == RESET) ;
//while( !I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) ; //ÁíÍâÒ»ÖÖд·¨
for( i = 0 ; i < len; i++)
{
while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) ;
*buf = I2C_ReceiveData(I2C1);
buf++;
}
I2C_AcknowledgeConfig( I2C1, DISABLE); //disable acknowledgement
I2C_GenerateSTOP( I2C1, ENABLE); //·¢ËÍֹͣλ
I2C_AcknowledgeConfig(I2C1, ENABLE);
}
读取程序:
void ReadEnvirTempAndHump()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init( GPIOB, &GPIO_InitStructure);
SET_POWER();
SET_NOINVASSENSOR();
GPIO_SetBits( GPIOB, GPIO_Pin_6);
Delay(10000);
I2C_IO_Init();
Delay(5000);
I2C1_ReadByte( DHT12_I2C_ADDRESS, SendData, 5);
} 一般都死在
I2C_Send7bitAddress( I2C1, ReadAddr, I2C_Direction_Transmitter);
这句话下面。 用模拟i2c呀。
这个芯片是I2C吗,不是单线协议吗
搞错了,12确实是I2C,
我这上面挂了两个i2c器件,想着用模拟太烦了,所以想把这个调试通了
没看出来问题。
我现在是SCL---PB6 能看到波形图,SDA---PB7 一直是低信号
I2C_SendData( I2C1, 0);//ReadAddr);
while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) ;
现在是到这一步就死机????? 用模拟i2c试试 自己用普通的管脚模拟一下试试看
用示波器看看 是不是严格的跟芯片要求相同
估计是你时序有些许差距
用示波器看看波形吧
用模拟吧如果你确认时序没有问题的话
时序问题微调一下试试看
用普通的管脚模拟一下试试 用模拟i2c试试