Wednesday, 21 February 2018

OLED display with STM32

I already showed you how to connect LCD 16×2 using I2C in a STM32 microcontroller, you can check that out here. Today we will take a step forward and interface OLED display using I2C with STM32.
The display I will be using is a 0.96″ 128×64 oled display with black and blue colors. 
This display uses the I2C protocol to communicate to the microcontroller. So here you need only 2 pins i.e. SDA and SCL from the microcontroller and the VCC and GND.  I am using STM32f103c8t6  but as I mentioned before, the code will remain same for all STM32 devices. I searched a lot for the libraries but all I could find were that of arduino. Than at last I found one by Alexander Lutsai, and I modified it so that it can be used with the STM32 CubeMx.
Let’s get to work now. Fire up your STM32cubeMx and generate a new project for your microcontroller with I2C protcol running at 400KHz and duty cycle at I2C_DUTYCYCLE_2. By default, the library will use I2C1 handler but you can change that later in the program.

Connect the  SCL of the controller with the SCL of the display and SDA to SDA by using a pull-up resistor for both the pins. You can see the schematic in the connections tab.

HOW TO:-

Download the code below, unzip it, Copy the fonts.h and ssd1306.h in the Inc folder of your project and fonts.cand ssd1306.c in the Src folder and open the project now.
Include the following files in the main.c:-
#include “ssd1306.h”
#include “i2c-lcd.h”
Open the ssd1306.c and change the handler accordingly 
extern I2C_HandleTypeDef hi2c1;
You can see the functions available in the functions tab under ssd1306.c or in the ssd1306.h file. I am going to initialize the display and print “Hello World !!”, you know the basic stuff.

PRINT HELLO WORLD

uint8_t check = SSD1306_Init ();  // initialize the diaply

SSD1306_Fill (0);  // fill the display with black color
SSD1306_UpdateScreen(); // update screen

SSD1306_GotoXY (10,10);  // goto 10, 10
SSD1306_Puts ("HELLO", &Font_11x18, 1);  // print Hello

SSD1306_GotoXY (10, 30);  
SSD1306_Puts ("WORLD !!", &Font_11x18, 1);

SSD1306_UpdateScreen(); // update screen


CODE

#include "ssd1306.h"
#include "i2c-lcd.h"

int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();

  /* USER CODE BEGIN 2 */
check = SSD1306_Init ();
SSD1306_Fill (0);
SSD1306_UpdateScreen(); //display
SSD1306_GotoXY (10,10);
SSD1306_Puts ("HELLO", &Font_11x18, 1);
SSD1306_GotoXY (10, 30);
SSD1306_Puts ("WORLD !!", &Font_11x18, 1);
SSD1306_UpdateScreen(); //display

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */

}


RESULT


Servo motor with STM32

I have already covered a tutorial about Pulse Width Modulation in STM32  HERE  and in this tutorial i am going to cover one of its applicat...