Description
So here I am starting I2C tutorial in STM32. I know it’s early because we still need to go through some basic things before jumping to I2C but the problem is LCD. It takes a whole lot of efforts to interface LCD using conventional methods, but by using I2C, we can do it really easy. I will cover other ways to interface LCD later in upcoming tutorials. Before using I2C to interface LCD, let’s first write a simple code to know about I2C functions.
I will use STM32CubeMX as usual to generate skeleton, and than edit it later. So follow along–
1.) Start STM32CubeMX and create a new project, select your board.
2.) Select I2Cx. Here x can be 1,2,or 3 depending on your board.
3.) My final setup looks like this
4.) Now go to configuration and select I2Cx.
5.) Make sure that your I2C setup is as follows
6.) Now Open the project and edit the code.
Some Insight into the code:-
If you look into i2c.c, you will find
1 2 3 4 5 6 7 8 9 10 |
HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout) HAL_I2C_Master_Transmit is used for transmitting data to the I2C device. It takes following arguments-- I2C_HandleTypeDef *hi2c //is the pointer to the i2c handler. uint16_t DevAddress //is the Address of the I2C device uint8_t *pData //is the pointer to the data to be transmitted uint16_t Size //is the size of the data in bytes uint32_t Timeout //timeout in case of any error This is the only function I am going to use for now. When we will advance in these tutorials, I will show you how to use dma (HAL_I2C_Master_Transmit_DMA) and interrupt (HAL_I2C_Master_Transmit_IT) to send and receive data from an I2C device. |
CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
/* Includes ------------------------------------------------------------------*/ #include "main.h" #include "stm32f4xx_hal.h" /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ I2C_HandleTypeDef hi2c1; /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); void Error_Handler(void); static void MX_GPIO_Init(void); static void MX_I2C1_Init(void); /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ uint8_t data[2]; /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_I2C1_Init(); /* USER CODE BEGIN 2 */ data[0]= 'o'; data[1] = 'k'; HAL_I2C_Master_Transmit (&hi2c1, 0x4E, data, 2, 50); /* Here hi2c1 is the hi2c1 handler 0x4E is the address of the slave data is data as defined above 2 is the size of data in bytes 50 is the timeout in ms in case of any error. */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /* I2C1 init function */ static void MX_I2C1_Init(void) { hi2c1.Instance = I2C1; hi2c1.Init.ClockSpeed = 100000; hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.OwnAddress2 = 0; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if (HAL_I2C_Init(&hi2c1) != HAL_OK) { Error_Handler(); } } /** Configure pins as * Analog * Input * Output * EVENT_OUT * EXTI */ static void MX_GPIO_Init(void) { /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); } |
3 Comments. Leave new
2.5
5
M glad to finally found a working code. Thank you very much. 🙂