AD7683 + stm32f407 + SPI

时间:2016-03-08 08:48:33

标签: stm32 spi

我有stm32f407discovery和ADC AD7683

我尝试将一个16位采样值读取为3x 8位SPI值并使用位移位,但我只接收' 1'在所有3 spi值。

有谁知道如何解决这个问题,或问题出在哪里?

#define CS_LOW GPIOA->BSRRH = GPIO_BSRR_BS_15;
#define CS_HIGH GPIOA->BSRRL = GPIO_BSRR_BS_15;

uint8_t DataHigh = 0;
uint8_t DataMid = 0;
uint8_t DataLow = 0;
uint16_t result = 0;
uint16_t res = 0;
//////////////////////////////////////////////////////////
void SPI_in()
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    GPIO_InitTypeDef  GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SPI3);
    //GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; // cs
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
    SPI_InitTypeDef SPI_InitStructure;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_RxOnly;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

    SPI_Init(SPI3, &SPI_InitStructure);
    //SPI_SSOutputCmd(SPI3, ENABLE);
    SPI_Cmd(SPI3, ENABLE);
    SPI_NSSInternalSoftwareConfig(SPI3, SPI_NSSInternalSoft_Set);

    CS_HIGH;
}
void TIM_in()
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
    TIM_TimeBaseInitTypeDef TIM_Init;
    TIM_Init.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_Init.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_Init.TIM_Period = 10;
    TIM_Init.TIM_Prescaler = 2000-1;
    TIM_TimeBaseInit(TIM2, &TIM_Init);
    NVIC_EnableIRQ(TIM2_IRQn);
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM2, ENABLE);
}
///////////////////////////////////////////////////////////
void TIM2_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
        CS_LOW;
        //while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY) == SET) {}
        if (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == SET)
        {
            DataHigh = SPI_I2S_ReceiveData(SPI3);
            //while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == SET) {}
            DataMid = SPI_I2S_ReceiveData(SPI3);
            //while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == SET) {}
            DataLow = SPI_I2S_ReceiveData(SPI3);
        }
        CS_HIGH;
        result = (DataHigh << 14) | (DataMid << 6) | (DataLow >> 2);
        res = result >> 4;
        DAC->DHR12R1 = res;
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    }
}

///////////////////////////////////////////////////////////
void DAC_in()
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitTypeDef  GPIO_InitDAC;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
    GPIO_InitDAC.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitDAC.GPIO_Mode = GPIO_Mode_AIN;
    GPIO_Init(GPIOA, &GPIO_InitDAC);

    DAC_InitTypeDef DAC_In;
    DAC_In.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
    DAC_In.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
    DAC_In.DAC_Trigger = DAC_Trigger_None;
    DAC_In.DAC_WaveGeneration = DAC_WaveGeneration_None;

    DAC_Init(DAC_Channel_1, &DAC_In);
    DAC_Cmd(DAC_Channel_1, ENABLE);
}

int main(void)
{
    SPI_in();
    TIM_in();
    DAC_in();
    while(1)
    {
    }
}

1 个答案:

答案 0 :(得分:0)

SPI_I2S_ReceiveData(SPI3)功能未阻塞,仅返回当前存储在SPI3数据寄存器中的值。您需要将一个字节装入SPI3数据寄存器以启动SPI外设发送该字节,然后需要等到RXNE标志置位,然后您将获得您的数据,如下所示:

CS_LOW;
SPI_I2S_SendData(SPI3, 0xFF); //Send dummy byte to start SPI transaction
while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);
DataHigh = SPI_I2S_ReceiveData(SPI3);
while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);
DataMid = SPI_I2S_ReceiveData(SPI3);
while(SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);
DataLow = SPI_I2S_ReceiveData(SPI3);
CS_HIGH;

//Do stuff with data