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.

PWM module using User Input

Other Parts Discussed in Thread: EK-TM4C129EXL

Hello, 

I'm writing a PWM program that allows users to type in a value into the terminal that can change the duty cycle and frequency.  I've been using the Tiva C series manual and also some code my professor left as references.  It's been a couple of days and still no headway, so any help at all would be greatly appreciated.

Here is my code so far:  

#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/pwm.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"

#define CONSOLE_BUF_SIZE 128
#define PWM_FREQUENCY 999 //default PWM frequency

char dutyInput[CONSOLE_BUF_SIZE]; //read user input
char freqInput[CONSOLE_BUF_SIZE];

//int PWMfactor = 9.99;   //default PWM factor


void
InitConsole(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTStdioConfig(0, 9600, SysCtlClockGet()); } /* * initializing the PWM */ void pwmConsole(){ SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); SysCtlPWMClockSet(SYSCTL_PWMDIV_1); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); GPIOPinConfigure(GPIO_PF3_M1PWM7); GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_3); PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN); PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, 320); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,100); PWMGenEnable(PWM1_BASE, PWM_GEN_3);
PWMOutputState(PWM1_BASE, PWM_OUT_7_BIT , true); } /* * Changes Duty Cycle. Needs to be fixed. Only changes it once * to make it brighter. */ int CHANGEDUTYCYCLE(char **argv){ int dutyCycle = atoi(argv[1]); PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0,dutyCycle); return 0; } /* * Changes Frequency. Needs to be fixed, nothing was changed. */ int CHANGEFREQUENCY(char **argv){ int frequency = atoi(argv[1]); PWMGenPeriodSet(PWM1_BASE, PWM_GEN_0, frequency); //PWMfactor = frequency/100; return 0; } /* * main.c */ int main(void) { SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); InitConsole(); pwmConsole(); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2); while(1){ UARTprintf("Type in the PWM Duty Cycle: \n"); UARTgets(dutyInput,sizeof(dutyInput)); UARTprintf("\n"); UARTprintf("Type in the PWM Frequency: \n"); UARTgets(freqInput,sizeof(freqInput)); CHANGEDUTYCYCLE(dutyInput); CHANGEFREQUENCY(freqInput); } //return 0; }

  • Hello Brittany

    Time to debug. If you put a breakpoint in the functions then does the duty cycle and frequency variables in the function reflect the correct value as passed by the argument?
  • Brittany Ko said:
     It's been a couple of days and still no headway

    None - nothing at all?

    Most often the forum is blessed w/"Does NOT Work!"   (ascribes blame to the vendor, forum, helpers...)

    No headway conveys NO MEANINGFUL DATA!  

    Should not you "Divide & Conquer?"   Your program has (at least) two main sections: Console Data Input and MCU PWM Control.   Running both - together - is not the best way to proceed.   Get one to work - only then switch to the second. (again by itself)   Only when both work independently should you attempt to "join them."

    Think (always) "KISS."    A sequence of small, manageable & measurable steps, is your best path to program/project success!

  • hello sorry for the last response, but the value being passed isn't correct (it's always zero). I'm using UARTget() to store the value typed from the terminal, but it's not working or perhaps I just don't know how to use it? If there is any tutorial or suggestions, anything would be a huge help.
  • sorry for the late response. But I have taken your suggestion and have tested them both separately from each other. I am still having troubles with using UART, specifically storing an integer from the terminal. If you have any suggestions or tutorials. Any help is appreciated, thank you.
  • This should be fairly basic C. Show us how you are reading from the UART, maybe we can ask some questions to point you in the right direction.

    Robert
  • Hello, I've been using UARTget(char,charlength) to read in from the terminal. Then I convert the char to int using atoi(). for ex: int num = atoi(argv[1]);
  • Hello Brittany

    To use command line entry, I would suggest using the cmdline.c/.h for processing. It will take out a lot of the complexity of the argument based processing. An example for the same would be under the following example.

    D:\ti\TivaWare_C_Series-2.1.3.156\examples\boards\ek-tm4c129exl\aes_example
  • Brittany Ko said:
    I have taken your suggestion and have tested them both separately from each other.

    Good that - yet you provide no reportage of your, "PWM module control results."   

    You've past reported the crystalline, "No Headway" and that itself identifies a weakness in your approach.  "No Headway" should be resisted w/all of your strength - it's both demoralizing & reveals lack of (considered) thought.   Cannot you strive - when (momentarily) blocked - to make "Some Headway?"

    We all "hit walls" - most always then a shift to other aspects of your program enables your escape from, "No Headway!"   You must have fellow students, or TAs, or work colleagues - someone who can assist.   Suffering by oneself proves an infrequent, "Road to success."

    You'll require a scope or logic analyzer to confirm PWM Duty Cycle variance to your spec - have you arranged to have these, "at the ready?"   While waiting for the, "Uart cavalry" you should shift focus to "part deux" and progress toward your duty cycle requirement.   (i.e. Realize SOME Headway...)

  • Needs more detail, which is why I asked you to show what you were doing. For instance from what you've shown I suspect that this

    Brittany Ko said:
    num = atoi(argv[1]);

    is wrong.

    Robert

    BTW, you can test this at the console prompt in Windows/linux.