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;
}