Ejemplo n.º 1
0
void Atomizer_Init() {
	// Select shunt value based on hardware version
	switch(Dataflash_info.hwVersion) {
		case 101:
		case 108:
			Atomizer_shuntRes = 125;
			break;
		case 103:
		case 104:
		case 105:
		case 106:
			Atomizer_shuntRes = 110;
			break;
		case 107:
		case 109:
			Atomizer_shuntRes = 120;
			break;
		case 110:
		case 111:
			Atomizer_shuntRes = 105;
			break;
		case 100:
		case 102:
		default:
			Atomizer_shuntRes = 115;
			break;
	}

	// Setup control pins
	PC1 = 0;
	GPIO_SetMode(PC, BIT1, GPIO_MODE_OUTPUT);
	PC3 = 0;
	GPIO_SetMode(PC, BIT3, GPIO_MODE_OUTPUT);

	// Both channels powered down
	Atomizer_ConfigureConverters(0, 0);

	// Configure 150kHz PWM
	PWM_ConfigOutputChannel(PWM0, ATOMIZER_PWMCH_BUCK, 150000, 0);
	PWM_ConfigOutputChannel(PWM0, ATOMIZER_PWMCH_BOOST, 150000, 0);

	// Start PWM
	PWM_EnableOutput(PWM0, PWM_CH_0_MASK);
	PWM_EnableOutput(PWM0, PWM_CH_2_MASK);
	PWM_Start(PWM0, PWM_CH_0_MASK);
	PWM_Start(PWM0, PWM_CH_2_MASK);

	// Set duty cycle to zero
	PWM_SET_CMR(PWM0, ATOMIZER_PWMCH_BUCK, 0);
	PWM_SET_CMR(PWM0, ATOMIZER_PWMCH_BOOST, 0);

	Atomizer_targetVolts = 0;
	Atomizer_curCmr = 0;
	Atomizer_curState = POWEROFF;

	// Setup 5kHz timer for negative feedback cycle
	// This function should run during system init, so
	// the user hasn't had time to create timers yet.
	Timer_CreateTimer(5000, 1, Atomizer_TimerCallback, 0);
}
Ejemplo n.º 2
0
/*************************************************************************************
 * This method calculates the voltage that should be output to the PWM peripheral.
 * It takes the reading from the light sensor, scales it appropriately, calculates
 * the P, I, and D values, converts it to the duty cycle value,  and outputs the
 * value to the PWM params.
 **************************************************************************************/
XStatus calc_PID()
{
    double deriv, integral, error, prev_error = 0;
    double volt_out;
    int duty_out;
    XStatus		Status;					// Xilinx return status

    // stabilize the PWM output (and thus the lamp intensity) at the
    // minimum before starting the test
    Status = PWM_SetParams(&PWMTimerInst, pwm_freq, dc_start);
    if (Status == XST_SUCCESS)
    {
        PWM_Start(&PWMTimerInst);
    }
    else
    {
        return -1;
    }
    //Wait for the LED output to settle before starting
    delay_msecs(1500);

    for (smpl_idx = 1; smpl_idx < NUM_FRQ_SAMPLES; smpl_idx++)
    {
        delay_msecs(100);

        // get count from light sensor and convert to voltage
        sample[smpl_idx] = LIGHTSENSOR_Capture(LIGHTSENSOR_BASEADDR, slope, offset, is_scaled, freq_min_cnt);
        volt_out = (-3.3 / 4095.0) * (sample[smpl_idx]) + 3.3;


        // calculate derivative;
        error = setpoint - volt_out;
        deriv = error - prev_error;

        // calculate integral
        if (error < setpoint/10) integral += error;
        else integral = 0;

        // Control offset is gotten from characterization
        volt_out = offset + (error * prop_gain) + (deriv * deriv_gain) + (integral * integral_gain);
        duty_out = (volt_out)* (MAX_DUTY+1)/VOLT_MAX;

        // establish bounds
        if (duty_out < 1) duty_out = 1;
        if (duty_out > 99)duty_out = 99;

        // activate PWM
        Status = PWM_SetParams(&PWMTimerInst, pwm_freq, duty_out);
        if (Status == XST_SUCCESS)
        {
            PWM_Start(&PWMTimerInst);
        }
    }
    return XST_SUCCESS;
}
Ejemplo n.º 3
0
/*****
 * DoTest_Step() - Perform the Step test
 * 
 * This function stabilizes the duty cycle at "dc_start" for
 * about a second and a half and then steps the duty cycle from min to max or
 * max to min depending on the test. NUM_FRQ_SAMPLES are collected
 * into the global array sample[].  An approximate sample interval
 * is written to the global variable "frq_smpl_interval"
 *****/ 
XStatus DoTest_Step(int dc_start)
{	
	XStatus		Status;					// Xilinx return status
	unsigned	tss;					// starting timestamp
	u16			frq_cnt;				// measured counts to display
		
	// stabilize the PWM output (and thus the lamp intensity) before
	// starting the test
	Status = PWM_SetParams(&PWMTimerInst, pwm_freq, dc_start);
	if (Status == XST_SUCCESS)
	{							
		PWM_Start(&PWMTimerInst);
	}
	else
	{
		return XST_FAILURE;
	}
	//Wait for the LED output to settle before starting
    delay_msecs(1500);
		
	if (dc_start > STEPDC_MAX / 2)
	{
		 Status = PWM_SetParams(&PWMTimerInst, pwm_freq, STEPDC_MIN); 
	}
	else
	{
		 Status = PWM_SetParams(&PWMTimerInst, pwm_freq, STEPDC_MAX); 
	}		
	if (Status == XST_SUCCESS)
	{							
		PWM_Start(&PWMTimerInst);
		pwm_duty = dc_start;
	}
	else
	{
		return XST_FAILURE;
	}
	
	// gather the samples
	smpl_idx = 0;
	tss = timestamp;
	while (smpl_idx < NUM_FRQ_SAMPLES)
	{
	
		//ECE544 Students:
        //make the light sensor measurement
		sample[smpl_idx++] = LIGHTSENSOR_Capture(LIGHTSENSOR_BASEADDR, slope, offset, is_scaled);
		
	}		
	frq_smple_interval = (timestamp - tss) / NUM_FRQ_SAMPLES;
	return XST_SUCCESS;
}
Ejemplo n.º 4
0
void motorsStart()
{
#ifdef M451
	PWM_Start(PWM0, PWM_CH_0_MASK);
	PWM_Start(PWM0, PWM_CH_1_MASK);
	PWM_Start(PWM0, PWM_CH_2_MASK);
	PWM_Start(PWM0, PWM_CH_3_MASK);
#else
	DrvPWM_Enable(DRVPWM_TIMER0, 1);
	DrvPWM_Enable(DRVPWM_TIMER1, 1);
	DrvPWM_Enable(DRVPWM_TIMER2, 1);
	DrvPWM_Enable(DRVPWM_TIMER3, 1);
#endif
}
Ejemplo n.º 5
0
XStatus calc_bang()
{
    Xfloat32 volt_out;
    XStatus		Status;					// Xilinx return status

    // stabilize the PWM output (and thus the lamp intensity) at the
    // minimum before starting the test
    Status = PWM_SetParams(&PWMTimerInst, pwm_freq, dc_start);
    if (Status == XST_SUCCESS)
    {
        PWM_Start(&PWMTimerInst);
    }
    else
    {
        return -1;
    }

    //Wait for the LED output to settle before starting
    delay_msecs(1500);

    for (smpl_idx = 1; smpl_idx < NUM_FRQ_SAMPLES; smpl_idx++)
    {
        // get count from light sensor and convert to voltage
        sample[smpl_idx] = LIGHTSENSOR_Capture(LIGHTSENSOR_BASEADDR, slope, offset, is_scaled, freq_min_cnt);

        volt_out = (-3.3 / 4095.0) * (sample[smpl_idx]) + 3.3;

        if (volt_out < setpoint)
        {
            Status = PWM_SetParams(&PWMTimerInst, pwm_freq, MAX_DUTY);
            delay_msecs(1);
            if (Status == XST_SUCCESS)
            {
                PWM_Start(&PWMTimerInst);
            }
        }
        else
        {
            Status = PWM_SetParams(&PWMTimerInst, pwm_freq, MIN_DUTY);
            delay_msecs(1);
            if (Status == XST_SUCCESS)
            {
                PWM_Start(&PWMTimerInst);
            }
        }
        delay_msecs(100);
    }
    return XST_SUCCESS;
}
Ejemplo n.º 6
0
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
    /* Init System, IP clock and multi-function I/O
       In the end of SYS_Init() will issue SYS_LockReg()
       to lock protected register. If user want to write
       protected register, please issue SYS_UnlockReg()
       to unlock protected register if necessary */

    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Init System, IP clock and multi-function I/O */
    SYS_Init();

    /* Lock protected registers */
    SYS_LockReg();

    /* Init UART to 115200-8n1 for print message */
    UART0_Init();

    printf("\n\nCPU @ %dHz(PLL@ %dHz)\n", SystemCoreClock, PllClock);
    printf("PWM0 clock is from %s\n", (CLK->CLKSEL2 & CLK_CLKSEL2_PWM0SEL_Msk) ? "CPU" : "PLL");
    printf("+------------------------------------------------------------------------+\n");
    printf("|                          PWM Driver Sample Code                        |\n");
    printf("|                                                                        |\n");
    printf("+------------------------------------------------------------------------+\n");
    printf("  This sample code will output PWM0 channel 0~3 with different\n");
    printf("  frequency and duty, enable dead zone function of all PWM0 pairs.\n");
    printf("  And also enable/disable PWM output every 1 second.\n");
    printf("  I/O configuration:\n");
    printf("    waveform output pin: PWM0_CH0(PC.0), PWM0_CH1(PC.1), PWM0_CH2(PC.2), PWM0_CH3(PC.3)\n");

    /*Set Pwm mode as complementary mode*/
    PWM_ENABLE_COMPLEMENTARY_MODE(PWM0);

    // PWM0 channel 0 frequency is 100Hz, duty 30%,
    PWM_ConfigOutputChannel(PWM0, 0, 100, 30);
    SYS_UnlockReg();
    PWM_EnableDeadZone(PWM0, 0, 400);
    SYS_LockReg();

    // PWM0 channel 2 frequency is 300Hz, duty 50%
    PWM_ConfigOutputChannel(PWM0, 2, 300, 50);
    SYS_UnlockReg();
    PWM_EnableDeadZone(PWM0, 2, 200);
    SYS_LockReg();

    // Enable output of PWM0 channel 0~3
    PWM_EnableOutput(PWM0, 0xF);

    // Enable PWM0 channel 0 period interrupt, use channel 0 to measure time.
    PWM_EnablePeriodInt(PWM0, 0, 0);
    NVIC_EnableIRQ(PWM0P0_IRQn);

    // Start
    PWM_Start(PWM0, 0xF);

    while(1);

}
Ejemplo n.º 7
0
/*****
 * DoTest_Track() - Perform the Tracking test
 * 
 * This function uses the global "pwm_freq" and "pwm_duty" values to adjust the PWM
 * duty cycle and thus the intensity of the LED.  The function displays
 * the light detector reading as it tracks changes in the
 * LED intensity.  This test runs continuously until a different test is selected.
 * Returns XST_SUCCESS since this test can't fail.  Returns approximate sample interval
 * in the global variable "frq_sample_interval"
 *****/ 
XStatus DoTest_Track(void)
{
	static int		old_pwm_freq = 0;			// old pwm_frequency and duty cycle
	static int		old_pwm_duty = 200;			// these values will force the initial display	
	volatile u16	frq_cnt;					// light detector counts to display
	XStatus			Status;						// Xilinx return status
	unsigned		tss;						// starting timestamp			

	if ((pwm_freq != old_pwm_freq) || (pwm_duty != old_pwm_duty))
	{	
		// set the new PWM parameters - PWM_SetParams stops the timer
		Status = PWM_SetParams(&PWMTimerInst, pwm_freq, pwm_duty);
		if (Status == XST_SUCCESS)
		{							
			PWM_Start(&PWMTimerInst);
		}

		tss = timestamp;	
		
		//ECE544 Students:
        //make the light sensor measurement
		frq_cnt = LIGHTSENSOR_Capture(LIGHTSENSOR_BASEADDR, slope, offset, is_scaled);

		delay_msecs(1);
		frq_smple_interval = timestamp - tss;
				
		// update the display and save the frequency and duty
		// cycle for next time
		update_lcd(pwm_duty, frq_cnt);
		old_pwm_freq = pwm_freq;
		old_pwm_duty = pwm_duty;
	}
	return XST_SUCCESS;
}
Ejemplo n.º 8
0
void main(void)
{
	M8C_EnableGInt; // Uncomment this line to enable Global Interrupts
	CPU_F |= 0b00000001;
	INT_MSK1 |= 0b00000001;
	//Insert your main routine code here.
	
	//Enable PWM
	PWM_Start();
	PWM_EnableInt();
	
	
	//Enable LCD for debugging
	LCD_Start();
	LCD_Init();
	
	
	while(1)//Control loop
	{
		LCD_Position(0,0);
		LCD_PrHexByte(PWM_bReadPulseWidth());
		LCD_Position(1,0);
		LCD_PrHexByte(ten_ms_counter);
		LCD_Position(0,6);
		LCD_PrHexByte(PWM_bReadCounter()); 
	}//End Control Loop
}
Ejemplo n.º 9
0
WEAK void error(const char* format, ...) {
#if DEVICE_STDIO_MESSAGES
    va_list arg;
    va_start(arg, format);
    vfprintf(stderr, format, arg);
    va_end(arg);
#endif
    
#ifndef EMUNO

    PWM_Start();
    PWM_WriteCompare2(64);
    CyDelay(200);
    PWM_WriteCompare2(0);
    CyDelay(600);
    PWM_WriteCompare2(64);
    CyDelay(200);
    PWM_WriteCompare2(0);
    
    exit(1);
    
#else
    
    printf("EMUNO HALT!");
    while(1);
    
#endif
    
//    Bootloadable_SET_RUN_TYPE(Bootloadable_START_APP);
//    CySoftwareReset();
}
Ejemplo n.º 10
0
WEAK void mbed_die(void) {
    
#ifndef EMUNO
    CyHalt(1);
    PWM_Start();
    PWM_WriteCompare2(0);
    CyPins_SetPinDriveMode(SW_USER, CY_PINS_DM_RES_UP);
    Bootloadable_SET_RUN_TYPE(0); // do not force bootloader
    
    while(1)
    {
        PWM_WriteCompare1(16);
        CyDelay(250);
        PWM_WriteCompare1(64);
        
        if (CyPins_ReadPin(SW_USER) == 0)
        {
            CyDelay(200); // allow yser to release btn, to not go into bootloader
            CySoftwareReset();
        }
        
        CyDelay(400);
        
        if (CyPins_ReadPin(SW_USER) == 0)
        {
            CyDelay(200); // allow yser to release btn, to not go into bootloader
            CySoftwareReset();
        }
    }
#endif

}
Ejemplo n.º 11
0
void Motor_Start(void)
{
	
	PWM_EnableOutput(PWM, 0x33);
	PWM_Start(PWM, 0x33);
	//MotorPwmOutput(0,0,0,0);
	//g_fSpeedControlIntegral = 0;
}
Ejemplo n.º 12
0
void MOTOR_Start(Motor motor, uint32_t speed) {

	int j = 0;

	if(motor == RIGHT_MOTOR) {

		PWM_Start(PWM1);
		for(j = 0 ; j < 1000 ; j++) {	}
		PWM_SetDutyCycle(PWM1, speed);

	} else if(motor == LEFT_MOTOR) {

		PWM_Start(PWM2);
		for(j = 0 ; j < 1000 ; j++) {	}
		PWM_SetDutyCycle(PWM2, speed);
	}
}
Ejemplo n.º 13
0
void MOTOR_Start(uint32_t motor, uint32_t speed) {

	int j = 0;

	if(motor == MOTOR_RIGHT) {

		PWM_Start(PWM1);
		for(j = 0 ; j < 1000 ; j++) {	}
		PWM_SetDutyCycle(PWM1, speed);

	} else if(motor == MOTOR_LEFT) {

		PWM_Start(PWM2);
		for(j = 0 ; j < 1000 ; j++) {	}
		PWM_SetDutyCycle(PWM2, speed);
	}
}
Ejemplo n.º 14
0
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Init System, IP clock and multi-function I/O */
    SYS_Init();

    /* Lock protected registers */
    SYS_LockReg();

    /* Init UART to 115200-8n1 for print message */
    UART0_Init();
    printf("+------------------------------------------------------------------------+\n");
    printf("|                          PWM Driver Sample Code                        |\n");
    printf("|                                                                        |\n");
    printf("+------------------------------------------------------------------------+\n");
    printf("  This sample code will use PWM0 channel 0 to output waveform\n");
    printf("  I/O configuration:\n");
    printf("    waveform output pin: PWM0 channel 0(PA.12)\n");
    printf("\nUse double buffer feature.\n");

    /*
        PWM0 channel 0 waveform of this sample shown below:

        |<-        CNR + 1  clk     ->|  CNR + 1 = 399 + 1 CLKs
                       |<-CMR+1 clk ->|  CMR + 1 = 199 + 1 CLKs
                                      |<-   CNR + 1  ->|  CNR + 1 = 99 + 1 CLKs
                                               |<CMR+1>|  CMR + 1 = 39 + 1 CLKs
      __                ______________          _______
        |______200_____|     200      |____60__|   40  |_____PWM waveform

    */


    /*
      Configure PWM0 channel 0 init period and duty.
      Period is __HXT / (prescaler * clock divider * (CNR + 1))
      Duty ratio = (CMR + 1) / (CNR + 1)
      Period = 12 MHz / (2 * 1 * (199 + 1)) =  30000 Hz
      Duty ratio = (99 + 1) / (199 + 1) = 50%
    */
    // PWM0 channel 0 frequency is 100Hz, duty 30%,
    PWM_ConfigOutputChannel(PWM0, 0, 30000, 30);

    // Enable output of PWM0 channel 0
    PWM_EnableOutput(PWM0, PWM_CH_0_MASK);

    // Enable PWM0 channel 0 period interrupt, use channel 0 to measure time.
    PWM_EnablePeriodInt(PWM0, 0, 0);
    NVIC_EnableIRQ(PWM0_IRQn);

    // Start
    PWM_Start(PWM0, PWM_CH_0_MASK);

    while(1);

}
Ejemplo n.º 15
0
// ----------------------------------------------------------------------------------------
//  Start PWM Output
// ----------------------------------------------------------------------------------------
void Buzzer_Alerm()
{
		BuzzerExecuteFlag=1;
		/* set PWMB channel 0 output configuration */
		PWM_ConfigOutputChannel(PWM0, 0, 1200, 20);
		/* Enable PWM Output path for PWMB channel 0 */
		PWM_EnableOutput(PWM0, PWM_CH_0_MASK);
	  // Start
    PWM_Start(PWM0, PWM_CH_0_MASK);
}
Ejemplo n.º 16
0
int main()
{
    /* Place your initialization/startup code here (e.g. MyInst_Start()) */
    PWM_Start();

    /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */
    for(;;)
    {
        /* Place your application code here. */
    }
}
Ejemplo n.º 17
0
//=========================================================================
//----- (00005C4C) --------------------------------------------------------
__myevic__ void InitPWM()
{
	PWM_ConfigOutputChannel( PWM0, BBC_PWMCH_BUCK, BBC_PWM_FREQ, 0 );
	PWM_ConfigOutputChannel( PWM0, BBC_PWMCH_BOOST, BBC_PWM_FREQ, 0 );

	PWM_EnableOutput( PWM0, 1 << BBC_PWMCH_BUCK );
	PWM_EnablePeriodInt( PWM0, BBC_PWMCH_BUCK, 0 );

	PWM_EnableOutput( PWM0, 1 << BBC_PWMCH_BOOST );
	PWM_EnablePeriodInt( PWM0, BBC_PWMCH_BOOST, 0 );

	PWM_Start( PWM0, 1 << BBC_PWMCH_BUCK );
	PWM_Start( PWM0, 1 << BBC_PWMCH_BOOST );

	BoostDuty = 0;
	PWM_SET_CMR( PWM0, BBC_PWMCH_BOOST, 0 );

	BuckDuty = 0;
	PWM_SET_CMR( PWM0, BBC_PWMCH_BUCK, 0 );

	if ( ISVTCDUAL || ISCUBOID || ISCUBO200 || ISRX200S || ISRX23 || ISRX300 )
	{
		PWM_ConfigOutputChannel( PWM0, BBC_PWMCH_CHARGER, BBC_PWM_FREQ, 0 );
		PWM_EnableOutput( PWM0, 1 << BBC_PWMCH_CHARGER );
		PWM_Start( PWM0, 1 << BBC_PWMCH_CHARGER );

		ChargerDuty = 0;
		PWM_SET_CMR( PWM0, BBC_PWMCH_CHARGER, 0 );

		if ( ISCUBO200 || ISRX200S || ISRX23 || ISRX300 )
		{
			MaxChargerDuty = 512;
		}
		else
		{
			MaxChargerDuty = 256;
		}
	}
}
Ejemplo n.º 18
0
int main()
{
    CyGlobalIntEnable; /* Enable global interrupts. */

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

    for(;;)
    {
        PWM_Start();
        CyDelay(500);
        PWM_Stop();
        CyDelay(500);
    }
}
Ejemplo n.º 19
0
/*---------------------------------------------------------------------------------------------------------*/
int32_t PWM_DeadZone(void)
{
    /* Init System, IP clock and multi-function I/O
       In the end of SYS_Init() will issue SYS_LockReg()
       to lock protected register. If user want to write
       protected register, please issue SYS_UnlockReg()
       to unlock protected register if necessary */

    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Init System, IP clock and multi-function I/O */
    SYS_Init();

    /* Lock protected registers */
    SYS_LockReg();

    /* Init UART to 115200-8n1 for print message */
    UART_Open(UART0, 115200);
    printf("+------------------------------------------------------------------------+\n");
    printf("|                          PWM Driver Sample Code                        |\n");
    printf("|                                                                        |\n");
    printf("+------------------------------------------------------------------------+\n");
    printf("  This sample code will output all PWMA channels with different\n");
    printf("  frequency and duty, enable dead zone function of all PWMA pairs.\n");
    printf("  And also enable/disable PWM output every 1 second.\n");
    printf("  I/O configuration:\n");
    printf("    waveform output pin: PWM0(P2.0), PWM1(P2.1), PWM2(P2.2), PWM3(P2.3)\n");

    // PWM0 frequency is 100Hz, duty 30%,
    PWM_ConfigOutputChannel(PWMA, PWM_CH0, 100, 30);
    PWM_EnableDeadZone(PWMA, PWM_CH0, 400);

    // PWM2 frequency is 300Hz, duty 50%
    PWM_ConfigOutputChannel(PWMA, PWM_CH2, 300, 50);
    PWM_EnableDeadZone(PWMA, PWM_CH2, 200);

    // Enable output of all PWMA channels
    PWM_EnableOutput(PWMA, 0xF);

    // Enable PWMA channel 0 period interrupt, use channel 0 to measure time.
    PWM_EnablePeriodInt(PWMA, PWM_CH0, 0);
    NVIC_EnableIRQ(PWMA_IRQn);

    // Start
    PWM_Start(PWMA, 0xF);

    while(1);

}
Ejemplo n.º 20
0
/**
 * @brief This function enable PWM1 module clock and set clock source
                    to start Buzzer module
 * @return None
 */
void Open_Buzzer(void)
{

    Initial_PWM_GPIO();
    Buzzer_Power_ON;

    /* Enable PWM clock */
    CLK_EnableModuleClock(PWM1_CH01_MODULE);

    /* Set HCLK as PWM clock source */
    CLK_SetModuleClock(PWM1_CH01_MODULE, (0x2UL<<4), 0);

    /* Start PWM1 channel 1 */
    PWM_Start(PWM1, 0x02);
}
Ejemplo n.º 21
0
int main()
{
    
	uint8 counter = 0;	
	
	/* Enable global interrupts*/
	CyGlobalIntEnable;
	
	/* Check if the switch is pressed during power up */ 
	if(Boot_P0_7_Read() == 0)
	{
		
		for(counter = 0; counter <= SWITCH_PRESS_TIMEOUT; counter++)
		{
			/* Delay for 1ms */
			//CyDelay(1);
			CyDelay(2);
				
			/* If the switch is released before specified time, do not enter the 
			 * bootloader */
			if(Boot_P0_7_Read() != 0)
				break;
		}
						 
		if(counter > SWITCH_PRESS_TIMEOUT)
		{
			/* If the switch was pressed for more than 100 millisecond counter 
			 * value will be 100. If so, set the flash run type as bootloader to 
			 * wait for a bootload operation */
			Bootloader_SET_RUN_TYPE (Bootloader_START_BTLDR);
		}
	}
		
	/*Indicate that you have entered the bootloader mode.*/
	PWM_Start();
	
	/* Start the Bootloader */
	Bootloader_Start();
	/* The “Bootloader_Start()” API will either wait for a bootload operation or 
	 * will launch the application depending on whether or not the API
	 * "Bootloader_SET_RUN_TYPE(Bootloader_START_BTLDR)" was called because of the 
	 * switch being pressed during power up */
			
    for(;;)
    {
        
    }
}
Ejemplo n.º 22
0
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);
    }
}
Ejemplo n.º 23
0
int32_t main (void)
{
    /* Init System, IP clock and multi-function I/O
       In the end of SYS_Init() will issue SYS_LockReg()
       to lock protected register. If user want to write
       protected register, please issue SYS_UnlockReg()
       to unlock protected register if necessary */
    SYS_Init();

    /* Init UART to 115200-8n1 for print message */
    UART_Open(UART0, 115200);

    printf("\nThis sample code demonstrate PWM channel 0 trigger ADC function\n");

    /* Enable channel 5 */
    ADC_Open(ADC, 0, 0, 0x01 << 5);

    /* Power on ADC */
    ADC_POWER_ON(ADC);

    /* Enable PWM trigger */
    ADC_EnableHWTrigger(ADC, ADC_TRIGGER_BY_PWM, ADC_FALLING_EDGE_TRIGGER);

    /* Enable ADC convert complete interrupt  */
    ADC_EnableInt(ADC, ADC_ADIF_INT);
    NVIC_EnableIRQ(ADC_IRQn);

    /* PWM frequency is 100Hz, duty 30% */
    PWM_ConfigOutputChannel(PWM, 0, 100, 30);
    /* Enable output PWM channel 0 */
    PWM_EnableOutput(PWM, 0x1);

    /* Set PWM channel 0 to center-aligned mode */
    PWM_SET_ALIGNED_TYPE(PWM, 0, PWM_CENTER_ALIGNED);

    /* Enable PWM channel 0 center-triggered ADC */
    PWM_EnableADCTrigger(PWM, 0, PWM_TRIGGER_ADC_CNTR_IS_CNR);

    /* PWM Start */
    PWM_Start(PWM, 0x1);

    while(1);

}
Ejemplo n.º 24
0
void Initial()
{
	CyGlobalIntEnable;
    //Check_Boot();
    Timer_RTOS_Start();
    PWM_Start();
    I2C_Start();
    UART_Start();
    RTC_Start();
    RTC_SetPeriod(1,1000);
    RTOS_Start();
    SetTask(Brightles_PID_Controll);
    LCD_WS0010Start();
    SetTask(WaitPowerStab);
    SetTimerTask(OPT_Get_Result, 1300);
    //BLE_Status();
    GlobalStruct.OS_Status = 0;
    SetTimerTask(Display_Controll, 2000);
}
Ejemplo n.º 25
0
int32_t PWM_DeadZone(void)
{
    /* Init System, IP clock and multi-function I/O
       In the end of SYS_Init() will issue SYS_LockReg()
       to lock protected register. If user want to write
       protected register, please issue SYS_UnlockReg()
       to unlock protected register if necessary */
    SYS_Init();

    /* Init UART to 115200-8n1 for print message */
    UART0_Init();

    printf("\nThis sample code will output PWM0 channel 0 to with different\n");
    printf("frequency and duty, enable dead zone function of all PWM0 pairs.\n");
    printf("And also enable/disable PWM0 output every 1 second.\n");
    // PWM0 frequency is 100Hz, duty 30%,
    PWM_ConfigOutputChannel(PWM0, 0, 100, 30);
    PWM_EnableDeadZone(PWM0, 0, 400);

    // PWM2 frequency is 300Hz, duty 50%
    PWM_ConfigOutputChannel(PWM0, 2, 300, 50);
    PWM_EnableDeadZone(PWM0, 2, 200);

    // PWM4 frequency is 500Hz, duty 70%
    PWM_ConfigOutputChannel(PWM0, 4, 600, 70);
    PWM_EnableDeadZone(PWM0, 4, 100);

    // Enable complementary mode
    PWM_ENABLE_COMPLEMENTARY_MODE(PWM0);

    // Enable output of all PWM channels
    PWM_EnableOutput(PWM0, 0x3F);

    // Enable PWM channel 0 period interrupt, use channel 0 to measure time.
    PWM_EnablePeriodInt(PWM0, 0, 0);
    NVIC_EnableIRQ(PWM0CH0_IRQn);

    // Start
    PWM_Start(PWM0, 0x3F);

    while(1);

}
Ejemplo n.º 26
0
/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*  Main function.
*
* Parameters:
*  None
*
* Return:
*  None
*
*******************************************************************************/
int main()
{
    CYBLE_API_RESULT_T apiResult;
    CYBLE_STATE_T bleState;

    CyGlobalIntEnable;
	
    PWM_Start();
	UART_Start();
	UART_UartPutString("Welcome to BLE OOB Pairing Demo\r\n");

    apiResult = CyBle_Start(StackEventHandler);

    if(apiResult != CYBLE_ERROR_OK)
    {
        /* BLE stack initialization failed, check your configuration */
        CYASSERT(0);
    }

    CyBle_IasRegisterAttrCallback(IasEventHandler);

    for(;;)
    {
        /* Single API call to service all the BLE stack events. Must be
         * called at least once in a BLE connection interval */
        CyBle_ProcessEvents();

        bleState = CyBle_GetState();

        if(bleState != CYBLE_STATE_STOPPED &&
            bleState != CYBLE_STATE_INITIALIZING)
        {
            /* Configure BLESS in DeepSleep mode  */
            CyBle_EnterLPM(CYBLE_BLESS_DEEPSLEEP);

            /* Configure PSoC 4 BLE system in sleep mode */
            CySysPmSleep();

            /* BLE link layer timing interrupt will wake up the system */
        }
    }
}
Ejemplo n.º 27
0
/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*  Main function performs following functions:
*   1. Start the LCD and PWM
*   2. Print 'Hello World' on the LCD
*
* Parameters:
*  None.
*
* Return:
*  None.
*
*******************************************************************************/
int main()
{
	/* Start LCD and PWM */
	LCD_Char_Start();
	PWM_Start();
	
	/* Print Hello World on LCD */

    /* Loop forever */
    char vout[15] = {'\0'};
	for(;;)
    {
        // TODO: get voltage at pot and control period of PWM using
        // PWM_WritePeriod(period)
        sprintf(vout, "%f", ADC_SAR_1_GetResult16());
        LCD_Char_PrintString(vout);
        vout[0] = '\0';
        
    }
}
Ejemplo n.º 28
0
/**
 * function that will change the frequency of the SCT.
 *
 * this function will stop the SCT output, first, set the frequency, and startup the sct again
 * @param p_pwm		the low level driver struct for the PWM that needs to be changed
 * @param frequency	the wanted frequency in Hz (0 - MAX_FREQUENCY)
 * @return	status_ok if succeeded (otherwise check status.h for details).
 */
status_t PWM_SetFrequency(pwm_t* p_pwm, uint32_t frequency)
{
	status_t status = status_ok;
	uint8_t i;

	p_pwm->frequency = frequency;
	if(frequency <= MAX_FREQUENCY)
	{
		/* first stop the sct */
		status = PWM_Stop();
		/* then set the rate */
		if(status == status_ok)
		{
			Chip_SCTPWM_SetRate(PWM_SCT, frequency);
		}
		/* set dutycycles */
		/* then set the dutycycles */
		for(i = 0; i < PWM_AMOUNTOFCHANNELS; i++)
		{
			if(status == status_ok)
			{
				if(p_pwm->usechannel[i])
				{
					status = PWM_setdutycycle(p_pwm, i, p_pwm->dutycycle[i]);
				}
			}
		}

		/* If ok, start the pwm */
		if(status == status_ok)
		{
			status = PWM_Start();
		}
	}
	else
	{
		status = pwm_frequency;
	}
	return status;
}
Ejemplo n.º 29
0
/******************************************************************************
**   Main Function  main()
******************************************************************************/
int main (void)
{	    
    unsigned long cycle = PWM_CYCLE, offset = 0;

    INIT_LEDS();           // initialize the GPIO's connected to LEDs
    turn_off_all_leds();   // Turnoff all LEDs
    init_VIC();            // Initialize Vectored interrupts

    if ( PWM_Init( 0 ) != TRUE )
    {
		while( 1 );			/* fatal error */
    }

    PWM_Set( cycle, offset );
    PWM_Start();

    while ( 1 )
    {
		if ( match_counter != 0 )
		{
		    match_counter = 0;
		    if( offset <= PWM_CYCLE )
			{
				offset += PWM_OFFSET;
			}
		    else
			{
				offset = 0;
			}
	    	PWM_Set( cycle, offset );
		}
	}
    PWM_Stop() ;
	
    return 0;
}
Ejemplo n.º 30
0
int main()
{
	XStatus 	Status;
	u32			btnsw, old_btnsw = 0x000000FF;
	int			rotcnt, old_rotcnt = 0x1000;	
	bool		done = false;
	
	// initialize devices and set up interrupts, etc.
 	Status = do_init();
 	if (Status != XST_SUCCESS)
 	{
 		LCD_setcursor(1,0);
 		LCD_wrstring("****** ERROR *******");
 		LCD_setcursor(2,0);
 		LCD_wrstring("INIT FAILED- EXITING");
 		exit(XST_FAILURE);
 	}
 	
	// initialize the global variables
	timestamp = 0;							
	pwm_freq = INITIAL_FREQUENCY;
	pwm_duty = INITIAL_DUTY_CYCLE;
	clkfit = 0;
	new_perduty = false;
    
	// start the PWM timer and kick of the processing by enabling the Microblaze interrupt
	PWM_SetParams(&PWMTimerInst, pwm_freq, pwm_duty);	
	PWM_Start(&PWMTimerInst);
    microblaze_enable_interrupts();
    
	// display the greeting   
    LCD_setcursor(1,0);
    LCD_wrstring(" PWM LIB TEST R0");
	LCD_setcursor(2,0);
	LCD_wrstring(" by Roy Kravitz ");
	NX3_writeleds(0x000000FF);
	delay_msecs(2000);
	NX3_writeleds(0x00000000);
		
   // write the static text to the display
    LCD_clrd();
    LCD_setcursor(1,0);
    LCD_wrstring("G|FR:    DCY:  %");
    LCD_setcursor(2,0);
    LCD_wrstring("Vavg:           ");
      
    // main loop
	do
	{ 
		// check rotary encoder pushbutton to see if it's time to quit
		NX3_readBtnSw(&btnsw);
		if (btnsw & msk_BTN_ROT)
		{
			done = true;
		}
		else
		{
			new_perduty = false;				
			if (btnsw != old_btnsw)
			{	 
				switch (btnsw & PWM_FREQ_MSK)
				{
					case 0x00:	pwm_freq = PWM_FREQ_10HZ;	break;
					case 0x01:	pwm_freq = PWM_FREQ_100HZ;	break;
					case 0x02:	pwm_freq = PWM_FREQ_1KHZ;	break;
					case 0x03:	pwm_freq = PWM_FREQ_10KHZ;	break;
                                        case 0x04: 	pwm_freq = PWM_FREQ_50KHZ;	break;
                                        case 0x05: 	pwm_freq = PWM_FREQ_100KHZ;	break;
                                        case 0x06: 	pwm_freq = PWM_FREQ_150KHZ;	break;
                                        case 0x07: 	pwm_freq = PWM_FREQ_200KHZ;	break;
				}
				old_btnsw = btnsw;
				new_perduty = true;
			}
		
			// read rotary count and handle duty cycle changes
			// limit duty cycle to 0% to 99%
			ROT_readRotcnt(&rotcnt);
			if (rotcnt != old_rotcnt)
			{
				pwm_duty = MAX(0, MIN(rotcnt, 99));
				old_rotcnt = rotcnt;
				new_perduty = true;
			}

			// update generated frequency and duty cycle	
			if (new_perduty)
			{
				u32 freq, dutycycle;
				float vavg;
				char	s[10];
			
				// set the new PWM parameters - PWM_SetParams stops the timer
				Status = PWM_SetParams(&PWMTimerInst, pwm_freq, pwm_duty);
				if (Status == XST_SUCCESS)
				{
					PWM_GetParams(&PWMTimerInst, &freq, &dutycycle);
					update_lcd(freq, dutycycle, 1);
					vavg = dutycycle * .01 * 3.3;
					voltstostrng(vavg, s);
					LCD_setcursor(2,5);
					LCD_wrstring(s); 
										
					PWM_Start(&PWMTimerInst);
				}
			}
		}			
	} while (!done);
	
	// wait until rotary encoder button is released		
	do
	{
		NX3_readBtnSw(&btnsw);
		delay_msecs(10);
	} while ((btnsw & msk_BTN_ROT) == 0x80);

	// and say goodbye
	LCD_clrd();
	LCD_wrstring("That's all folks");
	delay_msecs(2000);
	LCD_clrd();
	exit(XST_SUCCESS);
 }