Example #1
0
void PID_init()
{
    parameter_.Kp = Kp_def;
    parameter_.Ki = Ki_def;
    parameter_.Kd = Kd_def;
    parameter_.MAX = MAX_def;
    parameter_.MIN = MIN_def;
    
    PWM_1_Start();
    PWM_1_WriteCompare((unsigned char)parameter_.MIN);
    
}
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);
}
Example #4
0
float *PID_tick(float sensor, float input, float RPM)
{
    err = (input - sensor);
    
    float P_RPM = (RPM <= 1.0f ? 0 : P_RPM_reciprocal / RPM) ;

    float PIDval = 0;
    
	//Proportional part
	PIDval += P_RPM*parameter_.Kp*err;
    
    //intergral part
    iState += P_RPM*parameter_.Kp*parameter_.Ki*err*dt + anti_windup_back_calc*dt;
	PIDval += iState; 
	
    //differentiel
	PIDval += P_RPM*parameter_.Kp*parameter_.Kd*((err-pre_err)/dt);
	pre_err = err;
	
    //anti windup back calculation
    anti_windup_back_calc = PIDval;
    
	if(PIDval > parameter_.MAX)
		PIDval = parameter_.MAX;
    else if(PIDval < parameter_.MIN)
        PIDval = parameter_.MIN;
	
    anti_windup_back_calc = PIDval - anti_windup_back_calc;
    // --- //
    
    //use PIDval
	PWM_1_WriteCompare((uint8)PIDval);
    
    //Debug
    PID_debug[0] = PIDval;
    PID_debug[1] = err;
    PID_debug[2] = anti_windup_back_calc;
    return PID_debug;
}
void piezo_stop()
{
    PWM_1_WriteCompare(0);
}
Example #6
0
/*******************************************************************************
* 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) */
    }
Example #7
0
/*******************************************************************************
* 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) */
}
Example #8
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);
                 
            }
           
}
Example #9
0
void main()
{
	CYGlobalIntEnable; /* Enable global interrupts */
	
	ADC_DelSig_1_Start();/* Configure and power up ADC */
	LCD_Char_1_Start(); /* Initialize and clear the LCD */
	
	/* Move the cursor to Row 0 Column 0 */
	LCD_Char_1_Position(ROW_0,COLUMN_0); 
	/* Print Label for the pot voltage raw count */
	LCD_Char_1_PrintString("TEMP NOW:    C");
	
	LCD_Char_1_Position(ROW_1,COLUMN_0);
	LCD_Char_1_PrintString("TEMP SET:    C");
	
	ADC_DelSig_1_StartConvert(); /* Force ADC to initiate a conversion */
	
	/* Start capsense and initialize baselines and enable scan */
	CapSense_Start();
	CapSense_InitializeAllBaselines();
	CapSense_ScanEnabledWidgets();

    /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */
	//Start the pwm;
	PWM_1_Start();
    for(;;)
    {
		/* If scanning is completed update the baseline count and check if sensor is active */
		while(CapSense_IsBusy());
		
		/* Update baseline for all the sensors */
		CapSense_UpdateEnabledBaselines();
		CapSense_ScanEnabledWidgets();
		
		/* Test if button widget is active */
		stateB_1 = CapSense_CheckIsWidgetActive(CapSense_BUTTON0__BTN);
		stateB_2 = CapSense_CheckIsWidgetActive(CapSense_BUTTON1__BTN);
	
        /* Wait for end of conversion */
		ADC_DelSig_1_IsEndConversion(ADC_DelSig_1_WAIT_FOR_RESULT);
		/* Get converted result */
		voltageRawCount = ADC_DelSig_1_GetResult16();
		//Change voltageRawCount to Temperature;
		temp = voltageRawCount / 3.870 * 0.1017 + 0.5;
		cold = (9999 - (temp > temp_set ? temp - temp_set : 0) * 50);
		if(cold < 1000)
			cold = 1000;
		if(cold > 9999)
			cold = 9999;
		//Change the pwm;
		PWM_1_WriteCompare(cold);
		/* Set range limit */
		if (temp > 0x7FFF)
		{
			temp = 0;
		}
		else
		{
		/* Continue on */
		}
		if(show < 10)
		{
			show++;
		}
		else
		{
			show = 0;
			UpdateDisplay(temp, 0); /* Print result on LCD */
			UpdateButtonState(stateB_1, stateB_2);
		}
    }
}