//PWM, power output void pwro_output(uint8 value) { clutch_pwm = value; PWM_2_WriteCompare(clutch_pwm); }
//Configuration for the clutch void init_pwro(void) { //PWM2: Clutch PWM_2_Start(); PWM_2_WriteCompare(0); //Start at 0% }
int main() { CyGlobalIntEnable; /* Enable global interrupts. */ //PWM_1_Start(); PWM_1_Start(); USBUART_Start(0,USBUART_5V_OPERATION); while(!USBUART_bGetConfiguration()){} USBUART_CDC_Init(); int count = 0; uint8 buff[64]; buff[0] = 0; int up_b = 0; int left_b = 0; int down_b = 0; int right_b = 0; int PWM_count = 0; int judge_count = 0; while(1)//main loop { if(0 != USBUART_GetConfiguration()) { if(0 != USBUART_DataIsReady()) { count = USBUART_GetAll(buff); if( count != 0){ while(0 == USBUART_CDCIsReady()){} PWM_1_Start(); PWM_2_Start(); PWM_3_Start(); PWM_4_Start(); USBUART_PutData(buff,count); CyDelay(500); USBUART_PutCRLF(); switch(buff[0]){ case 48: up_b = 1; break; case 49: up_b = 0; break; case 50: left_b = 1; break; case 51: left_b = 0; break; case 52: right_b = 1; break; case 53: right_b = 0; break; case 54: down_b = 1; break; case 55: down_b = 0; break; default: break; } judge_count = up_b + left_b + down_b +right_b; if(judge_count == 0){ PWM_count = 0; } if(judge_count == 1){//押されているボタンの数が1つの場合 if(PWM_count <=100){ PWM_count++; int i = 0; for(i=0;i<=100;i++){} }else{} if(up_b == 1){//上ボタン PWM_1_WriteCompare(PWM_count); PWM_1_direction_Write(1); PWM_2_WriteCompare(PWM_count); PWM_2_direction_Write(1); PWM_3_WriteCompare(PWM_count); PWM_3_direction_Write(1); PWM_4_WriteCompare(PWM_count); PWM_4_direction_Write(1); } if(left_b == 1) PWM_1_WriteCompare(PWM_count); PWM_1_direction_Write(0); PWM_2_WriteCompare(PWM_count); PWM_2_direction_Write(1); PWM_3_WriteCompare(PWM_count); PWM_3_direction_Write(1); PWM_4_WriteCompare(PWM_count); PWM_4_direction_Write(0); } if(right_b == 1) PWM_1_WriteCompare(PWM_count); PWM_1_direction_Write(1); PWM_2_WriteCompare(PWM_count); PWM_2_direction_Write(0); PWM_3_WriteCompare(PWM_count); PWM_3_direction_Write(0); PWM_4_WriteCompare(PWM_count); PWM_4_direction_Write(1); } if(down_b == 1) PWM_1_WriteCompare(PWM_count); PWM_1_direction_Write(0); PWM_2_WriteCompare(PWM_count); PWM_2_direction_Write(0); PWM_3_WriteCompare(PWM_count); PWM_3_direction_Write(0); PWM_4_WriteCompare(PWM_count); PWM_4_direction_Write(0); } }else if(judge_count == 2){//2個の場合 if(PWM_count <=100){ PWM_count++; int i = 0; for(i=0;i<=100;i++){} }else{} if(up_b == 1&& left_b == 1){ PWM_1_WriteCompare(0); PWM_1_direction_Write(0); PWM_2_WriteCompare(PWM_count); PWM_2_direction_Write(1); PWM_3_WriteCompare(PWM_count); PWM_3_direction_Write(1); PWM_4_WriteCompare(0); PWM_4_direction_Write(0); }else if(up_b == 1&& right_b == 1){ PWM_1_WriteCompare(PWM_count); PWM_1_direction_Write(1); PWM_2_WriteCompare(0); PWM_2_direction_Write(0); PWM_3_WriteCompare(0); PWM_3_direction_Write(0); PWM_4_WriteCompare(PWM_count); PWM_4_direction_Write(1); }else if(down_b == 1&& right_b==1){ PWM_1_WriteCompare(0); PWM_1_direction_Write(0); PWM_2_WriteCompare(PWM_count); PWM_2_direction_Write(0); PWM_3_WriteCompare(PWM_count); PWM_3_direction_Write(0); PWM_4_WriteCompare(0); PWM_4_direction_Write(0); }else if(down_b == 1&& left_b == 1){ PWM_1_WriteCompare(PWM_count); PWM_1_direction_Write(0); PWM_2_WriteCompare(0); PWM_2_direction_Write(0); PWM_3_WriteCompare(0); PWM_3_direction_Write(0); PWM_4_WriteCompare(PWM_count); PWM_4_direction_Write(0); } }else{} //if(USBUART_IsLineChanged() == USBUART_LINE_CODING_CHANGED || USBUART_IsLineChanged() == USBUART_LINE_CONTROL_CHANGED) //{ USBUART_PutCRLF(); //CyDelay(100); //} if(64 == count){ while(0 == USBUART_CDCIsReady()){} //sent zero packet USBUART_PutData(NULL,0); } //LED_Write(0); } }
/******************************************************************************* * Function Name: PWM_2_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_2_Start(). * * Parameters: * None * * Return: * None * *******************************************************************************/ void PWM_2_Init(void) { #if (PWM_2_UsingFixedFunction || PWM_2_UseControl) uint8 ctrl; #endif /* (PWM_2_UsingFixedFunction || PWM_2_UseControl) */ #if(!PWM_2_UsingFixedFunction) #if(PWM_2_UseStatus) /* Interrupt State Backup for Critical Region*/ uint8 PWM_2_interruptState; #endif /* (PWM_2_UseStatus) */ #endif /* (!PWM_2_UsingFixedFunction) */ #if (PWM_2_UsingFixedFunction) /* You are allowed to write the compare value (FF only) */ PWM_2_CONTROL |= PWM_2_CFG0_MODE; #if (PWM_2_DeadBand2_4) PWM_2_CONTROL |= PWM_2_CFG0_DB; #endif /* (PWM_2_DeadBand2_4) */ ctrl = PWM_2_CONTROL3 & ((uint8 )(~PWM_2_CTRL_CMPMODE1_MASK)); PWM_2_CONTROL3 = ctrl | PWM_2_DEFAULT_COMPARE1_MODE; /* Clear and Set SYNCTC and SYNCCMP bits of RT1 register */ PWM_2_RT1 &= ((uint8)(~PWM_2_RT1_MASK)); PWM_2_RT1 |= PWM_2_SYNC; /*Enable DSI Sync all all inputs of the PWM*/ PWM_2_RT1 &= ((uint8)(~PWM_2_SYNCDSI_MASK)); PWM_2_RT1 |= PWM_2_SYNCDSI_EN; #elif (PWM_2_UseControl) /* Set the default compare mode defined in the parameter */ ctrl = PWM_2_CONTROL & ((uint8)(~PWM_2_CTRL_CMPMODE2_MASK)) & ((uint8)(~PWM_2_CTRL_CMPMODE1_MASK)); PWM_2_CONTROL = ctrl | PWM_2_DEFAULT_COMPARE2_MODE | PWM_2_DEFAULT_COMPARE1_MODE; #endif /* (PWM_2_UsingFixedFunction) */ #if (!PWM_2_UsingFixedFunction) #if (PWM_2_Resolution == 8) /* Set FIFO 0 to 1 byte register for period*/ PWM_2_AUX_CONTROLDP0 |= (PWM_2_AUX_CTRL_FIFO0_CLR); #else /* (PWM_2_Resolution == 16)*/ /* Set FIFO 0 to 1 byte register for period */ PWM_2_AUX_CONTROLDP0 |= (PWM_2_AUX_CTRL_FIFO0_CLR); PWM_2_AUX_CONTROLDP1 |= (PWM_2_AUX_CTRL_FIFO0_CLR); #endif /* (PWM_2_Resolution == 8) */ PWM_2_WriteCounter(PWM_2_INIT_PERIOD_VALUE); #endif /* (!PWM_2_UsingFixedFunction) */ PWM_2_WritePeriod(PWM_2_INIT_PERIOD_VALUE); #if (PWM_2_UseOneCompareMode) PWM_2_WriteCompare(PWM_2_INIT_COMPARE_VALUE1); #else PWM_2_WriteCompare1(PWM_2_INIT_COMPARE_VALUE1); PWM_2_WriteCompare2(PWM_2_INIT_COMPARE_VALUE2); #endif /* (PWM_2_UseOneCompareMode) */ #if (PWM_2_KillModeMinTime) PWM_2_WriteKillTime(PWM_2_MinimumKillTime); #endif /* (PWM_2_KillModeMinTime) */ #if (PWM_2_DeadBandUsed) PWM_2_WriteDeadTime(PWM_2_INIT_DEAD_TIME); #endif /* (PWM_2_DeadBandUsed) */ #if (PWM_2_UseStatus || PWM_2_UsingFixedFunction) PWM_2_SetInterruptMode(PWM_2_INIT_INTERRUPTS_MODE); #endif /* (PWM_2_UseStatus || PWM_2_UsingFixedFunction) */ #if (PWM_2_UsingFixedFunction) /* Globally Enable the Fixed Function Block chosen */ PWM_2_GLOBAL_ENABLE |= PWM_2_BLOCK_EN_MASK; /* Set the Interrupt source to come from the status register */ PWM_2_CONTROL2 |= PWM_2_CTRL2_IRQ_SEL; #else #if(PWM_2_UseStatus) /* CyEnterCriticalRegion and CyExitCriticalRegion are used to mark following region critical*/ /* Enter Critical Region*/ PWM_2_interruptState = CyEnterCriticalSection(); /* Use the interrupt output of the status register for IRQ output */ PWM_2_STATUS_AUX_CTRL |= PWM_2_STATUS_ACTL_INT_EN_MASK; /* Exit Critical Region*/ CyExitCriticalSection(PWM_2_interruptState); /* Clear the FIFO to enable the PWM_2_STATUS_FIFOFULL bit to be set on FIFO full. */ PWM_2_ClearFIFO(); #endif /* (PWM_2_UseStatus) */ #endif /* (PWM_2_UsingFixedFunction) */ }
/******************************************************************************* * Function Name: PWM_2_RestoreConfig ******************************************************************************** * * Summary: * Restores the current user configuration of the component. * * Parameters: * None * * Return: * None * * Global variables: * PWM_2_backup: Variables of this global structure are used to * restore the values of non retention registers on wakeup from sleep mode. * *******************************************************************************/ void PWM_2_RestoreConfig(void) { #if(!PWM_2_UsingFixedFunction) #if (CY_UDB_V0) /* Interrupt State Backup for Critical Region*/ uint8 PWM_2_interruptState; /* Enter Critical Region*/ PWM_2_interruptState = CyEnterCriticalSection(); #if (PWM_2_UseStatus) /* Use the interrupt output of the status register for IRQ output */ PWM_2_STATUS_AUX_CTRL |= PWM_2_STATUS_ACTL_INT_EN_MASK; PWM_2_STATUS_MASK = PWM_2_backup.InterruptMaskValue; #endif /* (PWM_2_UseStatus) */ #if (PWM_2_Resolution == 8) /* Set FIFO 0 to 1 byte register for period*/ PWM_2_AUX_CONTROLDP0 |= (PWM_2_AUX_CTRL_FIFO0_CLR); #else /* (PWM_2_Resolution == 16)*/ /* Set FIFO 0 to 1 byte register for period */ PWM_2_AUX_CONTROLDP0 |= (PWM_2_AUX_CTRL_FIFO0_CLR); PWM_2_AUX_CONTROLDP1 |= (PWM_2_AUX_CTRL_FIFO0_CLR); #endif /* (PWM_2_Resolution == 8) */ /* Exit Critical Region*/ CyExitCriticalSection(PWM_2_interruptState); PWM_2_WriteCounter(PWM_2_backup.PWMUdb); PWM_2_WritePeriod(PWM_2_backup.PWMPeriod); #if(PWM_2_UseOneCompareMode) PWM_2_WriteCompare(PWM_2_backup.PWMCompareValue); #else PWM_2_WriteCompare1(PWM_2_backup.PWMCompareValue1); PWM_2_WriteCompare2(PWM_2_backup.PWMCompareValue2); #endif /* (PWM_2_UseOneCompareMode) */ #if(PWM_2_DeadBandMode == PWM_2__B_PWM__DBM_256_CLOCKS || \ PWM_2_DeadBandMode == PWM_2__B_PWM__DBM_2_4_CLOCKS) PWM_2_WriteDeadTime(PWM_2_backup.PWMdeadBandValue); #endif /* deadband count is either 2-4 clocks or 256 clocks */ #if ( PWM_2_KillModeMinTime) PWM_2_WriteKillTime(PWM_2_backup.PWMKillCounterPeriod); #endif /* ( PWM_2_KillModeMinTime) */ #endif /* (CY_UDB_V0) */ #if (CY_UDB_V1) #if(!PWM_2_PWMModeIsCenterAligned) PWM_2_WritePeriod(PWM_2_backup.PWMPeriod); #endif /* (!PWM_2_PWMModeIsCenterAligned) */ PWM_2_WriteCounter(PWM_2_backup.PWMUdb); #if (PWM_2_UseStatus) PWM_2_STATUS_MASK = PWM_2_backup.InterruptMaskValue; #endif /* (PWM_2_UseStatus) */ #if(PWM_2_DeadBandMode == PWM_2__B_PWM__DBM_256_CLOCKS || \ PWM_2_DeadBandMode == PWM_2__B_PWM__DBM_2_4_CLOCKS) PWM_2_WriteDeadTime(PWM_2_backup.PWMdeadBandValue); #endif /* deadband count is either 2-4 clocks or 256 clocks */ #if(PWM_2_KillModeMinTime) PWM_2_WriteKillTime(PWM_2_backup.PWMKillCounterPeriod); #endif /* (PWM_2_KillModeMinTime) */ #endif /* (CY_UDB_V1) */ #if(PWM_2_UseControl) PWM_2_WriteControlRegister(PWM_2_backup.PWMControlRegister); #endif /* (PWM_2_UseControl) */ #endif /* (!PWM_2_UsingFixedFunction) */ }