Back to posts Edit this post
Copy content

20 Dec 11:05

umc umc umc
#include <stdint.h> #include <stdbool.h> #include "inc/hw_memmap.h" #include "driverlib/pin_map.h" #include "inc/hw_types.h" #include "driverlib/sysctl.h" #include "driverlib/gpio.h" #include "driverlib/debug.h" #include "driverlib/pwm.h" #include "inc/hw_gpio.h" #include "driverlib/rom.h" #define GPIO_PINS_ALL GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7 double currentVolume = 0.5; // Start with 50% volume double MAX_VOLUME = 1.0; double MIN_VOLUME = 0.0; double krok = 0.1; uint8_t freq = 4; void playSound(uint32_t frequency, uint32_t duration) { uint32_t PWMclock = ROM_SysCtlClockGet() / freq ; // get the current PWM clock value uint32_t period = (PWMclock/frequency) - 1; // calculate the period for PWM signal float numOfCyc = ROM_SysCtlClockGet()/1000.0; // calculate number of cycles per ms //float pulseWidth = currentVolume * period; // Calculate pulse width based on current volume uint32_t counter = duration * numOfCyc / 3; //calculate the counter for SysCtlDelay function; ROM_PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, period); // set the period of the PWM signal // Set the pulse width of PWM1 for a 50% duty cycle: ROM_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_2, currentVolume * period); ROM_PWMOutputState(PWM1_BASE, PWM_OUT_2_BIT, true); // enable PWM1 output ROM_PWMGenEnable(PWM1_BASE, PWM_GEN_1); // enable the PWM signal generation ROM_SysCtlDelay(counter); // add delay ROM_PWMGenDisable(PWM1_BASE, PWM_GEN_1); // disable the PWM signal generation } void noteC(int duration){playSound(523, duration);} void noteD(int duration){playSound(587, duration);} void noteE(int duration){playSound(659, duration);} void noteF(int duration){playSound(698, duration);} void noteG(int duration){playSound(784, duration);} void noteA(int duration){playSound(880, duration);} void noteB(int duration){playSound(988, duration);} void Melody() { noteC(300);noteD(300);noteE(300); noteF(300);noteG(300);noteA(300); noteB(600); } int main(void) { SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_5|GPIO_PIN_4); // Set the clocking to run directly from the crystal. ROM_SysCtlClockSet (SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // Set the PWM clock configuration. ROM_SysCtlPWMClockSet(SYSCTL_PWMDIV_4); ROM_SysCtlDelay(ROM_SysCtlClockGet() / 2); // Enable GPIOA and PWM module 1 ROM_SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOA); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); // Configure the PWM generator for count down mode with immediate updates // to the parameters. ROM_PWMGenConfigure(PWM1_BASE, PWM_GEN_1,PWM_GEN_MODE_DOWN); //Configure the pin mux to select module 1 PWM generator 1 for GPIO pin PA6 ROM_GPIOPinConfigure(GPIO_PA6_M1PWM2); // set the type of PA6 pin to PWM ROM_GPIOPinTypePWM(GPIO_PORTA_BASE,GPIO_PIN_6); for (;;) { Melody(); if (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_7)) { currentVolume = currentVolume + krok; if (currentVolume > MAX_VOLUME){ currentVolume = 0.90; } while (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PINS_ALL)); } if (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_6)) { currentVolume = currentVolume - krok; if (currentVolume < MIN_VOLUME) { currentVolume = 0.1; } while (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PINS_ALL)); } if (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_5)) { if (freq < 64) { freq = freq * 2; } while (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PINS_ALL)); } if (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4)) { if (freq > 1) { freq = freq / 2; } while (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PINS_ALL)); } ROM_SysCtlDelay(ROM_SysCtlClockGet() / 20); // add delay } return 0; }

No files