【ACM32G103RCT6】spi驱动SH1106OLED
[复制链接]
最新入手一个1.3寸sh1106的OLED屏,spi接口,这样显示速度相比IIC就会高很多。
【移植】
1、拷贝官方的驱动例程到工程,并把他添加进工程中。
2、选择驱动IO,定义端口与PIN如下:
#define OLED_PORT GPIOA
#define OLED_SCL_PIN GPIO_PIN_5
#define OLED_SDA_PIN GPIO_PIN_7
#define OLED_RES_PIN GPIO_PIN_2
#define OLED_DC_PIN GPIO_PIN_3
#define OLED_CS_PIN GPIO_PIN_4
3、修改SLC与SDA、RES、DC、CS的高平电平:
#define OLED_SCL_Clr() OLED_PORT->BSC = OLED_SCL_PIN << 16U //SCL
#define OLED_SCL_Set() OLED_PORT->BSC = OLED_SCL_PIN
#define OLED_SDA_Clr() OLED_PORT->BSC = OLED_SDA_PIN << 16U //SDA
#define OLED_SDA_Set() OLED_PORT->BSC = OLED_SDA_PIN
#define OLED_RES_Clr() OLED_PORT->BSC = OLED_RES_PIN << 16U //RES
#define OLED_RES_Set() OLED_PORT->BSC = OLED_RES_PIN
#define OLED_DC_Clr() OLED_PORT->BSC = OLED_DC_PIN << 16U //DC
#define OLED_DC_Set() OLED_PORT->BSC = OLED_DC_PIN
#define OLED_CS_Clr() OLED_PORT->BSC = OLED_CS_PIN << 16U //CS
#define OLED_CS_Set() OLED_PORT->BSC = OLED_CS_PIN
4、修改OLED—INIT如下:
//OLED的初始化
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = OLED_SCL_PIN | OLED_SDA_PIN | OLED_RES_PIN | OLED_DC_PIN | OLED_CS_PIN ;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Drive = GPIO_DRIVE_LEVEL3;
GPIO_InitStruct.Alternate = GPIO_FUNCTION_0;
HAL_GPIO_Init(OLED_PORT, &GPIO_InitStruct);
OLED_RES_Clr();
HAL_Delay(200);
OLED_RES_Set();
OLED_WR_Byte(0xAE,OLED_CMD); /*display off*/
OLED_WR_Byte(0x02,OLED_CMD); /*set lower column address*/
OLED_WR_Byte(0x10,OLED_CMD); /*set higher column address*/
OLED_WR_Byte(0x40,OLED_CMD); /*set display start line*/
OLED_WR_Byte(0xB0,OLED_CMD); /*set page address*/
OLED_WR_Byte(0x81,OLED_CMD); /*contract control*/
OLED_WR_Byte(0xcf,OLED_CMD); /*128*/
OLED_WR_Byte(0xA1,OLED_CMD); /*set segment remap*/
OLED_WR_Byte(0xA6,OLED_CMD); /*normal / reverse*/
OLED_WR_Byte(0xA8,OLED_CMD); /*multiplex ratio*/
OLED_WR_Byte(0x3F,OLED_CMD); /*duty = 1/64*/
OLED_WR_Byte(0xad,OLED_CMD); /*set charge pump enable*/
OLED_WR_Byte(0x8b,OLED_CMD); /* 0x8B 内供 VCC */
OLED_WR_Byte(0x33,OLED_CMD); /*0X30---0X33 set VPP 9V */
OLED_WR_Byte(0xC8,OLED_CMD); /*Com scan direction*/
OLED_WR_Byte(0xD3,OLED_CMD); /*set display offset*/
OLED_WR_Byte(0x00,OLED_CMD); /* 0x20 */
OLED_WR_Byte(0xD5,OLED_CMD); /*set osc division*/
OLED_WR_Byte(0x80,OLED_CMD);
OLED_WR_Byte(0xD9,OLED_CMD); /*set pre-charge period*/
OLED_WR_Byte(0x1f,OLED_CMD); /*0x22*/
OLED_WR_Byte(0xDA,OLED_CMD); /*set COM pins*/
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xdb,OLED_CMD); /*set vcomh*/
OLED_WR_Byte(0x40,OLED_CMD);
OLED_Clear();
OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
}
到此移植驱动结束。
编写测试程度:
/******************************************************************************
*[url=home.php?mod=space&uid=1307177]@File[/url] : main.c
*[url=home.php?mod=space&uid=159083]@brief[/url] : main program
******************************************************************************/
#include "main.h"
#include "app.h"
#include "oled.h"
#include "bmp.h"
/******************************************************************************
*@brief : main program
*@param : none
*@return: none
******************************************************************************/
int main(void)
{
uint8_t t=' ';
HAL_Init();
SystemClock_Config();
BSP_UART_Init();
printfS("\r\n\r\n");
printfS("************************************************************\r\n\r\n");
printfS("system startup\r\n");
get_reset_source();
BSP_MCO_Init();
printfS("HCK: %u\r\n", HAL_RCC_GetHCLKFreq());
printfS("PCLK1: %u\r\n", HAL_RCC_GetPCLK1Freq());
printfS("PCLK2: %u\r\n", HAL_RCC_GetPCLK2Freq());
printfS("\r\n");
printfS("************************************************************\r\n\r\n");
OLED_Init();
OLED_ColorTurn(0);//0正常显示,1 反色显示
OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
while(1)
{
OLED_ShowPicture(0,0,128,64,BMP1,1);
OLED_Refresh();
HAL_Delay(500);
OLED_Clear();
OLED_ShowChinese(0,0,0,16,1);//中
OLED_ShowChinese(18,0,1,16,1);//景
OLED_ShowChinese(36,0,2,16,1);//园
OLED_ShowChinese(54,0,3,16,1);//电
OLED_ShowChinese(72,0,4,16,1);//子
OLED_ShowChinese(90,0,5,16,1);//技
OLED_ShowChinese(108,0,6,16,1);//术
OLED_ShowString(8,16,"ZHONGJINGYUAN",16,1);
OLED_ShowString(20,32,"2014/05/01",16,1);
OLED_ShowString(0,48,"ASCII:",16,1);
OLED_ShowString(63,48,"CODE:",16,1);
OLED_ShowChar(48,48,t,16,1);//显示ASCII字符
t++;
if(t>'~')t=' ';
OLED_ShowNum(103,48,t,3,16,1);
OLED_Refresh();
HAL_Delay(500);
OLED_Clear();
OLED_ShowChinese(0,0,0,16,1); //16*16 中
OLED_ShowChinese(16,0,0,24,1); //24*24 中
OLED_ShowChinese(24,20,0,32,1);//32*32 中
OLED_ShowChinese(64,0,0,64,1); //64*64 中
OLED_Refresh();
HAL_Delay(500);
OLED_Clear();
OLED_ShowString(0,0,"ABC",8,1);//6*8 “ABC”
OLED_ShowString(0,8,"ABC",12,1);//6*12 “ABC”
OLED_ShowString(0,20,"ABC",16,1);//8*16 “ABC”
OLED_ShowString(0,36,"ABC",24,1);//12*24 “ABC”
OLED_Refresh();
HAL_Delay(500);
OLED_ScrollDisplay(11,4,1);
}
}
附视频如下:
|