IAR for STM8L标准库硬件I2C/SPI驱动SSD1306 OLED屏幕

IAR for STM8L标准库硬件I2C/SPI驱动SSD1306 OLED屏幕


  • 参考STM32硬件驱动 https://github.com/lamik/OLED_SSD1306_STM32_HAL

  • ✨说明,本工程仅验证了硬件I2C接口,硬件SPI接口的SSD1306 OLED屏幕手上没有,并没有验证是否可行。

  • 基于IAR开发环境,单片机:STM8L052R8

  • 效果演示:

  • 软件驱动方式可以参考屏幕驱动厂家提供的资料修改得来。

  • 图形取模方式,可以参考屏幕厂家资料。测试时,我也是直接拷贝的屏幕厂家资料中的取模图片数据粘贴过来的。注意:不要找我要取模软件(有截图,有名字,自行搜索下载)
    IAR for STM8L标准库硬件I2C/SPI驱动SSD1306 OLED屏幕_第1张图片

硬件接口说明

  • 硬件引脚映射关系图:

IAR for STM8L标准库硬件I2C/SPI驱动SSD1306 OLED屏幕_第2张图片

  • 硬件接口软件配置在hw_setup.h文件中,具体单片机驱动根据具体型号修改相关内容。
#define SSD1306_I2C                         I2C1
#define   SSD1306_I2C_CLK               CLK_Peripheral_I2C1
#define SSD1306_SPI_CLK             CLK_Peripheral_SPI1
......
/* SPI Connection */
#define SSD1306_SCK_Pin         GPIO_Pin_5
#define SSD1306_SCK_Port        GPIOC
#define SSD1306_MOSI_Pin        GPIO_Pin_6
#define SSD1306_MOSI_Port       GPIOC
#define SSD1306_MISO_Pin        GPIO_Pin_7
#define SSD1306_MISO_Port       GPIOC

#define SSD1306_CS_Pin          GPIO_Pin_4
#define SSD1306_CS_Port         GPIOC

#define SSD1306_DC_Pin          GPIO_Pin_3
#define SSD1306_DC_Port         GPIOC
#define SSD1306_RESET_Pin       GPIO_Pin_0
#define SSD1306_RESET_Port      GPIOE

/* I2C Connection */
#define SSD1306_SCL_Pin         GPIO_Pin_1
#define SSD1306_SCL_Port        GPIOC
#define SSD1306_SDA_Pin         GPIO_Pin_0
#define SSD1306_SDA_Port        GPIOC

OLED驱动相关

  • API相关显示函数说明,可以参考原创作者的blog文章:https://msalamon.pl/wyswietlacze-oled-ssd1306-i2c-czy-spi/
  • oled_ssd1306.h
/**
  *****************************************************************************
  * @file           : oled_ssd1306.h
  * @brief          : Header for SSD1306 display driver
  *
  *****************************************************************************
  * @attention
  *
  *  This code is a part of:
  *   OLED SSD1306 library for STM32
  *   https://github.com/lamik/OLED_SSD1306_STM32_HAL
  *
  *  The MIT License.
  *  Created on: 27.01.2017
  *      Author: Mateusz Salamon
  *      www.msalamon.pl
  *      [email protected]
  *
  *****************************************************************************
  */

#ifndef __OLED_SSD1306_H_
#define __OLED_SSD1306_H_

/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"
/* Exported defines ----------------------------------------------------------*/
/**
  * @brief  Resolution
  */
#define SSD1306_LCDWIDTH   128
#define SSD1306_LCDHEIGHT  64

/**
  * @brief  Select the instruction set
  * @note   Please set what functionality you want to use.
  *         Some functions need other functionalities.
  *         It should works automatically.
  * 条件编译
  *         1 - will be compiled
  *         0 - won't be compiled
  */
#define GRAPHIC_ACCELERATION_COMMANDS  1
#define ADVANCED_GRAPHIC_COMMANDS      1

/**
  *****************************************************************************
  */

/**
  * @brief  Commands
  */
#define SSD1306_SETCONTRAST          0x81
#define SSD1306_DISPLAYALLON_RESUME  0xA4
#define SSD1306_DISPLAYALLON         0xA5
#define SSD1306_NORMALDISPLAY        0xA6
#define SSD1306_INVERTDISPLAY        0xA7
#define SSD1306_DISPLAYOFF           0xAE
#define SSD1306_DISPLAYON            0xAF
#define SSD1306_SETDISPLAYOFFSET     0xD3
#define SSD1306_SETCOMPINS           0xDA
#define SSD1306_SETVCOMDETECT        0xDB
#define SSD1306_SETDISPLAYCLOCKDIV   0xD5
#define SSD1306_SETPRECHARGE         0xD9
#define SSD1306_SETMULTIPLEX         0xA8
#define SSD1306_SETLOWCOLUMN         0x00
#define SSD1306_SETHIGHCOLUMN        0x10
#define SSD1306_SETSTARTLINE         0x40
#define SSD1306_MEMORYMODE           0x20
#define SSD1306_COLUMNADDR           0x21
#define SSD1306_PAGEADDR             0x22
#define SSD1306_COMSCANINC           0xC0
#define SSD1306_COMSCANDEC           0xC8
#define SSD1306_SEGREMAP             0xA0
#define SSD1306_CHARGEPUMP           0x8D
#define SSD1306_EXTERNALVCC          0x01
#define SSD1306_SWITCHCAPVCC         0x02

/**
  * @brief  Scrolling defines
  */
#define SSD1306_ACTIVATE_SCROLL                       0x2F
#define SSD1306_DEACTIVATE_SCROLL                     0x2E
#define SSD1306_SET_VERTICAL_SCROLL_AREA              0xA3
#define SSD1306_RIGHT_HORIZONTAL_SCROLL               0x26
#define SSD1306_LEFT_HORIZONTAL_SCROLL                0x27
#define SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL  0x29
#define SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL   0x2A

/**
  * @brief  Advanced Graphic defines
  */
#define SSD1306_FADE_OUT  0x23
#define SSD1306_ZOOM_IN   0xD6

/**
  * @brief  Colors
  */
#define BLACK    0
#define WHITE    1
#define INVERSE  2

/* Private typedef -----------------------------------------------------------*/
/**
  * @brief  Scrolling enums
  */
typedef enum {
  SCROLL_EVERY_5_FRAMES,
  SCROLL_EVERY_64_FRAMES,
  SCROLL_EVERY_128_FRAMES,
  SCROLL_EVERY_256_FRAMES,
  SCROLL_EVERY_3_FRAMES,
  SCROLL_EVERY_4_FRAMES,
  SCROLL_EVERY_25_FRAMES,
  SCROLL_EVERY_2_FRAMES
} scroll_horizontal_speed;

/* Exported functions ------------------------------------------------------- */
/**
  * @brief  Functions
  */
#ifdef SSD1306_I2C_CONTROL
void SSD1306_I2cInit(void);
#endif

#ifdef SSD1306_SPI_CONTROL
void SSD1306_SpiInit(void);
#endif

/**
  * @brief  Configuration
  */
void SSD1306_DisplayON(uint8_t DisplayState);//开显示
void SSD1306_InvertColors(uint8_t Invert);//反色显示
void SSD1306_RotateDisplay(uint8_t Rotate);//屏幕旋转
void SSD1306_SetContrast(uint8_t Contrast);
void SSD1306_Command(uint8_t com);//写指令
/**
  * @brief  Drawing
  */
void SSD1306_DrawPixel(int16_t x, int16_t y, uint8_t Color);
void SSD1306_Clear(uint8_t Color);
void SSD1306_Display(void);
void SSD1306_Bitmap(uint8_t *bitmap);

#if GRAPHIC_ACCELERATION_COMMANDS == 1
/**
  * @brief  Graphic Acceleration Commands
  */
void SSD1306_StartScrollRight(uint8_t StartPage, uint8_t EndPage,
       scroll_horizontal_speed Speed);//页寻址模式
void SSD1306_StartScrollLeft(uint8_t StartPage, uint8_t EndPage,
       scroll_horizontal_speed Speed);
void SSD1306_StartScrollRightUp(uint8_t StartPage, uint8_t EndPage,
       scroll_horizontal_speed HorizontalSpeed, uint8_t VerticalOffset);
void SSD1306_StartScrollLeftUp(uint8_t StartPage, uint8_t EndPage,
       scroll_horizontal_speed HorizontalSpeed, uint8_t VerticalOffset);
void SSD1306_StopScroll(void);
#endif

#if ADVANCED_GRAPHIC_COMMANDS == 1
/**
  * @brief  Advanced Graphic Commands
  */
void SSD1306_StartFadeOut(uint8_t Interval);//逐渐降低对比度
void SSD1306_StartBlinking(uint8_t Interval);//效果与淡出类似,只是显示返回到“间隔”值并在循环中“闪烁”。
void SSD1306_StopFadeOutOrBlinking(void);//恢复正常显示效果
void SSD1306_ZoomIn(uint8_t Zoom);//缩放显示
#endif

#endif /* __OLED_SSD1306_H_ */

  • oled_ssd1306.c
/**
  *****************************************************************************
  * @file           : oled_ssd1306.c
  * @brief          : SSD1306 display driver
  *
  *****************************************************************************
  * @attention
  *
  *  This code is a part of:
  *   OLED SSD1306 library for STM32
  *   https://github.com/lamik/OLED_SSD1306_STM32_HAL
  *
  *  The MIT License.
  *  Created on: 27.01.2017
  *      Author: Mateusz Salamon
  *      www.msalamon.pl
  *      [email protected]
  *
  *****************************************************************************
  */


/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "oled_ssd1306.h"
#include "hw_setup.h"

/* Private includes ----------------------------------------------------------*/
#include 



/* Private variables ---------------------------------------------------------*/
static uint8_t buffer[SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8];

/* Exported functions ------------------------------------------------------- */
/**
  * @brief  Send command to the controller
  * @param  com: specifies the signgle command to send
  * @retval None
  */
void SSD1306_Command(uint8_t com)
{
#ifdef SSD1306_I2C_CONTROL
  /* ***I2C*** */
  I2C_CWrite(SSD1306_I2C_ADDRESS, 0x00, &com, sizeof(com));
#endif
#ifdef SSD1306_SPI_CONTROL
  /* ***SPI*** */
  GPIO_ResetBits(SSD1306_DC_Port, SSD1306_DC_Pin);
#ifndef SPI_CS_HARDWARE_CONTROL
  GPIO_ResetBits(SSD1306_CS_Port, SSD1306_CS_Pin);
#endif
  SPI_Transmit(&com, 1);
#ifndef SPI_CS_HARDWARE_CONTROL
  GPIO_SetBits(SSD1306_CS_Port, SSD1306_CS_Pin);
#endif
#endif
}

/**
  * @brief  Send data to the controller
  * @param  dat: specifies the single data to send
  * @retval None
  */
void SSD1306_Data(uint8_t dat)
{
#ifdef SSD1306_I2C_CONTROL
  /* ***I2C*** */
  I2C_CWrite(SSD1306_I2C_ADDRESS, 0x40, &dat, sizeof(dat));
#endif
#ifdef SSD1306_SPI_CONTROL
  /* ***SPI*** */
  GPIO_SetBits(SSD1306_DC_Port, SSD1306_DC_Pin);
#ifndef SPI_CS_HARDWARE_CONTROL
  GPIO_ResetBits(SSD1306_CS_Port, SSD1306_CS_Pin);
#endif
  SPI_Transmit(&dat, 1);
#ifndef SPI_CS_HARDWARE_CONTROL
  GPIO_SetBits(SSD1306_CS_Port, SSD1306_CS_Pin);
#endif
#endif
}

#if defined(SSD1306_SPI_CONTROL) || defined(SSD1306_RESET_USE)
/**
  * @brief  Pull reset pin to ground for 5ms to reset controller
  * @param  None
  * @retval None
  */
void SSD1306_Reset(void)
{
  GPIO_ResetBits(SSD1306_RESET_Port, SSD1306_RESET_Pin);
  Delay_ms(5);
  GPIO_SetBits(SSD1306_RESET_Port, SSD1306_RESET_Pin);
}
#endif

/**
  * @brief  Configuration functions
  */

/**
  * @brief  Set the global inversion mode
  * @param  Invert: Set to one for enable inverse mode
  * @retval None
  */
void SSD1306_InvertColors(uint8_t Invert)
{
  SSD1306_Command(Invert ? SSD1306_INVERTDISPLAY : SSD1306_NORMALDISPLAY);
}

/**
  * @brief  Set the display rotation
  * @param  Rotate: Set to one for enable rotated mode
  * @retval None
  */
void SSD1306_RotateDisplay(uint8_t Rotate)
{
  if(Rotate > 1) Rotate = 1;

  SSD1306_Command(SSD1306_SEGREMAP | (0x01 & Rotate));
               /* Set Segment Re-Map Default                        */
               /* 0xA0  (0x00) => column Address 0 mapped to 127    */
               /* 0xA1  (0x01) => Column Address 127 mapped to 0    */

  SSD1306_Command(SSD1306_COMSCANINC | (0x08 & (Rotate << 3)));
               /* Set COM Output Scan Direction                     */
               /* 0xC0  (0x00) => normal mode (RESET) Scan from     */
               /*  COM0 to COM[N-1];Where N is the Multiplex ratio. */
               /* 0xC8  (0xC8) => remapped mode. Scan from COM[N-1] */
               /*  to COM0;;Where N is the Multiplex ratio.         */
}

/**
  * @brief  Set the display on or off
  * @param  DisplayState: Set the current display state
  * @retval None
  */
void SSD1306_DisplayON(uint8_t DisplayState)
{
  SSD1306_Command(DisplayState ? SSD1306_DISPLAYON : SSD1306_DISPLAYOFF);
}

/**
  * @brief  Set the display contrast value
  * @param  Contrast: Contrast value
  * @retval None
  */
void SSD1306_SetContrast(uint8_t Contrast)
{
	/* Set Contrast Control */
  SSD1306_Command(SSD1306_SETCONTRAST);
  SSD1306_Command(Contrast);
}

#if GRAPHIC_ACCELERATION_COMMANDS == 1
/**
  * @brief  Graphic Acceleration Command
  */

/**
  * @brief  Enable the auto scroll functionality, right direction
  * @param  StartPage: Internal starting page number
  *         EndPage  : Internal endining page number
  *         Speed    : Frames per second from @ref scroll_horizontal_speed
  * @retval None
  */
void SSD1306_StartScrollRight(uint8_t StartPage, uint8_t EndPage, scroll_horizontal_speed Speed)
{
  SSD1306_Command(SSD1306_RIGHT_HORIZONTAL_SCROLL);
  SSD1306_Command(0x00);
  SSD1306_Command(StartPage);
  SSD1306_Command(Speed);
  SSD1306_Command(EndPage);
  SSD1306_Command(SSD1306_ACTIVATE_SCROLL);
}

/**
  * @brief  Enable the auto scroll functionality, left direction
  * @param  StartPage: Internal starting page number
  *         EndPage  : Internal endining page number
  *         Speed    : Frames per second from @ref scroll_horizontal_speed
  * @retval None
  */
void SSD1306_StartScrollLeft(uint8_t StartPage, uint8_t EndPage, scroll_horizontal_speed Speed)
{
  //可以参考:http://www.51hei.com/bbs/dpj-180145-1.html
  SSD1306_Command(0x2E);        //关闭滚动
  SSD1306_Command(SSD1306_LEFT_HORIZONTAL_SCROLL);//水平向左或者右滚动 26/27
  SSD1306_Command(0x00);//虚拟字节
  SSD1306_Command(StartPage); //起始页 0
  SSD1306_Command(Speed);  //滚动时间间隔
  SSD1306_Command(EndPage);//终止页 7
  SSD1306_Command(0x00);//虚拟字节
  SSD1306_Command(0xFF);//虚拟字节
  SSD1306_Command(SSD1306_ACTIVATE_SCROLL);//开启滚动
  
}

/**
  * @brief  Enable the auto scroll functionality, two direction
  * @param  Direction       : Direction
  *         StartPage       : Internal starting page number
  *         EndPage         : Internal endining page number
  *         HorizontalSpeed : Frames per second from
  *                           @ref scroll_horizontal_speed
  *         VerticalOffset  :
  * @retval None
  */
void SSD1306_ScrollingHorVer(uint8_t Direction, uint8_t StartPage,
       uint8_t EndPage, scroll_horizontal_speed HorizontalSpeed, uint8_t VerticalOffset)
{
  if(VerticalOffset > 127) VerticalOffset = 127;
  SSD1306_Command(Direction);
  SSD1306_Command(0x00);
  SSD1306_Command(StartPage);
  SSD1306_Command(HorizontalSpeed);
  SSD1306_Command(EndPage);
  SSD1306_Command(VerticalOffset);
  SSD1306_Command(SSD1306_ACTIVATE_SCROLL);
}

/**
  * @brief  Enable the auto scroll finctionality, left and up direction
  * @param  Direction       : Direction
  *         StartPage       : Internal starting page number开始页:[0-7]
  *         EndPage         : Internal endining page number结束页:
  *         HorizontalSpeed : Frames per second from
  *                           @ref scroll_horizontal_speed
  *         VerticalOffset  :垂直偏移量
  * @retval None
  */
void SSD1306_StartScrollLeftUp(uint8_t StartPage, uint8_t EndPage,
       scroll_horizontal_speed HorizontalSpeed, uint8_t VerticalOffset)
{
  SSD1306_ScrollingHorVer(SSD1306_VERTICAL_AND_LEFT_HORIZONTAL_SCROLL,
                          StartPage, EndPage, HorizontalSpeed, VerticalOffset);
}

/**
  * @brief  Enable the auto scroll finctionality, right and up direction
  * @param  Direction       : Direction
  *         StartPage       : Internal starting page number
  *         EndPage         : Internal endining page number
  *         HorizontalSpeed : Frames per second from
  *                           @ref scroll_horizontal_speed
  *         VerticalOffset  :
  * @retval None
  */
void SSD1306_StartScrollRightUp(uint8_t StartPage, uint8_t EndPage,
       scroll_horizontal_speed HorizontalSpeed, uint8_t VerticalOffset)
{
  SSD1306_ScrollingHorVer(SSD1306_VERTICAL_AND_RIGHT_HORIZONTAL_SCROLL,
                          StartPage, EndPage, HorizontalSpeed, VerticalOffset);
}

/**
  * @brief  Disable the auto scroll finctionality
  * @param  None
  * @retval None
  */
void SSD1306_StopScroll(void)
{
  SSD1306_Command(SSD1306_DEACTIVATE_SCROLL);
}
#endif

/**
  * @brief  Advanced Graphic Commands
  */
#if ADVANCED_GRAPHIC_COMMANDS == 1
/**
  * @brief  Enable auto fade out finctionality
  * @param  Interval : Time between fade steps
  * @retval None
  */
void SSD1306_StartFadeOut(uint8_t Interval)
{
  SSD1306_Command(SSD1306_FADE_OUT);
  SSD1306_Command(0x20 | (0x0F & Interval));
}

/**
  * @brief  Enable auto blink functionality
  * @param  Interval : Time between blink steps
  * @retval None
  */
void SSD1306_StartBlinking(uint8_t Interval)
{
  SSD1306_Command(SSD1306_FADE_OUT);
	SSD1306_Command(0x30 | (0x0F & Interval));
}

/**
  * @brief  Disable auto fade out/blinking functionality
  * @param  Interval : Time between fade steps
  * @retval None
  */
void SSD1306_StopFadeOutOrBlinking(void)
{
  SSD1306_Command(SSD1306_FADE_OUT);
  SSD1306_Command(0x00);
}

/**
  * @brief  Enable or disable zoom functionality
  * @param  Zoom : Set to 1 to enable zoom
  * @retval None
  */
void SSD1306_ZoomIn(uint8_t Zoom)
{
  SSD1306_Command(SSD1306_ZOOM_IN);
  SSD1306_Command(Zoom ? 0x01 : 0x00);
}
#endif

/**
  * @brief  Send the SSD1306 initialization set of commands
  * @param  None
  * @retval None
  */
void SSD1306_Init(void)
{
  /* Display Off */
  SSD1306_Command(SSD1306_DISPLAYOFF);
  SSD1306_Command(0x00);

  SSD1306_Command(SSD1306_SETHIGHCOLUMN);
  SSD1306_Command(0x40);
  /* Set addressing mode */
  SSD1306_Command(SSD1306_MEMORYMODE);
  /* Horizontal Addressing Mode */
  SSD1306_Command(0x00);

  SSD1306_SetContrast(0xFF);
  SSD1306_RotateDisplay(1);

  /* Set Normal Display */
  SSD1306_Command(SSD1306_NORMALDISPLAY);
  /* Select Multiplex Ratio */
  SSD1306_Command(SSD1306_SETMULTIPLEX);
  /* Default => 0x3F (1/64 Duty)	0x1F(1/32 Duty) */
  SSD1306_Command(0x3F);
  /* Setting Display Offset */
  SSD1306_Command(SSD1306_SETDISPLAYOFFSET);
  SSD1306_Command(0x00);  /* 00H Reset */
  /* Set Display Clock */
  SSD1306_Command(SSD1306_SETDISPLAYCLOCKDIV);
  SSD1306_Command(0x80);  /* 105HZ */
  /* Set Pre-Charge period */
  SSD1306_Command(SSD1306_SETPRECHARGE);
  SSD1306_Command(0x22);

  /* Set COM Hardware Configuration */
  SSD1306_Command(SSD1306_SETCOMPINS);
  SSD1306_Command(0x12);
              /* Alternative COM Pin---See IC Spec page 34       */
              /* (0x02)=> A4=0;Sequential COM pin configuration; */
              /*          A5=0;Disable COM Left/Right remap      */
  /* Set Deselect Vcomh level */
  SSD1306_Command(SSD1306_SETVCOMDETECT);
  SSD1306_Command(0x40);
  /* Set Charge Pump */
  SSD1306_Command(SSD1306_CHARGEPUMP);
  /* SSD1306_Command(0x10); */  /* Disable Charge Pump */
  SSD1306_Command(0x14);  /* Endable Charge Pump */
  /* Entire Display ON */
  SSD1306_Command(SSD1306_DISPLAYALLON_RESUME);

#if GRAPHIC_ACCELERATION_COMMANDS == 1
  SSD1306_StopScroll();
#endif

#if ADVANCED_GRAPHIC_COMMANDS == 1
  SSD1306_StopFadeOutOrBlinking();
#endif

  SSD1306_DisplayON(1);
}

#ifdef SSD1306_I2C_CONTROL
/**
  * @brief  Initialize SSD1306 through I2C, extra code container
  * @param  None
  * @retval None
  */
void SSD1306_I2cInit(void)
{
#if defined(SSD1306_RESET_USE)
  /* For reconfigured SPI module as I2C */
  SSD1306_Reset();
#endif
  SSD1306_Init();
}
#endif

#ifdef SSD1306_SPI_CONTROL
/**
  * @brief  Initialize SSD1306 through SPI, extra code container
  * @param  None
  * @retval None
  */
void SSD1306_SpiInit(void)
{
#if defined(SSD1306_RESET_USE)
  SSD1306_Reset();
#endif
  SSD1306_Init();
}
#endif

/**
  * @brief  Put single pixel in frame buffer
  * @param  x, y : Pixel position
  *         Color: Pixel color
  * @retval None
  */
void SSD1306_DrawPixel(int16_t x, int16_t y, uint8_t Color)
{
  if((x < 0) || (x >= SSD1306_LCDWIDTH) || (y < 0) || (y >= SSD1306_LCDHEIGHT))
    return;

  switch(Color)
  {
    case WHITE:
      buffer[x + (y / 8) * SSD1306_LCDWIDTH] |=  (1 << (y & 7));
      break;
    case BLACK:
      buffer[x + (y / 8) * SSD1306_LCDWIDTH] &= ~(1 << (y & 7));
      break;
    case INVERSE:
      buffer[x + (y / 8) * SSD1306_LCDWIDTH] ^=  (1 << (y & 7));
      break;
    default:
      break;
  }
}

/**
  * @brief  Clear the frame buffer
  * @param  Color : Fill color
  * @retval None
  */
void SSD1306_Clear(uint8_t Color)
{
  switch(Color)
  {
    case WHITE:
      memset(buffer, 0xFF, (SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8));
      break;
    case BLACK:
      memset(buffer, 0x00, (SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8));
      break;
    default:
      break;
  }
}

/**
  * @brief  Send buffer to GCRAM
  * @param  None
  * @retval None
  */
void SSD1306_Display(void)
{
  SSD1306_Command(SSD1306_PAGEADDR);
  SSD1306_Command(0x00);
  SSD1306_Command(0x07);

#ifdef SSD1306_I2C_CONTROL
  I2C_CWrite(SSD1306_I2C_ADDRESS, 0x40, (uint8_t*)&buffer,
            (SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8));
#endif

#ifdef SSD1306_SPI_CONTROL
  GPIO_WriteHigh(SSD1306_DC_Port, SSD1306_DC_Pin);
  #ifndef SPI_CS_HARDWARE_CONTROL
    GPIO_WriteLow(SSD1306_CS_Port, SSD1306_CS_Pin);
  #endif
  SPI_Transmit((uint8_t*)&buffer, (SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8));
  #ifndef SPI_CS_HARDWARE_CONTROL
    GPIO_WriteHigh(SSD1306_CS_Port, SSD1306_CS_Pin);
  #endif
#endif
}

/**
  * @brief  Display bitmap directly on screen
  * @param  bitmap : Pointer to the bitmap data
  * @retval None
  */
void SSD1306_Bitmap(uint8_t *bitmap)
{
  SSD1306_Command(SSD1306_PAGEADDR);
  SSD1306_Command(0x00);
  SSD1306_Command(0x07);

#ifdef SSD1306_I2C_CONTROL
  I2C_CWrite(SSD1306_I2C_ADDRESS, 0x40, bitmap,
            (SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8));
#endif

#ifdef SSD1306_SPI_CONTROL
  GPIO_WriteHigh(SSD1306_DC_Port, SSD1306_DC_Pin);
  #ifndef SPI_CS_HARDWARE_CONTROL
    GPIO_WriteLow(SSD1306_CS_Port, SSD1306_CS_Pin);
  #endif
  SPI_Transmit(bitmap, (SSD1306_LCDHEIGHT * SSD1306_LCDWIDTH / 8));
  #ifndef SPI_CS_HARDWARE_CONTROL
    GPIO_WriteHigh(SSD1306_CS_Port, SSD1306_CS_Pin);
  #endif
#endif
}

  • main函数
int main( void )
{
    /* STM8 configuration */
    MCU_Config();

    /* SSD1306 configuration */
#ifdef SSD1306_I2C_CONTROL
    SSD1306_I2cInit();
#endif
#ifdef SSD1306_SPI_CONTROL
    SSD1306_SpiInit();
#endif

    /* Benchmark variables */
    uint8_t i;
    uint16_t frames       = 0;
    uint16_t fps          = 0;
    uint16_t loops        = 0;
    uint16_t loops_overal = 0;

    char     fps_c[15];

    /* SSD1306 demo code */
    SSD1306_ZoomIn( 0 ); 
    SSD1306_Bitmap( ( uint8_t* )picture );

    Delay_ms( 2000 );
    SSD1306_Clear( BLACK );
    
    GFX_SetFont( font_7x7 );//字体:font_6x4 font_7x5 font_7x7 font_8x5
    GFX_SetFontSize( 1 );//0代表1/2  1代表1:1


    for( i = 0; i < 16; i++ )
    {
//    SSD1306_StartScrollLeftUp( 0, 7, SCROLL_EVERY_2_FRAMES, 0 );//左上滚动
        SSD1306_StartScrollLeft( 0, 7, SCROLL_EVERY_2_FRAMES );//整屏内容水平滚动
//   SSD1306_StartBlinking( 1 );//屏幕亮度调节
        SSD1306_ZoomIn( 0 ); //屏幕像数整体,比例缩放,0/1
        Delay_ms( 300 );
    }
    i=0;
    SSD1306_Command( 0x2E );      //关闭滚动,需要添加此指令,否则影响运行后面的显示
    SSD1306_ZoomIn( 1 ); //恢复原比例,否则对后面的显示内容有影响
    SSD1306_Clear( BLACK ); //BLACK WHITE INVERSE

    /* Infinite loop */
    for( ;; )
    {

        if( !timer_1s )
        {
            timer_1s     = 1000;
            fps          = frames;
            frames       = 0;
            loops_overal = loops;
            loops        = 0;
        }

        sprintf( fps_c, "LOOP: %04d %03d", loops_overal, i);
        SSD1306_Clear( BLACK );
        GFX_DrawString( 0, 0, fps_c, WHITE, BLACK );
        sprintf( fps_c, " FPS: %02d", fps );
        GFX_DrawString( 0, 8, fps_c, WHITE, BLACK );
        GFX_DrawString( 0, 16, "Perseverance51", WHITE, BLACK );
        GFX_DrawString( 0, 24, "Hello World", WHITE, BLACK );
        frames++;

        GPIO_SetBits( TEST_Port, TEST_Pin );
        SSD1306_Display();
        GPIO_ResetBits( TEST_Port, TEST_Pin );
        loops++;
        i++;
    }
}

项目工程

链接:https://pan.baidu.com/s/1rdkEzw3J8CzW-Ap2cCKzMQ 
提取码:rbv2

你可能感兴趣的:(STM8,#,STM8L系列,STM8L,ssd1306,I2C)