cancel
Showing results for 
Search instead for 
Did you mean: 

Help for I2C

stm32beginner2024
Associate II

Hello,

can someone help me with a sample code for STM32F12G-Disco?
I am controlling the SSD1306 via I2C, so an oled display 0x78.
I just want to output a text on the dsiplay.

 

This is a code excerpt. Most of it was done by code generation:

#define ssd1306_I2C_Port hi2c2
#define ssd1306_I2C_ADDR 0x78

I2C_HandleTypeDef hi2c2;

QSPI_HandleTypeDef hqspi;

UART_HandleTypeDef huart2;

SRAM_HandleTypeDef hsram1;

 

int main(void)
{
  char OledText[] = "Test ";
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_FSMC_Init();
  MX_I2C1_Init();
  MX_I2C2_Init();
  MX_I2S3_Init();
  MX_QUADSPI_Init();
  MX_USART2_UART_Init();
  MX_USB_HOST_Init();

  HAL_StatusTypeDef ret;
  uint8_t buf[12];
  int16_t val;
  float temp_c;
  ret = HAL_I2C_Master_Transmit(&hi2c2, ssd1306_I2C_ADDR, buf, 1, HAL_MAX_DELAY);
  if ( ret == HAL_OK ) {
        strcpy((char*)buf, "Error Tx\r\n");
   }

}

 

Is that roughly how you do it or how would you do it? I hope you can help me

 

4 REPLIES 4
padawan
Senior

Hello,

the ssd1306 does not have built-in characters like a 4x20 LCD.

You have to init the display and 

you need a font and a driver like

https://github.com/afiskon/stm32-ssd1306

or similar.

This was the first i found.... 

I can't judge the driver, mine looks different.... (c++)

hint: goggle ssd1306 driver  STM32 ;)

hth padawan

 

padawan
Senior

Ok,

in your mail the Defines are lowercase

#define ssd1306_I2C_Port hi2c2
#define ssd1306_I2C_ADDR 0x78

in the .h  they are Uppercase.

#ifndef SSD1306_I2C_PORT
#define SSD1306_I2C_PORT hi2c1
#endif

#ifndef SSD1306_I2C_ADDR
#define SSD1306_I2C_ADDR (0x3C << 1)
#endif

In the section of the h.file ssd_1306_Init()

#if defined(SSD1306_USE_I2C)
extern I2C_HandleTypeDef SSD1306_I2C_PORT;
#elif defined(SSD1306_USE_SPI)
extern SPI_HandleTypeDef SSD1306_SPI_PORT;
#else
#error "You should define SSD1306_USE_SPI or SSD1306_USE_I2C macro!"
#endif

So you have to define SSD1306_USE_I2C in your main.

Some default questions:

Pullups extern ok?

Pin out of the modul ok?

I have 2 different modules here where VDD and GND are swapped.

Go in to debug mode and set a Breakpoint at the ssd_1306_Init();

trace the init sequence and look at the HAL_Error,

You can modify the lib

 

void ssd1306_WriteCommand(uint8_t byte) {
    HAL_I2C_Mem_Write(&SSD1306_I2C_PORT, SSD1306_I2C_ADDR, 0x00, 1, &byte, 1, HAL_MAX_DELAY);
}
to
void ssd1306_WriteCommand(uint8_t byte) {
   HAL_StatusTypeDef _hal;
   _hal=HAL_I2C_Mem_Write(&SSD1306_I2C_PORT, SSD1306_I2C_ADDR, 0x00, 1, &byte, 1, HAL_MAX_DELAY);
}

 

So you can see if the HAL_I2C is working (_hal have to be HAL_OK)

hth 

padawan

 

 

 

 

 

 

Please use this button to properly post source code:

AndrewNeil_0-1714395013971.png

 

 

Better?

Padawan