/******************************************************************************* * Function Name: PWM_1_RestoreConfig ******************************************************************************** * * Summary: * Restores the current user configuration of the component. * * Parameters: * None * * Return: * None * * Global variables: * PWM_1_backup: Variables of this global structure are used to * restore the values of non retention registers on wakeup from sleep mode. * *******************************************************************************/ void PWM_1_RestoreConfig(void) { #if(!PWM_1_UsingFixedFunction) #if(!PWM_1_PWMModeIsCenterAligned) PWM_1_WritePeriod(PWM_1_backup.PWMPeriod); #endif /* (!PWM_1_PWMModeIsCenterAligned) */ PWM_1_WriteCounter(PWM_1_backup.PWMUdb); #if (PWM_1_UseStatus) PWM_1_STATUS_MASK = PWM_1_backup.InterruptMaskValue; #endif /* (PWM_1_UseStatus) */ #if(PWM_1_DeadBandMode == PWM_1__B_PWM__DBM_256_CLOCKS || \ PWM_1_DeadBandMode == PWM_1__B_PWM__DBM_2_4_CLOCKS) PWM_1_WriteDeadTime(PWM_1_backup.PWMdeadBandValue); #endif /* deadband count is either 2-4 clocks or 256 clocks */ #if(PWM_1_KillModeMinTime) PWM_1_WriteKillTime(PWM_1_backup.PWMKillCounterPeriod); #endif /* (PWM_1_KillModeMinTime) */ #if(PWM_1_UseControl) PWM_1_WriteControlRegister(PWM_1_backup.PWMControlRegister); #endif /* (PWM_1_UseControl) */ #endif /* (!PWM_1_UsingFixedFunction) */ }
void piezo_play(uint16 frequency_value, uint8 note) { TRIGGER_TIMER_Write(0); // duty cycle ticks is a percentage of frequency based on volume int period_ticks = f_clock * freq_mod / frequency_value; uint16 compare_ticks = period_ticks * volume / 100; // Set period to frequency value PWM_1_WritePeriod(period_ticks); // Set compare to toggle on duty cycle ticks PWM_1_WriteCompare(compare_ticks); // Figure out the duration of the note and set and trigger the interrupt timer uint16 duration = 60000/tempo/note; Timer_1_WriteCompare(duration); TRIGGER_TIMER_Write(1); }
void piezo_tone(uint16 frequency_value) { current_frequency = frequency_value; // duty cycle ticks is a percentage of frequency based on volume int period_ticks = f_clock * freq_mod / frequency_value; uint16 compare_ticks = period_ticks * volume / 100; // maximum duty cycle ticks must be at least 1 lower than frequency // to produce a vibration if (compare_ticks == period_ticks) { compare_ticks--; } // Set period to frequency value PWM_1_WritePeriod(period_ticks); // Set compare to toggle on duty cycle ticks PWM_1_WriteCompare(compare_ticks); }
/******************************************************************************* * Function Name: PWM_1_RestoreConfig ******************************************************************************** * * Summary: * Restores the current user configuration of the component. * * Parameters: * None * * Return: * None * * Global variables: * PWM_1_backup: Variables of this global structure are used to * restore the values of non retention registers on wakeup from sleep mode. * *******************************************************************************/ void PWM_1_RestoreConfig(void) { #if(!PWM_1_UsingFixedFunction) #if (CY_UDB_V0) /* Interrupt State Backup for Critical Region*/ uint8 PWM_1_interruptState; /* Enter Critical Region*/ PWM_1_interruptState = CyEnterCriticalSection(); #if (PWM_1_UseStatus) /* Use the interrupt output of the status register for IRQ output */ PWM_1_STATUS_AUX_CTRL |= PWM_1_STATUS_ACTL_INT_EN_MASK; PWM_1_STATUS_MASK = PWM_1_backup.InterruptMaskValue; #endif /* (PWM_1_UseStatus) */ #if (PWM_1_Resolution == 8) /* Set FIFO 0 to 1 byte register for period*/ PWM_1_AUX_CONTROLDP0 |= (PWM_1_AUX_CTRL_FIFO0_CLR); #else /* (PWM_1_Resolution == 16)*/ /* Set FIFO 0 to 1 byte register for period */ PWM_1_AUX_CONTROLDP0 |= (PWM_1_AUX_CTRL_FIFO0_CLR); PWM_1_AUX_CONTROLDP1 |= (PWM_1_AUX_CTRL_FIFO0_CLR); #endif /* (PWM_1_Resolution == 8) */ /* Exit Critical Region*/ CyExitCriticalSection(PWM_1_interruptState); PWM_1_WriteCounter(PWM_1_backup.PWMUdb); PWM_1_WritePeriod(PWM_1_backup.PWMPeriod); #if(PWM_1_UseOneCompareMode) PWM_1_WriteCompare(PWM_1_backup.PWMCompareValue); #else PWM_1_WriteCompare1(PWM_1_backup.PWMCompareValue1); PWM_1_WriteCompare2(PWM_1_backup.PWMCompareValue2); #endif /* (PWM_1_UseOneCompareMode) */ #if(PWM_1_DeadBandMode == PWM_1__B_PWM__DBM_256_CLOCKS || \ PWM_1_DeadBandMode == PWM_1__B_PWM__DBM_2_4_CLOCKS) PWM_1_WriteDeadTime(PWM_1_backup.PWMdeadBandValue); #endif /* deadband count is either 2-4 clocks or 256 clocks */ #if ( PWM_1_KillModeMinTime) PWM_1_WriteKillTime(PWM_1_backup.PWMKillCounterPeriod); #endif /* ( PWM_1_KillModeMinTime) */ #endif /* (CY_UDB_V0) */ #if (CY_UDB_V1) #if(!PWM_1_PWMModeIsCenterAligned) PWM_1_WritePeriod(PWM_1_backup.PWMPeriod); #endif /* (!PWM_1_PWMModeIsCenterAligned) */ PWM_1_WriteCounter(PWM_1_backup.PWMUdb); #if (PWM_1_UseStatus) PWM_1_STATUS_MASK = PWM_1_backup.InterruptMaskValue; #endif /* (PWM_1_UseStatus) */ #if(PWM_1_DeadBandMode == PWM_1__B_PWM__DBM_256_CLOCKS || \ PWM_1_DeadBandMode == PWM_1__B_PWM__DBM_2_4_CLOCKS) PWM_1_WriteDeadTime(PWM_1_backup.PWMdeadBandValue); #endif /* deadband count is either 2-4 clocks or 256 clocks */ #if(PWM_1_KillModeMinTime) PWM_1_WriteKillTime(PWM_1_backup.PWMKillCounterPeriod); #endif /* (PWM_1_KillModeMinTime) */ #endif /* (CY_UDB_V1) */ #if(PWM_1_UseControl) PWM_1_WriteControlRegister(PWM_1_backup.PWMControlRegister); #endif /* (PWM_1_UseControl) */ #endif /* (!PWM_1_UsingFixedFunction) */ }
/******************************************************************************* * Function Name: PWM_1_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_1_Start(). * * Parameters: * None * * Return: * None * *******************************************************************************/ void PWM_1_Init(void) { #if (PWM_1_UsingFixedFunction || PWM_1_UseControl) uint8 ctrl; #endif /* (PWM_1_UsingFixedFunction || PWM_1_UseControl) */ #if(!PWM_1_UsingFixedFunction) #if(PWM_1_UseStatus) /* Interrupt State Backup for Critical Region*/ uint8 PWM_1_interruptState; #endif /* (PWM_1_UseStatus) */ #endif /* (!PWM_1_UsingFixedFunction) */ #if (PWM_1_UsingFixedFunction) /* You are allowed to write the compare value (FF only) */ PWM_1_CONTROL |= PWM_1_CFG0_MODE; #if (PWM_1_DeadBand2_4) PWM_1_CONTROL |= PWM_1_CFG0_DB; #endif /* (PWM_1_DeadBand2_4) */ ctrl = PWM_1_CONTROL3 & ((uint8 )(~PWM_1_CTRL_CMPMODE1_MASK)); PWM_1_CONTROL3 = ctrl | PWM_1_DEFAULT_COMPARE1_MODE; /* Clear and Set SYNCTC and SYNCCMP bits of RT1 register */ PWM_1_RT1 &= ((uint8)(~PWM_1_RT1_MASK)); PWM_1_RT1 |= PWM_1_SYNC; /*Enable DSI Sync all all inputs of the PWM*/ PWM_1_RT1 &= ((uint8)(~PWM_1_SYNCDSI_MASK)); PWM_1_RT1 |= PWM_1_SYNCDSI_EN; #elif (PWM_1_UseControl) /* Set the default compare mode defined in the parameter */ ctrl = PWM_1_CONTROL & ((uint8)(~PWM_1_CTRL_CMPMODE2_MASK)) & ((uint8)(~PWM_1_CTRL_CMPMODE1_MASK)); PWM_1_CONTROL = ctrl | PWM_1_DEFAULT_COMPARE2_MODE | PWM_1_DEFAULT_COMPARE1_MODE; #endif /* (PWM_1_UsingFixedFunction) */ #if (!PWM_1_UsingFixedFunction) #if (PWM_1_Resolution == 8) /* Set FIFO 0 to 1 byte register for period*/ PWM_1_AUX_CONTROLDP0 |= (PWM_1_AUX_CTRL_FIFO0_CLR); #else /* (PWM_1_Resolution == 16)*/ /* Set FIFO 0 to 1 byte register for period */ PWM_1_AUX_CONTROLDP0 |= (PWM_1_AUX_CTRL_FIFO0_CLR); PWM_1_AUX_CONTROLDP1 |= (PWM_1_AUX_CTRL_FIFO0_CLR); #endif /* (PWM_1_Resolution == 8) */ PWM_1_WriteCounter(PWM_1_INIT_PERIOD_VALUE); #endif /* (!PWM_1_UsingFixedFunction) */ PWM_1_WritePeriod(PWM_1_INIT_PERIOD_VALUE); #if (PWM_1_UseOneCompareMode) PWM_1_WriteCompare(PWM_1_INIT_COMPARE_VALUE1); #else PWM_1_WriteCompare1(PWM_1_INIT_COMPARE_VALUE1); PWM_1_WriteCompare2(PWM_1_INIT_COMPARE_VALUE2); #endif /* (PWM_1_UseOneCompareMode) */ #if (PWM_1_KillModeMinTime) PWM_1_WriteKillTime(PWM_1_MinimumKillTime); #endif /* (PWM_1_KillModeMinTime) */ #if (PWM_1_DeadBandUsed) PWM_1_WriteDeadTime(PWM_1_INIT_DEAD_TIME); #endif /* (PWM_1_DeadBandUsed) */ #if (PWM_1_UseStatus || PWM_1_UsingFixedFunction) PWM_1_SetInterruptMode(PWM_1_INIT_INTERRUPTS_MODE); #endif /* (PWM_1_UseStatus || PWM_1_UsingFixedFunction) */ #if (PWM_1_UsingFixedFunction) /* Globally Enable the Fixed Function Block chosen */ PWM_1_GLOBAL_ENABLE |= PWM_1_BLOCK_EN_MASK; /* Set the Interrupt source to come from the status register */ PWM_1_CONTROL2 |= PWM_1_CTRL2_IRQ_SEL; #else #if(PWM_1_UseStatus) /* CyEnterCriticalRegion and CyExitCriticalRegion are used to mark following region critical*/ /* Enter Critical Region*/ PWM_1_interruptState = CyEnterCriticalSection(); /* Use the interrupt output of the status register for IRQ output */ PWM_1_STATUS_AUX_CTRL |= PWM_1_STATUS_ACTL_INT_EN_MASK; /* Exit Critical Region*/ CyExitCriticalSection(PWM_1_interruptState); /* Clear the FIFO to enable the PWM_1_STATUS_FIFOFULL bit to be set on FIFO full. */ PWM_1_ClearFIFO(); #endif /* (PWM_1_UseStatus) */ #endif /* (PWM_1_UsingFixedFunction) */ }