STM32
超强工具——STM32CubeMX 你会用吗?
集结出发! STM32全国研讨会系列之一:ST智能门铃中国首秀
关于STM32启动文件的几个小问题
【银杏科技ARM+FPGA双核心应用】STM32H7系列35——USB_VCP_FS
【银杏科技ARM+FPGA双核心应用】STM32H7系列28——USB_HID
粉丝分享 | 图说CRC原理应用及STM32硬件CRC外设
STM32L151进入低功耗,并由RTC唤醒的故事
[转]stm32控制NFC模块(PN532)源码(P2P,模拟卡,读写卡等
STM32G070RB+LVGL移植
微信公众号
手机版
回复:【ST MCU实战经验】栏目开通!官方解答应用问题,欢迎下载!
以下是全部代码:
#include"stm32F4xx.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void SPI_Configuration(void);
void Delay(int nCount);
int main(void)
{ RCC_Configuration();
GPIO_Configuration();
SPI_Configuration();
while(1)
{ int data=0;
SPI_SendData(SPI1,0x55);
while(SPI_GetFlagStatus(SPI2,SPI_FLAG_RXNE)==RESET);
data=SPI_ReceiveData(SPI2);
if(data==0x55)
{ while(1)
{ GPIO_SetBits(GPIOA,GPIO_Pin_4);
Delay(0xfffff);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
Delay(0xfffff);
};
}
else while(1)
{ GPIO_SetBits(GPIOA,GPIO_Pin_4);
//Delay(0xfffff);
//GPIO_ResetBits(GPIOA,GPIO_Pin_5);
//Delay(0xfffff);
};
}
}
void RCC_Configuration()
{ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);
}
void GPIO_Configuration()
{ GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void SPI_Configuration()
{ SPI_InitTypeDef SPI_InitStructure;
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);
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_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
SPI_Cmd(SPI2,ENABLE);
}
void Delay(int nCount)
{ int c1=nCount;
int c2=nCount;
for(;c1>0;c1--)
{
for(;c2>0;c2--);
};
}
先谢谢了~~
回复:【ST MCU实战经验】栏目开通!应用问题,官方解答,欢迎下载!
楼主,我想问一下,STM32F4 SPI1和SPI2自通信问题,SPI1为主模式,SPI2为从模式,可是我在设置波特率时,必须按二分频,SPI2才收得到SPI1发来的数据,如果设置为其他分频情况,将卡在while(SPI_GetFlagStatus(SPI2,SPI_FLAG_RXNE)==RESET); 这句上,还有就是SPI1和SPI2的波特率是不是必须为相同的,才能正确通信
以下是全部代码:
#include"stm32F4xx.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void SPI_Configuration(void);
void Delay(int nCount);
int main(void)
{ RCC_Configuration();
GPIO_Configuration();
SPI_Configuration();
while(1)
{ int data=0;
SPI_SendData(SPI1,0x55);
while(SPI_GetFlagStatus(SPI2,SPI_FLAG_RXNE)==RESET);
data=SPI_ReceiveData(SPI2);
if(data==0x55)
{ while(1)
{ GPIO_SetBits(GPIOA,GPIO_Pin_4);
Delay(0xfffff);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
Delay(0xfffff);
};
}
else while(1)
{ GPIO_SetBits(GPIOA,GPIO_Pin_4);
//Delay(0xfffff);
//GPIO_ResetBits(GPIOA,GPIO_Pin_5);
//Delay(0xfffff);
};
}
}
void RCC_Configuration()
{ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);
}
void GPIO_Configuration()
{ GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void SPI_Configuration()
{ SPI_InitTypeDef SPI_InitStructure;
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2);
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_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
SPI_Cmd(SPI2,ENABLE);
}
void Delay(int nCount)
{ int c1=nCount;
int c2=nCount;
for(;c1>0;c1--)
{
for(;c2>0;c2--);
};
}
先谢谢了~~
楼上的问题,看我帖子给出的提示哦~
RE:【ST MCU实战经验】栏目开通!应用问题,官方解答,欢迎下载!
回复:【ST MCU实战经验】栏目开通!应用问题,官方解答,欢迎下载!
回复:【ST MCU实战经验】栏目开通!应用问题,官方解答,欢迎下载!
多谢,真好的板块,学嵌入式的好地方啊,来对了,哈哈。
多谢支持!!
RE:【ST MCU实战经验】栏目开通!应用问题,官方解答,欢迎下载!
RE:【ST工程师 MCU实战经验30篇】之USB接口易损坏
回复:【ST工程师 MCU实战经验30篇】之USB接口易损坏
回复:【ST工程师 MCU实战经验30篇】之USB接口易损坏