本文介绍了STM32 HSE 不稳定频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 HSE 以 80mhz 的频率运行我的 Nucleo f401re

I'm trying to run my Nucleo f401re on 80mhz from HSE

int F4xxx::clockInit(int pllM, int pllN, int pllP, int pllQ)
{
    enableHse();

    //FLASH
    CLEAR_BIT(FLASH->ACR, FLASH_ACR_PRFTEN);
    FLASH->ACR&= ~FLASH_ACR_LATENCY;
    FLASH->ACR |= FLASH_ACR_LATENCY_5WS | FLASH_ACR_ICEN | FLASH_ACR_DCEN|FLASH_ACR_PRFTEN;

    //set HSE as PLL source
    RCC->PLLCFGR = RCC_PLLCFGR_PLLSRC_HSE;
    //
    RCC->CR &= ~(RCC_CR_PLLON); //disable PLL before changes
    //

    RCC->PLLCFGR = pllM|(pllN<<6)|(((pllP>>1)-1)<<16)|RCC_PLLCFGR_PLLSRC_HSE|(pllQ<<24);
    RCC->CR|=RCC_CR_PLLON;
    while(!(RCC->CR&RCC_CR_PLLRDY));

    RCC->CFGR &= ~(RCC_CFGR_HPRE);  //Prescaler 1
    RCC->CFGR |= RCC_CFGR_HPRE_DIV1; //AHB = SYSCLK/1


    //APB2 Prescaler 2
    RCC->CFGR &= ~(RCC_CFGR_PPRE2);
    RCC->CFGR |= RCC_CFGR_PPRE2_DIV1;  //APB2 /1

    RCC->CFGR &= ~(RCC_CFGR_PPRE1);
    RCC->CFGR|=RCC_CFGR_PPRE1_DIV1; // APB1 /2

    RCC->CFGR &= ~RCC_CFGR_SW; // reset SW0, SW1.
    RCC->CFGR |= RCC_CFGR_SW_PLL;
    RCC->CR|=RCC_CR_PLLON;

    while((RCC->CFGR & RCC_CFGR_SWS)!=RCC_CFGR_SWS_PLL); // wait for switching to PLL (while PLL is not used as system clock)

    // for power saving
    RCC->CR &= ~(RCC_CR_HSION);

    return 0;
}

void F4xxx::enableHse()
{
    // for control MCO2 (PC9): (freq=SYSCLK/5)
    RCC->AHB1ENR|=RCC_AHB1ENR_GPIOCEN;
    GPIOC->MODER&=~GPIO_MODER_MODE9;
    GPIOC->MODER|=GPIO_MODER_MODE9_1;
    GPIOC->OSPEEDR|=GPIO_OSPEEDER_OSPEEDR9;
    RCC->CFGR|=RCC_CFGR_MCO2PRE;

    RCC->CR |= (RCC_CR_HSEON); //Enable HSE
    while( !(RCC->CR & RCC_CR_HSERDY) ) {}; //ready to start HSE
}

然后像这样调用它:

f4.clockInit(8, 336, 2, 7);

但是我的逻辑分析仪显示频率不稳定

But my logic analyzer show that the frequency is unstable

level=1 的峰宽^-1 = 16 mhz但 level=0 的峰宽^-1 = 8 mhz 和 5.33 mhz

The peaks with level=1 have width^-1 = 16 mhzBut the peaks with level=0 have width^-1 = 8 mhz and 5.33 mhz

什么会导致如此不稳定的频率?

What could cause such an unstable frequency?

推荐答案

如果我没记错的话,你的逻辑分析仪截图来自 Saleae 软件.除非您拥有使用 USB 3 的最新型号之一,否则我猜您的采样频率最大限制为 24 MHz.基于 FX2 的廉价克隆也是这种情况.基本上,您需要 USB 3 OR 内部缓冲存储器 OR 能够对减少的通道数量进行采样,例如 3 个左右,以便以高于 24 MHz 的速度进行采样.

If I'm not mistaken, your logic analyzer screenshot is from the Saleae software. Unless you have one of the latest models which uses USB 3, I guess your sampling frequency is limited to 24 MHz max. This is also the case for FX2 based cheap clones. Basically, you need USB 3 OR Internal Buffer Memory OR ability to sample reduced number of channels, like 3 or so in order to sample faster than 24 MHz.

您没有告诉您的采样频率,但根据可用信息,我假设它限制为 24 MHz.奈奎斯特采样定理指出,您需要以比测量的信号快至少 2 倍的速度进行采样.因此,对于 16 MHz 信号,您至少需要 32 MHz 的采样率.在较低的采样频率下,您会观察到一种称为混叠的现象,您测量的信号似乎具有较低的频率.

You didn't tell your sampling frequency, but based on the available info, I assume that it's limited to 24 MHz. Nyquist Sampling Theorem states that you need to sample at least 2 times faster than the signal you measure. So, for a 16 MHz signal you need at least 32 MHz sampling rate. At lower sampling frequencies, you observe a phenomena called aliasing, where the signal you measure seems to have a lower frequency.

请记住,32 MHz 是理论上的最小值,您仍然可能(并且可能会)观察到信号中的失真.对于模拟信号,通常使用 x10 或 x20 采样率.对于您测量的数字信号,x4 可能没问题.

Keep it in mind that 32 MHz is the theoretical minimum and you still may (and probably will) observe distortions in the signal. For analog signals, x10 or x20 sampling rates are generally used. For digital signal like you measure, x4 is probably fine.

不久前,我不得不使用 Saleae 克隆来调试 USB 全速总线 (12 MHz).使用 24 MHz 采样率有时有效,有时无效.当它没有时,我按下按钮并再次尝试我的机会......

Not long ago, I had to debug USB Full Speed bus (12 MHz) with a Saleae clone. Using 24 MHz sampling rate sometimes worked and sometimes didn't. When it didn't, I hit the button and tried my chance again...

所以,您可能根本没有问题.由于设备的限制,您无法正确测量您的信号.当您重复测量时,您可能会不时遇到相同的采样问题.

So, you probably don't have an issue at all. You are just unable to measure your signal correctly because of the limitation of your equipment. When you repeat your measurements, you will probably have the same sampling issues from time to time.

这篇关于STM32 HSE 不稳定频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 18:08