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_4_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_4_Start(). * * Parameters: * None * * Return: * None * *******************************************************************************/ void PWM_4_Init(void) { #if (PWM_4_UsingFixedFunction || PWM_4_UseControl) uint8 ctrl; #endif /* (PWM_4_UsingFixedFunction || PWM_4_UseControl) */ #if(!PWM_4_UsingFixedFunction) #if(PWM_4_UseStatus) /* Interrupt State Backup for Critical Region*/ uint8 PWM_4_interruptState; #endif /* (PWM_4_UseStatus) */ #endif /* (!PWM_4_UsingFixedFunction) */ #if (PWM_4_UsingFixedFunction) /* You are allowed to write the compare value (FF only) */ PWM_4_CONTROL |= PWM_4_CFG0_MODE; #if (PWM_4_DeadBand2_4) PWM_4_CONTROL |= PWM_4_CFG0_DB; #endif /* (PWM_4_DeadBand2_4) */ ctrl = PWM_4_CONTROL3 & ((uint8 )(~PWM_4_CTRL_CMPMODE1_MASK)); PWM_4_CONTROL3 = ctrl | PWM_4_DEFAULT_COMPARE1_MODE; /* Clear and Set SYNCTC and SYNCCMP bits of RT1 register */ PWM_4_RT1 &= ((uint8)(~PWM_4_RT1_MASK)); PWM_4_RT1 |= PWM_4_SYNC; /*Enable DSI Sync all all inputs of the PWM*/ PWM_4_RT1 &= ((uint8)(~PWM_4_SYNCDSI_MASK)); PWM_4_RT1 |= PWM_4_SYNCDSI_EN; #elif (PWM_4_UseControl) /* Set the default compare mode defined in the parameter */ ctrl = PWM_4_CONTROL & ((uint8)(~PWM_4_CTRL_CMPMODE2_MASK)) & ((uint8)(~PWM_4_CTRL_CMPMODE1_MASK)); PWM_4_CONTROL = ctrl | PWM_4_DEFAULT_COMPARE2_MODE | PWM_4_DEFAULT_COMPARE1_MODE; #endif /* (PWM_4_UsingFixedFunction) */ #if (!PWM_4_UsingFixedFunction) #if (PWM_4_Resolution == 8) /* Set FIFO 0 to 1 byte register for period*/ PWM_4_AUX_CONTROLDP0 |= (PWM_4_AUX_CTRL_FIFO0_CLR); #else /* (PWM_4_Resolution == 16)*/ /* Set FIFO 0 to 1 byte register for period */ PWM_4_AUX_CONTROLDP0 |= (PWM_4_AUX_CTRL_FIFO0_CLR); PWM_4_AUX_CONTROLDP1 |= (PWM_4_AUX_CTRL_FIFO0_CLR); #endif /* (PWM_4_Resolution == 8) */ PWM_4_WriteCounter(PWM_4_INIT_PERIOD_VALUE); #endif /* (!PWM_4_UsingFixedFunction) */ PWM_4_WritePeriod(PWM_4_INIT_PERIOD_VALUE); #if (PWM_4_UseOneCompareMode) PWM_4_WriteCompare(PWM_4_INIT_COMPARE_VALUE1); #else PWM_4_WriteCompare1(PWM_4_INIT_COMPARE_VALUE1); PWM_4_WriteCompare2(PWM_4_INIT_COMPARE_VALUE2); #endif /* (PWM_4_UseOneCompareMode) */ #if (PWM_4_KillModeMinTime) PWM_4_WriteKillTime(PWM_4_MinimumKillTime); #endif /* (PWM_4_KillModeMinTime) */ #if (PWM_4_DeadBandUsed) PWM_4_WriteDeadTime(PWM_4_INIT_DEAD_TIME); #endif /* (PWM_4_DeadBandUsed) */ #if (PWM_4_UseStatus || PWM_4_UsingFixedFunction) PWM_4_SetInterruptMode(PWM_4_INIT_INTERRUPTS_MODE); #endif /* (PWM_4_UseStatus || PWM_4_UsingFixedFunction) */ #if (PWM_4_UsingFixedFunction) /* Globally Enable the Fixed Function Block chosen */ PWM_4_GLOBAL_ENABLE |= PWM_4_BLOCK_EN_MASK; /* Set the Interrupt source to come from the status register */ PWM_4_CONTROL2 |= PWM_4_CTRL2_IRQ_SEL; #else #if(PWM_4_UseStatus) /* CyEnterCriticalRegion and CyExitCriticalRegion are used to mark following region critical*/ /* Enter Critical Region*/ PWM_4_interruptState = CyEnterCriticalSection(); /* Use the interrupt output of the status register for IRQ output */ PWM_4_STATUS_AUX_CTRL |= PWM_4_STATUS_ACTL_INT_EN_MASK; /* Exit Critical Region*/ CyExitCriticalSection(PWM_4_interruptState); /* Clear the FIFO to enable the PWM_4_STATUS_FIFOFULL bit to be set on FIFO full. */ PWM_4_ClearFIFO(); #endif /* (PWM_4_UseStatus) */ #endif /* (PWM_4_UsingFixedFunction) */ }