This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LAUNCHXL-F28069M: C2000 LaunchPad Board (LAUNCHXL-F28069M)

Part Number: LAUNCHXL-F28069M
Other Parts Discussed in Thread: C2000WARE,

Hello everyone,

I have a problem while running my code.

According to my requirement, right after switching on the power I want to blink my red & blue LEDs to blink for 4 second(which is working perfectly) and then right after that I want my buzzer(external device to be beep for 2 seconds) and then off.

So, the problem is with my code is that, whenever the Power is ON the LED and Buzzer are working simultaneously.

I want my buzzer to work after 4 second for 2 second only. 

Could you please how can i control the buzzer because I have tried to change the example provided a lot but I was not succeeded with this. 

The problem is still the same.

Sharing my code just for reference:

//
// Included Files
//
#include "DSP28x_Project.h"     // Device Headerfile and Examples Include File


//Buzzer code-----------
#include "F2806x_Device.h"         // F2806x Headerfile
#include "F2806x_Examples.h"       // F2806x Examples Headerfile
#include "F2806x_EPwm_defines.h"   // useful defines for initialization

void HRPWM1_Config(Uint16);
void HRPWM1_Config_1(Uint16 period);

Uint16 i,j, DutyFine, n,update;
Uint32 temp;
//Buzzer code-----------


//
// Function Prototypes statements for functions found within this file.
//
__interrupt void cpu_timer0_isr(void);

//logic for LED------------------------------------------------------------------
//variable to count number of timer interrupts
int tim_int_cnts = 0;
//------------------------------------------------------------------


//
// Main
//
void main(void)
{


    //
    // Step 1. Initialize System Control:
    // PLL, WatchDog, enable Peripheral Clocks
    // This example function is found in the F2806x_SysCtrl.c file.
    //
    InitSysCtrl();

    //Buzzer code------------
    InitEPwm1Gpio();
    //Buzzer code-----------

    // Step 2. Initalize GPIO:
    // This example function is found in the F2806x_Gpio.c file and
    // illustrates how to set the GPIO to it's default state.
    //
    // InitGpio();  // Skipped for this example

    //
    // Step 3. Clear all interrupts and initialize PIE vector table:
    // Disable CPU interrupts
    //


    DINT;

    //
    // Initialize the PIE control registers to their default state.
    // The default state is all PIE interrupts disabled and flags
    // are cleared.
    // This function is found in the F2806x_PieCtrl.c file.
    //
    InitPieCtrl();

    //
    // Disable CPU interrupts and clear all CPU interrupt flags
    //
    IER = 0x0000;
    IFR = 0x0000;

    //
    // Initialize the PIE vector table with pointers to the shell Interrupt
    // Service Routines (ISR).
    // This will populate the entire table, even if the interrupt
    // is not used in this example.  This is useful for debug purposes.
    // The shell ISR routines are found in F2806x_DefaultIsr.c.
    // This function is found in F2806x_PieVect.c.
    //
    InitPieVectTable();

    //Buzzer code---------
                       update = 1;
                       DutyFine =0;

                       EALLOW;
                       SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
                       EDIS;

                      // ePWM and HRPWM register initializaition
                      //
                       HRPWM1_Config(10);      // ePWM1 target, Period = 10
                       EALLOW;
                         SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
                       EDIS;

             //Buzzer code


    //
    // Interrupts that are used in this example are re-mapped to
    // ISR functions found within this file.
    //
    EALLOW;    // This is needed to write to EALLOW protected registers
    PieVectTable.TINT0 = &cpu_timer0_isr;
    EDIS;      // This is needed to disable write to EALLOW protected registers

    //
    // Step 4. Initialize the Device Peripheral. This function can be
    //         found in F2806x_CpuTimers.c
    //
    InitCpuTimers();   // For this example, only initialize the Cpu Timers
    
    //
    // Configure CPU-Timer 0 to interrupt every 500 milliseconds:
    // 80MHz CPU Freq, 50 millisecond Period (in uSeconds)
    //
    ConfigCpuTimer(&CpuTimer0, 80, 500000);

    //
    // To ensure precise timing, use write-only instructions to write to the
    // entire register. Therefore, if any of the configuration bits are changed
    // in ConfigCpuTimer and InitCpuTimers (in F2806x_CpuTimers.h), the
    // below settings must also be updated.
    //
    
    //
    // Use write-only instruction to set TSS bit = 0
    //
    CpuTimer0Regs.TCR.all = 0x4001;

    //
    // Step 5. User specific code, enable interrupts:
    //

    //
    // Configure GPIO34 as a GPIO output pin
    //
   /* EALLOW;
    GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0;
    GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1;
    EDIS;*/

// Logic for LED Blink for 4 second after POWER ON------------------------------------
        EALLOW;

        GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1;
       // GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 1;

        GpioCtrlRegs.GPBDIR.bit.GPIO39 = 1;
       // GpioCtrlRegs.GPBMUX1.bit.GPIO39 = 0;
        EDIS;

//------------------------------------------------------------------------------------------------------------

    //
    // Enable CPU INT1 which is connected to CPU-Timer 0
    //
    IER |= M_INT1;

    //
    // Enable TINT0 in the PIE: Group 1 interrupt 7
    //
    PieCtrlRegs.PIEIER1.bit.INTx7 = 1;


    //
    // Enable global Interrupts and higher priority real-time debug events
    //
    EINT;   // Enable Global interrupt INTM
    ERTM;   // Enable Global realtime interrupt DBGM

    EALLOW;
    SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
    EDIS;
//    HRPWM1_Config_1(0);
//    EALLOW;
//    SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
//    EDIS;
    //
    // Step 6. IDLE loop. Just sit and loop forever (optional)
    //
   // for(;;);   // was in original code

// logic for LED blinking----------------------------------------------

    while(tim_int_cnts < 8)
        {
         //Wait here until LEd blinks for 4 seconds
      //  HRPWM1_Config_1(0);

        }


        GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1;
        GpioCtrlRegs.GPBDIR.bit.GPIO39 = 1;
        EDIS;
//---------------------------------------------- ----------------------------------------------

        DINT;
        EALLOW;
        SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
        EDIS;
        HRPWM1_Config(10);
        EALLOW;
        SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
        EDIS;

//Buzzer Code------------------------------
        while ((tim_int_cnts > 7) && (tim_int_cnts < 12))
           {

//               for(DutyFine =1; DutyFine <256 ;DutyFine ++)
//               {
//                   //
//                   // Example, write to the HRPWM extension of CMPA
//                   //
//
//                   //
//                   // Left shift by 8 to write into MSB bits
//                   //
//                   EPwm1Regs.CMPA.half.CMPAHR = DutyFine << 8;
//
//                   //
//                   // Left shift by 8 to write into MSB bits
//                   //
//                   EPwm2Regs.CMPA.half.CMPAHR = DutyFine << 8;
//
//                   //
//                   // Example, 32-bit write to CMPA:CMPAHR
//                   //
//                   EPwm3Regs.CMPA.all = ((Uint32)EPwm3Regs.CMPA.half.CMPA << 16) +
//                                         (DutyFine << 8);
//                   EPwm4Regs.CMPA.all = ((Uint32)EPwm4Regs.CMPA.half.CMPA << 16) +
//                                         (DutyFine << 8);
//
//                   //
//                   // Dummy delay between MEP changes
//                   //
//                   for (i=0;i<10000;i++)
//                   {
//
//                   }
//               }
           }
//Buzzer Code------------------------------
        HRPWM1_Config_1(0);

}

//
// cpu_timer0_isr - 
//
__interrupt void
cpu_timer0_isr(void)
{
    CpuTimer0.InterruptCount++;
    //
    // Toggle GPIO34 once per 500 milliseconds

//logic for LED-----------------------------------------------------------------
               if(tim_int_cnts < 8){
                   GpioCtrlRegs.GPBDIR.bit.GPIO39 ^=0x01;
                   GpioCtrlRegs.GPBDIR.bit.GPIO34 ^=0x01;

                   GpioDataRegs.GPBTOGGLE.bit.GPIO34 = 1;
                   GpioDataRegs.GPBTOGGLE.bit.GPIO39 = 1;


                   tim_int_cnts++;
                  }
                  else if((tim_int_cnts > 7) && (tim_int_cnts < 12)){
                      tim_int_cnts++;
                  }
                  else {
                      //Do nothing
                  }
//--------------------------------------------------------------------------------------
    //
    // Acknowledge this interrupt to receive more interrupts from group 1
    //
    PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}

void
HRPWM1_Config(Uint16 period)
{
    //
    // ePWM1 register configuration with HRPWM
    // ePWM1A toggle low/high with MEP control on Rising edge
    //
    EPwm1Regs.TBCTL.bit.PRDLD = TB_IMMEDIATE;    // set Immediate load
    EPwm1Regs.TBPRD = period-1;                  // PWM frequency = 1 / period
    EPwm1Regs.CMPA.half.CMPA = period / 2;       // set duty 50% initially
    EPwm1Regs.CMPA.half.CMPAHR = (1 << 8);       // initialize HRPWM extension
    EPwm1Regs.CMPB = period / 2;                 // set duty 50% initially
    EPwm1Regs.TBPHS.all = 0;
    EPwm1Regs.TBCTR = 0;

    EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP;
    EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE;      // EPwm1 is the Master
    EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE;
    EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;
    EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;

    EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;
    EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;
    EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
    EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;

    EPwm1Regs.AQCTLA.bit.ZRO = AQ_CLEAR;        // PWM toggle low/high
    EPwm1Regs.AQCTLA.bit.CAU = AQ_SET;
    EPwm1Regs.AQCTLB.bit.ZRO = AQ_CLEAR;
    EPwm1Regs.AQCTLB.bit.CBU = AQ_SET;

    EALLOW;
    EPwm1Regs.HRCNFG.all = 0x0;
    EPwm1Regs.HRCNFG.bit.EDGMODE = HR_REP;      // MEP control on Rising edge
    EPwm1Regs.HRCNFG.bit.CTLMODE = HR_CMP;
    EPwm1Regs.HRCNFG.bit.HRLOAD  = HR_CTR_ZERO;
    EDIS;
}

void
HRPWM1_Config_1(Uint16 period)
{
    //
    // ePWM1 register configuration with HRPWM
    // ePWM1A toggle low/high with MEP control on Rising edge
    //
    EPwm1Regs.TBCTL.bit.PRDLD = TB_IMMEDIATE;    // set Immediate load
    EPwm1Regs.TBPRD = period-1;                  // PWM frequency = 1 / period
    EPwm1Regs.CMPA.half.CMPA = period / 2;       // set duty 50% initially
    EPwm1Regs.CMPA.half.CMPAHR = (1 << 8);       // initialize HRPWM extension
    EPwm1Regs.CMPB = period / 2;                 // set duty 50% initially


}

void
InitEPwm(void)
{
    // Initialize EPwm1
}


void
InitEPwmGpio(void)
{
    #if DSP28_EPWM1
        InitEPwm1Gpio();
    #endif // endif DSP28_EPWM1
}

#if DSP28_EPWM1

// InitEPwmGpio -  This function initializes GPIO pins to function as EPwm pins
//
// Each GPIO pin can be configured as a GPIO pin or up to 3 different
// peripheral functional pins. By default all pins come up as GPIO
// inputs after reset.
//
void
InitEPwm1Gpio(void)
{
    EALLOW;
        // Disable internal pull-up for the selected output pins for reduced power
        // consumption. Pull-ups can be enabled or disabled by the user.
        // Comment out other unwanted lines.
        //
    GpioCtrlRegs.GPAPUD.bit.GPIO0 = 1;    // Disable pull-up on GPIO0 (EPWM1A)
    GpioCtrlRegs.GPAPUD.bit.GPIO1 = 1;    // Disable pull-up on GPIO1 (EPWM1B)
        // Configure EPWM-1 pins using GPIO regs
        // This specifies which of the possible GPIO pins will be EPWM1 functional
        // pins.
        // Comment out other unwanted lines.
        //
    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;   // Configure GPIO0 as EPWM1A
    GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1;   // Configure GPIO1 as EPWM1B

    EDIS;
}
#endif // endif DSP28_EPWM1

void
InitEPwmSyncGpio(void)
{
        // Enable internal pull-up for the selected pins
        // Pull-ups can be enabled or disabled by the user.
        // This will enable the pullups for the specified pins.
        // Comment out other unwanted lines.
    GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0; // Enable pull-up on GPIO32 (EPWMSYNCI)
        // Set qualification for selected pins to asynch only
        // This will select synch to SYSCLKOUT for the selected pins.
    GpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 0;
        // Configure EPwmSync pins using GPIO regs
        // This specifies which of the possible GPIO pins will be EPwmSync
        // functional pins.
    GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 2;
        // Disable internal pull-up for the selected output pins for reduced power
        // consumption
        // Pull-ups can be enabled or disabled by the user.
    GpioCtrlRegs.GPBPUD.bit.GPIO33 = 1;
    GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 2;  // Configures GPIO33 for EPWMSYNCO
}

// InitTzGpio - This function initializes GPIO pins to function as
// Trip Zone (TZ) pins
//
// Each GPIO pin can be configured as a GPIO pin or up to 3 different
// peripheral functional pins. By default all pins come up as GPIO
// inputs after reset.
void
InitTzGpio(void)
{
    EALLOW;
        // Enable internal pull-up for the selected pins
        // Pull-ups can be enabled or disabled by the user.
        // This will enable the pullups for the specified pins.
    GpioCtrlRegs.GPAPUD.bit.GPIO12 = 0;    // Enable pull-up on GPIO12 (TZ1)
    GpioCtrlRegs.GPAPUD.bit.GPIO13 = 0;    // Enable pull-up on GPIO13 (TZ2)
    GpioCtrlRegs.GPAPUD.bit.GPIO14 = 0;    // Enable pull-up on GPIO14 (TZ3)
        // Set qualification for selected pins to asynch only
        // Inputs are synchronized to SYSCLKOUT by default.
        // This will select asynch (no qualification) for the selected pins.
    GpioCtrlRegs.GPAQSEL1.bit.GPIO12 = 3;  // Asynch input GPIO12 (TZ1)
    GpioCtrlRegs.GPAQSEL1.bit.GPIO13 = 3;  // Asynch input GPIO13 (TZ2)
    GpioCtrlRegs.GPAQSEL1.bit.GPIO14 = 3;  // Asynch input GPIO14 (TZ3)
        // Configure TZ pins using GPIO regs
        // This specifies which of the possible GPIO pins will be TZ functional
        // pins.
    GpioCtrlRegs.GPAMUX1.bit.GPIO12 = 1;  // Configure GPIO12 as TZ1
    GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 1;  // Configure GPIO13 as TZ2
    GpioCtrlRegs.GPAMUX1.bit.GPIO14 = 1;  // Configure GPIO14 as TZ3
    EDIS;
}

Regards,

Divya Tripathi