STM32驱动OLED屏完成汉字滚动显示

STM32驱动OLED屏完成汉字串滚动显示

硬件平台

  • STM32开发板
  • 0.96寸OLED屏(SPI接口)

功能实现

  在OLED屏上实现字符串滚动显示。
  0.96寸OLED分辨率为128*64(128列,64行)。我们可以定义二维数组缓冲区buff[64][128],先将要显示的数据放在缓冲驱动中,然后按固定频率将数据更新到屏幕上。

示例效果

STM32驱动OLED屏滚动显示


参考示例:https://download.csdn.net/download/weixin_44453694/19484176

软件设计

  显示一列数据函数

/*
函数功能: 显示一列
函数参数:
        u8 x  - 横坐标位置
        u8 y  - 纵坐标位置
        u8 size -字体大小
        u8 *p - 源数据
说    明:保证纵向为8的倍数
取模方式:纵向取模,以8个点为单位进行取模,高位在前
*/
void OLED_DrawRecData2(u8 x,u8 y,u8 size,const u8 *p)
{
     
    u8 y0=y; //保存y坐标
    u8 i,j;
    u8 data=0;
    for(i=0;i<size/8;i++)
    {
     
      data=*(p+i);
      //显示上半部分
      for(j=0;j<8;j++)
      {
     
         if(data&0x80)OLED_DrawPoint(x,y0,1); //点亮OLED指定位置灯
         else OLED_DrawPoint(x,y0,0); //关闭
         data<<=1; //继续判断高位
         y0++; //纵坐标自增
      }
    }
}

  滚动显示函数(左移显示)

/******************************字符串滚动显示****************************
**左移滚动显示
**  u8 x,u8 y  --- 显示的起始位置
**  u8 size    --- 字体大小
**  u8 *p      --- 需要滚动显示的数据
**  u8 number  --- 要显示的汉字个数
*******************************************************************/
void OLED_Left_Move_Display(u8 x,u8 y,u8 size,u8 *p,u8 number)
{
     
  int x0=x;//显示的起始位置
  int i=0;
  u8 *data=p;
  u16 cnt=0,offset=0;
  while(1)
  {
     
    for(cnt=0,i=x0;i<128;i++)
    {
     
      OLED_DrawRecData2(i,y,size,data+cnt);//显示一列
      cnt+=size/8;//一列的偏移量
    }
    OLED_RefreshGRAM(); //更新一次显示
    x0--;//左移显示
    if(x0<=0)//完整显示完一行数据
    {
     
       x0=0;
       data+=size/8;//数据的起始位置往后移动
    }  
    offset++;
    if(offset>=number*size+128)//保存显示的数据完整滚动完一遍
     {
     
        offset=0; //归位
        x0=x;
        data=p;//数据地址归位
     } 
  }   
}

  主函数

/*测试数据:  纵向取模,以8个点为单位进行取模,高位在前*/
 u8 font_test2[28][32]=
{
     
{
     0x20,0x08,0x24,0x10,0x22,0x60,0x21,0x80,0x26,0x41,0x39,0x32,0x02,0x04,0x0C,0x18,0xF0,0x60,0x13,0x80,0x10,0x60,0x10,0x18,0x14,0x04,0x18,0x02,0x00,0x01,0x00,0x00},/*"欢",0*/

{
     0x02,0x00,0x02,0x02,0x42,0x04,0x33,0xF8,0x00,0x04,0x00,0x02,0x3F,0xF2,0x20,0x22,0x40,0x42,0x00,0x02,0x3F,0xFE,0x20,0x42,0x20,0x22,0x3F,0xC2,0x00,0x02,0x00,0x00},/*"迎",1*/

{
     0x00,0x84,0x10,0x84,0x10,0x88,0x14,0x88,0x13,0x90,0x10,0xA0,0x10,0xC0,0xFF,0xFF,0x10,0xC0,0x10,0xA0,0x11,0x90,0x16,0x88,0x10,0x88,0x10,0x84,0x00,0x84,0x00,0x00},/*"来",2*/

{
     0x20,0x01,0x20,0x02,0x20,0x04,0x20,0x18,0x20,0x60,0x3F,0x80,0x22,0x00,0x22,0x00,0x22,0x02,0x22,0x01,0x22,0x02,0x23,0xFC,0x20,0x00,0x20,0x00,0x20,0x00,0x00,0x00},/*"万",3*/

{
     0x10,0x41,0x12,0x42,0x12,0x44,0x12,0x58,0xFF,0xE0,0x12,0x40,0x12,0x40,0x10,0x40,0x00,0x00,0x7F,0xFF,0x40,0x10,0x44,0x08,0x5B,0x10,0x60,0xE0,0x00,0x00,0x00,0x00},/*"邦",4*/

{
     0x00,0x00,0x00,0x10,0x00,0x20,0x00,0x42,0xFE,0xC4,0x93,0x48,0x92,0x71,0x92,0x42,0x92,0x4C,0x92,0x70,0x92,0x42,0xFE,0x41,0x00,0x42,0x00,0x7C,0x00,0x00,0x00,0x00},/*"易",5*/

{
     0x01,0x00,0x01,0x00,0x77,0xFE,0x11,0x24,0x11,0x24,0x11,0x24,0x17,0xFE,0xF1,0x00,0x10,0x81,0x11,0x02,0x1E,0x0C,0x12,0xF0,0x72,0x0C,0x02,0x82,0x03,0x01,0x00,0x00},/*"嵌",6*/

{
     0x02,0x20,0x0C,0x20,0x88,0x20,0x69,0x20,0x09,0x20,0x09,0x22,0x89,0x21,0x69,0x7E,0x09,0x60,0x09,0xA0,0x19,0x20,0x28,0x20,0xC8,0x20,0x0A,0x20,0x0C,0x20,0x00,0x00},/*"学",7*/

{
     0x00,0x00,0x40,0x10,0x40,0x18,0x40,0x10,0x48,0x20,0x44,0x20,0x43,0x20,0x40,0x40,0x40,0x40,0x40,0x82,0x40,0x81,0x40,0x02,0x7F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00},/*"习",8*/

{
     0x01,0x00,0x01,0x00,0x77,0xFE,0x11,0x24,0x11,0x24,0x11,0x24,0x17,0xFE,0xF1,0x00,0x10,0x81,0x11,0x02,0x1E,0x0C,0x12,0xF0,0x72,0x0C,0x02,0x82,0x03,0x01,0x00,0x00},/*"嵌",9*/

{
     0x00,0x01,0x00,0x02,0x00,0x04,0x00,0x08,0x00,0x30,0x80,0xC0,0x47,0x00,0x38,0x00,0x07,0x00,0x00,0xC0,0x00,0x30,0x00,0x0C,0x00,0x02,0x00,0x01,0x00,0x01,0x00,0x00},/*"入",10*/

{
     0x08,0x00,0x08,0x04,0x09,0x06,0x09,0x04,0x09,0xFC,0x09,0x08,0x09,0x08,0x08,0x08,0x08,0x00,0xFF,0xC0,0x08,0x30,0x08,0x08,0x88,0x04,0x68,0x02,0x08,0x1F,0x00,0x00},/*"式",11*/

{
     0x01,0x00,0x41,0x01,0x41,0x02,0x41,0x0C,0x7F,0xF0,0x41,0x00,0x41,0x00,0x41,0x00,0x41,0x00,0x41,0x00,0x7F,0xFF,0x41,0x00,0x41,0x00,0x41,0x00,0x01,0x00,0x00,0x00},/*"开",12*/

{
     0x00,0x00,0x00,0x04,0x18,0x08,0x68,0x31,0x08,0xC1,0x0B,0x01,0x1D,0x82,0xE9,0x62,0x09,0x14,0x09,0x08,0x09,0x14,0x49,0x22,0x29,0xC2,0x08,0x01,0x00,0x01,0x00,0x00},/*"发",13*/
};
int main(void)
{
       
  u8 *p;
  BEEP_Init(); //初始化蜂鸣器
  LED_Init();  //初始化LED灯
  KEY_Init();  //初始化按键
  USART1_Init(115200);  //初始化串口1  
  OLED_Init(); 				  //OLED显示屏初始化
	OLED_Clear(0x00);    	//清屏  
  p=font_test2[0]; //要显示的字符串
  while(1)
  {
     
    OLED_Left_Move_Display(127,10,16,p,14);//滚动显示
  }
}

  结论:0.96寸OLED屏硬件自带滚动显示效果,但使用OLED屏硬件方式实现滚动无法灵活显示,采用软件方式实现滚动显示更加灵活便捷。

你可能感兴趣的:(STM32,stm32,单片机,嵌入式)