标题:
求助!STM32控制ADF4351锁相环,芯片无输出
[打印本页]
作者:
TDWFE
时间:
2021-7-25 21:46
标题:
求助!STM32控制ADF4351锁相环,芯片无输出
输入和电源正常,CE使能端高电平,但从CPOUT就没有输出了,换了一次芯片也不好用
代码(从坛子里找的改了改):
//ADF4351.c
#include "ADF4351.h"
#include "delay.h"
#include "stm32g4xx_hal.h"
//#define
#define ADF4351_R0 ((uint32_t)0X2C8018)
#define ADF4351_R1 ((uint32_t)0X8029)
#define ADF4351_R2 ((uint32_t)0X10E42)
#define ADF4351_R3 ((uint32_t)0X4B3)
#define ADF4351_R4 ((uint32_t)0XEC803C)
#define ADF4351_R5 ((uint32_t)0X580005)
#define ADF4351_R1_Base ((uint32_t)0X8001)
#define ADF4351_R4_Base ((uint32_t)0X8C803C)
#define ADF4351_R4_ON ((uint32_t)0X8C803C)
#define ADF4351_R4_OFF ((uint32_t)0X8C883C)
#define ADF4351_RF_OFF ((uint32_t)0XEC801C)
#define ADF4351_PD_ON ((uint32_t)0X10E42)
#define ADF4351_PD_OFF ((uint32_t)0X10E02)
void delay (int length)
{
while (length >0)
length--;
}
void WriteToADF4351(uint8_t count, uint8_t *buf)
{
uint8_t ValueToWrite = 0;
uint8_t i = 0;
uint8_t j = 0;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
delay_us(1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
delay_us(1);
for(i = count; i>0; i--)
{
ValueToWrite = *(buf+i-1);
for(j=0; j<8; j++)
{
if(0x80 == (ValueToWrite & 0x80))
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);
}
delay_us(1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
delay_us(1);
ValueToWrite <<= 1;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
}
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
delay_us(1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
delay_us(1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
}
void ReadToADF4351(uint8_t count, uint8_t *buf)
{
uint8_t m = 0;
uint8_t l = 0;
uint8_t iTemp = 0;
uint8_t RotateData = 0;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
delay_us(1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
delay_us(1);
for(m = count; m>0; m--)
{
for(l = 0; l<8; l++)
{
RotateData <<=1;
delay_us(1);
iTemp = ADF4351_INPUT_DATA;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
if(0x01 == (iTemp&0x01))
{
RotateData |= 1;
}
delay_us(1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
}
*(buf+m-1) = RotateData;
}
delay_us(1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
delay_us(1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
}
void ADF4351Init(void)
{
uint8_t buf[4] = {0,0,0,0};
buf[3] = 0x00;
buf[2] = 0x58; //R5
buf[1] = 0x00; //write communication register 0x00580005 to control the progress
buf[0] = 0x05; //to write Register 5 to set digital lock detector
WriteToADF4351(4,buf);
buf[3] = 0x00; //R4
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
buf[1] = 0x80; //(DB11=0)VCO powerd up;
buf[0] = 0x3C; //(DB5=1)RF output is enabled;(DB4-3=3H)Output power level is 5
WriteToADF4351(4,buf);
buf[3] = 0x00;
buf[2] = 0x00; //R3
buf[1] = 0x04; //(DB14-3:96H)clock divider value is 150.
buf[0] = 0xB3;
WriteToADF4351(4,buf);
buf[3] = 0x00; //R2
// buf[2] = 0x01; //(DB6=1)set PD polarity is positive;(DB7=1)LDP is 6nS;
// buf[1] = 0x0E; //(DB8=0)enable fractional-N digital lock detect;
buf[2] = 0x00;
buf[1] = 0x4E;
buf[0] = 0x42; //(DB12-9:7H)set Icp 2.50 mA;
WriteToADF4351(4,buf); //(DB23-14:1H)R counter is 1
buf[3] = 0x00;
buf[2] = 0x00; //R1
buf[1] = 0x80; //(DB14-3:6H)MOD counter is 6;
buf[0] = 0x29; //(DB26-15:6H)PHASE word is 1,neither the phase resync
WriteToADF4351(4,buf); //nor the spurious optimization functions are being used
//(DB27=1)prescaler value is 8/9
buf[3] = 0x00;
buf[2] = 0x2c;
buf[1] = 0x80; //R0
buf[0] = 0x18; //(DB14-3:0H)FRAC value is 0;
WriteToADF4351(4,buf); //(DB30-15:140H)INT value is 320;
// WriteOneRegToADF4351(ADF4351_R0);
// WriteOneRegToADF4351(ADF4351_R1);
// WriteOneRegToADF4351(ADF4351_R2);
// WriteOneRegToADF4351(ADF4351_R3);
// WriteOneRegToADF4351(ADF4351_R4);
// WriteOneRegToADF4351(ADF4351_R5);
}
void WriteOneRegToADF4351(uint32_t Regster)
{
uint8_t buf[4] = {0,0,0,0};
buf[3] = (uint8_t)((Regster>>24)&(0X000000FF));
buf[2] = (uint8_t)((Regster>>16)&(0X000000FF));
buf[1] = (uint8_t)((Regster>>8) &(0X000000FF));
buf[0] = (uint8_t)((Regster)&(0X000000FF));
WriteToADF4351(4,buf);
}
void ADF4351_Init_some(void)
{
WriteOneRegToADF4351(ADF4351_R2);
WriteOneRegToADF4351(ADF4351_R3);
WriteOneRegToADF4351(ADF4351_R5);
}
void ADF4351WriteFreq(float Fre) // (xx.x) M Hz
{
uint16_t Fre_temp, N_Mul = 1, Mul_Core = 0;
uint16_t INT_Fre, Frac_temp, Mod_temp, i;
uint32_t W_ADF4351_R0 = 0, W_ADF4351_R1 = 0, W_ADF4351_R4 = 0;
float multiple;
if(Fre < 35.0)
Fre = 35.0;
if(Fre > 4400.0)
Fre = 4400.0;
Mod_temp = 250;
Fre = ((float)((uint32_t)(Fre*10)))/10;
Fre_temp = Fre;
for(i = 0; i < 10; i++)
{
if(((Fre_temp*N_Mul) >= 2199.9) && ((Fre_temp*N_Mul) <= 4400.1))
break;
Mul_Core++;
N_Mul = N_Mul*2;
}
multiple = (Fre*N_Mul)/25;
INT_Fre = (uint16_t)multiple;
// if(((uint32_t)(multiple*10000)%10)> 5)
// multiple += 0.001;
Frac_temp = ((uint32_t)(multiple*250))%250;
while(((Frac_temp%5) == 0) && ((Mod_temp%5) == 0))
{
Frac_temp = Frac_temp/5;
Mod_temp = Mod_temp/5;
}
while(((Frac_temp%2) == 0)&&((Mod_temp%2) == 0))
{
Frac_temp = Frac_temp/2;
Mod_temp = Mod_temp/2;
}
Mul_Core %= 7;
W_ADF4351_R0 = (INT_Fre<<15)+(Frac_temp<<3);
W_ADF4351_R1 = ADF4351_R1_Base + (Mod_temp<<3);
W_ADF4351_R4 = ADF4351_R4_ON + (Mul_Core<<20);
// WriteOneRegToADF4351(ADF4351_PD_OFF); //ADF4351_RF_OFF
// WriteOneRegToADF4351((uint32_t)(ADF4351_R4_OFF + (6<<20)));
WriteOneRegToADF4351(ADF4351_RF_OFF);
WriteOneRegToADF4351(W_ADF4351_R1);
WriteOneRegToADF4351(W_ADF4351_R0);
WriteOneRegToADF4351(W_ADF4351_R4);
// WriteOneRegToADF4351(ADF4351_PD_ON);
// WriteOneRegToADF4351((uint32_t)(ADF4351_R4_ON + (Mul_Core<<20)));
}
复制代码
//ADF4351.h
#ifndef _ADF4351_H_
#define _ADF4351_H_
#include "stm32g474xx.h"
#include "sys.h"
#define ADF4351_INPUT_DATA PAin(1)
void ADF4351Init(void);
void ReadToADF4351(uint8_t count, uint8_t *buf);
void WriteToADF4351(uint8_t count, uint8_t *buf);
void WriteOneRegToADF4351(uint32_t Regster);
void ADF4351_Init_some(void);
void ADF4351WriteFreq(float Fre); // (xx.x) M Hz
#endif
复制代码
//main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "ADF4351.h"
#include "delay.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
delay_us(1);
ADF4351Init();
//ADF4351WriteFreq(400);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV6;
RCC_OscInitStruct.PLL.PLLN = 85;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_SET);
/*Configure GPIO pin : PA1 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : PA4 PA5 PA6 PA7 */
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1