微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 求大神,STM32F103rct6测频

求大神,STM32F103rct6测频

时间:10-02 整理:3721RD 点击:
在线等,急求!求大神,怎么用STM32F103RCT6的PC0口编写测试方波的频率代码啊

测试方波频率  首先应该从输入捕获考虑  看有没有定时器的输入捕获与你说的 PC0 直接相连或者是映射  将其配置好之后  然后就是 对应的时钟打开  I/O模式配置好  应该是输入模式

下面这个函数是arduino中测量引脚高地平或者低电平维持时间的函数,小编可以参照一下,只要测得引脚输入的高电平或者低电平维持时间,其倒数就是频率了。

unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
{
        // cache the port and bit of the pin in order to speed up the
        // pulse width measuring loop and achieve finer resolution.  calling
        // digitalRead() instead yields much coarser resolution.
        uint8_t bit = digitalPinToBitMask(pin);
        uint8_t port = digitalPinToPort(pin);
        uint8_t stateMask = (state ? bit : 0);
        unsigned long width = 0; // keep initialization out of time critical area
       
        // convert the timeout from microseconds to a number of times through
        // the initial loop; it takes 16 clock cycles per iteration.
        unsigned long numloops = 0;
        unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
       
        // wait for any previous pulse to end
        while ((*portInputRegister(port) & bit) == stateMask)
                if (numloops++ == maxloops)
                        return 0;
       
        // wait for the pulse to start
        while ((*portInputRegister(port) & bit) != stateMask)
                if (numloops++ == maxloops)
                        return 0;
       
        // wait for the pulse to stop
        while ((*portInputRegister(port) & bit) == stateMask) {
                if (numloops++ == maxloops)
                        return 0;
                width++;
        }
        // convert the reading to microseconds. There will be some error introduced by
        // the interrupt handlers.
        // Conversion constants are compiler-dependent, different compiler versions
        // have different levels of optimization.
#if __GNUC__==4 && __GNUC_MINOR__==3 && __GNUC_PATCHLEVEL__==2
        // avr-gcc 4.3.2
        return clockCyclesToMicroseconds(width * 21 + 16);
#elif __GNUC__==4 && __GNUC_MINOR__==8 && __GNUC_PATCHLEVEL__==1
        // avr-gcc 4.8.1
        return clockCyclesToMicroseconds(width * 24 + 16);
#elif __GNUC__<=4 && __GNUC_MINOR__<=3
        // avr-gcc <=4.3.x
        #warning "pulseIn() results may not be accurate"
        return clockCyclesToMicroseconds(width * 21 + 16);
#else
        // avr-gcc >4.3.x
        #warning "pulseIn() results may not be accurate"
        return clockCyclesToMicroseconds(width * 24 + 16);
#endif
}

抱歉,你的这个程序我真没看懂!arduino是什么?

我只需要用KEIL编程的测试方波频率的程序代码!

感谢你的建议,但我现在学的是STM32F103RCT6啊!

小编解决了吗        

用定时器的输入捕获功能实现。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top