Hello everyone,
I am new to this but could you please help me with my code? I am trying to get the mic to turn the red and blu color on pwm on and off if a button is pressed but it's not working and I'm not sre why...here's the code:
Thank you so much <3
#include <stdint.h>
#include <stdbool.h>
#include <math.h> // for sine wave
#include <stdio.h> // for sprintf()
#include <inc/hw_memmap.h>
#include <inc/hw_types.h>
#include <inc/hw_gpio.h>
#include <driverlib/sysctl.h>
#include <driverlib/debug.h>
#include <driverlib/adc.h>
#include <driverlib/fpu.h> // float support
#include <driverlib/gpio.h>
#include <driverlib/pwm.h>
#include <driverlib/pin_map.h>
#include "CFAF128128B0145T/CFAF128128B0145T.h"
// The error routine that is called if the driver library encounters an error.
#ifdef DEBUG
void __error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif
#define PWM_FREQUENCY 100 // PWM frequency must be > 100 Hz for human eyes
#define APP_PI 3.1415926535897932384626433832795f
#define STEPS 256
int main(void)
{
bool buttonPressed = false; // Variable to track the button state
volatile uint32_t ui32Load; // PWM period
volatile uint32_t ui32RedLevel; // PWM duty cycle for red LED
volatile uint32_t ui32GreenLevel; // PWM duty cycle for green LED
volatile uint32_t ui32BlueLevel; // PWM duty cycle for blue LED
volatile uint32_t ui32PWMClock; // PWM clock frequency
volatile uint32_t ui32SysClock;
volatile uint32_t ui32Index; // iteration counter
float fAngle;
// char strBuffer[30]; // for sprintf()
// Run from the PLL at 120 MHz.
ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000);
// Display, Parameters
CFAF128128B0145T_init(1, ui32SysClock, 20000000);
// Write text
CFAF128128B0145T_text(30, 60, "Hello World!", CFAF128128B0145T_color_white, CFAF128128B0145T_color_black, 1, 1);
// Init Hardware
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0); // enable PWM0
// Setup GPIOs, configure all three RGB LED pins to PWM
GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, GPIO_PIN_0);
GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
GPIOPinTypePWM(GPIO_PORTG_BASE, GPIO_PIN_0);
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);
GPIOPinConfigure(GPIO_PG0_M0PWM4);
GPIOPinConfigure(GPIO_PF2_M0PWM2);
GPIOPinConfigure(GPIO_PF3_M0PWM3);
// Setup PWM clock
PWMClockSet(PWM0_BASE, PWM_SYSCLK_DIV_64); // 120MHz/64 = 1.875MHz
// Calculate PWM load values
ui32PWMClock = ui32SysClock/64; // 120 MHz/64
ui32Load = (ui32PWMClock/PWM_FREQUENCY)-1; // 1875000/100 = 18750
// PWM Configuration (Red LED)
PWMGenConfigure(PWM0_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, ui32Load);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, 0); // a starter for the load value
PWMGenEnable(PWM0_BASE, PWM_GEN_1);
PWMOutputState(PWM0_BASE, PWM_OUT_2_BIT, true);
// PWM Configuration (Blue LED)
PWMGenConfigure(PWM0_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_2, ui32Load);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_4, 0); // a starter for the load value
PWMOutputState(PWM0_BASE, PWM_OUT_4_BIT, true); // switch on PWM output 4
PWMGenEnable(PWM0_BASE, PWM_GEN_2); // switch on generator 2
ui32Index = 0; // iteration counter
while(1)
{
if(GPIOPinRead(GPIO_PORTJ_BASE, GPIO_PIN_0) == 0)
{
buttonPressed = true;
}
else {
// Button is released, reset the buttonPressed flag
buttonPressed = false;
fAngle = ui32Index * (2.0f * APP_PI/STEPS);
}
if (buttonPressed){
// Red LED
PWMGenDisable(PWM0_BASE, PWM_GEN_2);
ui32RedLevel = (uint32_t) (9370.0f * (1 + sinf(fAngle))); // 18750/2 > 9370 to keep load value < max
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, ui32RedLevel +1);
}
else {
// Blue LED
PWMGenDisable(PWM0_BASE, PWM_GEN_1);
ui32BlueLevel = (uint32_t) (9370.0f * (1 + sinf(fAngle+2*2.0f * APP_PI/3)));
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_4, ui32BlueLevel +1);
}
SysCtlDelay(ui32PWMClock / PWM_FREQUENCY);
}
}