Cześć, od kilku dni męczę się by poprawnie skonfigurować i coś odebrać za pomocą I2C z MCP9808(czujnik temp) w stm32f3 (Discovery). Przeszukałem już chyba wszystkie strony w internecie i nie mogę znaleźć informacji jak to poprawnie rozwiązać. Ciężko wzorować się na przykładach z f1, f4 bo mają inne funkcje. Starałem się już przerobić nawet kod z obsługi lsm303. Znalazłem takie info. w necie bo też mi program się wiesza w tym miejscu:
while(I2C_GetFlagStatus(MCP9808_I2C, I2C_ISR_TXIS) == RESET)
{
if((MCP9808_Timeout--) == 0) USART_SendBuff("2");
}
link
ale nic to nie dało.
Tak wygląda część kodu do odczytu:
uint16_t MCP9808_Read(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToRead)
{
/* Test on BUSY Flag */
uint32_t MCP9808_Timeout = MCP9808_LONG_TIMEOUT;
while(I2C_GetFlagStatus(MCP9808_I2C, I2C_ISR_BUSY) != RESET)
{
if((MCP9808_Timeout--) == 0) USART_SendBuff("1");
}
/* Configure slave address, nbytes, reload, end mode and start or stop generation */
I2C_TransferHandling(MCP9808_I2C, DeviceAddr, 1, I2C_SoftEnd_Mode, I2C_GenerateF) == RESET)
{
if((MCP9808_Timeout--) == 0) USART_SendBuff("5");
}
/* Clear STOPF flag */
I2C_ClearFlag(MCP9808_I2C, I2C_ICR_STOPCF);
/* If all operations OK */
return 0;
}
Ustawienia RCC:
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
Ustawienia GPIO:
GPIO_InitTypeDef GPIO_InitStructure;
/*--------------------------I2C----------------------------------------*/
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_4);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType=GPIO_OType_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*---------------------------UART--------------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7); // USART1_TX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7); // USART1_RX
I2C Init:
void I2C_Config(void)
{
I2C_InitTypeDef I2C_InitStructure;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_Timing = 0x2000090E;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
Będę wdzięczny za każdą pomoc bo to do projektu na studia i terminy gonią 🙁