先踏踏實實的把stm32的外設串口,SPI搞清楚,不要眼高手低,看不起小事。用SPI通信將pixy的數據讀出來,將數據用串口發到串口助手上,然后處理數據,利用STM32的定時器調節pwm,控制電機,先讓小車跑起來,隨后在寫小車的程序,和調節PID參數
1.1閱讀手冊&.C源碼:
使能串口外設時鍾(掛在APB1下)所以調用
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState) 具體可以看一下RCC模塊,和時鍾樹,總線架構。
初始化GPIO:這時要到GPIO模塊來(PA9,PA10),復用功能
如果需要重映射配置AFIO寄存器;以串口一為例:AFIO_MAPR寄存器

初始化串口(usart):閱讀源碼:
typedef struct
{
uint32_t USART_BaudRate;
uint16_t USART_WordLength;
uint16_t USART_StopBits;
uint16_t USART_Parity;
uint16_t USART_Mode;
uint16_t USART_HardwareFlowControl;
} USART_InitTypeDef;
將串口的初始化的狀態封裝成一個結構體,就像GPIO模塊那樣,自己先定義這樣一個結構體類型,賦值初始化,調用此函數
void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
USART_TypeDef* USARTx是一個結構體指針,它指向USART的首地址,結構體的內存字節對齊,和寄存器的地址一一對應
typedef struct
{
__IO uint16_t SR;
uint16_t RESERVED0; //填充用的,因為C語言結構體內存對齊原則。不然就和寄存器一一不對應了。
__IO uint16_t DR; //這里看看USART的寄存器地址映射圖增強理解。
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;
__IO uint16_t GTPR;
uint16_t RESERVED6;
} USART_TypeDef;
開啟串口一接受中斷:
使能串口一:
編寫串口中斷函數:
串口發送數據:USART_SendData(USART_TypeDef* USARTx, uint16_t Data) //
1.2:中斷向量控制器(NVIC):設置中斷優先級:搶占優先級和子優先級;使用中斷前,先要設置優先級分組,配置NVIC。
1.3:SPI外設模塊使用:閱讀源碼&手冊:
使能spi外設(APB1下)先使能外設模塊時鍾(和串口的套路差不多)pin要復用配置好GPIO的模式。初始化 最后使能外設模塊。
SPI1-SPI3 SPI1和SPI3支持重映射,SPI2不支持重映射,默認PB12-PB15 (片選,SCK,SPI2_MISO,SPI2_MOSI)io配置:
51單片機模擬SPI時序操作ds1302(軟件模擬)通過時序圖模擬時序,通過位於,移位讀出(寫入)數據,注意高低位

typedef struct
{
uint16_t SPI_Direction; /*!< Specifies the SPI unidirectional or bidirectional data mode.單向雙向數據模式
This parameter can be a value of @ref SPI_data_direction */
uint16_t SPI_Mode; /*!< Specifies the SPI operating mode. 主從模式
This parameter can be a value of @ref SPI_mode */
uint16_t SPI_DataSize; /*!< Specifies the SPI data size.
This parameter can be a value of @ref SPI_data_size */8或16位
uint16_t SPI_CPOL; /*!< Specifies the serial clock steady state.
This parameter can be a value of @ref SPI_Clock_Polarity */ //時鍾的高低電平
uint16_t SPI_CPHA; /*!< Specifies the clock active edge for the bit capture.
This parameter can be a value of @ref SPI_Clock_Phase *///數據采樣時從第一個還是第二個時 鍾跳變開始
uint16_t SPI_NSS; /*!< Specifies whether the NSS signal is managed by
hardware (NSS pin) or by software using the SSI bit. This parameter can be a value of @ref SPI_Slave_Select_management */ //片選
uint16_t SPI_BaudRatePrescaler;
/*!< Specifies the Baud Rate prescaler value whch will be
used to configure the transmit and receive SCK clock.This parameter can be a value of @ref SPI_BaudRate_Prescaler.@note The communication clock is derived from the masterclock. The slave clock does not need to be set. *///波特率
uint16_t SPI_FirstBit; /*!< Specifies whether data transfers start from MSB or LSB bit.第一位是最高位還是最低位
This parameter can be a value of @ref SPI_MSB_LSB_transmission */
uint16_t SPI_CRCPolynomial; /*!< Specifies the polynomial used for the CRC calculation. */
}SPI_InitTypeDef;
2.1:先明白Pixy的串行協議:SPI通信,在Data Out Port選項中設置:

一個數據16位,7個數據,共14字節的數據。
01 00 9B 00 7B 00 A4 00 23 00 55 AA 3A 01 01 00 EE 00 1F 00 1C 00 10 00 55 AA 55 AA
DC 01 01 00 9B 00 7E 00 A5 00 1D 00 55 AA 39 01 01 00 EE 00 20 00 1D 00 0D 00 55 AA
9A 00 7E 00 A5 00 1F 00 55 AA 36 01 01 00 EE 00 1E 00 1C 00 0D 00 55 AA 55 AA E4 01
A2 00 1F 00 55 AA 37 01 01 00 EE 00 1F 00 1C 00 0D 00 55 AA 55 AA D9 01 01 00 9B 00
這幾串數據浪費了我4天時間,真坑。注意SPI與串口讀出的數據開始標志數據不同,
學會用上位機配置好pixy。使用spi協議讀出數據發給串口:
2.4:處理數據:

接下來用讀到的pixy的數據就做你想做的事了。



代碼分享:
鏈接:https://pan.baidu.com/s/1tZpToGphMbimu6dnAQ2V_Q
提取碼:ms0q
