I have an OLED screen with SPI interface, 128*64 in size, and the control chip is SSD1306
This screen is tested by me under the STM32 development board. The specific parameters of STM32 are as follows
CPOL = low;CPHA = 1 Edge;Boud = 2.625MB;NSS=softWare;
DataSize = 8bit;CRC = 7;CRC=Disable;FirstBit = SPI_MSB;
OLED Interface
DO = SPI1_SCK --> PA5;SCK
D1 = SPI1_SDIN--> PA7;MISO
DC = Data/Cmd --> PB1;
RES =Reset --> PB0
CS = CS -->PA4;NSS
PA6 is not connected to any pins, the program code is as follows
/*******************************************************************************
* Function Name : SPI_FullDuplex_Init
* Description : Configuring the SPI for full-duplex communication.
* Input : None
* Return : None
*******************************************************************************/
void SPI_FullDuplex_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOA, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init( GPIOA, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOA, &GPIO_InitStructure );
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
//SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure );
SPI_CalculateCRC(SPI1,DISABLE);
SPI_Cmd(SPI1, ENABLE );
}
GPIO pin initialization
void ssd1306_Reset(void) {
// CS = High (not selected)
GPIO_WriteBit(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_SET);
// Reset the OLED
GPIO_WriteBit(SSD1306_Reset_Port, SSD1306_Reset_Pin, GPIO_PIN_RESET);
Delay_Ms(10);
GPIO_WriteBit(SSD1306_Reset_Port, SSD1306_Reset_Pin, GPIO_PIN_SET);
Delay_Ms(10);
}
/*******************************************************************************
* Function Name : GPIO_Toggle_INIT
* Description : Initializes GPIOA.0
* Input : None
* Return : None
*******************************************************************************/
void GPIO_Toggle_INIT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//EXTI_InitTypeDef EXTI_InitStructure;
//NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
// Send a byte to the command register
void ssd1306_WriteCommand(u8 bytedat) {
GPIO_WriteBit(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_RESET); // select OLED
GPIO_WriteBit(SSD1306_DC_Port, SSD1306_DC_Pin, GPIO_PIN_RESET); // command
SPI_I2S_SendData(SSD1306_SPI_PORT,bytedat);
GPIO_WriteBit(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_SET); // un-select OLED
}
void SPI_Transmit(SPI_TypeDef* SPIx,u8* TxData, u16 TxData_size)
{
u16 i = 0;
u8 tmpdat = 0;
for(i=0;i<TxData_size;i++)
{
if( SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE ) != RESET )
{
tmpdat = TxData[i];
SPI_I2S_SendData(SPIx, tmpdat);
}
}
}
// Send data
void ssd1306_WriteData(u8 * TxData, u16 TxData_size) {
GPIO_WriteBit(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_RESET); // select OLED
GPIO_WriteBit(SSD1306_DC_Port, SSD1306_DC_Pin, GPIO_PIN_SET); // data
SPI_Transmit(SSD1306_SPI_PORT, TxData, TxData_size);
GPIO_WriteBit(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_SET); // un-select OLED
// value = memcmp( TxData, RxData, Size );
}
Other programs such as initialization are basically written according to the STM32 program.
But unfortunately, this screen just doesn't work. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
This parameter, tried SPI_BaudRatePrescaler_32, SPI_BaudRatePrescaler_128
I tested it with an oscilloscope and found that both SCK and SDIN have signals, which seems normal.
At present, I have no idea. I wonder if anyone has succeeded. Please give me some suggestions.
|