Author Topic: ch32v003 how to change TIM1 PWM ouput pin  (Read 1030 times)

0 Members and 1 Guest are viewing this topic.

Offline nt_ptrTopic starter

  • Newbie
  • Posts: 3
  • Country: th
ch32v003 how to change TIM1 PWM ouput pin
« on: January 07, 2024, 07:22:38 am »
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 me  :)

Code: [Select]
/********************************** (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;

#endif


    TIM_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)
    {


    }
}



 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14510
  • Country: fr
Re: ch32v003 how to change TIM1 PWM ouput pin
« Reply #1 on: January 07, 2024, 07:50:41 am »
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.
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5933
  • Country: es
Re: ch32v003 how to change TIM1 PWM ouput pin
« Reply #2 on: January 07, 2024, 04:47:28 pm »
Same as STM32, because it's the CHN pin, you must use the N settings:
Code: [Select]
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;

Always read the library headers, will give a lot of hints:
Code: [Select]
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;
« Last Edit: January 07, 2024, 04:52:18 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: paf

Offline nt_ptrTopic starter

  • Newbie
  • Posts: 3
  • Country: th
Re: ch32v003 how to change TIM1 PWM ouput pin
« Reply #3 on: January 08, 2024, 08:58:01 am »
Thank you very much  DavidAlfa  :) It's working well.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf