打印
[技术问答]

小华HC32L110定时器

[复制链接]
178|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ewrwer|  楼主 | 2025-4-23 10:08 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 ewrwer 于 2025-4-23 10:18 编辑

#技术资源# 使用了官方demo,修改了定时器的代码,系统时钟是4MHZ,分频4,使用的重装载值是999,理论上讲1ms中断一次,但是我这边用分析仪测试的是64ms中断一次,不知道是啥原因引起的
/******************************************************************************
* Copyright (C) 2016, Xiaohua Semiconductor Co.,Ltd All rights reserved.
*
* This software is owned and published by:
* Xiaohua Semiconductor Co.,Ltd ("XHSC").
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with XHSC
* components. This software is licensed by XHSC to be adapted only
* for use in systems utilizing XHSC components. XHSC shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. XHSC is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* Disclaimer:
* XHSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
* ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
* WARRANTY OF NONINFRINGEMENT.
* XHSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
* NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
* LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
* INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
* SAVINGS OR PROFITS,
* EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
* INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
* FROM, THE SOFTWARE.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*/
/******************************************************************************/
/** \file main.c
**
** A detailed description is available at
** @link Sample Group Some description @endlink
**
**   - 2016-02-16  1.0  XYZ First version for Device Driver Library of Module.
**
******************************************************************************/

/******************************************************************************
* Include files
******************************************************************************/
#include "ddl.h"
#include "bt.h"
#include "lpm.h"
#include "gpio.h"

/******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/

/******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/

/*******************************************************************************
* Local variable definitions ('static')
******************************************************************************/
static volatile uint32_t u32BtTestFlag = 0;
static volatile uint32_t u32Cnt = 0;

/*******************************************************************************
* Local function prototypes ('static')
******************************************************************************/


/*******************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/

/*******************************************************************************
* BT1中断服务函数
******************************************************************************/
void Bt1Int(void)
{
    if (TRUE == Bt_GetIntFlag(TIM1))
    {
        Bt_ClearIntFlag(TIM1);
        //u32BtTestFlag = 0x02;
                                Gpio_SetIO(1, 4, !Gpio_GetIO(1, 4));
                                Gpio_SetIO(0, 3, !Gpio_GetIO(0, 3));
    }
}

/*******************************************************************************
* BT定时功能测试 (重载模式)
******************************************************************************/
en_result_t BtTimerTest(void)
{
    stc_bt_config_t   stcConfig;
    en_result_t       enResult = Error;
    uint16_t          u16ArrData = 999;
    uint16_t          u16InitCntData = 0;
   
    stcConfig.pfnTim1Cb = Bt1Int;
    //P25设置为门控使能IO
    Gpio_SetFunc_TIM1_GATE_P25();
        
    stcConfig.enGateP = BtPositive;
    stcConfig.enGate  = BtGateDisable;
    stcConfig.enPRS   = BtPCLKDiv4;
    stcConfig.enTog   = BtTogDisable;
    stcConfig.enCT    = BtTimer;
    stcConfig.enMD    = BtMode2;
    //Bt初始化
    if (Ok != Bt_Init(TIM1, &stcConfig))
    {
        enResult = Error;
    }
   
    //TIM1中断使能
    Bt_ClearIntFlag(TIM1);
    Bt_EnableIrq(TIM1);
    EnableNvic(TIM1_IRQn, 3, TRUE);
   
    //设置重载值和计数值,启动计数
    Bt_ARRSet(TIM1, u16ArrData);
    Bt_Cnt16Set(TIM1, u16InitCntData);
    Bt_Run(TIM1);
   
    //此处进入中断……
    while(1)
    {
        //判断是否第二次进入中断
        if (0x02 == u32BtTestFlag)
        {
            u32BtTestFlag = u32BtTestFlag & (~0x02);
            if (1 == u32Cnt)
            {
                Bt_Stop(TIM1);
                enResult = Ok;
                break;
            }
            u32Cnt++;
        }
    }   
   
    return enResult;
}


/**
******************************************************************************
** \brief  Main function of project
**
** \return uint32_t return value, if needed
**
** This sample
**
******************************************************************************/

int32_t main(void)
{
    volatile uint8_t u8TestFlag = 0;
   
    //CLK INIT
    stc_clk_config_t stcClkCfg;
                Clk_SetPeripheralGate(ClkPeripheralGpio, TRUE);
    stcClkCfg.enClkSrc  = ClkRCH;
    stcClkCfg.enHClkDiv = ClkDiv1;
    stcClkCfg.enPClkDiv = ClkDiv1;
                Gpio_SetFunc_HCLKOUT_P24();
    Clk_Init(&stcClkCfg);
   
    //打开GPIO、BT外设时钟
    Clk_SetPeripheralGate(ClkPeripheralGpio, TRUE);
    Clk_SetPeripheralGate(ClkPeripheralBt, TRUE);
                Gpio_InitIO(1,4 ,GpioDirOut );
                Gpio_InitIO(0,3 ,GpioDirOut );
    //BtTimerTest();  
    if(Ok != BtTimerTest())
    {
        u8TestFlag |= 0x02;
    }
     
    while (1);
}

/******************************************************************************
* EOF (not truncated)
******************************************************************************/

442068084bde614cb.png (27.43 KB )

442068084bde614cb.png

9525768084bc5a34cf.png (18.77 KB )

9525768084bc5a34cf.png

使用特权

评论回复
沙发
Amazingxixixi| | 2025-4-24 15:08 | 只看该作者
学习一下啊!感觉有点知识慌了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

2

帖子

0

粉丝