change PWM period with buttons

This commit is contained in:
Killorin 2025-04-24 12:07:27 -04:00
parent 1a1d72be7e
commit 081baecac9
3 changed files with 52 additions and 28 deletions

View file

@ -9,9 +9,43 @@
#include "driverlib/interrupt.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/fpu.h"
#include <math.h>
#include "driverlib/pwm.h"
#include "frequency.h"
// start a pwm test signal
void start_signal() {
// configure M0PWM2, at GPIO PF2, BoosterPack 1 header C1 pin 2
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);
GPIOPinConfigure(GPIO_PF2_M0PWM2);
GPIOPinConfigure(GPIO_PF3_M0PWM3);
GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3, GPIO_STRENGTH_2MA,
GPIO_PIN_TYPE_STD);
// configure the PWM0 peripheral, gen 1, outputs 2 and 3
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
// use system clock without division
PWMClockSet(PWM0_BASE, PWM_SYSCLK_DIV_1);
PWMGenConfigure(PWM0_BASE, PWM_GEN_1,
PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
gPWMPeriod = roundf((float)gSystemClock / PWM_FREQUENCY);
set_pwm_period(gPWMPeriod);
PWMOutputState(PWM0_BASE, PWM_OUT_2_BIT | PWM_OUT_3_BIT, true);
PWMGenEnable(PWM0_BASE, PWM_GEN_1);
}
void set_pwm_period(uint32_t period) {
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1,
gPWMPeriod);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2,
roundf((float)gPWMPeriod * 0.4f));
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3,
roundf((float)gPWMPeriod * 0.4f));
}
void start_frequency_scan() {
// config GPIO PD0 as timer input T0CCP0 at BoosterPack Connector #1 pin 14

View file

@ -1,6 +1,12 @@
#ifndef FREQUENCY_C
#define FREQUENCY_C
#define gSystemClock 120000000 // [Hz] system clock frequency
#define PWM_FREQUENCY 20000 // PWM frequency = 20 kHz
void start_frequency_scan(void);
void set_pwm_period(uint32_t period);
uint32_t gPeriod;
uint32_t gPWMPeriod;
#endif

40
main.c
View file

@ -35,9 +35,7 @@
#include "kiss_fft.h"
#include "_kiss_fft_guts.h"
#define PWM_FREQUENCY 20000 // PWM frequency = 20 kHz
uint32_t gSystemClock = 120000000; // [Hz] system clock frequency
#define PI 3.14159265358979f
@ -93,30 +91,7 @@ const float gVoltageScale[VOLTAGE_SCALES] = {
int Trigger(bool rising);
// start a pwm test signal
void start_signal() {
// configure M0PWM2, at GPIO PF2, BoosterPack 1 header C1 pin 2
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);
GPIOPinConfigure(GPIO_PF2_M0PWM2);
GPIOPinConfigure(GPIO_PF3_M0PWM3);
GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3, GPIO_STRENGTH_2MA,
GPIO_PIN_TYPE_STD);
// configure the PWM0 peripheral, gen 1, outputs 2 and 3
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
// use system clock without division
PWMClockSet(PWM0_BASE, PWM_SYSCLK_DIV_1);
PWMGenConfigure(PWM0_BASE, PWM_GEN_1,
PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1,
roundf((float)gSystemClock / PWM_FREQUENCY));
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2,
roundf((float)gSystemClock / PWM_FREQUENCY * 0.4f));
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3,
roundf((float)gSystemClock / PWM_FREQUENCY * 0.4f));
PWMOutputState(PWM0_BASE, PWM_OUT_2_BIT | PWM_OUT_3_BIT, true);
PWMGenEnable(PWM0_BASE, PWM_GEN_1);
}
uint32_t cpu_load_count(void)
{
@ -274,6 +249,12 @@ void handle_user_input() {
case Down: // previous scale
local_options.voltage_scale = (local_options.voltage_scale + VOLTAGE_SCALES - 1) % VOLTAGE_SCALES;
break;
case SW1:
set_pwm_period(++gPWMPeriod);
break;
case SW2:
set_pwm_period(--gPWMPeriod);
break;
}
Semaphore_pend(options_sem, BIOS_WAIT_FOREVER);
@ -320,11 +301,14 @@ void display_waveform(UArg arg1, UArg arg2)
GrContextForegroundSet(&sContext, ClrWheat);
snprintf(str, sizeof(str), "CPU Load %.1f%%", usage_percent);
GrStringDraw(&sContext, str, /*length*/ -1, /*x*/ 0, /*y*/ HEIGHT - 20, /*opaque*/ false);
GrStringDraw(&sContext, str, /*length*/ -1, /*x*/ 0, /*y*/ HEIGHT - 30, /*opaque*/ false);
float frequency = (float) gSystemClock / (float) gPeriod;
uint32_t period = gPeriod;
float frequency = (float) gSystemClock / (float) period;
snprintf(str, sizeof(str), "f = %.1fhz", frequency);
GrStringDraw(&sContext, str, /*length*/ -1, /*x*/ 0, /*y*/ HEIGHT - 10, /*opaque*/ false);
snprintf(str, sizeof(str), "T = %dc", period);
GrStringDraw(&sContext, str, /*length*/ -1, /*x*/ 0, /*y*/ HEIGHT - 20, /*opaque*/ false);
if (local_options.fft) {