Example #1
0
void play_song(int* notebuf, int* accidentalsbuf, int* octavebuf, int length) {
	PWM_1_Start();
	PWM_2_Start();
	PWM_3_Start();
	PWM_4_Start();
	for (int i = 0; i < length; i+=4) {
		PWM_1_WriteCompare((note_lengths[note_lookup[notebuf[i]]+accidentalsbuf[i]]/2)>>octavebuf[i]);
		PWM_1_WritePeriod(note_lengths[note_lookup[notebuf[i]]+accidentalsbuf[i]]>>octavebuf[i]);
		PWM_1_WriteCounter(0);
		PWM_2_WriteCompare((note_lengths[note_lookup[notebuf[i+1]]+accidentalsbuf[i]]/2)>>octavebuf[i+1]);
		PWM_2_WritePeriod(note_lengths[note_lookup[notebuf[i+1]]+accidentalsbuf[i]]>>octavebuf[i+1]);
		PWM_2_WriteCounter(0);
		PWM_3_WriteCompare((note_lengths[note_lookup[notebuf[i+2]]+accidentalsbuf[i]]/2)>>octavebuf[i+2]);
		PWM_3_WritePeriod(note_lengths[note_lookup[notebuf[i+2]]+accidentalsbuf[i]]>>octavebuf[i+2]);
		PWM_3_WriteCounter(0);
		PWM_4_WriteCompare((note_lengths[note_lookup[notebuf[i+3]]+accidentalsbuf[i]]/2)>>octavebuf[i+3]);
		PWM_4_WritePeriod(note_lengths[note_lookup[notebuf[i+3]]+accidentalsbuf[i]]>>octavebuf[i+3]);
		PWM_4_WriteCounter(0);
		if (Keypad_1_Status_Reg_Read() & 0x10) break;
		CyDelay(TEMPO_MS);
	}
	PWM_1_Stop();
	PWM_2_Stop();
	PWM_3_Stop();
	PWM_4_Stop();
}
/*******************************************************************************
* Function Name: PWM_3_Init
********************************************************************************
*
* Summary:
*  Initialize component's parameters to the parameters set by user in the
*  customizer of the component placed onto schematic. Usually called in
*  PWM_3_Start().
*
* Parameters:
*  None
*
* Return:
*  None
*
*******************************************************************************/
void PWM_3_Init(void) 
{
    #if (PWM_3_UsingFixedFunction || PWM_3_UseControl)
        uint8 ctrl;
    #endif /* (PWM_3_UsingFixedFunction || PWM_3_UseControl) */

    #if(!PWM_3_UsingFixedFunction)
        #if(PWM_3_UseStatus)
            /* Interrupt State Backup for Critical Region*/
            uint8 PWM_3_interruptState;
        #endif /* (PWM_3_UseStatus) */
    #endif /* (!PWM_3_UsingFixedFunction) */

    #if (PWM_3_UsingFixedFunction)
        /* You are allowed to write the compare value (FF only) */
        PWM_3_CONTROL |= PWM_3_CFG0_MODE;
        #if (PWM_3_DeadBand2_4)
            PWM_3_CONTROL |= PWM_3_CFG0_DB;
        #endif /* (PWM_3_DeadBand2_4) */

        ctrl = PWM_3_CONTROL3 & ((uint8 )(~PWM_3_CTRL_CMPMODE1_MASK));
        PWM_3_CONTROL3 = ctrl | PWM_3_DEFAULT_COMPARE1_MODE;

         /* Clear and Set SYNCTC and SYNCCMP bits of RT1 register */
        PWM_3_RT1 &= ((uint8)(~PWM_3_RT1_MASK));
        PWM_3_RT1 |= PWM_3_SYNC;

        /*Enable DSI Sync all all inputs of the PWM*/
        PWM_3_RT1 &= ((uint8)(~PWM_3_SYNCDSI_MASK));
        PWM_3_RT1 |= PWM_3_SYNCDSI_EN;

    #elif (PWM_3_UseControl)
        /* Set the default compare mode defined in the parameter */
        ctrl = PWM_3_CONTROL & ((uint8)(~PWM_3_CTRL_CMPMODE2_MASK)) &
                ((uint8)(~PWM_3_CTRL_CMPMODE1_MASK));
        PWM_3_CONTROL = ctrl | PWM_3_DEFAULT_COMPARE2_MODE |
                                   PWM_3_DEFAULT_COMPARE1_MODE;
    #endif /* (PWM_3_UsingFixedFunction) */

    #if (!PWM_3_UsingFixedFunction)
        #if (PWM_3_Resolution == 8)
            /* Set FIFO 0 to 1 byte register for period*/
            PWM_3_AUX_CONTROLDP0 |= (PWM_3_AUX_CTRL_FIFO0_CLR);
        #else /* (PWM_3_Resolution == 16)*/
            /* Set FIFO 0 to 1 byte register for period */
            PWM_3_AUX_CONTROLDP0 |= (PWM_3_AUX_CTRL_FIFO0_CLR);
            PWM_3_AUX_CONTROLDP1 |= (PWM_3_AUX_CTRL_FIFO0_CLR);
        #endif /* (PWM_3_Resolution == 8) */

        PWM_3_WriteCounter(PWM_3_INIT_PERIOD_VALUE);
    #endif /* (!PWM_3_UsingFixedFunction) */

    PWM_3_WritePeriod(PWM_3_INIT_PERIOD_VALUE);

        #if (PWM_3_UseOneCompareMode)
            PWM_3_WriteCompare(PWM_3_INIT_COMPARE_VALUE1);
        #else
            PWM_3_WriteCompare1(PWM_3_INIT_COMPARE_VALUE1);
            PWM_3_WriteCompare2(PWM_3_INIT_COMPARE_VALUE2);
        #endif /* (PWM_3_UseOneCompareMode) */

        #if (PWM_3_KillModeMinTime)
            PWM_3_WriteKillTime(PWM_3_MinimumKillTime);
        #endif /* (PWM_3_KillModeMinTime) */

        #if (PWM_3_DeadBandUsed)
            PWM_3_WriteDeadTime(PWM_3_INIT_DEAD_TIME);
        #endif /* (PWM_3_DeadBandUsed) */

    #if (PWM_3_UseStatus || PWM_3_UsingFixedFunction)
        PWM_3_SetInterruptMode(PWM_3_INIT_INTERRUPTS_MODE);
    #endif /* (PWM_3_UseStatus || PWM_3_UsingFixedFunction) */

    #if (PWM_3_UsingFixedFunction)
        /* Globally Enable the Fixed Function Block chosen */
        PWM_3_GLOBAL_ENABLE |= PWM_3_BLOCK_EN_MASK;
        /* Set the Interrupt source to come from the status register */
        PWM_3_CONTROL2 |= PWM_3_CTRL2_IRQ_SEL;
    #else
        #if(PWM_3_UseStatus)

            /* CyEnterCriticalRegion and CyExitCriticalRegion are used to mark following region critical*/
            /* Enter Critical Region*/
            PWM_3_interruptState = CyEnterCriticalSection();
            /* Use the interrupt output of the status register for IRQ output */
            PWM_3_STATUS_AUX_CTRL |= PWM_3_STATUS_ACTL_INT_EN_MASK;

             /* Exit Critical Region*/
            CyExitCriticalSection(PWM_3_interruptState);

            /* Clear the FIFO to enable the PWM_3_STATUS_FIFOFULL
                   bit to be set on FIFO full. */
            PWM_3_ClearFIFO();
        #endif /* (PWM_3_UseStatus) */
    #endif /* (PWM_3_UsingFixedFunction) */
}