2503 views|4 replies

2865

Posts

4

Resources
The OP
 

[RISC-V MCU CH32V103 Review] SPI Test [Copy link]

 

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.

This post is from Domestic Chip Exchange

Latest reply

I just found out that this member SPI_BaudRatePrescaler needs to be divided by 2 to be driven. I will study the specific reason later.   Details Published on 2021-2-8 20:21
 
 

1w

Posts

25

Resources
2
 

Is the SPI phase setting correct?

This post is from Domestic Chip Exchange

Comments

It should be set, SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; to trigger on the first edge. The parameters I know are the same as the STM32 program.  Details Published on 2021-2-6 15:17
 
 
 

2865

Posts

4

Resources
3
 
dcexpert posted on 2021-2-6 10:29 Is the SPI phase setting correct?

It should be set, SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; to trigger on the first edge. The parameters I know are the same as the STM32 program.

This post is from Domestic Chip Exchange
 
 
 

58

Posts

0

Resources
4
 

I just found out that this member SPI_BaudRatePrescaler needs to be divided by 2 to be driven. I will study the specific reason later.

This post is from Domestic Chip Exchange

Comments

Thanks, I tried what you said, 2, 4, 8 are all OK. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; It may be because of the clock divider.  Details Published on 2021-2-9 10:15
 
 
 

2865

Posts

4

Resources
5
 
eew_VHNtMb posted on 2021-2-8 20:21 I just got through, this member SPI_BaudRatePrescaler needs 2 frequency division to drive, the specific reason will be studied later,

Thanks, I tried what you said, 2, 4, 8 are all OK.

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;

It may be due to the clock divider.

This post is from Domestic Chip Exchange
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list