stm32spi1初始化程序
时间: 2023-11-24 13:42:44 浏览: 123
STM32F103_初始化程序_STM32F103_
5星 · 资源好评率100%
stm32spi1的初始化程序可以参考以下步骤:
1. 在头文件中定义SPI引脚和参数:
```
#ifndef _BSP_SPI_1101_H_
#define _BSP_SPI_1101_H_
#include <stdio.h>
#include "stm32f10x.h"
#include "misc.h"
#include "os_cfg_app.h"
#include "os.h"
#include "core_cm3.h"
#include "bsp.h"
#include "stm32f10x_spi.h"
#define CSN_CC1101_GPIO GPIOB
#define CSN_CC1101_GPIO_PIN GPIO_Pin_12
#define SPI_CC1101_GPIO GPIOB
#define SCK_CC1101_GPIO_PIN GPIO_Pin_13
#define MISO_CC1101_GPIO_PIN GPIO_Pin_14
#define MOSI_CC1101_GPIO_PIN GPIO_Pin_15
#define GDO0_CC1101_GPIO GPIOA
#define GDO0_CC1101_GPIO_PIN GPIO_Pin_11
#define SPI_GPIO_RCC_1 RCC_APB2Periph_GPIOB
#define SPI_GPIO_RCC_2 RCC_APB2Periph_GPIOA
#define CC1101_SPI SPI2
#define CSN_SET GPIO_SetBits(GPIOB,GPIO_Pin_12)
#define CSN_RESET GPIO_ResetBits(GPIOB,GPIO_Pin_12)
void SPI_CC1101_Init(void);
u8 SPI2_ReadWriteByte(u8 TxData);
#endif
```
2. 实现初始化函数SPI_CC1101_Init(),在该函数中进行SPI的初始化:
```
void SPI_CC1101_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(SPI_GPIO_RCC_1 | SPI_GPIO_RCC_2, ENABLE);
GPIO_InitStructure.GPIO_Pin = SCK_CC1101_GPIO_PIN | MOSI_CC1101_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_CC1101_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MISO_CC1101_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(SPI_CC1101_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CSN_CC1101_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(CSN_CC1101_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GDO0_CC1101_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GDO0_CC1101_GPIO, &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_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(CC1101_SPI, &SPI_InitStructure);
SPI_Cmd(CC1101_SPI, ENABLE);
}
```
3. 实现SPI2_ReadWriteByte()函数,用于SPI总线读写一个字节:
```
u8 SPI2_ReadWriteByte(u8 TxData) {
while (SPI_I2S_GetFlagStatus(CC1101_SPI, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(CC1101_SPI, TxData);
while (SPI_I2S_GetFlagStatus(CC1101_SPI, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(CC1101_SPI);
}
```
阅读全文