永发信息网

stm32f103怎么用c语言初始化spi1

答案:1  悬赏:0  手机版
解决时间 2021-02-20 17:33
stm32f103怎么用c语言初始化spi1
最佳答案
#include "stm32f10x_conf.h"

uint8_t SPI1_Rx_Buf[4096];
uint8_t SPI1_Tx_Buf[4096];

void SPI1_Config(void){
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, DISABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLE);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SPI1,ENABLE);
SPI_Cmd(SPI2, DISABLE);
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_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);

DMA_DeInit(DMA1_Channel2);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&SPI1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI1_Rx_Buf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 0;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);

DMA_Cmd(DMA1_Channel2, DISABLE);


DMA_DeInit(DMA1_Channel3);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&SPI1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI1_Tx_Buf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 0;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel3, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel3, DMA_IT_TC, ENABLE);

DMA_Cmd(DMA1_Channel3, DISABLE);

NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel3_IRQn;
NVIC_Init(&NVIC_InitStructure);

SPI_Cmd(SPI1, ENABLE);
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
交警扣车后货主有权提取车上的货物吗?
【吃的英语】吃的英语单词怎么说
口袋妖怪漆黑的魅影卡比兽哪里抓
大旋里在什么地方啊,我要过去处理事情
杂交稻米怎做饭好吃
飞利浦云智能电视软件太卡用不了怎样办
我是交的自由职业社保准备迁户口,已满三个月
一个从传销里出来的女孩子会倒回去吗???
女生右脸有痣
上岛咖啡武清店这个地址在什么地方,我要处理
华景新城会所在什么地方啊,我要过去处理事情
裸车在10万元左右的自动档三厢车
雅思 申请悉尼大学商科语言
【孙悟空七十二变歇后语】孙悟空七十二变的歇
联想B545升级windows10最新版时显卡驱动打不
推荐资讯
项目的投资回收期的小数部分是()。A.1B.0C.0.
交通银行浙江省分行停车场(出口)地址在什么地
樱花农庄地址在什么地方,想过去办事
上官塘地址有知道的么?有点事想过去
飞机国航延误给钱吗?
路虎绿标与黑标的音响一样吗
现在辞职报告打过了,领导也批了,想反悔该怎
【硅谷位于美国的哪个州】硅谷位于美国哪个州
山西离山东有多远
whese是特殊疑问词吗
天赋人权是启蒙思想家们所追求的一种权利,是
365+1牛仔旗舰店地址有知道的么?有点事想过
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?