Hello guys,
I would like to ask for some help concerning I2C communication. I am an amateur regarding this type of communication and struggle making an easy write cycle.
Here are the facts:
-My SDA is on PinA14, SCL is on PinA15. -The slave's ID is 0xC0, on 7 Bits. -I would like to write on slaves register 0x80 the data 0x30.
As far as i understood i have to send: First : Slaves ID Second : Register adress from my Slave Third : Data i like to be written on this Register adress
Moreover, i have turned the interrupt when the Adress matched, and when the transfer is completed.
My questions are :
Why dont i receive any interrupt? Do i get an interrupt when i receive an acknowledge from my slave?
The code is the following:
#include "stm32f30x.h" void init_I2C() { I2C_InitTypeDef i2c; NVIC_InitTypeDef nvic; GPIO_InitTypeDef Gpio; I2C_InitTypeDef I2C; RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA ,ENABLE); Gpio.GPIO_Pin=GPIO_Pin_15; Gpio.GPIO_Mode = GPIO_Mode_AF; Gpio.GPIO_OType = GPIO_OType_OD; Gpio.GPIO_Speed = GPIO_Speed_50MHz; GPIO_PinAFConfig(GPIOA,GPIO_PinSource15,GPIO_AF_4); GPIO_Init(GPIOA,&Gpio); Gpio.GPIO_Mode = GPIO_Mode_AF; Gpio.GPIO_Pin = GPIO_Pin_14; Gpio.GPIO_OType = GPIO_OType_OD; Gpio.GPIO_Speed = GPIO_Speed_50MHz; GPIO_PinAFConfig(GPIOA,GPIO_PinSource14,GPIO_AF_4); GPIO_Init(GPIOA,&Gpio); I2C.I2C_Mode = I2C_Mode_I2C; I2C.I2C_OwnAddress1 = 0x00; I2C.I2C_Timing = 400000; I2C.I2C_Ack = I2C_Ack_Enable; I2C.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_Init(I2C1,&I2C); // Enable Transfer Adress Matched, transfer completed I2C_ITConfig(I2C1,I2C_IT_ADDR, ENABLE); I2C_ITConfig(I2C1,I2C_IT_TCI,ENABLE); // SET ADRESS I2C_SlaveAddressConfig(I2C1,0xC0); // Transfer direction I2C_MasterRequestConfig(I2C1,I2C_Direction_Transmitter); // Number of bytes send per Communication. I2C_NumberOfBytesConfig(I2C1,2); I2C_AutoEndCmd(I2C1,ENABLE); nvic.NVIC_IRQChannel = I2C1_EV_IRQn; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); } int main() { init_I2C(); I2C_SendData(I2C1,0x80); I2C_Cmd(I2C1,ENABLE); I2C_GenerateSTART(I2C1, ENABLE); while(1) { } } void I2C1_EV_IRQHandler() { // Acknoledge bit received! if( I2C_GetFlagStatus(I2C1, I2C_FLAG_ADDR)) // Adressed Matched { c I2C_SendData(I2C1,0x30); // Send the data } // Reset to 0 if(I2C_GetFlagStatus(I2C1,I2C_FLAG_TC)) // Transfer completed { I2C_ClearITPendingBit(I2C1,I2C_IT_STOPF); } }