/********************************** (C) COPYRIGHT *******************************
* File Name : ch32f10x_tim.c
* Author : WCH
* Version : V1.0.0
* Date : 2019/10/15
* Description : This file provides all the TIM firmware functions.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "ch32f10x_tim.h"
#include "ch32f10x_rcc.h"
/* TIM registers bit mask */
#define SMCFGR_ETR_Mask ((uint16_t)0x00FF)
#define CHCTLR_Offset ((uint16_t)0x0018)
#define CCER_CCE_Set ((uint16_t)0x0001)
#define CCER_CCNE_Set ((uint16_t)0x0004)
static void TI1_Config( TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
uint16_t TIM_ICFilter );
static void TI2_Config( TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
uint16_t TIM_ICFilter );
static void TI3_Config( TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
uint16_t TIM_ICFilter );
static void TI4_Config( TIM_TypeDef *TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
uint16_t TIM_ICFilter );
/*********************************************************************
* @fn TIM_DeInit
*
* @brief Deinitializes the TIMx peripheral registers to their default
* reset values.
*
* @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
*
* @return none
*/
void TIM_DeInit( TIM_TypeDef *TIMx )
{
if( TIMx == TIM1 )
{
RCC_APB2PeriphResetCmd( RCC_APB2Periph_TIM1, ENABLE );
RCC_APB2PeriphResetCmd( RCC_APB2Periph_TIM1, DISABLE );
}
else if( TIMx == TIM2 )
{
RCC_APB1PeriphResetCmd( RCC_APB1Periph_TIM2, ENABLE );
RCC_APB1PeriphResetCmd( RCC_APB1Periph_TIM2, DISABLE );
}
else if( TIMx == TIM3 )
{
RCC_APB1PeriphResetCmd( RCC_APB1Periph_TIM3, ENABLE );
RCC_APB1PeriphResetCmd( RCC_APB1Periph_TIM3, DISABLE );
}
else if( TIMx == TIM4 )
{
RCC_APB1PeriphResetCmd( RCC_APB1Periph_TIM4, ENABLE );
RCC_APB1PeriphResetCmd( RCC_APB1Periph_TIM4, DISABLE );
}
}
/*********************************************************************
* @fn TIM_TimeBaseInit
*
* @brief Initializes the TIMx Time Base Unit peripheral according to
* the specified parameters in the TIM_TimeBaseInitStruct.
*
* @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
* TIM_TimeBaseInitStruct - pointer to a TIM_TimeBaseInitTypeDef
* structure.
*
* @return none
*/
void TIM_TimeBaseInit( TIM_TypeDef *TIMx, TIM_TimeBaseInitTypeDef *TIM_TimeBaseInitStruct )
{
uint16_t tmpcr1 = 0;
tmpcr1 = TIMx->CTLR1;
if( ( TIMx == TIM1 ) || ( TIMx == TIM2 ) || ( TIMx == TIM3 ) || ( TIMx == TIM4 ) )
{
tmpcr1 &= ( uint16_t )( ~( ( uint16_t )( TIM_DIR | TIM_CMS ) ) );
tmpcr1 |= ( uint32_t )TIM_TimeBaseInitStruct->TIM_CounterMode;
}
tmpcr1 &= ( uint16_t )( ~( ( uint16_t )TIM_CTLR1_CKD ) );
tmpcr1 |= ( uint32_t )TIM_TimeBaseInitStruct->TIM_ClockDivision;
TIMx->CTLR1 = tmpcr1;
TIMx->ATRLR = TIM_TimeBaseInitStruct->TIM_Period ;
TIMx->PSC = TIM_TimeBaseInitStruct->TIM_Prescaler;
if( TIMx == TIM1 )
{
TIMx->RPTCR = TIM_TimeBaseInitStruct->TIM_RepetitionCounter;
}
TIMx->SWEVGR = TIM_PSCReloadMode_Immediate;
}
/*********************************************************************
* @fn TIM_OC1Init
*
* @brief Initializes the TIMx Channel1 according to the specified
* parameters in the TIM_OCInitStruct.
*
* @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
* TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
*
* @return none
*/
void TIM_OC1Init( TIM_TypeDef *TIMx, TIM_OCInitTypeDef *TIM_OCInitStruct )
{
uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
TIMx->CCER &= ( uint16_t )( ~( uint16_t )TIM_CC1E );
tmpccer = TIMx->CCER;
tmpcr2 = TIMx->CTLR2;
tmpccmrx = TIMx->CHCTLR1;
tmpccmrx &= ( uint16_t )( ~( ( uint16_t )TIM_OC1M ) );
tmpccmrx &= ( uint16_t )( ~( ( uint16_t )TIM_CC1S ) );
tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
tmpccer &= ( uint16_t )( ~( ( uint16_t )TIM_CC1P ) );
tmpccer |= TIM_OCInitStruct->TIM_OCPolarity;
tmpccer |= TIM_OCInitStruct->TIM_OutputState;
if( TIMx == TIM1 )
{
tmpccer &= ( uint16_t )( ~( ( uint16_t )TIM_CC1NP ) );
tmpccer |= TIM_OCInitStruct->TIM_OCNPolarity;
tmpccer &= ( uint16_t )( ~( ( uint16_t )TIM_CC1NE ) );
tmpccer |= TIM_OCInitStruct->TIM_OutputNState;
tmpcr2 &= ( uint16_t )( ~( ( uint16_t )TIM_OIS1 ) );
tmpcr2 &= ( uint16_t )( ~( ( uint16_t )TIM_OIS1N ) );
tmpcr2 |= TIM_OCInitStruct->TIM_OCIdleState;
tmpcr2 |= TIM_OCInitStruct->TIM_OCNIdleState;
}
TIMx->CTLR2 = tmpcr2;
TIMx->CHCTLR1 = tmpccmrx;
TIMx->CH1CVR = TIM_OCInitStruct->TIM_Pulse;
TIMx->CCER = tmpccer;
}
/*********************************************************************
* @fn TIM_OC2Init
*
* @brief Initializes the TIMx Channel2 according to the specified
* parameters in the TIM_OCInitStruct.
*
* @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
* TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
*
* @return none
*/
void TIM_OC2Init( TIM_TypeDef *TIMx, TIM_OCInitTypeDef *TIM_OCInitStruct )
{
uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
TIMx->CCER &= ( uint16_t )( ~( ( uint16_t )TIM_CC2E ) );
tmpccer = TIMx->CCER;
tmpcr2 = TIMx->CTLR2;
tmpccmrx = TIMx->CHCTLR1;
tmpccmrx &= ( uint16_t )( ~( ( uint16_t )TIM_OC2M ) );
tmpccmrx &= ( uint16_t )( ~( ( uint16_t )TIM_CC2S ) );
tmpccmrx |= ( uint16_t )( TIM_OCInitStruct->TIM_OCMode << 8 );
tmpccer &= ( uint16_t )( ~( ( uint16_t )TIM_CC2P ) );
tmpccer |= ( uint16_t )( TIM_OCInitStruct->TIM_OCPolarity << 4 );
tmpccer |= ( uint16_t )( TIM_OCInitStruct->TIM_OutputState << 4 );
if( TIMx == TIM1 )
{
tmpccer &= ( uint16_t )( ~( ( uint16_t )TIM_CC2NP ) );
tmpccer |= ( uint16_t )( TIM_OCInitStruct->TIM_OCNPolarity << 4 );
tmpccer &= ( uint16_t )( ~( ( uint16_t )TIM_CC2NE ) );
tmpccer |= ( uint16_t )( TIM_OCInitStruct->TIM_OutputNState << 4 );
tmpcr2 &= ( uint16_t )( ~( ( uint16_t )TIM_OIS2 ) );
tmpcr2 &= ( uint16_t )( ~( ( uint16_t )TIM_OIS2N ) );
tmpcr2 |= ( uint16_t )( TIM_OCInitStruct->TIM_OCIdleState << 2 );
tmpcr2 |= ( uint16_t )( TIM_OCInitStruct->TIM_OCNIdleState << 2 );
}
TIMx->CTLR2 = tmpcr2;
TIMx->CHCTLR1 = tmpccmrx;
TIMx->CH2CVR = TIM_OCInitStruct->TIM_Pulse;
TIMx->CCER = tmpccer;
}
/*********************************************************************
* @fn TIM_OC3Init
*
* @brief Initializes the TIMx Channel3 according to the specified
* parameters in the TIM_OCInitStruct.
*
* @param TIMx - where x can be 1 to 4 to select the TIM peripheral.
* TIM_OCInitStruct - pointer to a TIM_OCInitTypeDef structure.
*
* @return none
*/
void TIM_OC3Init( TIM_TypeDef *TIMx, TIM_OCInitTypeDef *TIM_OCInitStruct )
{
uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
TIMx->CCER &= ( uint16_t )( ~( ( uint16_t )TIM_CC3E ) );
tmpccer = TIMx->CCER;
tmpcr2 = TIMx->CTLR
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
CH32F103C8T6+BMP280+0.96OLED (168个子文件)
CH32F10x.axf 263KB
ch32f10x_tim.c 82KB
ch32f10x_adc.c 37KB
ch32f10x_can.c 34KB
ch32f10x_i2c.c 32KB
ch32f10x_flash.c 29KB
ch32f10x_rcc.c 29KB
ch32f10x_usart.c 27KB
ch32f10x_dma.c 23KB
ch32f10x_usb_host.c 23KB
ch32f10x_spi.c 21KB
system_ch32f10x.c 19KB
ch32f10x_gpio.c 18KB
core_cm3.c 17KB
OLED_Font.c 15KB
OLED.c 14KB
ch32f10x_dac.c 8KB
ch32f10x_rtc.c 8KB
ch32f10x_bkp.c 7KB
ch32f10x_pwr.c 7KB
bmp280.c 7KB
myiic.c 7KB
ch32f10x_misc.c 5KB
ch32f10x_exti.c 5KB
debug.c 5KB
ch32f10x_wwdg.c 4KB
ch32f10x_dbgmcu.c 4KB
ch32f10x_iwdg.c 4KB
ch32f10x_usb.c 4KB
ch32f10x_crc.c 3KB
ch32f10x_it.c 3KB
Main.c 2KB
oled.crf 292KB
ch32f10x_tim.crf 288KB
bmp280.crf 287KB
lcd.crf 285KB
main.crf 279KB
myiic.crf 278KB
ch32f10x_flash.crf 278KB
ch32f10x_can.crf 278KB
ch32f10x_adc.crf 276KB
ch32f10x_rcc.crf 276KB
debug.crf 275KB
ch32f10x_i2c.crf 275KB
spi.crf 274KB
ch32f10x_usart.crf 274KB
ch32f10x_spi.crf 273KB
ch32f10x_gpio.crf 272KB
ch32f10x_dma.crf 272KB
system_ch32f10x.crf 270KB
ch32f10x_dac.crf 270KB
ch32f10x_rtc.crf 270KB
ch32f10x_bkp.crf 270KB
ch32f10x_pwr.crf 270KB
ch32f10x_exti.crf 270KB
ch32f10x_wwdg.crf 269KB
ch32f10x_misc.crf 269KB
ch32f10x_iwdg.crf 269KB
ch32f10x_crc.crf 269KB
ch32f10x_it.crf 269KB
ch32f10x_dbgmcu.crf 269KB
core_cm3.crf 4KB
oled_font.crf 118B
ch32f10x_dbgmcu.d 2KB
ch32f10x_usart.d 2KB
ch32f10x_flash.d 2KB
ch32f10x_exti.d 2KB
ch32f10x_gpio.d 2KB
ch32f10x_misc.d 2KB
ch32f10x_wwdg.d 2KB
ch32f10x_iwdg.d 2KB
bmp280.d 2KB
ch32f10x_rcc.d 2KB
ch32f10x_tim.d 2KB
ch32f10x_spi.d 2KB
ch32f10x_dac.d 2KB
ch32f10x_adc.d 2KB
ch32f10x_can.d 2KB
ch32f10x_dma.d 2KB
ch32f10x_i2c.d 2KB
ch32f10x_pwr.d 2KB
ch32f10x_crc.d 2KB
ch32f10x_bkp.d 2KB
ch32f10x_rtc.d 2KB
system_ch32f10x.d 2KB
oled.d 2KB
ch32f10x_it.d 2KB
main.d 2KB
lcd.d 2KB
myiic.d 2KB
debug.d 2KB
spi.d 2KB
core_cm3.d 108B
startup_ch32f10x.d 64B
oled_font.d 32B
CH32F10x_CH32F103R8_1.0.0.dbgconf 7KB
CH32F10x_CH32F10x.dep 53KB
ch32f10x.h 425KB
core_cm3.h 84KB
ch32f10x_usb.h 30KB
共 168 条
- 1
- 2
资源评论
单片机菜鸟、
- 粉丝: 952
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功