Several issues that need to be paid attention to when using SPI communication with STM8

Publisher:tanjunhuiLatest update time:2019-05-07 Source: eefocusKeywords:STM8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Overview:

     When using the STM8L101F3 microcontroller and SPI communication to read the data of the three-axis sensor ADXL362, a series of problems occurred. I will make a note here and hope it can give you a small reference.


Question one:

      During the SPI initialization process, the three pins MISO, MOSI, and SCLK were not initialized, which caused problems in writing and reading data during the communication process. So here is the SPI initialization code that can run normally after testing. (STM8L101F3, using the official standard library)


#include "stm8l10x.h"

#include "stm8l10x_spi.h"

#include "stm8l10x_gpio.h"

 

/***************************SPI related definitions**************************/

#define SPI_GPIO_PORT GPIOB

#define SPI_CS_PIN GPIO_Pin_4

#define SPI_SCLK_PIN GPIO_Pin_5

#define SPI_MOSI_PIN GPIO_Pin_6

#define SPI_MISO_PIN GPIO_Pin_7

 

/*The following settings are in accordance with the corresponding configuration of my three-axis sensor*/

#define SPI_FIRSTBIT_TYPE SPI_FirstBit_MSB

#define SPI_SPEED_PRESC SPI_BaudRatePrescaler_4

#define SPI_MODE SPI_Mode_Master

#define SPI_CPOL SPI_CPOL_Low

#define SPI_CPHA SPI_CPHA_1Edge

#define SPI_DATA_MODE SPI_Direction_2Lines_FullDuplex

#define SPI_CS_CTRL SPI_NSS_Soft

 

 

/*SPI initialization*/

void spi_init(void)

{

  //Start the SPI peripheral clock

  CLK_PeripheralClockConfig(CLK_Peripheral_SPI, ENABLE);

  //SPI reset

  SPI_DeInit();

  //SPI related GPIO initialization

  GPIO_Init(SPI_GPIO_PORT, SPI_CS_PIN, GPIO_Mode_Out_PP_High_Fast);

  GPIO_Init(SPI_GPIO_PORT, SPI_SCLK_PIN, GPIO_Mode_Out_PP_High_Fast);

  GPIO_Init(SPI_GPIO_PORT, SPI_MOSI_PIN, GPIO_Mode_Out_PP_High_Fast);

  //This setting is critical. As the master device, it must be set as input

  GPIO_Init(SPI_GPIO_PORT, SPI_MISO_PIN, GPIO_Mode_In_PU_No_IT);

  //SPI initialization

  SPI_Init(SPI_FIRSTBIT_TYPE, SPI_SPEED_PRESC, SPI_MODE, SPI_CPOL, SPI_CPHA,           

  SPI_DATA_MODE, SPI_CS_CTRL);

  //SPI start

  SPI_Cmd(ENABLE);

}

Question 2:

     Regarding the data writing and data reading issues, during my use, the official library provides two functions, one for writing data and the other for receiving data. Because in my application, I need to write two bytes of command and address data first, and then read the subsequent data. Therefore, in use, I first use the SPI_SendData() function to write data without reading the corresponding received data. (In fact, to read data through SPI communication, you must write it first before you can read it, and only when you write to the slave device will there be a clock.) Then I started to read the data, and found that the data I read was always wrong. Later, through instrument analysis, I found that the data I needed was in the response data, but the first data I read was actually the data of the first byte response when I wrote it. That is to say, when writing data, if we do not read the corresponding data, and then read the data after writing the data, it is actually the data returned when the first byte is written, and the subsequent data will not be overwritten and updated. Here is the modified code.


/*This part is the code for writing and reading data. Remember to turn on and off the chip select signal before and after reading and writing data*/

 

void spi_write(uint8_t data)

{

    while(SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET);

    SPI_SendData(data);

    //It must not be omitted. If the corresponding data is not received, reading the data immediately will cause incorrect data to be read.

    while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET);

    u8 dd = SPI_ReceiveData();

}

 

uint8_t spi_read()

{

    //Select an invalid data to send (customized, just to provide a clock to the slave device), and then read the corresponding data

    uint8_t data = 0xff;

    while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET);

    SPI_SendData(data);

    while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET);

    uint8_t rxdata = SPI_ReceiveData();

    return rxdata;

}

 

//Write data and read required data

void spi_write2read(uint8_t *wdata, uint8_t wlen, uint8_t *rdata, uint8_t rlen)

{

    uint8_t i;

    if (wdata == NULL || rdata == NULL) {

        return;

    }

    //Write data

    for (i = 0; i < wlen; i++) {

        spi_write(wdata[i]);

    }

    //Read data

    for (i = 0; i < rlen; i++) {

        rdata[i] = spi_read();

    }

}

     Summarize:

              I have a better understanding of using SPI on microcontrollers, haha.


Keywords:STM8 Reference address:Several issues that need to be paid attention to when using SPI communication with STM8

Previous article:ADC sampling frequency calculation and clock frequency selection
Next article:The difference between FLASH and EEPROM

Recommended ReadingLatest update time:2024-11-25 04:13

stm8 note 3-ad continuous sampling
1. Main functions 1, 10-bit resolution 2. Single and continuous conversion modes. In single conversion mode, the conversion stops after one conversion; in continuous conversion mode, once started, the conversion continues. 3. Pre-scaling can be set programmably. The adc clock can be provided by the system main clock a
[Microcontroller]
stm8 note 3-ad continuous sampling
STM8 study notes---Implementation of serial port printf function
When using a microcontroller, the serial port is a frequently used function, especially in the process of debugging the code, it is often necessary to use the serial port to print out certain variable values ​​to determine whether the program execution process is normal. However, the microcontroller does not have a pr
[Microcontroller]
STM8 study notes---Implementation of serial port printf function
STM8 4-wire driver 1602 garbled code
Recently, I used STM8 to drive 1602 in a 4-wire mode. Garbled characters appeared when I powered on for the first time. Now I have found a solution online and made this summary. According to an analysis by an online expert, the reason for the garbled code is that when the microcontroller sends 0x28 for initializatio
[Microcontroller]
IAR environment STM8 project quickly changes the project name
If you obtain a reference code project package in the IAR environment on the Internet and want to use it for your own project, you often need to modify the project name. So how do you modify it in IAR? In this example, the original project name is text, and now you want to change it to UF_Lgt. The steps are as follow
[Microcontroller]
IAR environment STM8 project quickly changes the project name
STM8 8-bit basic timer (TIM4, TIM6)
introduce The timer consists of an 8-bit auto-reloadable up-counter, which can be used as a time-base generator with overflow interrupt function. The TIM6 clock signal controller is used for timer synchronization and cascading. The main functions of STM8 general-purpose timer TIM4 TIM4 features include: 8-bit up
[Microcontroller]
STM8 8-bit basic timer (TIM4, TIM6)
Based on the use of STM8 microcontroller I2C method to realize read and write operations
STM8 hardware I2C knowledge The I2C module of STM8S can not only receive and send data, but also convert data from serial to parallel data when receiving and from parallel to serial data when sending. Interrupts can be enabled or disabled. The interface is connected to the I2C bus through the data pin (SDA) and the cl
[Microcontroller]
Based on the use of STM8 microcontroller I2C method to realize read and write operations
STM8 IO output LED display program
System functions Use STM8 to control 8-bit LED and demonstrate STM8 microcontroller running light. hardware design For details about the I/O structure and related introduction of STM8, please refer to the Datasheet. Here we only briefly introduce some of them. The following is the I/O pin configuration table of
[Microcontroller]
STM8 IO output LED display program
STM8S study notes 3 (STM8 SysClk)
      The STM8S system clock setting is very important for the microcontroller. Different clocks must be used for different purposes. For example, when making AVR, the clock used for high-stability serial communication is generally 3.6864M, mainly because this baud rate is accurate. STM8S is also important.       STM8
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号