Back to posts Edit this post
Copy content

20 Dec 11:07

szczeszol
#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) void playSound(uint32_t frequency, uint32_t duration, double volume) { uint32_t PWMclock = ROM_SysCtlClockGet() / 4 ; // 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 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, volume * 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, double volume){playSound(523, duration, volume);} void noteD(int duration, double volume){playSound(587, duration, volume);} void noteE(int duration, double volume){playSound(659, duration, volume);} void noteF(int duration, double volume){playSound(698, duration, volume);} void noteG(int duration, double volume){playSound(784, duration, volume);} void noteA(int duration, double volume){playSound(880, duration, volume);} void noteB(int duration, double volume){playSound(988, duration, volume);} void Melody(double volume) { noteC(300, volume);noteD(300, volume);noteE(300, volume); noteF(300, volume);noteG(300, volume);noteA(300, volume); noteB(600, volume); } int main(void) { double volume = 0.001; SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PINS_ALL); GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PINS_ALL); // 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); ROM_SysCtlDelay(ROM_SysCtlClockGet() / 2); // 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 (;;) { // play the melody if (GPIOPinRead(GPIO_PORTB_BASE, GPIO_PINS_ALL)) { volume = 0.001; } if (GPIOPinRead(GPIO_PORTD_BASE, GPIO_PINS_ALL)) { volume = 0.5; } Melody(volume); ROM_SysCtlDelay(ROM_SysCtlClockGet() / 2); // add delay } return 0; }

No files