linux与stm32 spi通信,STM32 实现SPI简单通信配置

/*******************************************************

说明:

次程序成功实现SPI简单通信配置,能实现SPI1发送

数据,SPI2接收数据简单通信功能,仅供测试使用。

*******************************************************/

#include "stm32f10x_lib.h"

#include "stm32f10x_conf.h"

#include "ili9320.h"

/****************************/

#define  LCD_X_ADDR  190

#define  LCD_Y_ADDR  100

vu16  SPI_IDx  = 0; //Reveive Data

u16   SPI_ID_Temp = 0; //接收数据备份,用于判断接收到的数据是否发生变化

u16   SPI_ID[4];   //位显示数据

/********** 函数声明 *********/

void RCC_Configuration(void);

void NVIC_Configuration(void);

void GPIO_Configuration(void);

void SPI_Configuration(void);

void Sys_Init(void);

void LCD_Print_Out(void);

/********** 系统初始化 ******************/

void Sys_Init(void)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。以下是一个简单的基于STM32SPI通信程序: #include "stm32f4xx.h" void SPI_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; SPI_InitTypeDef SPI_InitStruct; // Enable clock for GPIOB RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // Enable clock for SPI1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); /* Configure SPI1 pins: SCK, MISO and MOSI */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStruct); // Connect SPI1 pins to SPI alternate function GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1); /* Configure SPI1 */ SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStruct.SPI_Mode = SPI_Mode_Master; SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStruct.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStruct); // Enable SPI1 SPI_Cmd(SPI1, ENABLE); } void SPI_SendData(uint8_t data) { // Wait until transmit buffer is empty while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); // Send data SPI_I2S_SendData(SPI1, data); // Wait until receive buffer is not empty while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); // Read received data to clear RXNE flag SPI_I2S_ReceiveData(SPI1); } int main(void) { // Initialize SPI SPI_Init(); // Send data SPI_SendData(0x55); while (1); return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值