{"id":847,"date":"2024-08-14T16:56:35","date_gmt":"2024-08-14T08:56:35","guid":{"rendered":"http:\/\/8.153.64.192\/?page_id=847"},"modified":"2024-08-16T14:15:11","modified_gmt":"2024-08-16T06:15:11","slug":"%e9%a9%b1%e5%8a%a8%e8%a1%a5%e5%85%85%ef%bc%9aexample_gptimer_capture","status":"publish","type":"page","link":"http:\/\/www.agm-support.com.cn\/index.php\/sample-page\/%e9%a9%b1%e5%8a%a8%e8%a1%a5%e5%85%85%ef%bc%9aexample_gptimer_capture\/","title":{"rendered":"\u9a71\u52a8\u8865\u5145\uff1aexample_gptimer_capture"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\/\/example_gptimer_capture.c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">include \"example.h\"<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">static uint32_t capture_VAL[20];<br>static uint8_t capture_idx = 0;<br>static uint8_t capture_en = 0;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">volatile uint16_t testUpdateCnt = 0;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">int global_autoload; \/\/timer\u4e2dautoload\u7684\u503c<br>\/\/ \u8ba1\u7b97\u4e24\u6b21\u83b7\u53d6\u5230\u7684\u65f6\u95f4\u5dee\uff0c\u7528\u4ee5\u8ba1\u7b97\u4e00\u4e2a\u8f93\u5165\u5468\u671f\u7684\u957f\u5ea6<br>int getLastTimeInterval(uint32_t curCount)<br>{<br>    static uint32_t s_lastTimeCount = 0;<br>    uint32_t ret;<br>    if (curCount >= s_lastTimeCount) {<br>        ret = curCount - s_lastTimeCount;<br>    } else {<br>        ret = global_autoload - s_lastTimeCount + curCount;<br>    }<br>    s_lastTimeCount = curCount;<br>    return ret;<br>}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">void GPTIMER2_isr(void)<br>{<br>    \/\/\u8f93\u5165\u6ce2\u5f62\u89e6\u53d1\u7684\u4e2d\u65ad<br>    if (GPTIMER_IsActiveFlagCC1(GPTIMER2) == true)<br>    {<br>        uint32_t value = GPTIMER_IC_GetCapture(GPTIMER2, GPTIMER_CHANNEL_CH1);<br>        capture_VAL[capture_idx++] = getLastTimeInterval(value); \/\/\u8ba1\u7b97\u548c\u4e0a\u6b21\u4e4b\u95f4\u7684\u5dee\u503c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">        \/\/\u91c7\u96c620\u6b21\u5c31\u505c\u6389\uff0c\u5728\u540e\u8fb9\u6765\u5206\u6790<br>        if(capture_idx >= 20)<br>        {<br>            GPTIMER_DisableIntCC1(GPTIMER2);<br>            GPTIMER_DisableIntUpdate(GPTIMER2);<br><br>            capture_en = 1;<br>            capture_idx = 0;<br>        }<br><br>        GPTIMER_ClearFlagCC1(GPTIMER2);<br>    }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">    else if (GPTIMER_IsActiveFlagUpdate(GPTIMER2) == true)<br>    {<br>        \/\/\u5982\u679c\u8fde\u7eed\u8fdb\u5230\u8fd9\u91cc\uff0c\u5219\u662f\u8f93\u5165\u8109\u51b2\u505c\u6389\u4e86<br>        \/\/to do...<br>        testUpdateCnt++;<br>        GPTIMER_ClearFlagUpdate(GPTIMER2);<br>    }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/\/\u8ba1\u7b97\u6355\u83b7\u7684\u9891\u7387<br>int get_captureFrequency(void)<br>{<br>    uint32_t average, sum = 0;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">    if (capture_en == 1)<br>    {<br>        \/\/\u53d6\u4e2d\u95f4\u768410\u4e2a\uff0c\u8ba1\u7b97\u5e73\u5747\u503c<br>        for (int i = 5; i &lt; 15; i++)<br>        {<br>            sum += capture_VAL[i];<br>        }<br>        average = sum \/ 10;<br>        printf(\"Testing capture average = %d\\r\\n\", average);<br><br>        \/\/\u8f93\u5165pwm\u7684\u9891\u7387\uff0c\u4e3a timer\u7684\u4e3b\u9891 \/ clk\u8ba1\u6570<br>        uint32_t timerFreq, inFreq;<br>        timerFreq = SYS_GetPclkFreq() \/ 1;<br>        inFreq = timerFreq \/ average;<br><br>        \/\/\u91cd\u65b0\u5f00\u542f\u65b0\u7684\u91c7\u96c6<br>        capture_en = 0;<br>        GPTIMER_EnableIntCC1(GPTIMER2); <br>        GPTIMER_EnableIntUpdate(GPTIMER2);<br><br>        return inFreq;<br>    }<br>    return 0;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/\/\u4f8b\u7a0b\uff1a<br>\/\/ \u542f\u52a8\u4e00\u4e2atimer\u8f93\u5165\u6355\u83b7capture\u6765\u6355\u83b7\u4e0a\u5347\u6cbf\uff0c\u7136\u540e\u7528\u53e6\u4e00\u4e2atimer\u5f00\u542f\u4e00\u4e2apwm\u8f93\u51fa\u3002<br>\/\/ \u7136\u540e\u5c06\u8fd9\u4e24\u4e2apin\u811a\u77ed\u63a5\uff0c\u7528\u4ee5\u6d4b\u8bd5\u8fd9\u91cc\u7684\u6355\u83b7\u3002<br>\/\/VE\u91cc\u9700\u8981\u914d\u5957\u8bbe\u7f6e\uff1a<br>\/\/ GPTIMER4_CH0 PIN_85<br>\/\/ GPTIMER2_CH1 PIN_87:INPUT<br>\/\/<br>extern void TestGpTimerPwm();<br>void TestGpTimerCapture()<br>{<br>    SYS_EnableAPBClock(APB_MASK_GPTIMER2);<br>    GPTIMER_InitTypeDef tm_init;<br>    GPTIMER_StructInit(&amp;tm_init);<br>    tm_init.Autoreload = SYS_GetPclkFreq(); \/\/\u8fd9\u91cc\u914d\u7f6e\u7684\u6570\u503c\uff0c\u8981\u5927\u4e8e\u8f93\u5165\u6ce2\u5f62\u7684\u5468\u671f\u3002\u5728\u4e0d\u5206\u9891\u60c5\u51b5\u4e0b\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u6210sysClk\uff0c\u5c31\u662f1\u79d2\u6765\u4e00\u6b21update\u4e2d\u65ad<br>    tm_init.Prescaler = 0; \/\/10 - 1; \/\/\u5982\u679c\u8bbe\u7f6e10\u5206\u9891\uff0c\u662f\u8bbe\u7f6e\u6355\u83b7timer\u7684\u65f6\u949f\u4e3asysclk\u76841\/10<br>    GPTIMER_Init(GPTIMER2, &amp;tm_init);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">    global_autoload = tm_init.Autoreload; \/\/\u7528\u4e8e\u65f6\u95f4\u8ba1\u7b97\u91cc\u7684\u8ba1\u6570\u6ea2\u51fa<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">    GPTIMER_IC_InitTypeDef ic_init;<br>    ic_init.ICPolarity = GPTIMER_IC_POLARITY_FALLING; \/\/\u8f93\u5165\u901a\u9053\u89e6\u53d1\u65b9\u5f0f\uff1a\u4e0a\u5347\u6cbf\u3001\u4e0b\u964d\u6cbf\u3001\u53cc\u8fb9<br>    ic_init.ICMode = GPTIMER_ICMODE_DIRECTTI; \/\/\u6620\u5c04\u5230T1\u4e0a<br>    ic_init.ICPrescaler = GPTIMER_ICPSC_DIV1; \/\/\u914d\u7f6e\u8f93\u5165\u5206\u914d\u3002DIV1\u4e3a\u4e0d\u5206\u9891\u3002<br>    ic_init.ICFilter = GPTIMER_IC_FILTER_FDIV1; \/\/\u8bbe\u7f6e\u8f93\u5165\u6ee4\u6ce2\u5668,\u9632\u6296\u52a8\u3002FDIV1\u4e3a\u4e0d\u6ee4\u6ce2<br>    GPTIMER_IC_Init(GPTIMER2, GPTIMER_CHANNEL_CH1, &amp;ic_init);<br>    GPIO_AF_ENABLE(GPTIMER2_CH1); \/\/\u8f93\u5165IO<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">    INT_EnableIRQ(GPTIMER2_IRQn, TIMER_PRIORITY); \/\/\u6253\u5f00\u4e2d\u65ad<br>    GPTIMER_EnableIntUpdate(GPTIMER2); \/\/\u4f7f\u80fd\u66f4\u65b0\u4e2d\u65ad\u3002\u5982\u679c\u4e2d\u65ad\u4e2d\u6765\u7684\u662f\u66f4\u65b0\u4e2d\u65ad\uff08\u800c\u4e0d\u662f\u6355\u83b7\u4e2d\u65ad\uff09\uff0c\u8bf4\u660e\u8109\u51b2\u592a\u5bbd\u4e86\u3002<br>    GPTIMER_EnableIntCC1(GPTIMER2); \/\/\u4f7f\u80fd\u6355\u83b7\u4e2d\u65ad<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">    GPTIMER_EnableCounter(GPTIMER2);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">    \/\/test<br>    TestGpTimerPwm(); \/\/\u5f00\u542f\u53e6\u4e00\u4e2a\u5f15\u811a\u7684pwm\u8f93\u51fa\u3002\u5c06\u4e24\u5f15\u811a\u77ed\u63a5\uff0cpwm\u7684\u8f93\u51fa\u4f5c\u4e3a\u8fd9\u91cc\u7684\u8f93\u5165\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">    int ret = 0;<br>    while (1)<br>    {<br>        UTIL_IdleUs(1000e3);<br>        ret = get_captureFrequency();<br>        printf(\"calc freq = %d, testUpdateCnt = %d\\r\\n\", ret, testUpdateCnt);<br>    }<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/example_gptimer_capture.c include \"example.h\" static  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","footnotes":""},"class_list":["post-847","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"http:\/\/www.agm-support.com.cn\/index.php\/wp-json\/wp\/v2\/pages\/847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.agm-support.com.cn\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.agm-support.com.cn\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.agm-support.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.agm-support.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=847"}],"version-history":[{"count":2,"href":"http:\/\/www.agm-support.com.cn\/index.php\/wp-json\/wp\/v2\/pages\/847\/revisions"}],"predecessor-version":[{"id":849,"href":"http:\/\/www.agm-support.com.cn\/index.php\/wp-json\/wp\/v2\/pages\/847\/revisions\/849"}],"up":[{"embeddable":true,"href":"http:\/\/www.agm-support.com.cn\/index.php\/wp-json\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"http:\/\/www.agm-support.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}