[Qinheng RISC-V core CH582] 5-SPI drive OLED
[Copy link]
This post was last edited by manhuami2007 on 2022-3-13 20:20
The SPI provided by CH582 supports mode 0 and mode 3, can be configured as 3-wire or 2-wire mode, supports master and slave modes, and supports DMA. SPI also supports FIFO, which is equivalent to 8 data registers. When sending and receiving small amounts of data, even in polling mode, it can improve CPU efficiency. The official SPI library function is quite convenient to use.
The OLED used this time uses the SPI interface, and the pins are:
- MOSI-PA14
- CLK-PA13
- CS-PA12
- RES-PA3
- DC-PA1
When porting the OLED driver, you only need to modify 3 functions:
- Initialization of peripherals
- Data Bytes Send
- Sending command byte
Initialization of peripherals
void oled_spi_init()
{
GPIOA_SetBits( GPIO_Pin_12 );
GPIOA_ModeCfg( GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14, GPIO_ModeOut_PP_5mA );
SPI0_MasterDefInit();
GPIOA_ModeCfg(GPIO_Pin_1|GPIO_Pin_3,GPIO_ModeOut_PP_5mA);
GPIOA_SetBits( GPIO_Pin_1 );
GPIOA_SetBits( GPIO_Pin_3 );
}
Data and command bytes are sent
void OLED_WR_Byte(uint8_t dat,uint8_t cmd)
{
uint8_t i;
if(cmd)
GPIOA_SetBits(OLED_DC_PIN);
else
GPIOA_ResetBits(OLED_DC_PIN);
GPIOA_ResetBits(OLED_CS_PIN);
SPI0_MasterSendByte( dat );
GPIOA_SetBits(OLED_DC_PIN);
}
void OLED_Write_Multi_Data(uint8_t* data,uint16_t len)
{
uint16_t i;
GPIOA_SetBits(OLED_DC_PIN);
GPIOA_ResetBits(OLED_CS_PIN);
SPI0_MasterTrans( data, len );
GPIOA_SetBits(OLED_CS_PIN);
}
void OLED_Write_Multi_Cmd(uint8_t* data,uint16_t len)
{
uint16_t i;
GPIOA_ResetBits(OLED_DC_PIN);
GPIOA_ResetBits(OLED_CS_PIN);
SPI0_MasterTrans( data, len );
GPIOA_SetBits(OLED_CS_PIN);
GPIOA_SetBits(OLED_DC_PIN);
}
display image
|