Pixy & STM32 Usage Records (Serial Pull & SPI Peripherals)

First, steadfastly figure out the stm32 peripheral serial port and SPI, don’t look down on trivial matters. Use SPI communication to read out the data of pixy, send the data to the serial port assistant through the serial port, then process the data, use the timer of STM32 to adjust pwm, control the motor, first let the car run, and then write the program and adjust the car PID parameters
1.1 Read the manual &. C source code:
Enable the serial peripheral clock (hanging under APB1) so call
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState) Specifically, you can look at the RCC module, clock tree, and bus architecture.
Initialize GPIO: At this time, you need to go to the GPIO module (PA9, PA10) to reuse functions
share picture
If you need to remap, configure the AFIO register ; Take serial port one as an example: AFIO_MAPR register
share picture < p>share picture

Initialize the serial port (usart): Read the source code:

typedef struct
{
uint32_t USART_BaudRate;
uint16_t USART_WordLength;
uint16_t USART_StopBits; uint16_t USART_StopBits;
uint16_t USART_Parity; div>

uint16_t USART_Mode;
uint16_t USART_HardwareFlowControl;
} USART_InitTypeDef;
initialize the serial port The state is encapsulated into a structure, just like the GPIO module, you first define such a structure type, initialize the value, and call this function
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)

USART_TypeDef* USARTx is a structure pointer, it points to the first address of USART, the structure’s Memory byte alignment, one-to-one correspondence with register address
typedef struct
{
__IO uint16_t SR;
uint16_t RESERVED0; / / Filling, because of the C language structure memory alignment principle. Otherwise, it will not correspond to the register one by one.
__IO uint16_t DR; //Look at the register address map of USART to enhance understanding.
uint16_t RESERVED1;
__IO uint16_t BRR;
uint16_t RESERVED2;
__IO uint16_t CR1;
uint16_t RESERVED3;
__IO uint16_t CR2;
uint16_t RESERVED4;
__IO uint16_t CR3;
uint16_t RESERVED5;
< span style="font-size: 18pt"> __IO uint16_t GTPR;
uint16_t RESERVED6;

< div> } USART_TypeDef; USART_TypeDef; //At this time, you will experience the beauty of C language, which is just right.

Open the serial port and accept interruption:
Enable serial port one:
write serial port interrupt function:
Send data from serial port: USART_SendData(USART_TypeDef* USARTx, uint16_t Data) ///
< span style="font-size: 18pt">
1.2:Interrupt Vector Controller (NVIC): Set interrupt priority: preempt priority and sub-priority; before using interrupt, first set priority grouping and configure NVIC.
1.3: outside of SPI Set the module to use: Read the source code & manual:
Enable the spi peripheral (under APB1), first enable the peripheral module clock (and The serial port routines are almost the same) The pin should be reused to configure the GPIO mode. Initialization Finally, the peripheral modules are enabled.
SPI1-SPI3 SPI1 and SPI3 support remapping, SPI2 does not support remapping, the default is PB12-PB15 (chip select, SCK, SPI2_MISO , SPI2_MOSI) io configuration:
share picture

51 The single-chip microcomputer simulates the SPI timing operation ds1302 (software simulation) through the timing diagram to simulate the timing, read (write) the data by positioning, shift, pay attention to the high and low bits

share picture

typedef struct
{
uint16_t SPI_Direction; /*!< Specifies the SPI unidirectional or bidirectional data mode. One-way two-way data mode
This parameter can be a value of @ref SPI_data_direction */ div

uint16_t SPI_Mod e;