两个STM32之间进行SPI通信
2016-03-24 15:42
447 查看
采用双向通信模式,主机mosi—从机的mosi
主机从机配置一致。从机的片选引脚要设置成浮空输入。主机片选引脚设置成推挽输出
主机配置
void MB90092_CS_Configuration(void)
{
GPIO_InitTypeDef MB90092_CS_GPIO_InitStructure;
/* GPIOC Periph clock enable */
RCC_APB2PeriphClockCmd(MB90092_GPIO_CLK, ENABLE);
/* Configure PC1 in output pushpull mode */
MB90092_CS_GPIO_InitStructure.GPIO_Pin = MB90092_CS_OUT;
MB90092_CS_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
MB90092_CS_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_Init(MB90092_GPIO, &MB90092_CS_GPIO_InitStructure);
}
void MB90092_CS(u8 sw)
{
if(sw == MB90092_CS_ENABLE)
GPIO_WriteBit(MB90092_GPIO, MB90092_CS_OUT , Bit_RESET);
else
GPIO_WriteBit(MB90092_GPIO, MB90092_CS_OUT,Bit_SET);
}
/**************************************************************/
void RCC_MB90092_SPI_Configuration(void)
{
RCC_APB2PeriphClockCmd(MB90092_GPIO_CLK |RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(MB90092_CLK,ENABLE);
}
void GPIO_MB90092_SPI_Configuration(void)
{
GPIO_InitTypeDef MB90092_GPIO_InitStructure;
/* Configure SPIy pins: SCK and MOSI ---------------------------------*/
MB90092_GPIO_InitStructure.GPIO_Pin = MB90092_SCK_SPI_SCK |MB90092_SIN_SPI_MOSI ;
MB90092_GPIO_InitStructure.GPIO_Speed =GPIO_Speed_2MHz;
MB90092_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(MB90092_GPIO, &MB90092_GPIO_InitStructure);
// MB90092_GPIO_InitStructure.GPIO_Pin = MB90092_SIN_SPI1_MISO;
// MB90092_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
// GPIO_Init(MB90092_GPIO, &MB90092_GPIO_InitStructure);
}
void MB90092_SPI_Configuration(void)
{
SPI_InitTypeDef MB90092_SPI_InitStructure;
SPI_Cmd(MB90092_SPI, DISABLE); //必须先禁能,才能改变MODE
//MB90092_SPI_InitStructure.SPI_Direction =SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;SPI_Direction_1Line_Tx //单线双向模式:只发模式,8bit
MB90092_SPI_InitStructure.SPI_Direction =SPI_Direction_2Lines_FullDuplex;
MB90092_SPI_InitStructure.SPI_Mode= SPI_Mode_Master;
MB90092_SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
MB90092_SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;//数据在clk的下降沿锁定
MB90092_SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
MB90092_SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
MB90092_SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_256;
MB90092_SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
MB90092_SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(MB90092_SPI,&MB90092_SPI_InitStructure);
//使能
SPI_Cmd(MB90092_SPI, ENABLE);
}
//初始化与MB90092、Encode相连接的SPI口
void MB90092_SPI_Initialize(void)
{
MB90092_CS_Configuration(); //片选管脚配置
RCC_MB90092_SPI_Configuration(); //系统时钟配置函数
GPIO_MB90092_SPI_Configuration(); //GPIO配置函数
MB90092_SPI_Configuration(); //SPI配置函数
}
从机配置
void SPI _Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/*Enable SPI1 and GPIO clocks */
/*!< SPI_FLASH_SPI_CS_GPIO, SPI_FLASH_SPI_MOSI_GPIO,
SPI_FLASH_SPI_MISO_GPIO, SPI_FLASH_SPI_DETECT_GPIO
and SPI_FLASH_SPI_SCK_GPIO Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD,ENABLE);
/*!< SPI_FLASH_SPI Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/*!< Configure SPI_FLASH_SPI pins: SCK */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: MISO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI_CS_PIN pin: SPI_FLASH Card CS pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*Deselect the FLASH: Chip Select high */
// SPI_FLASH_CS_HIGH();
/*SPI1 configuration */
//W25X16: data input on the DIO pin is sampled on the rising edge of the CLK.
//Data on the DO and DIO pins are clocked out on the falling edge of CLK.
//SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
//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_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/*Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);
}
主机从机配置一致。从机的片选引脚要设置成浮空输入。主机片选引脚设置成推挽输出
主机配置
void MB90092_CS_Configuration(void)
{
GPIO_InitTypeDef MB90092_CS_GPIO_InitStructure;
/* GPIOC Periph clock enable */
RCC_APB2PeriphClockCmd(MB90092_GPIO_CLK, ENABLE);
/* Configure PC1 in output pushpull mode */
MB90092_CS_GPIO_InitStructure.GPIO_Pin = MB90092_CS_OUT;
MB90092_CS_GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
MB90092_CS_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_Init(MB90092_GPIO, &MB90092_CS_GPIO_InitStructure);
}
void MB90092_CS(u8 sw)
{
if(sw == MB90092_CS_ENABLE)
GPIO_WriteBit(MB90092_GPIO, MB90092_CS_OUT , Bit_RESET);
else
GPIO_WriteBit(MB90092_GPIO, MB90092_CS_OUT,Bit_SET);
}
/**************************************************************/
void RCC_MB90092_SPI_Configuration(void)
{
RCC_APB2PeriphClockCmd(MB90092_GPIO_CLK |RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(MB90092_CLK,ENABLE);
}
void GPIO_MB90092_SPI_Configuration(void)
{
GPIO_InitTypeDef MB90092_GPIO_InitStructure;
/* Configure SPIy pins: SCK and MOSI ---------------------------------*/
MB90092_GPIO_InitStructure.GPIO_Pin = MB90092_SCK_SPI_SCK |MB90092_SIN_SPI_MOSI ;
MB90092_GPIO_InitStructure.GPIO_Speed =GPIO_Speed_2MHz;
MB90092_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(MB90092_GPIO, &MB90092_GPIO_InitStructure);
// MB90092_GPIO_InitStructure.GPIO_Pin = MB90092_SIN_SPI1_MISO;
// MB90092_GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
// GPIO_Init(MB90092_GPIO, &MB90092_GPIO_InitStructure);
}
void MB90092_SPI_Configuration(void)
{
SPI_InitTypeDef MB90092_SPI_InitStructure;
SPI_Cmd(MB90092_SPI, DISABLE); //必须先禁能,才能改变MODE
//MB90092_SPI_InitStructure.SPI_Direction =SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;SPI_Direction_1Line_Tx //单线双向模式:只发模式,8bit
MB90092_SPI_InitStructure.SPI_Direction =SPI_Direction_2Lines_FullDuplex;
MB90092_SPI_InitStructure.SPI_Mode= SPI_Mode_Master;
MB90092_SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
MB90092_SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;//数据在clk的下降沿锁定
MB90092_SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
MB90092_SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
MB90092_SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_256;
MB90092_SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
MB90092_SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(MB90092_SPI,&MB90092_SPI_InitStructure);
//使能
SPI_Cmd(MB90092_SPI, ENABLE);
}
//初始化与MB90092、Encode相连接的SPI口
void MB90092_SPI_Initialize(void)
{
MB90092_CS_Configuration(); //片选管脚配置
RCC_MB90092_SPI_Configuration(); //系统时钟配置函数
GPIO_MB90092_SPI_Configuration(); //GPIO配置函数
MB90092_SPI_Configuration(); //SPI配置函数
}
从机配置
void SPI _Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/*Enable SPI1 and GPIO clocks */
/*!< SPI_FLASH_SPI_CS_GPIO, SPI_FLASH_SPI_MOSI_GPIO,
SPI_FLASH_SPI_MISO_GPIO, SPI_FLASH_SPI_DETECT_GPIO
and SPI_FLASH_SPI_SCK_GPIO Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD,ENABLE);
/*!< SPI_FLASH_SPI Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/*!< Configure SPI_FLASH_SPI pins: SCK */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: MISO */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI_CS_PIN pin: SPI_FLASH Card CS pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*Deselect the FLASH: Chip Select high */
// SPI_FLASH_CS_HIGH();
/*SPI1 configuration */
//W25X16: data input on the DIO pin is sampled on the rising edge of the CLK.
//Data on the DO and DIO pins are clocked out on the falling edge of CLK.
//SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
//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_32;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/*Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);
}
相关文章推荐
- FC 交换机端口类型
- day04 面试题Schema约束与DTD约束的区别
- 怎么用eclipse修改web工程的访问路径
- 更新后的Xcode7.3
- C#解leetcode 152. Maximum Product Subarray
- ABAP 数量单位转换
- session four stations
- ssh-key的生成、分发及配置
- iOS程序运行,报错Assertion failure in +[UIView al_attributeForALAttribute:]等相关问题
- 数据结构---栈----栈的初次见面
- 数据结构---栈----栈的初次见面
- 数据结构---栈----栈的初次见面
- 数据结构---栈----栈的初次见面
- 数据结构---栈----栈的初次见面
- 数据结构---栈----栈的初次见面
- 数据结构---栈----栈的初次见面
- 数据结构---栈----栈的初次见面
- 数据结构---栈----栈的初次见面
- 数据结构---栈----栈的初次见面
- 数据结构---栈----栈的初次见面