U8Glib I2C sh1106

Working libraries, libraries being ported and related hardware
Post Reply
Mabez
Posts: 8
Joined: Tue Nov 22, 2016 12:05 am

U8Glib I2C sh1106

Post by Mabez » Tue Nov 22, 2016 12:19 am

Hi all,

I have been working on getting my sh1106 I2C OLED working with the STM32F103C using U8Glib but with no success, I previously managed to get it working with a teensy a few months back and I figured it would be a simple fix to get it working with this.

Here is the code for my u8g hardware function:

Code: Select all

#include "u8g_arm.h"
#include <Wire.h>

const uint16_t SLAVE_ADR = 0x3C;    // == 0x78
const uint16_t DATA_MODE = 0x40;    // i2c data command
const uint16_t COMMAND_MODE = 0x00; // i2c command command; 0x80 -- either seems to work

uint16_t CTRL_CMD = 0; 

uint8_t u8g_com_hw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr)
{
  switch(msg)
  {
    case U8G_COM_MSG_STOP:
      //STOP THE DEVICE
    break;

    case U8G_COM_MSG_INIT:

      Wire.begin();
  
    break;

    case U8G_COM_MSG_ADDRESS:  
      //SWITCH FROM DATA TO COMMAND MODE (arg_val == 0) for command mode
   
        CTRL_CMD = (arg_val == 0) ? COMMAND_MODE : DATA_MODE;
    
    break;

    case U8G_COM_MSG_RESET:
      // there's no reset pin .. ~ U8G_I2C_OPT_NO_ACK 
    break;

    case U8G_COM_MSG_WRITE_BYTE:
      //WRITE BYTE TO DEVICE
    
      Wire.beginTransmission(SLAVE_ADR);    // slave addr
      Wire.write(CTRL_CMD);                 // cmd
      Wire.write(arg_val);                  // data
      Wire.endTransmission();  
     
    break;

    case U8G_COM_MSG_WRITE_SEQ:
    case U8G_COM_MSG_WRITE_SEQ_P:
   {
    //WRITE A SEQUENCE OF BYTES TO THE DEVICE
    register uint8_t *ptr = static_cast<uint8_t *>(arg_ptr);
    
    Wire.beginTransmission(SLAVE_ADR);
    Wire.write(CTRL_CMD); 
    Wire.write(ptr, arg_val); 
    Wire.endTransmission(); 
  }
    break;

  }
  return 1;
}
and here is the simple sketch:

Code: Select all

#include <u8g_arm.h>

u8g_t u8g;

void setup() {
  u8g_InitComFn(&u8g, &u8g_dev_sh1106_128x64_i2c, u8g_com_hw_i2c_fn);
}

void loop() {
  u8g_FirstPage(&u8g);
  do {
      u8g_DrawStr(&u8g,42,55,"Loading...");
  } while( u8g_NextPage(&u8g) );
}
All the code compiles, but the display does not show anything. The display has been tested with a arduino nano and works fine. Using the wire scanner examples the display is found at the address 0x3C.

I am really stumped, any help would be appreciated!

Cheers,

Mabez

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest