STM32如何配置使用SPI通信
来源:互联网 发布:守望先锋 网络质量 编辑:程序博客网 时间:2024/06/06 01:46
SPI是一种高速的,全双工,同步的通信总线,原理和使用简单,占用引脚资源少,是一种常用的通信方式。
STM32通常有2~3个SPI接口
根据STM32手册的使用方法:
1. Enable peripheral clock
2. Enable SCK, MOSI, MISO and NSS GPIOclocks
3. Peripherals alternate function:
Connect the pin to the desired peripherals' AlternateFunction (AF)
Call GPIO_Init() function.
4. Program the Polarity, Phase, First Data, Baud RatePrescaler, Slave Management,Peripheral Mode and CRC Polynomialvalues using the SPI_Init() function in SPI mode.
5. Configure the FIFO threshold usingSPI_RxFIFOThresholdConfig() to select at which hreshold the RXNEevent is generated.
6. Enable the NVIC and the corresponding interrupt using thefunction SPI_I2S_ITConfig() if you need to use interruptmode.
7. When using the DMA mode
Configure the DMA using DMA_Init() function.
Active the needed channel Request using SPI_I2S_DMACmd()function.
8. Enable the SPI using the SPI_Cmd() function .
9. Enable the DMA using the DMA_Cmd() function when using DMAmode.
10. To use the CRC Hardware calculation feature refer to thePeripheral CRC hardware Calculation subsection.
基于STM32F3discovery开发板,STM32F303VC
使用SPI1,从机连接的是L3GD20陀螺仪,引脚连接:
SPI1_MOSI——PA7
SPI1_SCK——PA5
SPI1_MISO——PA6
NSS——PE3
代码:
void SPI_GPIO_Config(void)
{
//将SPI1_MOSI—PA7,SPI1_SCK—PA5,SPI1_MISO—PA6引脚配置为对应复用功能
//NSS—PE3为从机片选引脚,配置为推挽输出
}
void SPI_Config(void)
{
SPI_InitTypeDef SPI_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_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_I2S_DeInit(SPI1);
SPI_Init(SPI1,&SPI_InitStructure);
SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
SPI_Cmd(SPI1,ENABLE);
}
然后就可以用SPI接口进行收发数据,具体收发方式要根据从机设备决定
需要的时候可以配置中断,DMA等功能
发送或接收数据前,先将相应从机的片选信号置零
SPI是两个设备移位寄存器的数据交换,接收数据前先向从机发送虚拟字节,收到的数据就是所要读取的数据
风子
2015,05,20
0 0
- STM32如何配置使用SPI通信
- STM32 学习笔记--SPI通信配置
- STM32之间SPI通信
- STM32 SPI 通信
- STM32 SPI DMA通信
- STM32之SPI通信
- stm32 SPI NSS引脚的使用配置
- STM32的使用之SPI通信DMA模式
- STM32 SPI配置
- stm32配置spi
- s5pv210与stm32 spi通信
- stm32 SPI通信 操作寄存器
- stm32 io模拟spi通信
- STM32 spi使用
- STM32 的SPI总线配置
- STM32 spi与FPGA的通信
- STM32双机SPI中断通信机制
- 两个STM32之间进行SPI通信
- linux主机名称的小问题
- char* 和char []的区别
- 如何彻底卸载和重装ADS1.2//ADS与K…
- STM32输出PWM状态总结
- 为所有li节点增加onclick事件
- STM32如何配置使用SPI通信
- STM32的使用之SPI通信DMA模式
- STM32F3硬件I2C与LSM303DLHC通信
- STM32F3-PWM输入捕获测量频率脉宽…
- STM32高分辨率定时器(HRTIM)使用笔…
- STM32数模转换-DAC_DMA
- STM32F3—CAN通信自发自收
- STM32F3实时时钟RTC-闹钟和唤醒中…
- Keil/MDK中如何生成***.bin格式文…