This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

basic pwm



i need help, i have to control the position dc motor with encoder, i can not find a code with basic use to pwm to tivac tm4c123g6pm i used code composer to programming

  • If you download the TivaWare drivers, you will find some examples for the PWM in:
    C:\ti\TivaWare_C_Series-2.1.4.178\examples\peripherals\pwm

  • i try first to control the speed,

    this is my code

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/gpio.h"
    #include "driverlib/debug.h"
    #include "driverlib/pwm.h"
    #include "driverlib/pin_map.h"
    #include "inc/hw_gpio.h"
    #include "driverlib/rom.h"
    #include "driverlib/adc.h"

    #define PWM_FREQUENCY 55

    int main(void)
    {
        volatile uint32_t ui32Load;
        volatile uint32_t ui32PWMClock;
        uint32_t ui32ADC0Value[1];
        volatile uint8_t ui8Adjust;

        SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
        SysCtlPWMClockSet(SYSCTL_PWMDIV_64);



        SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);

        GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_0);
        GPIOPinConfigure(GPIO_PD0_M1PWM0);



        ui32PWMClock = SysCtlClockGet() / 64;
        ui32Load = (ui32PWMClock / PWM_FREQUENCY) - 1;
        PWMGenConfigure(PWM1_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN);

        PWMGenPeriodSet(PWM1_BASE, PWM_GEN_0,ui32Load );


        PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT, true);
        PWMGenEnable(PWM1_BASE, PWM_GEN_0);




        SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
        GPIOPinTypeADC(GPIO_PORTE_BASE,GPIO_PIN_3);
        ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
        ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE |ADC_CTL_END);

        ADCSequenceEnable(ADC0_BASE, 3);

        while(1)
        {


            ADCIntClear(ADC0_BASE, 3);
            ADCProcessorTrigger(ADC0_BASE, 3);

              while(!ADCIntStatus(ADC0_BASE, 3, false))
              {
              }


              ADCSequenceDataGet(ADC0_BASE, 3, ui32ADC0Value); // Watch on ui32ADC0Value
              ui8Adjust=ui32ADC0Value[0];


               PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, ui8Adjust * ui32Load / 500);


               SysCtlDelay(100000);
         }


        }



    but i think the problem in this line PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, ui8Adjust * ui32Load / 500) the motor change the speed but not normal

  • Ignoring the motor, is the PWM signal the correct frequency and pulse width?
  • i dont know, how can i calculate the parameters

  • Perhaps we should start at the beginning. What type of motor are you trying to control? What is the frequency of the PWM signal you want? How do you determine the duty cycle you want?
  • i am using a dc motor, the frequency i dont know, i only want to control speed of motor for example

    PWM1_Init(5000);

    PWM1_Start();

    while(1)
    {
    pot=adc_read(0)>>2;
    PWM1_Set_Duty(pot);
    delay_ms(30);
    }

    this is what I want to do
  • I have attached a CCS7.3 project that reads the value on AIN0 and outputs a PWM on M0PWM2 at 40KHz..

    /cfs-file/__key/communityserver-discussions-components-files/81/Forum646554.zip