CH32V003 Programming: How to interface OLED Display (GPIO Bit-Banging)

In this article, we will learn how to interface OLED display with CH32V003. The OLED display comes in various sizes and configuration, here we are talking about mono color OLED graphic displays.

Here we will read Temperature and Humidity reading from SHTC3 Sensor from Sensirion and show the readings on the OLED Display. OLED Display we will drive over SPI and SPI will bit bang and not use the hardware SPI for this experiement.

In my previous article I have already shown how to interface STHC3 Sensor with CH32V003, please go through that if you have not read it yet.

CH32V003 Programming: How to interface OLED Display (GPIO Bit-Banging) 1

Bit-banging helps when you don’t have SPI on an MCU or want to hardware SPI to interface any other device and don’t want to share it with anything else. That time SPI via Bit Banging helps.

If you don’t know what is bit-banging:

GPIO bit banging is a technique of using general-purpose input/output (GPIO) pins to implement communication protocols or other digital functions in software. This can be useful in a number of ways, including:

  • To implement communication protocols that are not supported by the microcontroller’s hardware. For example, a microcontroller that does not have a built-in UART can use bit banging to implement a software UART.
  • To add flexibility and control to existing communication protocols. For example, a microcontroller can use bit banging to implement a custom SPI protocol with a non-standard baud rate or data format.
  • To implement digital functions that are not supported by the microcontroller’s hardware. For example, a microcontroller can use bit banging to implement a pulse-width modulation (PWM) signal generator or a digital-to-analog converter (DAC).

Bit banging can be a powerful tool for microcontroller developers, but it is important to be aware of the limitations. One limitation is that bit banging can be more CPU-intensive than using dedicated hardware peripherals. This is because the software must handle all of the timing and protocol sequencing. Another limitation is that bit banging can be more difficult to debug than using dedicated hardware peripherals.

Hardware IO Connections:

SHTC3 Sensor

  • SHTC3 SCL – GPIO C2
  • SHTC3 SDA – GPIO C1

OLED Dislplay

  • OLED CS – GPIO C0
  • OLED DC- GPIO D5
  • OLED RESET – GPIO D0
  • OLED SDA – GPIO D4
  • OLED SCLK- GPIO D6

First, I made the OLED working and then added the STHC3 code and integrated it.

In this article, I am not getting to details of how OLED display works and how to send commands and data to the display.

If you want to understand deeply how to interface OLED display how data need to be send to display a pixel it is very important for you to go through the OLED display controller datasheet.

Here is another article from SparkFun Electronics which will help you get a good overview of how display memory map works.

CH32V003 Programming: How to interface OLED Display (GPIO Bit-Banging) 2
CH32V003 Programming: How to interface OLED Display (GPIO Bit-Banging) 3

The code which is called in the main loop is here for the display & sensor.

OledPutCharXX function is used for display one Character at a time. It has four input parameters, first parameter is Line number - from which line data should start, second parameter is start position which is from which column data should start, third parameter is actual data to be printed and fourth column is how many bytes to read from the font buffer to print the character on the display.

There are other functions also in the OLED display driver library which you can try.

int main(void)
{
    uint8_t data[10] = {0};
    uint8_t tDigit[3] = {0};
    uint8_t hDigit[3] = {0};

    SystemCoreClockUpdate();
    Delay_Init();

    IIC_Init( 50000, 0x70);

    OledIoConfig();  // Configure IO used for OLED Display Interface


    while(1)
    {
        //OledPutString(0, 0, "PALLAV", 6);
        //OledPutChar2Line(0, 0, '0'-32, 20);
        //OledPutChar2Line(0, 10, '1'-32, 20);
        //OledPutChar2Line(0, 20, '2'-32, 20);
        //OledPutChar2Line(0, 30, '3'-32, 20);

        //Send wakeup command to SHTC3 Sensor
        SHTC3Wakeup(0x70);
        Delay_Ms(1);

        //Send measurement command to SHTC3 Sensor
        SHTC3MeasureCMDTFCE(0x70);
        Delay_Ms(50);

        //Read 6 bytes from the sensor
        I2CReadBytes(0x70, data, 6);

        //Raw data to real value conversion
        T1 = (((data[0]*256 + data[1]) * 1750)/65536)-450;

        //Separating temperature value to digits
        tDigit[0] = (T1/100)%10;
        tDigit[1] = (T1/10)%10;
        tDigit[2] = (T1%10);

        //Raw data to real value conversion
        H1 = ((data[3]*256 + data[4])*1000)/65536;

        //Separating Humidity value to digits
        hDigit[0] = (H1/100)%10;
        hDigit[1] = (H1/10)%10;
        hDigit[2] = (H1%10);

        // OLED Display - Temperature Reading T- XX.X 'C
        OledPutCharXX(0,  0, 'B', 42);
        OledPutCharXX(0, 16, '-', 42);
        OledPutCharXX(0, 40, tDigit[0]+48, 42);
        OledPutCharXX(0, 56, tDigit[1]+48, 42);
        OledPutCharXX(0, 72, '.', 42);
        OledPutCharXX(0, 80, tDigit[2]+48, 42);
        OledPutCharXX(0, 95, 58, 42);
        OledPutCharXX(0, 105, ';', 42);

        // OLED Display - Humidity Reading H- XX.X %
        OledPutCharXX(3,  0, '=', 42);
        OledPutCharXX(3, 16, '-', 42);
        OledPutCharXX(3, 40, hDigit[0]+48, 42);
        OledPutCharXX(3, 56, hDigit[1]+48, 42);
        OledPutCharXX(3, 72, '.', 42);
        OledPutCharXX(3, 80, hDigit[2]+48, 42);
        OledPutCharXX(3, 96, '>', 42);

        // Delay of one Second
        Delay_Ms(1000);
    }

}

The OLED library Files and Font files are in the project, working code is available here on the GitHub.

I hope you will find it useful.


See also other articles on CH32V003


I am currently working as an embedded systems design consultant and helping companies build custom embedded products, develop test automation solution for their PCB.

If you need help in designing a product based on CH32V003, please contact me.

If you have any feedback about the blog, you can share in the comments below or you can also contact me directly.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.