【STM32H5开发板】使用SPI点亮OLED
<p>开发平台:KEIL</p><p>开发板:STM32H5</p>
<p>目地:本篇是利用STM32H5点亮OLED,显示相关信息hello world</p>
<p>1、原理图:</p>
<p>OLED原理图:</p>
<p></p>
<p>MCU脚位:</p>
<p></p>
<p>2.使用的引脚:</p>
<p>PB3--CS</p>
<p>PB4--DC</p>
<p>PB5--MOSI</p>
<p>PB9--CLK </p>
<p>H5开发板与OLED连接方式</p>
<p></p>
<p>3、SPI连接位置</p>
<p></p>
<p>4、核心代码:</p>
<pre>
<code>引脚定义:
#define OLED_CS_Clr()HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3,GPIO_PIN_RESET);
#define OLED_CS_Set()HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3,GPIO_PIN_SET);
#define OLED_DC_Clr()HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4,GPIO_PIN_RESET);
#define OLED_DC_Set() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4,GPIO_PIN_SET);
#define OLED_RST_Clr()HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5,GPIO_PIN_RESET);
#define OLED_RST_Set()HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5,GPIO_PIN_SET);
#define OLED_MOSI_Clr()HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8,GPIO_PIN_RESET);
#define OLED_MOSI_Set()HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8,GPIO_PIN_SET);
#define OLED_CLK_Clr() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9,GPIO_PIN_RESET);
#define OLED_CLK_Set() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9,GPIO_PIN_SET);
初始化:
__GPIOB_CLK_ENABLE();
GPIO_InitTypeDefGPIO_InitStruct;
GPIO_InitStruct.Mode= GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull= GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_8|GPIO_PIN_9;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/**************???SSD1306*****************/
OLED_WR_Byte(0xAE,OLED_CMD); /*display off*/
OLED_WR_Byte(0x00,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(0xFF,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(0xC8,OLED_CMD); /*Com scan direction*/
OLED_WR_Byte(0xD3,OLED_CMD); /*set display offset*/
OLED_WR_Byte(0x00,OLED_CMD);
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(0XF1,OLED_CMD);
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(0x30,OLED_CMD);
OLED_WR_Byte(0x8D,OLED_CMD); /*set charge pump disable*/
OLED_WR_Byte(0x14,OLED_CMD);
OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 0x7;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
hspi1.Init.ReadyMasterManagement = SPI_RDY_MASTER_MANAGEMENT_INTERNALLY;
hspi1.Init.ReadyPolarity = SPI_RDY_POLARITY_HIGH;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
显示
if(Char_Size == 16)
{
csize = Char_Size/2;
}
else if(Char_Size == 8)
{
csize = Char_Size/2+2;
}
else
{
return;
}
while (chr!='\0')
{
GUI_ShowChar(x,y,chr,Char_Size,mode);
x+=csize;
if(x>120)
{
x=0;
y+=Char_Size/8;
}
j++;
}
GUI_ShowString(0,0,(unsigned char*)"6x8:hello word",8,1);</code></pre>
<p>5.视频:</p>
<p>e774d1310517d4a554afec55391198ce<br />
</p>
<p>用SPI点亮OLED 了</p>
<p> </p>
<p>那一串OLED_WR_Byte好魔幻,器件手册里也没说为啥要这样设置。</p>
火辣西米秀 发表于 2023-6-23 20:46
用SPI点亮OLED 了
<p>是的呢</p>
页:
[1]