Back to posts Edit this post
Copy content

10 Jan 09:58

Programowanie
#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(uint32_t frequency, int duration, double volume){playSound(frequency, duration, volume);} void noteD(uint32_t frequency,int duration, double volume){playSound(frequency, duration, volume);} void noteE(uint32_t frequency, int duration, double volume){playSound(frequency, duration, volume);} void noteF(uint32_t frequency, int duration, double volume){playSound(frequency, duration, volume);} void noteG(uint32_t frequency, int duration, double volume){playSound(frequency, duration, volume);} void noteA(uint32_t frequency, int duration, double volume){playSound(frequency, duration, volume);} void noteB(uint32_t frequency, int duration, double volume){playSound(frequency, duration, volume);} void Melody(double volume) { noteC(523,300,volume);noteD(587,300, volume);noteE(659,300, volume); noteF(698,300, volume);noteG(784,300, volume);noteA(784,300, volume); noteB(784,600, volume); } int main(void) { uint32_t frequency=500; double volume=0; // 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); SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOH); SysCtlPeripheralEnable (SYSCTL_PERIPH_GPIOJ); GPIOPinTypeGPIOInput(GPIO_PORTH_BASE, GPIO_PINS_ALL); GPIOPinTypeGPIOInput(GPIO_PORTJ_BASE, GPIO_PINS_ALL); GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PINS_ALL); // 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 (;;) { if(GPIOPinRead(GPIO_PORTA_BASE,GPIO_PIN_0) == GPIO_PIN_0) { volume=0.5; } if(GPIOPinRead(GPIO_PORTA_BASE,GPIO_PIN_1) == GPIO_PIN_1) { volume=0.01; } if(GPIOPinRead(GPIO_PORTA_BASE,GPIO_PIN_4) == GPIO_PIN_4) { frequency+=100; } if(GPIOPinRead(GPIO_PORTA_BASE,GPIO_PIN_5) == GPIO_PIN_5) { frequency-=100; } if(GPIOPinRead(GPIO_PORTH_BASE,GPIO_PIN_3) == GPIO_PIN_3) noteC(frequency, 300, volume); if(GPIOPinRead(GPIO_PORTJ_BASE,GPIO_PIN_3) == GPIO_PIN_3) noteD(frequency, 300, volume); if(GPIOPinRead(GPIO_PORTH_BASE,GPIO_PIN_5) == GPIO_PIN_5) Melody(volume); //Melody(volume); ROM_SysCtlDelay(ROM_SysCtlClockGet() / 2); // add delay } return 0; }

No files