21ic问答首页 - MM32F103RET6 SPI1设置问题
MM32F103RET6 SPI1设置问题
Sher1ockHolmes2021-06-19
void SPI1_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); //SPI1 clk enable
GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //spi1_cs pa4
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //spi1_sck pa5
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 推免复用输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //spi1_mosi pa7
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 推免复用输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //spi1_miso pa6
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP; //推免复用输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GT22L_HOLD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GT22L_HOLD_PORT, &GPIO_InitStructure);
GT22L_HOLD_H;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;//DMA数据宽度
SPI_InitStructure.SPI_DataWidth = SPI_DataWidth_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // mode0 SPI_CPOL_Low, SPI_CPHA_1Edge;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // mode3 SPI_CPOL_High,SPI_CPHA_2Edge
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;//SPI最高40MHz,72M最多2分频
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE); //Enables the specified SPI peripheral SPI使能、主机模式 8位数据模式 SPI 的波特率
SPIM_TXEn(SPI1);
SPIM_RXEn(SPI1);
}
/********************************************************************************************************
**函数信息 :SPIMReadWriteByte(SPI_TypeDef* SPIx,unsigned char tx_data)
**功能描述 : 通过外设 SPIx 收发数据 ,用于全双工模式(同时收发)
**输入参数 :SPI_TypeDef* SPIx,可选择SPI1,SPI2 ; tx_data
**输出参数 :无
********************************************************************************************************/
u8 SPI1_ReadWriteByte(u8 TxData)
{
#if IWDG_ON_OFF==IWDG_ON //独立看门狗开启
IWDG_ReloadCounter(); //喂狗
#endif
while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXEPT)==SET){}
assert_param(IS_SPI_ALL_PERIPH(SPI1));
SPI1->TXREG = TxData;
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXAVL)==RESET){}
assert_param(IS_SPI_ALL_PERIPH(SPI1));
return SPI1->RXREG;
}
我想问下该芯片的SPI1配置有没有问题
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); //SPI1 clk enable
GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //spi1_cs pa4
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //spi1_sck pa5
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 推免复用输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //spi1_mosi pa7
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 推免复用输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //spi1_miso pa6
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP; //推免复用输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GT22L_HOLD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GT22L_HOLD_PORT, &GPIO_InitStructure);
GT22L_HOLD_H;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;//DMA数据宽度
SPI_InitStructure.SPI_DataWidth = SPI_DataWidth_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // mode0 SPI_CPOL_Low, SPI_CPHA_1Edge;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // mode3 SPI_CPOL_High,SPI_CPHA_2Edge
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;//SPI最高40MHz,72M最多2分频
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE); //Enables the specified SPI peripheral SPI使能、主机模式 8位数据模式 SPI 的波特率
SPIM_TXEn(SPI1);
SPIM_RXEn(SPI1);
}
/********************************************************************************************************
**函数信息 :SPIMReadWriteByte(SPI_TypeDef* SPIx,unsigned char tx_data)
**功能描述 : 通过外设 SPIx 收发数据 ,用于全双工模式(同时收发)
**输入参数 :SPI_TypeDef* SPIx,可选择SPI1,SPI2 ; tx_data
**输出参数 :无
********************************************************************************************************/
u8 SPI1_ReadWriteByte(u8 TxData)
{
#if IWDG_ON_OFF==IWDG_ON //独立看门狗开启
IWDG_ReloadCounter(); //喂狗
#endif
while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXEPT)==SET){}
assert_param(IS_SPI_ALL_PERIPH(SPI1));
SPI1->TXREG = TxData;
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXAVL)==RESET){}
assert_param(IS_SPI_ALL_PERIPH(SPI1));
return SPI1->RXREG;
}
我想问下该芯片的SPI1配置有没有问题
赞0
评论
2021-06-23
赞0
评论
2021-06-21
赞0
评论
2021-06-21
赞0
评论
2021-06-20
赞0
评论
2021-06-20
赞0
评论
2021-06-20
赞0
{
#if IWDG_ON_OFF==IWDG_ON //独立看门狗开启
IWDG_ReloadCounter(); //喂狗
#endif
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(GT22L_CLK|GT22L_HOLD_CLK|GT22L_SPI_CLK|RCC_APB2Periph_AFIO , ENABLE); //SPI引脚使能 开启SPI时钟 开启复用功能 开启HOLD线引脚
GPIO_InitStructure.GPIO_Pin = GT22L_SCLK|GT22L_MOSI|GT22L_MISO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GT22L_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GT22L_CS;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GT22L_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GT22L_HOLD;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GT22L_HOLD_PORT, &GPIO_InitStructure);
//SPI模式设置
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex; //全双工模式
SPI_InitStructure.SPI_Mode=SPI_Mode_Master; //主设备模式
SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b; //8字节数据通信
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //时钟空闲时为低电平 SPI_CPOL_Low
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //时钟奇数边沿采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft ; //软件控制NSS(CSN)位
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //通信速率设定,波特率预分频值 SPI_BaudRatePrescaler_256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据传输从MSB开始
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC计算多项式
SPI_Init(SPI1, &SPI_InitStructure); //初始化
SPI_Cmd(SPI1, ENABLE); //使能.
GT22L_HOLD_H;//HOLD 上拉释放总线
}
void SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)
{
#if IWDG_ON_OFF==IWDG_ON //独立看门狗开启
IWDG_ReloadCounter(); //喂狗
#endif
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));//判断有效性
SPI1->CR1&=0XFFC7;//位3-5清零,用来设置波特率
SPI1->CR1|=SPI_BaudRatePrescaler; //设置SPI1速度
SPI_Cmd(SPI1,ENABLE); //使能SPI1
}
//SPI1 读写一个字节
//TxData:要写入的字节
//返回值:读取到的字节
u8 SPI1_ReadWriteByte(u8 TxData)
{
#if IWDG_ON_OFF==IWDG_ON //独立看门狗开启
IWDG_ReloadCounter(); //喂狗
#endif
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){
}//等待发送区空
SPI_I2S_SendData(SPI1, TxData); //通过外设SPIx发送一个byte 数据
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){
} //等待接收完一个byte
return SPI_I2S_ReceiveData(SPI1); //返回通过SPIx最近接收的数据
}
对应STM32的HAL库配置移植
评论
2021-06-19
您需要登录后才可以回复 登录 | 注册