大学生
最后登录1970-1-1
在线时间 小时
注册时间2016-1-26
|
我首先用模拟SPI和一个DAC8552芯片通信,信号正常,图模拟SPI程序是普通的模式程序,通过示波器观看时这样的,模拟SPI的波形输出图中黄色是片选信号,当需要通信时,片选脚从高电平拉到低电平,
但是用stm32f407的SPI1通信时出现问题,无法通信,下面是我的程序,PA7是MOSI,PB3是sCK,PA15是片选脚,软件模拟的时候也是选择一样的脚
void initialize_spi(void)
{
GPIO_InitTypeDef GPIO_uInitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); //PA7 SPI1_MOSI
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI1); //PB3 SPI1_SCK
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI1); //PB4 SPI1_MISO
GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4;
GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB,&GPIO_uInitStructure);
//SPI1_MOSI
GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_uInitStructure);
//PA15 SPI1_NSS
GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_uInitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 2M
GPIO_Init(GPIOA,&GPIO_uInitStructure);
SPI1_CS_ON;
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);//复位SPI1
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);//停止复位SPI1
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_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1,&SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void SPI1_SetSpeed(unsigned char SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));//判断有效性
SPI1->CR1&=0XFFC7;//位3-5清零,用来设置波特率
SPI1->CR1|=SPI_BaudRatePrescaler; //设置SPI1速度
SPI_Cmd(SPI1,ENABLE); //使能SPI1
}
unsigned char send_data(unsigned char data)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1,data);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
然后在main还是里执行循环执行发送数据
SPI1_CS_OFF;
send_data(0x10);
send_data(0x5F);
send_data(0x76);
SPI1_CS_ON;
DAC8552_delay();
得到的SPI波形中SPI的片选脚一开始就是低电平了,然后拉低的时候感觉是变成了负的电压了,请问这是为什么呢?用的还是模拟SPI时的片选脚,而且本身是软件片选,请问是什么情况呢?各位大神
在线等,急,急,急。
|
-
模拟SPI
-
模拟SPI的波形输出
-
stm32SPI,黄色是片选,但感觉出错了
|