已回答假定已回答

AD2S1210的SPI读写

Zhongshan 在 2016-9-13 詢問的問題
最後回覆由nandin於2017-1-17提供

没有接旋变,上电后没有复位,在配置模式下(图中的电阻去掉,A0,A1接5V电源)度去寄存器0x88,返回数据总跳变3次最后输出固定值0x08

硬件图如图所示

代码

#define CS_RD0_Low      GPIO_ResetBits(GPIOA,GPIO_Pin_4); //  CS置低

#define CS_RD0_High                                    GPIO_SetBits(GPIOA,GPIO_Pin_4); // CS置高

 

#define SYS_RD0_Low      GPIO_ResetBits(GPIOB,GPIO_Pin_5); // WR置低

#define SYS_RD0_High                                  GPIO_SetBits(GPIOB,GPIO_Pin_5); // WR置高

           

 

void RCC_Config(void)

{

  RCC_DeInit();              //RCC??????

  RCC_HSEConfig(RCC_HSE_ON);  //??????

  if (RCC_WaitForHSEStartUp() == SUCCESS) //????????

   {

    RCC_PLLCmd(DISABLE);                    //??PLL??????PLL

    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //??PLL???????

    RCC_HCLKConfig(RCC_SYSCLK_Div1);     //HCLK(AHB)???????1??

    RCC_PCLK1Config(RCC_HCLK_Div4);     //PCLK1(APB1)???HCLK??8??,?TIM2???HCLK??4??

    RCC_PCLK2Config(RCC_HCLK_Div2);     //PCLK2(APB2)???HCLK??2??

    RCC_PLLConfig(RCC_PLLSource_HSE, 8, 160, 2, 4); //PLL????,????‘system_stm43f4xx.c’ Line149

    RCC_PLLCmd(ENABLE);        //PLL????

    while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } //??PLL?????

   }

            //RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOA | RCC_APB1Periph_TIM3, ENABLE);

 

}

 

void GPIO_Config(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

 

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE);

GPIO_StructInit(&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ

GPIO_Init(GPIOD, &GPIO_InitStructure);//³õʼ»¯  

 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_3;//AD0CS

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ

GPIO_Init(GPIOC, &GPIO_InitStructure);//³õʼ»¯  

          

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_8;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ

GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯  

          

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_4;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//ÆÕͨÊä³öģʽ

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³ö

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ

GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯  

 

GPIO_PinAFConfig(GPIOA,GPIO_PinSource13,GPIO_AF_SWJ);

GPIO_PinAFConfig(GPIOA,GPIO_PinSource14,GPIO_AF_SWJ);

}

 

/*

SPI1初始化:

8位数据格式,配置管脚,主模式全双工,CLK时钟空闲为高,下降沿(第一边沿)发送(结收)数据,2.5M时钟频率(160/64=2.5MHZ),CS片选置低

*/

void SPI1_RD_Config(void)

{

            GPIO_InitTypeDef  GPIO_InitStructure;

  SPI_InitTypeDef  SPI_InitStructure;

          

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE); //ê1?üSPI1ê±?ó

          

  //GPIOFB3,4,53?ê??ˉéè??

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//PA5~7?′ó?1|?üê?3?     

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//?′ó?1|?ü

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//í?íìê?3?

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//é?à-

  GPIO_Init(GPIOA, &GPIO_InitStructure);//3?ê??ˉ

          

            GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1); //PA5?′ó??a SPI1

            GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1); //PA6?′ó??a SPI1

            GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1); //PA7?′ó??a SPI1

 

            //?aà???????SPI?ú3?ê??ˉ

            RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);//?′??SPI1

            RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);//í?1?′??SPI1

 

            SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  //éè??SPIμ?ò?ò?????òμ?êy?Y?ê?:SPIéè???a???????òè???1¤

            SPI_InitStructure.SPI_Mode = SPI_Mode_Master;                  //éè??SPI1¤×÷?ê?:éè???a?÷SPI

            SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;               //éè??SPIμ?êy?Y′óD?:SPI·?í?óê?8?????á11

            SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;                    //′?DDí?2?ê±?óμ????D×′ì??aμíμ???

            SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;     //′?DDí?2?ê±?óμ?μú?t??ì?±???¨é?éy?ò???μ?êy?Y±?2é?ù

            SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;              //NSSD?o?óéó2?t¨NSS1ü????1ê?èí?t¨ê1ó?SSI???1üàí:?ú2?NSSD?o?óDSSI??????

            SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;                  //?¨ò?2¨ì??ê?¤·??μμ??μ:2¨ì??ê?¤·??μ?μ?a8

            SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;     //???¨êy?Y′?ê?′óMSB???1ê?LSB???aê?:êy?Y′?ê?′óMSB???aê?

            SPI_InitStructure.SPI_CRCPolynomial = 7;      //CRC?μ????μ??à??ê?

            SPI_Init(SPI1, &SPI_InitStructure);  //?ù?YSPI_InitStruct?D???¨μ?2?êy3?ê??ˉíaéèSPIx??′??÷

 

            SPI_Cmd(SPI1, ENABLE); //ê1?üSPIíaéè

        CS_RD_Low//CS片选置低

}

 

u8 SPI_RD_SendByte(u8 byte)

{

            u8 Result;

 

  while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)

            {

                       

            }

           

  SPI_I2S_SendData(SPI1, byte);

 

  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)

            {

 

            }

                      

            Result = SPI_I2S_ReceiveData(SPI1);

  return Result;

}

 

void Initilize_GPIO_StartStaus(void)

{

            CS_FM_High;

            CS_AD0_High;

            CS_AD1_High;

            Convert_AD_Low;

            CS_RD0_Low;

            SYS_RD0_High;

            Sample_RD0_High;

}

 

 

u8 SPI_RD_WriteREG(u8 Address)

{

            u16 i;

 

            u8 ReadData = 0;

                      

        RDDownEdge;//SPI下降沿发送数据设置

 

            SYS_RD0_Low;//WR置低

            for(i=0;i<10;i++);

            ReadData = SPI_RD_SendByte(Address);

            SYS_RD0_High;//WR置高

 

            RDUpEdge;//上升沿接受数据

            for(i=0;i<100;i++);

          

            SYS_RD0_Low;

            for(i=0;i<10;i++);

            ReadData = SPI_RD_SendByte(0xFF);

            for(i=0;i<10;i++);

            SYS_RD0_High;

          

return ReadData

}

 

void main(void)

{

  u8 Read = 0;

  u16 i;

            RCC_Config();

            GPIO_Config();

            NVIC_Config();

 

 

            SPI1_RD_Config();

            Initilize_GPIO_StartStaus();

 

  while(1)

  {

     Read  = SPI_RD_WriteREG(0x91,0x7F);

     for(i=0;i<2000;i++);

   }

}

結果