找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1774|回复: 0
收起左侧

求助!STM32控制ADF4351锁相环,芯片无输出

[复制链接]
ID:956164 发表于 2021-7-25 21:46 | 显示全部楼层 |阅读模式
输入和电源正常,CE使能端高电平,但从CPOUT就没有输出了,换了一次芯片也不好用
代码(从坛子里找的改了改):
  1. //ADF4351.c

  2. #include "ADF4351.h"
  3. #include "delay.h"
  4. #include "stm32g4xx_hal.h"

  5. //#define
  6. #define ADF4351_R0                        ((uint32_t)0X2C8018)
  7. #define ADF4351_R1                        ((uint32_t)0X8029)
  8. #define ADF4351_R2                        ((uint32_t)0X10E42)
  9. #define ADF4351_R3                        ((uint32_t)0X4B3)
  10. #define ADF4351_R4                        ((uint32_t)0XEC803C)
  11. #define ADF4351_R5                        ((uint32_t)0X580005)

  12. #define ADF4351_R1_Base        ((uint32_t)0X8001)
  13. #define ADF4351_R4_Base        ((uint32_t)0X8C803C)
  14. #define ADF4351_R4_ON          ((uint32_t)0X8C803C)
  15. #define ADF4351_R4_OFF        ((uint32_t)0X8C883C)

  16. #define ADF4351_RF_OFF        ((uint32_t)0XEC801C)

  17. #define ADF4351_PD_ON                ((uint32_t)0X10E42)
  18. #define ADF4351_PD_OFF        ((uint32_t)0X10E02)


  19. void delay (int length)
  20. {
  21.         while (length >0)
  22.             length--;
  23. }

  24. void WriteToADF4351(uint8_t count, uint8_t *buf)
  25. {
  26.         uint8_t ValueToWrite = 0;
  27.         uint8_t i = 0;
  28.         uint8_t j = 0;
  29.        
  30.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
  31.         delay_us(1);
  32.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
  33.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  34.         delay_us(1);
  35.        
  36.         for(i = count; i>0; i--)
  37.         {
  38.                 ValueToWrite = *(buf+i-1);
  39.                 for(j=0; j<8; j++)
  40.                 {
  41.                         if(0x80 == (ValueToWrite & 0x80))
  42.                         {
  43.                                 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET);
  44.                         }
  45.                         else
  46.                         {
  47.                                 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);
  48.                         }
  49.                         delay_us(1);
  50.                         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
  51.                         delay_us(1);
  52.                         ValueToWrite <<= 1;
  53.                         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
  54.                 }
  55.         }
  56.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  57.         delay_us(1);
  58.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
  59.         delay_us(1);
  60.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  61. }


  62. void ReadToADF4351(uint8_t count, uint8_t *buf)
  63. {
  64.         uint8_t m = 0;
  65.         uint8_t l = 0;
  66.         uint8_t iTemp = 0;
  67.         uint8_t RotateData = 0;

  68.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
  69.         delay_us(1);
  70.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
  71.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  72.         delay_us(1);
  73.        
  74.         for(m = count; m>0; m--)
  75.         {
  76.                 for(l = 0; l<8; l++)
  77.                 {
  78.                         RotateData <<=1;
  79.                         delay_us(1);
  80.                         iTemp = ADF4351_INPUT_DATA;
  81.                         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
  82.                         if(0x01 == (iTemp&0x01))
  83.                         {
  84.                                 RotateData |= 1;
  85.                         }
  86.                         delay_us(1);
  87.                         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
  88.                 }
  89.                 *(buf+m-1) = RotateData;
  90.         }
  91.         delay_us(1);
  92.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
  93.         delay_us(1);
  94.         HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  95. }


  96. void ADF4351Init(void)
  97. {
  98.         uint8_t buf[4] = {0,0,0,0};
  99.        
  100.         buf[3] = 0x00;
  101.         buf[2] = 0x58;                                //R5
  102.         buf[1] = 0x00;                                //write communication register 0x00580005 to control the progress
  103.         buf[0] = 0x05;                                //to write Register 5 to set digital lock detector
  104.         WriteToADF4351(4,buf);               

  105.         buf[3] = 0x00;                                //R4
  106.         buf[2] = 0xec;                                //(DB23=1)The signal is taken from the VCO directly;(DB22-20:4H)the RF divider is 16;(DB19-12:C8H)R is 200
  107.         buf[1] = 0x80;                                //(DB11=0)VCO powerd up;
  108.         buf[0] = 0x3C;                                //(DB5=1)RF output is enabled;(DB4-3=3H)Output power level is 5
  109.         WriteToADF4351(4,buf);               

  110.         buf[3] = 0x00;
  111.         buf[2] = 0x00;                                //R3
  112.         buf[1] = 0x04;                                //(DB14-3:96H)clock divider value is 150.
  113.         buf[0] = 0xB3;
  114.         WriteToADF4351(4,buf);       
  115.        
  116.         buf[3] = 0x00;                                //R2
  117. //        buf[2] = 0x01;                                //(DB6=1)set PD polarity is positive;(DB7=1)LDP is 6nS;
  118. //        buf[1] = 0x0E;                                //(DB8=0)enable fractional-N digital lock detect;
  119.         buf[2] = 0x00;
  120.         buf[1] = 0x4E;
  121.         buf[0] = 0x42;                                //(DB12-9:7H)set Icp 2.50 mA;
  122.         WriteToADF4351(4,buf);                //(DB23-14:1H)R counter is 1

  123.         buf[3] = 0x00;
  124.         buf[2] = 0x00;                                //R1
  125.         buf[1] = 0x80;                           //(DB14-3:6H)MOD counter is 6;
  126.         buf[0] = 0x29;                           //(DB26-15:6H)PHASE word is 1,neither the phase resync
  127.         WriteToADF4351(4,buf);           //nor the spurious optimization functions are being used
  128.                                                            //(DB27=1)prescaler value is 8/9

  129.         buf[3] = 0x00;
  130.         buf[2] = 0x2c;
  131.         buf[1] = 0x80;                                //R0
  132.         buf[0] = 0x18;                                //(DB14-3:0H)FRAC value is 0;
  133.         WriteToADF4351(4,buf);                //(DB30-15:140H)INT value is 320;
  134.        
  135.        
  136. //        WriteOneRegToADF4351(ADF4351_R0);
  137. //        WriteOneRegToADF4351(ADF4351_R1);
  138. //        WriteOneRegToADF4351(ADF4351_R2);
  139. //        WriteOneRegToADF4351(ADF4351_R3);
  140. //        WriteOneRegToADF4351(ADF4351_R4);
  141. //        WriteOneRegToADF4351(ADF4351_R5);
  142.        
  143. }
  144. void WriteOneRegToADF4351(uint32_t Regster)
  145. {
  146.         uint8_t buf[4] = {0,0,0,0};
  147.         buf[3] = (uint8_t)((Regster>>24)&(0X000000FF));
  148.         buf[2] = (uint8_t)((Regster>>16)&(0X000000FF));
  149.         buf[1] = (uint8_t)((Regster>>8) &(0X000000FF));
  150.         buf[0] = (uint8_t)((Regster)&(0X000000FF));
  151.         WriteToADF4351(4,buf);       
  152. }
  153. void ADF4351_Init_some(void)
  154. {
  155.         WriteOneRegToADF4351(ADF4351_R2);
  156.         WriteOneRegToADF4351(ADF4351_R3);
  157.         WriteOneRegToADF4351(ADF4351_R5);
  158. }

  159. void ADF4351WriteFreq(float Fre)                //        (xx.x) M Hz
  160. {
  161.         uint16_t Fre_temp, N_Mul = 1, Mul_Core = 0;
  162.         uint16_t INT_Fre, Frac_temp, Mod_temp, i;
  163.         uint32_t W_ADF4351_R0 = 0, W_ADF4351_R1 = 0, W_ADF4351_R4 = 0;
  164.         float multiple;
  165.        
  166.         if(Fre < 35.0)
  167.                 Fre = 35.0;
  168.         if(Fre > 4400.0)
  169.                 Fre = 4400.0;
  170.         Mod_temp = 250;
  171.         Fre = ((float)((uint32_t)(Fre*10)))/10;
  172.        
  173.         Fre_temp = Fre;
  174.         for(i = 0; i < 10; i++)
  175.         {
  176.                 if(((Fre_temp*N_Mul) >= 2199.9) && ((Fre_temp*N_Mul) <= 4400.1))
  177.                         break;
  178.                 Mul_Core++;
  179.                 N_Mul = N_Mul*2;
  180.         }
  181.        
  182.         multiple = (Fre*N_Mul)/25;
  183.         INT_Fre = (uint16_t)multiple;
  184. //        if(((uint32_t)(multiple*10000)%10)> 5)
  185. //                multiple += 0.001;
  186.         Frac_temp = ((uint32_t)(multiple*250))%250;
  187.         while(((Frac_temp%5) == 0) && ((Mod_temp%5) == 0))
  188.         {
  189.                 Frac_temp = Frac_temp/5;
  190.                 Mod_temp = Mod_temp/5;
  191.         }
  192.         while(((Frac_temp%2) == 0)&&((Mod_temp%2) == 0))
  193.         {
  194.                 Frac_temp = Frac_temp/2;
  195.                 Mod_temp = Mod_temp/2;
  196.         }
  197.        
  198.         Mul_Core %= 7;
  199.         W_ADF4351_R0 = (INT_Fre<<15)+(Frac_temp<<3);
  200.         W_ADF4351_R1 = ADF4351_R1_Base + (Mod_temp<<3);
  201.         W_ADF4351_R4 = ADF4351_R4_ON + (Mul_Core<<20);
  202.        
  203.        
  204. //        WriteOneRegToADF4351(ADF4351_PD_OFF); //ADF4351_RF_OFF
  205. //        WriteOneRegToADF4351((uint32_t)(ADF4351_R4_OFF + (6<<20)));
  206.         WriteOneRegToADF4351(ADF4351_RF_OFF);
  207.         WriteOneRegToADF4351(W_ADF4351_R1);
  208.         WriteOneRegToADF4351(W_ADF4351_R0);
  209.         WriteOneRegToADF4351(W_ADF4351_R4);
  210. //        WriteOneRegToADF4351(ADF4351_PD_ON);
  211.        
  212. //        WriteOneRegToADF4351((uint32_t)(ADF4351_R4_ON + (Mul_Core<<20)));
  213. }
复制代码

  1. //ADF4351.h

  2. #ifndef _ADF4351_H_
  3. #define _ADF4351_H_

  4. #include "stm32g474xx.h"
  5. #include "sys.h"

  6. #define ADF4351_INPUT_DATA PAin(1)

  7. void ADF4351Init(void);
  8. void ReadToADF4351(uint8_t count, uint8_t *buf);
  9. void WriteToADF4351(uint8_t count, uint8_t *buf);
  10. void WriteOneRegToADF4351(uint32_t Regster);
  11. void ADF4351_Init_some(void);
  12. void ADF4351WriteFreq(float Fre);                //        (xx.x) M Hz
  13. #endif

复制代码

  1. //main.c

  2. /* USER CODE BEGIN Header */
  3. /**
  4.   ******************************************************************************
  5. * @file           : main.c
  6.   * @brief          : Main program body
  7.   ******************************************************************************
  8.   * @attention
  9.   *
  10.   * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  11.   * All rights reserved.</center></h2>
  12.   *
  13.   * This software component is licensed by ST under BSD 3-Clause license,
  14.   * the "License"; You may not use this file except in compliance with the
  15.   * License. You may obtain a copy of the License at:
  16.   *                        opensource.org/licenses/BSD-3-Clause
  17.   *
  18.   ******************************************************************************
  19.   */
  20. /* USER CODE END Header */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"

  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include "ADF4351.h"
  26. #include "delay.h"
  27. /* USER CODE END Includes */

  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */

  30. /* USER CODE END PTD */

  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33. /* USER CODE END PD */

  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN PM */

  36. /* USER CODE END PM */

  37. /* Private variables ---------------------------------------------------------*/

  38. /* USER CODE BEGIN PV */

  39. /* USER CODE END PV */

  40. /* Private function prototypes -----------------------------------------------*/
  41. void SystemClock_Config(void);
  42. static void MX_GPIO_Init(void);
  43. /* USER CODE BEGIN PFP */

  44. /* USER CODE END PFP */

  45. /* Private user code ---------------------------------------------------------*/
  46. /* USER CODE BEGIN 0 */

  47. /* USER CODE END 0 */

  48. /**
  49.   * @brief  The application entry point.
  50.   * @retval int
  51.   */
  52. int main(void)
  53. {
  54.   /* USER CODE BEGIN 1 */

  55.   /* USER CODE END 1 */

  56.   /* MCU Configuration--------------------------------------------------------*/

  57.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  58.   HAL_Init();

  59.   /* USER CODE BEGIN Init */

  60.   /* USER CODE END Init */

  61.   /* Configure the system clock */
  62.   SystemClock_Config();

  63.   /* USER CODE BEGIN SysInit */

  64.   /* USER CODE END SysInit */

  65.   /* Initialize all configured peripherals */
  66.   MX_GPIO_Init();
  67.   /* USER CODE BEGIN 2 */
  68. delay_us(1);
  69. ADF4351Init();
  70. //ADF4351WriteFreq(400);
  71.   /* USER CODE END 2 */

  72.   /* Infinite loop */
  73.   /* USER CODE BEGIN WHILE */
  74.   while (1)
  75.   {
  76.     /* USER CODE END WHILE */

  77.     /* USER CODE BEGIN 3 */
  78.   }
  79.   /* USER CODE END 3 */
  80. }

  81. /**
  82.   * @brief System Clock Configuration
  83.   * @retval None
  84.   */
  85. void SystemClock_Config(void)
  86. {
  87.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  88.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  89.   /** Configure the main internal regulator output voltage
  90.   */
  91.   HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
  92.   /** Initializes the RCC Oscillators according to the specified parameters
  93.   * in the RCC_OscInitTypeDef structure.
  94.   */
  95.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  96.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  97.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  98.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  99.   RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV6;
  100.   RCC_OscInitStruct.PLL.PLLN = 85;
  101.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  102.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  103.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  104.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  105.   {
  106.     Error_Handler();
  107.   }
  108.   /** Initializes the CPU, AHB and APB buses clocks
  109.   */
  110.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  111.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  112.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  113.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  114.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  115.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  116.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  117.   {
  118.     Error_Handler();
  119.   }
  120. }

  121. /**
  122.   * @brief GPIO Initialization Function
  123.   * @param None
  124.   * @retval None
  125.   */
  126. static void MX_GPIO_Init(void)
  127. {
  128.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  129.   /* GPIO Ports Clock Enable */
  130.   __HAL_RCC_GPIOF_CLK_ENABLE();
  131.   __HAL_RCC_GPIOA_CLK_ENABLE();

  132.   /*Configure GPIO pin Output Level */
  133.   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_SET);

  134.   /*Configure GPIO pin : PA1 */
  135.   GPIO_InitStruct.Pin = GPIO_PIN_1;
  136.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  137.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  138.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  139.   /*Configure GPIO pins : PA4 PA5 PA6 PA7 */
  140.   GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  141.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  142.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  143.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  144.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  145. }

  146. /* USER CODE BEGIN 4 */

  147. /* USER CODE END 4 */

  148. /**
  149.   * @brief  This function is executed in case of error occurrence.
  150.   * @retval None
  151.   */
  152. void Error_Handler(void)
  153. {
  154.   /* USER CODE BEGIN Error_Handler_Debug */
  155.   /* User can add his own implementation to report the HAL error return state */
  156.   __disable_irq();
  157.   while (1)
  158.   {
  159.   }
  160.   /* USER CODE END Error_Handler_Debug */
  161. }

  162. #ifdef  USE_FULL_ASSERT
  163. /**
  164.   * @brief  Reports the name of the source file and the source line number
  165.   *         where the assert_param error has occurred.
  166.   * @param  file: pointer to the source file name
  167.   * @param  line: assert_param error line source number
  168.   * @retval None
  169.   */
  170. void assert_failed(uint8_t *file, uint32_t line)
  171. {
  172.   /* USER CODE BEGIN 6 */
  173.   /* User can add his own implementation to report the file name and line number,
  174.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  175.   /* USER CODE END 6 */
  176. }
  177. #endif /* USE_FULL_ASSERT */

  178. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表