Arm Mbed OS support forum

I2C to STM32F334R8

Hi, I’m working on a college project and I can’t manage to get my I2C to work on my board, I imported the TextLCD library and used the following code but nothing happened
The online compiler compiles it but the code does nothing and the mbed studio says that my board is not compatible with Mbed os 5 (I searched and tried to modify the targets.json file to make it work but it didn’t)


/* Hello World! for the TextLCD Enhanced Library*/
 
#include "mbed.h"
#include "TextLCD.h"
 
// Host PC Communication channels
Serial pc(USBTX, USBRX); // tx, rx
 
// I2C Communication
I2C i2c_lcd(D15,D14); // SDA, SCL
//I2C i2c_lcd(p28,p27); // SDA, SCL
 
// SPI Communication
//SPI spi_lcd(p5, NC, p7); // MOSI, MISO, SCLK
 
// LCD instantiation 
//TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x2);        // 4bit bus: rs, e, d4-d7
TextLCD_I2C lcd(&i2c_lcd, 0x40, TextLCD::LCD20x4);                  // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type
//TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD16x2, TextLCD::WS0010); // I2C exp: I2C bus, PCF8574 Slaveaddress, LCD Type, Ctrl Type
//TextLCD_I2C lcd(&spi_lcd, p8, TextLCD::LCD24x4D);                   // I2C exp: SPI bus, CS pin, LCD Type
//TextLCD_SPI_N lcd(&spi_lcd, p8, p9, TextLCD::LCD16x2, NC, TextLCD::ST7032_3V3); // SPI native: SPI bus, CS pin, RS pin, LCDType=LCD16x2, BL=NC, LCDTCtrl=ST7032
//TextLCD_I2C_N lcd(&i2c_lcd, ST7032_SA, TextLCD::LCD16x2, NC, TextLCD::ST7032_3V3); // I2C native: I2C bus, slaveaddress, LCDType=LCD16x2, BL=NC, LCDTCtrl=ST7032 =Ok
//TextLCD_I2C_N lcd(&i2c_lcd, SSD1803_SA1, TextLCD::LCD20x4D, NC, TextLCD::SSD1803_3V3); // I2C native: I2C bus, slaveaddress, LCDType=LCD20x4D, BL=NC, LCDTCtrl=SSD1803 =Ok
 
int main() {   
  Timer t;
 
 pc.printf("TextLCD Enhanced Test. Columns=%d, Rows=%d\n\r", lcd.columns(), lcd.rows());
    
  for (int row=0; row<lcd.rows(); row++) {
    int col=0;
      
    pc.printf("MemAddr(Col=%d, Row=%d)=0x%02X\n\r", col, row, lcd.getAddress(col, row));      
//    lcd.putc('-');
    lcd.putc('0' + row);      
      
    for (col=1; col<lcd.columns()-1; col++) {    
  //    lcd.putc('*');
    }
 
    pc.printf("MemAddr(Col=%d, Row=%d)=0x%02X\n\r", col, row, lcd.getAddress(col, row));      
    lcd.putc('+');       
  }    
 
// Fill screen again and time it
  t.start();
 
  for (int row=0; row<lcd.rows(); row++) {
    int col=0;
      
    lcd.putc('0' + row);      
      
    for (col=1; col<lcd.columns()-1; col++) {    
      lcd.putc('*');
    }
 
    lcd.putc('+');       
  }    
  t.stop();    
  pc.printf("All my hard work took %f sec\r\n", t.read());          
 
// Show cursor as blinking character
   lcd.setCursor(TextLCD::CurOff_BlkOn);
 
// Set and show user defined characters. A maximum of 8 UDCs are supported by the HD44780.
// They are defined by a 5x7 bitpattern. 
  lcd.setUDC(0, (char *) udc_0);  // Show |>
  lcd.putc(0);    
  lcd.setUDC(1, (char *) udc_1);  // Show <|
  lcd.putc(1);    
    
  pc.printf("Bye now\r\n");          
}

Hello,

  • from your description I’m not sure if your board is a dev board NUCLEO-F334R8 | Mbed or a custom board with same MCU.
  • F334R8 has only 64 KB Flash and 12 KB SRAM, so I think it still possible to use it with MbedOS 5.15.7 but under Bare metal profile becasue it is low memory target, for full MbedOS.
  • do not forgot to use pull-up resistors for both wires of I2C, if are not already implemented somewhere!
  • with displays usually needs to set up a contrast, with bad settings you will see nothing
  • what is good to do, is verified your I2C connection by this simple example code that will return an address if the conenction is OK
  • I use this TextLCD and also this useful guide about all things around it
  • also take care about correct setting in TextLCD_Config.h
  • last, how is written on I2C API page, be sure about correct address format

Note: The Arm Mbed API uses 8 bit addresses, so make sure to left-shift 7 bit addresses by 1 bit before passing them

.

BR, Jan

LCD display with i2C backpack usually needs to be powered by 5V but I2C from microcontroller is 3V3 in which case you need a level shifter. You can also try my library which i tested a few months ago with STM32F303 so i know it’s working with full OS6 (I didn’t try with bare metal OS)