现在选用的STM32L52 去读取DHT12 通过I2C1 PB6 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[0] = 0x04;
// GPIOB->AFR[0] = 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)) ; //EV5 test 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)) ; //EV6 test 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);
}
|