STM32/STM8技术william hill官网
直播中

甘鹏飞

10年用户 48经验值
擅长:电源/新能源 模拟技术 控制/MCU
私信 关注
[问答]

求大神,STM32F103rct6测频

在线等,急求!求大神,怎么用STM32F103RCT6的PC0口编写测试方波的频率代码啊
已退回60积分

回帖(7)

陈林

2015-9-25 14:01:08
引用: 甘鹏飞 发表于 2015-4-20 14:07
感谢你的建议,但我现在学的是STM32F103RCT6啊!

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

韩山童

2015-4-18 18:40:08

下面这个函数是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

}
举报

甘鹏飞

2015-4-19 08:38:07
抱歉,你的这个程序我真没看懂!arduino是什么?
举报

甘鹏飞

2015-4-20 14:06:22
我只需要用KEIL编程的测试方波频率的程序代码!
举报

甘鹏飞

2015-4-20 14:07:10
感谢你的建议,但我现在学的是STM32F103RCT6啊!
举报

王安国

2016-7-1 12:38:23
楼主解决了吗        
举报

superassent

2016-12-6 23:31:19
用定时器的输入捕获功能实现。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分