cancel
Showing results for 
Search instead for 
Did you mean: 

Code Examples

Kenshow
Esteemed Contributor

Hi all,

The electronic dice I posted last time was very popular. So, I add a piezoelectric buzzer to generate sound with PWM. Please refer to the link for details regarding the electronic dice and the piezoelectric buzzer of PWM.

1.png

The environment used is as follows:

PSoC Creator 4.2

CY8CKIT-062-BLE

The counter of the dice used the timer last time, but I changed it to PWM (PWM_1) because I want to control the length of the sound with Low/High signales. It is PWM_2 that produces the beep sound. PWM_2 used Start and Stop (Kill) inputs to control the blinking of the LED and the duration of the sound. The circuit is as follows:

2.png

The period of PWM_1 is set to the same period as the previous timer setting (Period0 = 399: 25Hz). Compare0 is set to 200 as the length of beep.

3.png

PWM_2 determines the frequency of the beep. This time, it is set to 2KHz (input clock is 1MHz, Period=499). Compare0 is 200 counts and is fixedly used in the program. The settings are as follows:

4.png

In the Advance tab, set Start Input to Rising Edge and Kill Input to Falling Edge to enable Start and Stop (Kill) inputs.

5.png

The buzzer pin is set to P12 [0] and the other pins are set as follows:

Clipboard02.jpg

The program contains the control of PWM_1and 2.

#include "project.h"

uint32_t sw=0;  //sw is off (Stop rolling dicce)

int main(void)

{

    __enable_irq(); /* Enable global interrupts. */

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

    Clock_Enable() ;

    CapSense_Start();

    CapSense_ScanAllWidgets(); // Start Initial Scan

    for(;;)

    {

        if(!CapSense_IsBusy())

        {

            CapSense_ProcessAllWidgets();

           

            if(sw==0){ // If sw is on, check start botton0.

                if(CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID)) // Check button 0 state

                {

                    /* Start rolling dice*/

                    PWM_1_SetCounter(0);

                    PWM_1_SetPeriod0(399);

                    //PWM_1_SetCompare0(200);

                    PWM_1_Start();  // Start rolling dice

                    PWM_2_Start();

                    sw=1; // sw is on

                }

            }

           

            if(sw==1){ // If sw is on, check stop botton0.

                if(CapSense_IsWidgetActive(CapSense_BUTTON1_WDGT_ID)) // Check button 1 state

                {

                    /* Roll dice slowly */

                    PWM_1_SetCounter(0);

                    PWM_1_SetPeriod0(1999);

                    //PWM_1_SetCompare0(200);

                    CyDelay(1500); // keep rolling slowly for 1.5sec

                    PWM_1_Disable();// Stop rolling dice                   

                    PWM_2_Disable();

                    sw=0;           // sw is off

                }

            }

           

            CapSense_UpdateAllBaselines();

            CapSense_ScanAllWidgets(); // Start next scan

        }

    }

}

Thanks,

Kenshow

0 Likes
1 Reply
Praveen
Community Manager
Community Manager

Interesting kenshow san.

Thanks for sharing . Appreciate the contributions !!

Cheers

Praveen

Cheers
Praveen