【NUCLEO H533RE】OLED SSD1306 screen SPI interface test
[Copy link]
1. Test introduction
This test is mainly for the SPI interface peripheral Master Half-Duplex mode test of NUCLEO-H533RE, and verifies the SPI programming process. The test uses a 0.96-inch OLED screen, and the driver type is the SSD1306 chip.
SSD1306 has two interface modes: IIC and SPI. This test uses the SPI mode. NUCLEO-H533RE uses the SPI peripheral Master Half-Duplex mode. The test displays a string.
2. Hardware connection
There are two main hardware
SSD1306 SPI and NUCLEO-H533RE
VCC 3.3 GND
D0 <===>PC1 MOSI
D1 <===>PC1 SCLK
RES <===>PC1 GPIO
DC <===> PA1 GPIO
3. Parameter configuration
The parameter settings are mainly for the system clock and SPI peripheral parameters.
(1) The system is set to an external 24M crystal oscillator, the system clock is 250Mhz, and the PLL2Q clock is 4MHZ
(2) SPI clock setting
Set the SPI clock to PLL2Q, that is, the SPI4 clock is 4MHZ.
(3) SPI parameter mode
SPI mode is Master Half-Duplex mode, PC1 is MOSI, PA0 is SCLK. Data bit is 8bit, rate control is 2MHZ
Here, Prescale is used to control the SPI rate. Too fast a rate may cause SSD1306 to not work.
(4) GPIO settings
Set PB1 and PA1 to output mode.
4. Procedure and working principle
The program mainly consists of SSD1306 driver and font files. For detailed code, please refer to the appendix program
/* USER CODE BEGIN 2 */
ssd1306_Init();
ssd1306_Fill(Black);
ssd1306_UpdateScreen();
/* USER CODE END 2 */
/* Initialize leds */
BSP_LED_Init(LED_GREEN);
/* Initialize USER push-button, will be used to trigger an interrupt each time it's pressed.*/
BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
/* Initialize COM1 port (115200, 8 bits (7-bit data + 1 stop bit), no parity */
BspCOMInit.BaudRate = 115200;
BspCOMInit.WordLength = COM_WORDLENGTH_8B;
BspCOMInit.StopBits = COM_STOPBITS_1;
BspCOMInit.Parity = COM_PARITY_NONE;
BspCOMInit.HwFlowCtl = COM_HWCONTROL_NONE;
if (BSP_COM_Init(COM1, &BspCOMInit) != BSP_ERROR_NONE)
{
Error_Handler();
}
/* USER CODE BEGIN BSP */
/* -- Sample board code to send message over COM1 port ---- */
printf("Welcome to STM32 world !\n\r");
// ssd1306_SetCursor(0, 0);
// ssd1306_WriteString(title, Font_11x18, White);
// ssd1306_UpdateScreen();
/* -- Sample board code to switch on leds ---- */
BSP_LED_On(LED_GREEN);
/* USER CODE END BSP */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* -- Sample board code for User push-button in interrupt mode ---- */
if (BspButtonState == BUTTON_PRESSED)
{
/* Update button state */
BspButtonState = BUTTON_RELEASED;
/* -- Sample board code to toggle leds ---- */
BSP_LED_Toggle(LED_GREEN);
/* ..... Perform your action ..... */
}
ssd1306_SetCursor(0, 0);
ssd1306_WriteString(timerStr, Font_11x18, White);
ssd1306_UpdateScreen();
HAL_Delay(100);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
5. Testing process
Burn the program and run
Appendix: Project Files
OLEDSPI.zip
(7.06 MB, downloads: 0)
|