2024-04-18 04:01 AM
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;
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
2024-04-18 04:22 AM - edited 2024-04-18 04:25 AM
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
2024-04-29 05:47 AM - edited 2024-04-29 06:09 AM
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
2024-04-29 05:50 AM
Please use this button to properly post source code:
2024-04-29 06:09 AM
Better?
Padawan