新手上路
- 积分
- 40
- 金钱
- 40
- 注册时间
- 2016-6-6
- 在线时间
- 49 小时
|
31金钱
#include "delay.h"
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); //spi1时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //A时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);//B时钟使能
GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1);
//GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource5,GPIO_AF_SPI1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_6;//设置PA4,6
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
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_5;//设置PA5
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_5;//设置PB5
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(GPIOB, &GPIO_InitStructure);
//GPIO_SetBits(GPIOA,GPIO_Pin_5);
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx ; //发
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //SPI数据大小:发送16位帧数据结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High ; //设备空闲状态时同步时钟SCK的状态,High表示高电平,Low表示低电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //时钟相位,1表示在同步时钟SCK的奇数沿边采样,2表示偶数沿边采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS由软件控件片选
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;//时钟的预分频值
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //MSB高位在前
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC较验和的多项式
SPI_Init(SPI1, &SPI_InitStructure); //初始化SPI1的配置项
SPI_Cmd(SPI1, ENABLE); //使能SPI1
}
#include "stm32_eval.h"
#include "spi1.h"
#include "stm32f2xx.h"
#include "delay.h"
int main()
{ SPI1_Init();
while (1)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
SPI_I2S_SendData(SPI1,0x00);
}
// GPIO_SetBits(GPIOA,GPIO_Pin_4);
//GPIO_ResetBits(GPIOA,GPIO_Pin_6);
//delay_mms(588);//20us延时
//while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)//????????????????
// return SPI_I2S_ReceiveData(SPI1);
//delay_mms(29412);//1ms延时
//GPIO_SetBits(GPIOA,GPIO_Pin_6);
//GPIO_ResetBits(GPIOA,GPIO_Pin_4);
}
程序已上传 求解
|
|