2014-07-31 07:30 AM
Hello Everyone,
I have started to work on the STM32F0308 discovery board. Now I am trying to read data from the RTC PCF8583 using I2C. I do not have prior experience with I2C. My problem is that I am not able to read data from the RTC (Output on terminal in Run mode: <0><0><0>) . Please check my code and let me know what I have done is correct ... Please educate me on this.CODE:void usartInit(){ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); USART_InitTypeDef USART_InitStructure ; USART_StructInit(&USART_InitStructure); USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE);}void TIM17_IRQHandler(){ TIM_ClearITPendingBit(TIM17 ,TIM_IT_Update); i2c_call();}void vTimer17_init(){ TIM_TimeBaseInitTypeDef TIM_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE); TIM_TimeBaseStructInit(&TIM_InitStructure); TIM_InitStructure.TIM_Prescaler = 4800-1; TIM_InitStructure.TIM_Period = 100-1; TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_InitStructure.TIM_ClockDivision = 0; TIM_TimeBaseInit(TIM17,&TIM_InitStructure ); TIM_Cmd(TIM17, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM17_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init (& NVIC_InitStructure); NVIC_EnableIRQ(TIM17_IRQn); TIM_ITConfig(TIM17 , TIM_IT_Update , ENABLE);}void i2c_call(){ uint16_t sec; I2C_TransferHandling(I2C1, 0xA0,1, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); I2C_SendData(I2C1, 0x02); I2C_TransferHandling(I2C1, 0xA1,1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); sec = I2C_ReceiveData(I2C1); I2C_GenerateSTOP(I2C1, ENABLE); while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USART1, sec);}void i2c1_init(){ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_1); I2C_InitTypeDef I2C_InitStructure; I2C_StructInit(&I2C_InitStructure); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_Ack = I2C_Ack_Disable; I2C_InitStructure.I2C_OwnAddress1 = 0x5A; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Disable; I2C_InitStructure.I2C_DigitalFilter = 0x00; I2C_InitStructure.I2C_Timing = 0x00B01A4F; I2C_Init(I2C1, &I2C_InitStructure); I2C_Cmd(I2C1, ENABLE);}int main(void){ i2c1_init(); usartInit(); vTimer17_init(); while(1) { } return 0;}PS : I know there is an inbuilt RTC in the chip which I am using. But I want to try interfacing with an external RTC and to get some familiarity with I2C. Thanks in AdvancePrashanth #discovery #stm32 #i2c #stm32f02014-07-31 09:31 AM
You must configure the I2C pins in OD (Open Drain) mode, with either an internal or external pull up.
2014-07-31 10:01 AM
I have configured the I2C pin in OD mode and it is externally pulled up. Still it does not work. But when I run in debug mode by stepping in I can see some values. Unfortunately the values does not appear in normal run mode.
Thanks, Prashanth