ch32v003  how to change TIM1 PWM ouput pin - Page 1
Forum section: electronics / general

author: nt_ptr
author_rank: Newbie
timestamp: January 07, 2024, 07:22:38 am
content: Hello everyone, I'm a newbie with ch32v and MounRiver as well. First project, I tried to get PWM with PD2 pin. It works, but I want to change PWM pin to PD0 and looking for different ways. on the internet But I haven't found a way to do it. Please guide me. I thought it must be easy but I couldn't do it.please help meCode:/********************************** (C) COPYRIGHT ******************************** File Name : main.c* Author : WCH* Version : V1.0.0* Date : 2022/08/08* Description : Main program body.********************************************************************************** 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.*******************************************************************************//**@Note*Multiprocessor communication mode routine:*Master:USART1_Tx(PD5)\USART1_Rx(PD6).*This routine demonstrates that USART1 receives the data sent by CH341 and inverts*it and sends it (baud rate 115200).**Hardware connection:PD5 -- Rx* PD6 -- Tx**/#include "debug.h"/* Global define *//* Global Variable */vu8 val;/* PWM Output Mode Definition */#define PWM_MODE1 0#define PWM_MODE2 1/* PWM Output Mode Selection */#define PWM_MODE PWM_MODE1//#define PWM_MODE PWM_MODE2/********************************************************************** @fn TIM1_OutCompare_Init** @brief Initializes TIM1 output compare.** @param arr - the period value.* psc - the prescaler value.* ccp - the pulse value.** @return none*/void TIM1_PWMOut_Init(u16 arr, u16 psc, u16 ccp){GPIO_InitTypeDef GPIO_InitStructure={0};TIM_OCInitTypeDef TIM_OCInitStructure={0};TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD | RCC_APB2Periph_TIM1 , ENABLE );GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;GPIO_Init( GPIOD, &GPIO_InitStructure );TIM_TimeBaseInitStructure.TIM_Period = arr;TIM_TimeBaseInitStructure.TIM_Prescaler = psc;TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);#if (PWM_MODE == PWM_MODE1)TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;#elif (PWM_MODE == PWM_MODE2)TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;#endifTIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_Pulse = ccp;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;TIM_OC1Init( TIM1, &TIM_OCInitStructure );TIM_CtrlPWMOutputs(TIM1, ENABLE );TIM_OC1PreloadConfig( TIM1, TIM_OCPreload_Disable );TIM_ARRPreloadConfig( TIM1, ENABLE );TIM_Cmd( TIM1, ENABLE );}/********************************************************************** @fn main** @brief Main program.** @return none*/int main(void){SystemCoreClockUpdate();Delay_Ms(10);TIM1_PWMOut_Init( 255, 12-1, 100);while(1){}}

author: SiliconWizard
author_rank: Super Contributor
timestamp: January 07, 2024, 07:50:41 am
content: At first sight, what seems to be happening is that PD0's alternate function is the "negative" output of the PWM complementary output mode, while PD2 is the "positive" output.I'm not very familiar with this chip, but it may not be possible to only enable the negative output of a PWM channel - it may be only the positive one, or both. Just a guess though, but something I'd definitely consider looking at.

author: DavidAlfa
author_rank: Super Contributor
timestamp: January 07, 2024, 04:47:28 pm
content: Same as STM32, because it's the CHNpin, you must use theNsettings:Code:TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;Always read thelibrary headers, will give a lot of hints:Code:typedef struct{uint16_t TIM_OCMode; /* Specifies the TIM mode.This parameter can be a value of @ref TIM_Output_Compare_and_PWM_modes */uint16_t TIM_OutputState; /* Specifies the TIM Output Compare state.This parameter can be a value of @ref TIM_Output_Compare_state */uint16_t TIM_OutputNState; /* Specifies the TIM complementary Output Compare state.This parameter can be a value of @ref TIM_Output_Compare_N_state@note This parameter is valid only for TIM1 and TIM8. */uint16_t TIM_Pulse; /* Specifies the pulse value to be loaded into the Capture Compare Register.This parameter can be a number between 0x0000 and 0xFFFF */uint16_t TIM_OCPolarity; /* Specifies the output polarity.This parameter can be a value of @ref TIM_Output_Compare_Polarity */uint16_t TIM_OCNPolarity; /* Specifies the complementary output polarity.This parameter can be a value of @ref TIM_Output_Compare_N_Polarity@note This parameter is valid only for TIM1 and TIM8. */uint16_t TIM_OCIdleState; /* Specifies the TIM Output Compare pin state during Idle state.This parameter can be a value of @ref TIM_Output_Compare_Idle_State@note This parameter is valid only for TIM1 and TIM8. */uint16_t TIM_OCNIdleState; /* Specifies the TIM Output Compare pin state during Idle state.This parameter can be a value of @ref TIM_Output_Compare_N_Idle_State@note This parameter is valid only for TIM1 and TIM8. */} TIM_OCInitTypeDef;

author: nt_ptr
author_rank: Newbie
timestamp: January 08, 2024, 08:58:01 am
content: Thank you very much DavidAlfaIt's working well.