/******************************************************************************* * Function Name: PWM_RestoreConfig ******************************************************************************** * * Summary: * Restores the current user configuration of the component. * * Parameters: * None * * Return: * None * * Global variables: * PWM_backup: Variables of this global structure are used to * restore the values of non retention registers on wakeup from sleep mode. * *******************************************************************************/ void PWM_RestoreConfig(void) { #if(!PWM_UsingFixedFunction) #if(!PWM_PWMModeIsCenterAligned) PWM_WritePeriod(PWM_backup.PWMPeriod); #endif /* (!PWM_PWMModeIsCenterAligned) */ PWM_WriteCounter(PWM_backup.PWMUdb); #if (PWM_UseStatus) PWM_STATUS_MASK = PWM_backup.InterruptMaskValue; #endif /* (PWM_UseStatus) */ #if(PWM_DeadBandMode == PWM__B_PWM__DBM_256_CLOCKS || \ PWM_DeadBandMode == PWM__B_PWM__DBM_2_4_CLOCKS) PWM_WriteDeadTime(PWM_backup.PWMdeadBandValue); #endif /* deadband count is either 2-4 clocks or 256 clocks */ #if(PWM_KillModeMinTime) PWM_WriteKillTime(PWM_backup.PWMKillCounterPeriod); #endif /* (PWM_KillModeMinTime) */ #if(PWM_UseControl) PWM_WriteControlRegister(PWM_backup.PWMControlRegister); #endif /* (PWM_UseControl) */ #endif /* (!PWM_UsingFixedFunction) */ }
void main() { /* Place your initialization/startup code here (e.g. MyInst_Start()) */ uint16 adc, compare; LCD_Start(); ADC_Start(); PWM_Start(); /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */ for(;;) { /* Place your application code here. */ // LCD_ClearDisplay(); LCD_Start(); adc = 0; ADC_StartConvert(); ADC_IsEndConversion(ADC_WAIT_FOR_RESULT); ADC_StopConvert(); adc = ADC_GetResult16(); if(adc > 255) { if(adc == 0xFFFF) /* underflow correction */ { adc = 0x00; } else adc = 0xFF; /* Overflow correction */ } LCD_Position(0,0); LCD_PrintHexUint8(adc); compare = (uint16)(1000 + ((float)((float)((float)adc / (float)255) * (float)1000))); LCD_Position(1,0); LCD_PrintDecUint16(compare); PWM_WriteCompare(compare); PWM_WritePeriod(compare + 39999); } }
void updateLED() { double f1=(double)(QuadDec1_GetCounter()); double f2=(double)(QuadDec2_GetCounter()); f1=f1/10.0; f2=f2/10.0; double difference=f1-f2; if (difference<0) { difference=difference*(-1.0); } if (difference<1) difference=1.0; //difference=difference+0.1; int wc=(1000/difference); int wp=wc/2; PWM_WritePeriod(wc); PWM_WriteCompare(wp); bf=difference;//global }
/******************************************************************************* * Function Name: PWM_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_Start(). * * Parameters: * None * * Return: * None * *******************************************************************************/ void PWM_Init(void) { #if (PWM_UsingFixedFunction || PWM_UseControl) uint8 ctrl; #endif /* (PWM_UsingFixedFunction || PWM_UseControl) */ #if(!PWM_UsingFixedFunction) #if(PWM_UseStatus) /* Interrupt State Backup for Critical Region*/ uint8 PWM_interruptState; #endif /* (PWM_UseStatus) */ #endif /* (!PWM_UsingFixedFunction) */ #if (PWM_UsingFixedFunction) /* You are allowed to write the compare value (FF only) */ PWM_CONTROL |= PWM_CFG0_MODE; #if (PWM_DeadBand2_4) PWM_CONTROL |= PWM_CFG0_DB; #endif /* (PWM_DeadBand2_4) */ /* Set the default Compare Mode */ #if(CY_PSOC5A) ctrl = PWM_CONTROL2 & ((uint8)(~PWM_CTRL_CMPMODE1_MASK)); PWM_CONTROL2 = ctrl | PWM_DEFAULT_COMPARE1_MODE; #endif /* (CY_PSOC5A) */ #if(CY_PSOC3 || CY_PSOC5LP) ctrl = PWM_CONTROL3 & ((uint8 )(~PWM_CTRL_CMPMODE1_MASK)); PWM_CONTROL3 = ctrl | PWM_DEFAULT_COMPARE1_MODE; #endif /* (CY_PSOC3 || CY_PSOC5LP) */ /* Clear and Set SYNCTC and SYNCCMP bits of RT1 register */ PWM_RT1 &= ((uint8)(~PWM_RT1_MASK)); PWM_RT1 |= PWM_SYNC; /*Enable DSI Sync all all inputs of the PWM*/ PWM_RT1 &= ((uint8)(~PWM_SYNCDSI_MASK)); PWM_RT1 |= PWM_SYNCDSI_EN; #elif (PWM_UseControl) /* Set the default compare mode defined in the parameter */ ctrl = PWM_CONTROL & ((uint8)(~PWM_CTRL_CMPMODE2_MASK)) & ((uint8)(~PWM_CTRL_CMPMODE1_MASK)); PWM_CONTROL = ctrl | PWM_DEFAULT_COMPARE2_MODE | PWM_DEFAULT_COMPARE1_MODE; #endif /* (PWM_UsingFixedFunction) */ #if (!PWM_UsingFixedFunction) #if (PWM_Resolution == 8) /* Set FIFO 0 to 1 byte register for period*/ PWM_AUX_CONTROLDP0 |= (PWM_AUX_CTRL_FIFO0_CLR); #else /* (PWM_Resolution == 16)*/ /* Set FIFO 0 to 1 byte register for period */ PWM_AUX_CONTROLDP0 |= (PWM_AUX_CTRL_FIFO0_CLR); PWM_AUX_CONTROLDP1 |= (PWM_AUX_CTRL_FIFO0_CLR); #endif /* (PWM_Resolution == 8) */ PWM_WriteCounter(PWM_INIT_PERIOD_VALUE); #endif /* (!PWM_UsingFixedFunction) */ PWM_WritePeriod(PWM_INIT_PERIOD_VALUE); #if (PWM_UseOneCompareMode) PWM_WriteCompare(PWM_INIT_COMPARE_VALUE1); #else PWM_WriteCompare1(PWM_INIT_COMPARE_VALUE1); PWM_WriteCompare2(PWM_INIT_COMPARE_VALUE2); #endif /* (PWM_UseOneCompareMode) */ #if (PWM_KillModeMinTime) PWM_WriteKillTime(PWM_MinimumKillTime); #endif /* (PWM_KillModeMinTime) */ #if (PWM_DeadBandUsed) PWM_WriteDeadTime(PWM_INIT_DEAD_TIME); #endif /* (PWM_DeadBandUsed) */ #if (PWM_UseStatus || PWM_UsingFixedFunction) PWM_SetInterruptMode(PWM_INIT_INTERRUPTS_MODE); #endif /* (PWM_UseStatus || PWM_UsingFixedFunction) */ #if (PWM_UsingFixedFunction) /* Globally Enable the Fixed Function Block chosen */ PWM_GLOBAL_ENABLE |= PWM_BLOCK_EN_MASK; /* Set the Interrupt source to come from the status register */ PWM_CONTROL2 |= PWM_CTRL2_IRQ_SEL; #else #if(PWM_UseStatus) /* CyEnterCriticalRegion and CyExitCriticalRegion are used to mark following region critical*/ /* Enter Critical Region*/ PWM_interruptState = CyEnterCriticalSection(); /* Use the interrupt output of the status register for IRQ output */ PWM_STATUS_AUX_CTRL |= PWM_STATUS_ACTL_INT_EN_MASK; /* Exit Critical Region*/ CyExitCriticalSection(PWM_interruptState); /* Clear the FIFO to enable the PWM_STATUS_FIFOFULL bit to be set on FIFO full. */ PWM_ClearFIFO(); #endif /* (PWM_UseStatus) */ #endif /* (PWM_UsingFixedFunction) */ }
/******************************************************************************* * Function Name: PWM_RestoreConfig ******************************************************************************** * * Summary: * Restores the current user configuration of the component. * * Parameters: * void * * Return: * void * * Global variables: * PWM_backup: Variables of this global structure are used to * restore the values of non retention registers on wakeup from sleep mode. * *******************************************************************************/ void PWM_RestoreConfig(void) { #if(!PWM_UsingFixedFunction) #if (CY_PSOC5A) /* Interrupt State Backup for Critical Region*/ uint8 PWM_interruptState; /* Enter Critical Region*/ PWM_interruptState = CyEnterCriticalSection(); #if (PWM_UseStatus) /* Use the interrupt output of the status register for IRQ output */ PWM_STATUS_AUX_CTRL |= PWM_STATUS_ACTL_INT_EN_MASK; PWM_STATUS_MASK = PWM_backup.InterruptMaskValue; #endif /* (PWM_UseStatus) */ #if (PWM_Resolution == 8) /* Set FIFO 0 to 1 byte register for period*/ PWM_AUX_CONTROLDP0 |= (PWM_AUX_CTRL_FIFO0_CLR); #else /* (PWM_Resolution == 16)*/ /* Set FIFO 0 to 1 byte register for period */ PWM_AUX_CONTROLDP0 |= (PWM_AUX_CTRL_FIFO0_CLR); PWM_AUX_CONTROLDP1 |= (PWM_AUX_CTRL_FIFO0_CLR); #endif /* (PWM_Resolution == 8) */ /* Exit Critical Region*/ CyExitCriticalSection(PWM_interruptState); PWM_WriteCounter(PWM_backup.PWMUdb); PWM_WritePeriod(PWM_backup.PWMPeriod); #if(PWM_UseOneCompareMode) PWM_WriteCompare(PWM_backup.PWMCompareValue); #else PWM_WriteCompare1(PWM_backup.PWMCompareValue1); PWM_WriteCompare2(PWM_backup.PWMCompareValue2); #endif /* (PWM_UseOneCompareMode) */ #if(PWM_DeadBandMode == PWM__B_PWM__DBM_256_CLOCKS || \ PWM_DeadBandMode == PWM__B_PWM__DBM_2_4_CLOCKS) PWM_WriteDeadTime(PWM_backup.PWMdeadBandValue); #endif /* deadband count is either 2-4 clocks or 256 clocks */ #if ( PWM_KillModeMinTime) PWM_WriteKillTime(PWM_backup.PWMKillCounterPeriod); #endif /* ( PWM_KillModeMinTime) */ #endif /* (CY_PSOC5A) */ #if (CY_PSOC3 || CY_PSOC5LP) #if(!PWM_PWMModeIsCenterAligned) PWM_WritePeriod(PWM_backup.PWMPeriod); #endif /* (!PWM_PWMModeIsCenterAligned) */ PWM_WriteCounter(PWM_backup.PWMUdb); #if (PWM_UseStatus) PWM_STATUS_MASK = PWM_backup.InterruptMaskValue; #endif /* (PWM_UseStatus) */ #if(PWM_DeadBandMode == PWM__B_PWM__DBM_256_CLOCKS || \ PWM_DeadBandMode == PWM__B_PWM__DBM_2_4_CLOCKS) PWM_WriteDeadTime(PWM_backup.PWMdeadBandValue); #endif /* deadband count is either 2-4 clocks or 256 clocks */ #if(PWM_KillModeMinTime) PWM_WriteKillTime(PWM_backup.PWMKillCounterPeriod); #endif /* (PWM_KillModeMinTime) */ #endif /* (CY_PSOC3 || CY_PSOC5LP) */ #if(PWM_UseControl) PWM_WriteControlRegister(PWM_backup.PWMControlRegister); #endif /* (PWM_UseControl) */ #endif /* (!PWM_UsingFixedFunction) */ }