我要实现STM32做从设备读取AD芯片做主设备的数据,我的AD7765芯片是主动发出10M时钟信号的,而且还发出数据有效控制信号FSO低电平有效,其有效时间是32个时钟周期,我把它接到NSS上了,选硬件置位。AD对STM接线如下:SDO-MOSI,SDI-MISO,FSO-NSS,SCO-SCK。我下载了程序,但是SPI的接收状态位一直没变,说明就移位寄存器就没接到数据,怎么解决呢?
程序如下:
void SPI3_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC
|RCC_APB2Periph_GPIOD, ENABLE );//PORTʱÖÓʹÄÜ
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI3, ENABLE );//SPI3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //PB3/4/5
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIOB
GPIO_SetBits(GPIOB,GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5); //PB3/4/5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //PA15=FSO
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA
GPIO_SetBits(GPIOA,GPIO_Pin_15);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//RESET,FSI
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_10|GPIO_Pin_11);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //PC12=OVE
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC,GPIO_Pin_12);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx; //
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; //
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //
SPI_InitStructure.SPI_NSS = SPI_NSS_Hard; //
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //
SPI_InitStructure.SPI_CRCPolynomial = 7; //
SPI_Init(SPI3, &SPI_InitStructure); //
NVIC_InitStructure.NVIC_IRQChannel = SPI3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
SPI_I2S_ITConfig(SPI3,SPI_I2S_IT_RXNE,ENABLE);
SPI_Cmd(SPI3, ENABLE); //
SPI3_ReadWriteByte(0xff);//
RSET = 0; //AD芯片复位
delay_us(5);
RSET = 1;
delay_us(5);
FSI =1;
SYNC = 0;
delay_us(5);
SYNC = 1; //同步AD时钟
}
void SPI3_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
SPI3->CR1&=0XFFC7;
SPI3->CR1|=SPI_BaudRatePrescaler; //
SPI_Cmd(SPI3,ENABLE);
}
u8 SPI3_ReadWriteByte(u8 TxData)
{
u8 retry=0;
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET) //
{
retry++;
if(retry>200)return 0;
}
SPI_I2S_SendData(SPI3, TxData); //ͨ¹ýÍâÉèSPIx·¢ËÍÒ»¸öÊý¾Ý
retry=0;
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET) //
{
retry++;
if(retry>200)return 0;
}
return SPI_I2S_ReceiveData(SPI3); //
//data1=SPI_I2S_ReceiveData(SPI3);
}
void SPI3_IRQHandler(void)
{
u8 aa;
static u32 data[1000];
if(SPI_I2S_GetFlagStatus(SPI3,SPI_I2S_FLAG_RXNE)==SET)
{
aa++;
data[aa]= SPI_I2S_ReceiveData(SPI3);
}
//SPI_ClearITPendingBit(SPI3,SPI_IT_RXNE);
}
|