// --------------------------------------------------
// Initialization of the backlight LCD. Using PWM.
void Init_LCD_BackLight(void)
{
    uint32_t pclk;
	PWM_TIMERCFG_Type PWMCfgDat;
	PWM_MATCHCFG_Type PWMMatchCfgDat;

	pclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
	PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
	PWMCfgDat.PrescaleValue = 1;
	PWM_Init(LCD_BL_PWM_PERI_ID, PWM_MODE_TIMER, (void *) &PWMCfgDat);
	PINSEL_ConfigPin(LCD_BL_PWM_PORT, LCD_BL_PWM_PIN, 1);		// funct.no.1 - PWM1[2] — Pulse Width Modulator 1, channel 2 output.
	PWM_MatchUpdate(LCD_BL_PWM_PERI_ID, LCD_BL_PWM_PERI_CHA, pclk / LCD_BL_PWM_BASE, PWM_MATCH_UPDATE_NOW);

	// UPDATE VALUE OF THE PWM DUTY CYCLE
	PWM_MatchUpdate(LCD_BL_PWM_PERI_ID, LCD_BL_PWM_PERI_CHB , 0 *(( pclk / LCD_BL_PWM_BASE) / 100), PWM_MATCH_UPDATE_NOW); // switch off backlight
	PWMMatchCfgDat.IntOnMatch = DISABLE;							// without interrupt
	PWMMatchCfgDat.MatchChannel = LCD_BL_PWM_PERI_CHB;			// Match channel register - duty cycle	- xx %
	PWMMatchCfgDat.ResetOnMatch = DISABLE;						//
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(LCD_BL_PWM_PERI_ID, &PWMMatchCfgDat);		// store it
	PWM_ChannelCmd(LCD_BL_PWM_PERI_ID, LCD_BL_PWM_PERI_CHB, ENABLE);	// Enable PWM Channel Output

	PWM_ResetCounter(LCD_BL_PWM_PERI_ID);						// reset and start counter
	PWM_CounterCmd(LCD_BL_PWM_PERI_ID, ENABLE);					// start PWM Counter
	PWM_Cmd(LCD_BL_PWM_PERI_ID, ENABLE);						// start PWM
}
예제 #2
0
/**
 * Measure time between two lightbariers. Afterwards computes weight and duty cycle for shooting action
 * @param shoot - 0 if we meassure just reference weight
 * 				  1 if we want also shoot and compute weight
 */
float measure_time_weight(int8_t shoot)
{
	float weight;
	uint32_t shoot_duty = 0;

	if (check_temperatur(MAX_TEMP)) // check if temperatur is not too high
		return 0;

	TIM_Cmd(LPC_TIM0, DISABLE);
	TIM_ResetCounter(LPC_TIM0 );

	PWM_MatchUpdate(LPC_PWM1, pwm_channel, 0, PWM_MATCH_UPDATE_NOW);
	delay_ms(500);
	if (shoot)
	{
		printf("Waiting for up wall time and shooting, ");
	}
	else
	{
		printf("Waiting for up wall time ");
	}
	uint32_t duty_measure = (uint32_t) roundNo((float) period * 0.61);

	up_wall_passed = 0;
	down_wall_passed = 0;

	PWM_MatchUpdate(LPC_PWM1, pwm_channel, duty_measure, PWM_MATCH_UPDATE_NOW);
	while (up_wall_passed == 0)
		;
	unsigned long measured_tick = time_wall_up - time_wall_down;

	//shooting procedure
	if (shoot == 1)
	{
		weight = (measured_tick - reference_time) / time_one_gram; //compute weight
		shoot_duty = get_duty_shoot(weight);
		PWM_MatchUpdate(LPC_PWM1, pwm_channel, shoot_duty, PWM_MATCH_UPDATE_NOW);
		delay_ms(100);
	}

	PWM_MatchUpdate(LPC_PWM1, pwm_channel, 0, PWM_MATCH_UPDATE_NOW);

	up_wall_passed = 0;
	down_wall_passed = 0;
	LPC_SC ->EXTINT = EINT0; /* clear interrupt */
	LPC_SC ->EXTINT = EINT1; /* clear interrupt */

	printf(", tick %lu , shoot_duty: %ld ,hard c. table:%d, temp:%g \n", measured_tick, shoot_duty, hard_coded_table, compute_temperatur());

	if (!shoot)
	{
		weight = measured_tick;
		calibration_temp = compute_temperatur();
	}

	return weight;
}
예제 #3
0
파일: pwm.c 프로젝트: jnugen/robus
void pwm_init(void)
{
	PWM_TIMERCFG_Type PWMCfgDat;
	PWM_MATCHCFG_Type PWMMatchCfgDat;
	PINSEL_CFG_Type PinCfg;

	/* Initialize PWM peripheral, timer mode
	 * PWM prescale value = 1 (absolute value - tick value) */
	PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
	PWMCfgDat.PrescaleValue = 500;
	PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat);

	/* Initialize PWM pin connect for PWM1[4] on P2[3] */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 2;
	PinCfg.Pinnum = (EN_PWM_CHN - 1);	// hack
	PINSEL_ConfigPin(&PinCfg);

	/* Set match value for PWM match channel 0 = 256, update immediately */
	PWM_MatchUpdate(LPC_PWM1, 0, 256, PWM_MATCH_UPDATE_NOW);

	/* PWM Timer/Counter will be reset when channel 0 matching
	 * no interrupt when match
	 * no stop when match */
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);

	/* Configure PWM channel edge option for EN_PWM_CHN */
	PWM_ChannelConfig(LPC_PWM1, EN_PWM_CHN, PWM_CHANNEL_SINGLE_EDGE);

	/* Set initial match value for EN_PWM_CHN to disable output change */
	PWM_MatchUpdate(LPC_PWM1, EN_PWM_CHN, 0, PWM_MATCH_UPDATE_NOW);

	/* Configure match option for EN_PWM_CHN*/
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = EN_PWM_CHN;
	PWMMatchCfgDat.ResetOnMatch = DISABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);

	/* Enable PWM Channel Output for EN_PWM_CHN */
	PWM_ChannelCmd(LPC_PWM1, EN_PWM_CHN, ENABLE);

	/* Reset and Start counter */
	PWM_ResetCounter(LPC_PWM1);
	PWM_CounterCmd(LPC_PWM1, ENABLE);

	/* Start PWM now */
	PWM_Cmd(LPC_PWM1, ENABLE);
}
예제 #4
0
static void buzzer_pwm_set_frequency (uint16_t frequency)
{
  uint32_t match_value = (((float) 1/frequency)) * 1000000;

  PWM_TIMERCFG_Type PWMCfgDat;
  PWM_MATCHCFG_Type PWMMatchCfgDat;

  /* PWM block section -------------------------------------------- */
  /* Initialize PWM peripheral, timer mode
   * PWM prescale value = 1 (absolute value - tick value) */
  PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_USVAL;
  PWMCfgDat.PrescaleValue = 1;
  PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat);

  /* Set match value for PWM match channel 0 = match_value, update immediately */
  PWM_MatchUpdate(LPC_PWM1, 0, match_value, PWM_MATCH_UPDATE_NOW);
  /* PWM Timer/Counter will be reset when channel 0 matching
   * no interrupt when match
   * no stop when match */
  PWMMatchCfgDat.IntOnMatch = DISABLE;
  PWMMatchCfgDat.MatchChannel = 0;
  PWMMatchCfgDat.ResetOnMatch = ENABLE;
  PWMMatchCfgDat.StopOnMatch = DISABLE;
  PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);

  /* Configure PWM channel: --------------------------------------------- */
  /* - Single edge
   * - PWM Duty on each PWM channel determined by
   * the match on channel 0 to the match of that match channel.
   * Example: PWM Duty on PWM channel 1 determined by
   * the match on channel 0 to the match of match channel 1.
   */

  /* Configure PWM channel edge option
   * Note: PWM Channel 1 is in single mode as default state and
   * can not be changed to double edge mode */
  PWM_ChannelConfig(LPC_PWM1, 3, PWM_CHANNEL_SINGLE_EDGE);

  /* Set up match value */
  PWM_MatchUpdate(LPC_PWM1, 3, match_value/2, PWM_MATCH_UPDATE_NOW);
  /* Configure match option */
  PWMMatchCfgDat.IntOnMatch = DISABLE;
  PWMMatchCfgDat.MatchChannel = 3;
  PWMMatchCfgDat.ResetOnMatch = DISABLE;
  PWMMatchCfgDat.StopOnMatch = DISABLE;
  PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);
  /* Enable PWM Channel Output */
  PWM_ChannelCmd(LPC_PWM1, 3, ENABLE);
}
// --------------------------------------------------
// Set nev intensity [%] of backlight.
void Set_LCD_BackLight(uint16_t level)		// Level: 0-100%
{
	uint32_t pclk;
	pclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
	if (level > 100) level = 100;
	PWM_MatchUpdate(LCD_BL_PWM_PERI_ID, LCD_BL_PWM_PERI_CHB,level * ((pclk / LCD_BL_PWM_BASE) / 100), PWM_MATCH_UPDATE_NOW);
}
예제 #6
0
파일: PWM.cpp 프로젝트: koson/OpenOBC
void PWM::setFrequency(float frequency)
{
	if(frequency > 1)
		frequency = 1;
	if(frequency < 0)
		frequency = 0;
	this->frequency = frequency;
	uint32_t pwmclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_PWM1);
	PWM_MatchUpdate(this->peripheral, 0, pwmclk / this->frequency, PWM_MATCH_UPDATE_NOW);

	PWM_MATCHCFG_Type PWMMatchCfgDat;
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat);
	
// 	PWM_ChannelCmd(this->peripheral, this->channel, ENABLE);
// 	
// 	PWM_ResetCounter(this->peripheral);
// 	PWM_CounterCmd(this->peripheral, ENABLE);
// 
// 	PWM_Cmd(this->peripheral, ENABLE);
	
}
예제 #7
0
void pwmSetValue(int channel, float val, int base){
  /*Set the value for the match register
   * @param channel: The channel to wish to change the MR for
   * @param val: The percentage of base time you wish to have a pulse width
   * @param base: your channel 0 base time
   */

  PWM_MatchUpdate(LPC_PWM1, channel, (int)(val*base), PWM_MATCH_UPDATE_NOW);
}
예제 #8
0
/**
 * Set specific duty on pwm output
 * @param - specific duty
 */
uint8_t set_duty(uint32_t duty)
{
	uint16_t step_delay = 25;
	PWM_MatchUpdate(LPC_PWM1, pwm_channel, duty, PWM_MATCH_UPDATE_NOW);
#ifdef DEBUG_WEIGHT
	temp[index++] = compute_temperatur();
#endif
	delay_ms(step_delay);
	return true;
}
예제 #9
0
파일: platform.c 프로젝트: ARMinARM/elua
u32 platform_pwm_setup( unsigned id, u32 frequency, unsigned duty )
{
  PWM_MATCHCFG_Type PWMMatchCfgDat;
  u32 divisor = platform_pwm_get_clock( id ) / frequency - 1;
    
  PWM_MatchUpdate( LPC_PWM1, 0, divisor, PWM_MATCH_UPDATE_NOW ); // PWM1 cycle rate
  PWM_MatchUpdate( LPC_PWM1, id, ( divisor * duty ) / 100, PWM_MATCH_UPDATE_NOW ); // PWM1 channel edge position
  
  if ( id > 1 ) // Channel one is permanently single-edge
    PWM_ChannelConfig( LPC_PWM1, id, PWM_CHANNEL_SINGLE_EDGE );
  
  PWMMatchCfgDat.IntOnMatch = DISABLE;
  PWMMatchCfgDat.MatchChannel = id;
  PWMMatchCfgDat.ResetOnMatch = DISABLE;
  PWMMatchCfgDat.StopOnMatch = DISABLE;
  PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);

  PWM_ResetCounter(LPC_PWM1);
  PWM_CounterCmd(LPC_PWM1, ENABLE);

  PWM_ChannelCmd(LPC_PWM1, id, ENABLE);

  return platform_pwm_get_clock( id ) / divisor;
}
예제 #10
0
int tape_measure_mode(char previous) {
    sensor_changer(&sensor_selector, &previous);
    count = 18;
    lcd_display_top_row("Tape");
    lcd_display_bottom_row();


    char a = read_keypad(33);
    if (a == 'A'&& previous != a) {
        SYSTICK_IntCmd(DISABLE);
        clear_display(59);
        char a = read_keypad(33);
        previous = keypad_check(a, previous);
        return 0;
    }
    else if (a == 'B'&& previous != a) {
        SYSTICK_IntCmd(DISABLE);
        char a = read_keypad(33);
        previous = keypad_check(a, previous);
        return 1;
    }
    else if (a == 'C'&& previous != a) {
        servoreset();
        SYSTICK_IntCmd(ENABLE);
        clear_display(59);
        char a = read_keypad(33);
        previous = keypad_check(a, previous);
        return 2;
    }
    else if (a == 'D'&& previous != a) {
        servoreset();
        SYSTICK_IntCmd(ENABLE);
        clear_display(59);
        char a = read_keypad(33);
        previous = keypad_check(a, previous);
        return 3;
    }
    else {
        //distanceircalc();
        //RTC_AlarmIntConfig((LPC_RTC_TypeDef *) LPC_RTC, RTC_TIMETYPE_SECOND, DISABLE);
        PWM_MatchUpdate((LPC_PWM_TypeDef *) LPC_PWM1,2,18,PWM_MATCH_UPDATE_NOW);
        keypad_change_sample_rate(&samplerate, a, &previous);
        average_calculator(us_dist_arr, ir_dist_arr, array_counter, &us_avg, &ir_avg);
        previous = keypad_check(a, previous);
        return 1;
    }
}
예제 #11
0
int _PWM_MatchUpdate(uint8_t * args)
{
	uint8_t * arg_ptr;
	LPC_PWM_TypeDef* PWMx;
	uint8_t MatchChannel;
	uint32_t MatchValue;
	uint8_t UpdateType;

	if ((arg_ptr = (uint8_t *) strtok(NULL, " ")) == NULL) return 1;
	PWMx = (LPC_PWM_TypeDef*) strtoul((char *) arg_ptr, NULL, 16);
	if ((arg_ptr = (uint8_t *) strtok(NULL, " ")) == NULL) return 1;
	MatchChannel = (uint8_t) strtoul((char *) arg_ptr, NULL, 16);
	if ((arg_ptr = (uint8_t *) strtok(NULL, " ")) == NULL) return 1;
	MatchValue = (uint32_t) strtoul((char *) arg_ptr, NULL, 16);
	if ((arg_ptr = (uint8_t *) strtok(NULL, " ")) == NULL) return 1;
	UpdateType = (uint8_t) strtoul((char *) arg_ptr, NULL, 16);

	PWM_MatchUpdate(PWMx, MatchChannel, MatchValue, UpdateType);
	return 0;
}
예제 #12
0
void pwmInit(int base, int channel){
  /*Initialise the pwm controller
   * @param base: Your base time for channel 0
   * @param channel: The channel you wish to activat
   */

  PINSEL_CFG_Type pinCfg;
  PWM_TIMERCFG_Type PWMCfgDat;
  PWM_MATCHCFG_Type PWMMatchCfgDat;
  PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
  PWMCfgDat.PrescaleValue = 1;
  PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void*) &PWMCfgDat);

  pinCfg.Funcnum = 1;
  pinCfg.OpenDrain = 0;
  pinCfg.Pinmode = 0;
  pinCfg.Portnum = 2;
  pinCfg.Pinnum = 5;
  PINSEL_ConfigPin(&pinCfg);

  PWM_MatchUpdate(LPC_PWM1, 0, base, PWM_MATCH_UPDATE_NOW);
  PWMMatchCfgDat.IntOnMatch = DISABLE;
  PWMMatchCfgDat.MatchChannel = 0;
  PWMMatchCfgDat.ResetOnMatch = ENABLE;
  PWMMatchCfgDat.StopOnMatch = DISABLE;
  PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);

  PWM_ChannelConfig(LPC_PWM1, channel, PWM_CHANNEL_SINGLE_EDGE);

  PWMMatchCfgDat.IntOnMatch = DISABLE;
  PWMMatchCfgDat.MatchChannel = channel;
  PWMMatchCfgDat.ResetOnMatch = DISABLE;
  PWMMatchCfgDat.StopOnMatch = DISABLE;
  PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);
  PWM_ChannelCmd(LPC_PWM1, channel, ENABLE);

  PWM_ResetCounter(LPC_PWM1);
  PWM_CounterCmd(LPC_PWM1, ENABLE);
  PWM_Cmd(LPC_PWM1, ENABLE);
}
예제 #13
0
/*
 * Setup a match condition for count cycles on channel ch.
 */
int _initMatch(uint8_t * args)
{
    uint8_t * arg_ptr;
    uint8_t MatchChannel; 
    uint32_t MatchValue;
    PWM_MATCHCFG_Type PWMMatchCfgDat;

    if ((arg_ptr = (uint8_t *) strtok(NULL, " ")) == NULL) return 1;
    MatchChannel = (uint8_t) strtoul((char *) arg_ptr, NULL, 16);
    if ((arg_ptr = (uint8_t *) strtok(NULL, " ")) == NULL) return 1;
    MatchValue = (uint32_t) strtoul((char *) arg_ptr, NULL, 16);
    
    PWM_MatchUpdate(LPC_PWM1, MatchChannel, MatchValue, PWM_MATCH_UPDATE_NOW);
    PWMMatchCfgDat.IntOnMatch = DISABLE;
    PWMMatchCfgDat.MatchChannel = MatchChannel;
    if (!MatchChannel)
        PWMMatchCfgDat.ResetOnMatch = ENABLE;
    else
        PWMMatchCfgDat.ResetOnMatch = DISABLE;
    PWMMatchCfgDat.StopOnMatch = DISABLE;
    PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);

    return 0;
}
예제 #14
0
int calibration_mode(char previous) {
    sensor_changer(&sensor_selector, &previous);
    count = 18;
    PWM_MatchUpdate((LPC_PWM_TypeDef *) LPC_PWM1,2,18,PWM_MATCH_UPDATE_NOW);


    char a = read_keypad(33);
    /*
    Selector to change the mode using the keypad keys A,B,C and D. Replicated in the rest of the modes too.
    When the system is started for the first time, it starts in calibration mode and requests its values unless one of the
    B, C or D keys is held at startup.
    */
    if (a == 'A'&& previous != a) {
        SYSTICK_IntCmd(DISABLE);
        char a = read_keypad(33);
        previous = keypad_check(a, previous);
        return 0;
    }
    else if (a == 'B'&& previous != a) {
        SYSTICK_IntCmd(DISABLE);
        clear_display(59);
        char a = read_keypad(33);
        previous = keypad_check(a, previous);
        return 1;
    }
    else if (a == 'C'&& previous != a) {
        servoreset();
        SYSTICK_IntCmd(ENABLE);
        clear_display(59);
        char a = read_keypad(33);
        previous = keypad_check(a, previous);
        return 2;
    }
    else if (a == 'D'&& previous != a) {
        servoreset();
        SYSTICK_IntCmd(ENABLE);
        clear_display(59);
        char a = read_keypad(33);
        previous = keypad_check(a, previous);
        return 3;
    }
    else {
        int ir_calib_arr[3];
        int us_calib_arr[3];
        int ir_reported;
        int us_reported;
        int ir_calib_val;
        int us_calib_val;
        int u = 0;
        char write[2];
        strcpy(write, "");
        /*
        Code to get values from the keypad. The user places an object at a set distance away from the head, enters how far it is from
        the head, then finalises this value with the # key. If a mistake is made, the screen can be cleared with the A key,
        Then repostions it closer or further and enters the new distance.
        This is repeated once more to obtain three different calibration values.
        */
        while(calib_tracker <3) {
            if (u == 1000) {
                sensor_changer_cali_mode(&sensor_selector, &previous);
                int addr = 0x80;
                int i;
                for(i=0; i < strlen("Input actual dist: "); i++) {
                    addr = alloc_lcd_addr(addr, i, "Input actual dist: ");
                }
                for(i=0; i < strlen(write); i++) {
                    addr = alloc_lcd_addr(addr, i, write);
                }
                char x;
                x = read_keypad(33);
                if (x == previous) {
                    previous = x;
                }
                else if (x != 'Z' && x != previous) {
                    if (x == 'A') {
                        clear_display(59);
                        strcpy(write, "");
                        previous = x;
                    }
                    else if (isalpha(x) || x == '*' || isdigit(x)) {
                        addr = 0x80;
                        append(write, x);
                        clear_display(59);
                        for(i=0; i < strlen("Input actual dist: "); i++) {
                            addr = alloc_lcd_addr(addr, i, "Input actual dist: ");
                        }
                        for(i=0; i < strlen(write); i++) {
                            addr = alloc_lcd_addr(addr, i, write);
                        }
                        previous = x;
                    }
                    else if(x == '#') {
                        clear_display(59);
                        int act_val = atoi(write);
                        int anglemax = ((servo_stop-8)*9);
                        int anglemin = ((servo_start-8)*9);
                        int angle = ((count-8)*9);
                        char port3[90] = "";
                        sprintf(port3, ";%i;%i;%i;%i;%i;%i;%i;%i;%i;%i;%i;%i;\n\r", ir_raw, us_raw, ir_dist, us_dist, angle, anglemax, anglemin, act_val, sweep_num, multicheck, mode, newmulti);
                        write_usb_serial_blocking(port3 ,90);
                        ir_reported = ir_dist;
                        char port[3] = "";
                        sprintf(port, "%i", ir_reported);
                        //write_usb_serial_blocking("ir_report: ", 11);
                        //write_usb_serial_blocking(port, 3);
                        //write_usb_serial_blocking("\n\r", 2);
                        us_reported = us_dist;
                        char port1[3] = "";
                        sprintf(port1, "%i", us_reported);
                        //write_usb_serial_blocking("us_report: ", 11);
                        //write_usb_serial_blocking(port1, 3);
                        //write_usb_serial_blocking("\n\r", 2);
                        ir_calib_val = (act_val - ir_reported);
                        us_calib_val = (act_val - us_reported);
                        ir_calib_arr[calib_tracker] = ir_calib_val;
                        us_calib_arr[calib_tracker] = us_calib_val;
                        calib_tracker++;
                        strcpy(write, "");
                        previous = x;
                    }
                }
                else if (x == 'Z' && previous != 'Z') {
                    previous = 'Z';
                }
            }
            else {
                u++;
            }
        }
        /*
        If the calibration values have been entered, but the calibration is not yet complete, this calculates the calibration adjustments
        for both IR and US, then sets calibrated_flag to stop them from being recalculated.
        */
        if (calibrated_flag == 0) {
            for (calib_tracker = 0; calib_tracker <3; calib_tracker++) {
                ir_calib_total += ir_calib_arr[calib_tracker];
                us_calib_total += us_calib_arr[calib_tracker];
            }
            calibrated_flag = 1;
            ir_calibration_adjust = ir_calib_total/3;
            us_calibration_adjust = us_calib_total/3;
            char port1[3] = "";
            sprintf(port1, "%i", ir_calib_total);
            /*write_usb_serial_blocking("IR total: ", 8);
            write_usb_serial_blocking(port1, 3);
            write_usb_serial_blocking("\n\r", 2);*/
            char port2[3] = "";
            sprintf(port2, "%i", us_calib_total);
            /*write_usb_serial_blocking("US total: ", 8);
            write_usb_serial_blocking(port2, 3);
            write_usb_serial_blocking("\n\r", 2);*/
            char port3[3] = "";
            sprintf(port3, "%i", ir_calibration_adjust);
            /*write_usb_serial_blocking("IR adjust: ", 9);
            write_usb_serial_blocking(port3, 3);
            write_usb_serial_blocking("\n\r", 2);*/
            char port4[3] = "";
            sprintf(port4, "%i", us_calibration_adjust);
            /*write_usb_serial_blocking("US adjust: ", 9);
            write_usb_serial_blocking(port4, 3);
            write_usb_serial_blocking("\n\r", 2);*/
        }

        lcd_display_top_row("Cali");
        lcd_display_bottom_row();
        //distanceircalc();
        //RTC_AlarmIntConfig((LPC_RTC_TypeDef *) LPC_RTC, RTC_TIMETYPE_SECOND, DISABLE);
        //keypad_change_sample_rate(&samplerate, a, &previous);
        average_calculator(us_dist_arr, ir_dist_arr, array_counter, &us_avg, &ir_avg);
        previous = keypad_check(a, previous);
        return 0;
    }
}
예제 #15
0
/*********************************************************************//**
 * @brief		c_entry: Main PWM program body
 * @param[in]	None
 * @return 		int
 **********************************************************************/
int c_entry(void)
{
	uint8_t temp;
	PWM_TIMERCFG_Type PWMCfgDat;
	PWM_MATCHCFG_Type PWMMatchCfgDat;
	PINSEL_CFG_Type PinCfg;

	/* PWM block section -------------------------------------------- */
	/* Initialize PWM peripheral, timer mode
	 * PWM prescale value = 1 (absolute value - tick value) */
	PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
	PWMCfgDat.PrescaleValue = 1;
	PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat);

	/*
	 * Initialize PWM pin connect
	 *
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 2;
	for (temp = 0; temp <= 6; temp++){
		PinCfg.Pinnum = temp;
		PINSEL_ConfigPin(&PinCfg);
	}


	/* Set match value for PWM match channel 0 = 100, update immediately */
	PWM_MatchUpdate(LPC_PWM1, 0, 100, PWM_MATCH_UPDATE_NOW);
	/* PWM Timer/Counter will be reset when channel 0 matching
	 * no interrupt when match
	 * no stop when match */
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);

	/* Configure each PWM channel: --------------------------------------------- */
	/* - Channel 2: Double Edge
	 * - Channel 4: Double Edge
	 * - Channel 5: Single Edge
	 * The Match register values are as follows:
	 * - MR0 = 100 (PWM rate)
	 * - MR1 = 41, MR2 = 78 (PWM2 output)
	 * - MR3 = 53, MR4 = 27 (PWM4 output)
	 * - MR5 = 65 (PWM5 output)
	 * PWM Duty on each PWM channel:
	 * - Channel 2: Set by match 1, Reset by match 2.
	 * - Channel 4: Set by match 3, Reset by match 4.
	 * - Channel 5: Set by match 0, Reset by match 5.
	 */

	/* Edge setting ------------------------------------ */
	PWM_ChannelConfig(LPC_PWM1, 2, PWM_CHANNEL_DUAL_EDGE);
	PWM_ChannelConfig(LPC_PWM1, 4, PWM_CHANNEL_DUAL_EDGE);
	PWM_ChannelConfig(LPC_PWM1, 5, PWM_CHANNEL_SINGLE_EDGE);

	/* Match value setting ------------------------------------ */
	PWM_MatchUpdate(LPC_PWM1, 1, 41, PWM_MATCH_UPDATE_NOW);
	PWM_MatchUpdate(LPC_PWM1, 2, 78, PWM_MATCH_UPDATE_NOW);
	PWM_MatchUpdate(LPC_PWM1, 3, 53, PWM_MATCH_UPDATE_NOW);
	PWM_MatchUpdate(LPC_PWM1, 4, 27, PWM_MATCH_UPDATE_NOW);
	PWM_MatchUpdate(LPC_PWM1, 5, 65, PWM_MATCH_UPDATE_NOW);


	/* Match option setting ------------------------------------ */
	for (temp = 1; temp < 6; temp++)
	{
		/* Configure match option */
		PWMMatchCfgDat.IntOnMatch = DISABLE;
		PWMMatchCfgDat.MatchChannel = temp;
		PWMMatchCfgDat.ResetOnMatch = DISABLE;
		PWMMatchCfgDat.StopOnMatch = DISABLE;
		PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);
	}

	/* Enable PWM Channel Output ------------------------------------ */
	/* Channel 2 */
	PWM_ChannelCmd(LPC_PWM1, 2, ENABLE);
	/* Channel 4 */
	PWM_ChannelCmd(LPC_PWM1, 4, ENABLE);
	/* Channel 5 */
	PWM_ChannelCmd(LPC_PWM1, 5, ENABLE);

	/* Reset and Start counter */
	PWM_ResetCounter(LPC_PWM1);
	PWM_CounterCmd(LPC_PWM1, ENABLE);

	/* Start PWM now */
	PWM_Cmd(LPC_PWM1, ENABLE);

    /* Loop forever */
    while(1);
    return 1;
}
/*********************************************************************//**
 * @brief		c_entry: Main PWM program body
 * @param[in]	None
 * @return 		None
 **********************************************************************/
void c_entry(void)
{
	uint8_t pwmChannel;
	PWM_TIMERCFG_Type PWMCfgDat;
	PWM_MATCHCFG_Type PWMMatchCfgDat;

	/* PWM block section -------------------------------------------- */
	/* Initialize PWM peripheral, timer mode
	 * PWM prescale value = 1 (absolute value - tick value) */
	PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
	PWMCfgDat.PrescaleValue = 1;
	PWM_Init(_USING_PWM_NO, PWM_MODE_TIMER, (void *) &PWMCfgDat);

	// Initialize PWM pin connect
#if (_USING_PWM_NO == 1)
	for (pwmChannel = 0; pwmChannel <= 6; pwmChannel++)
	{
		PINSEL_ConfigPin (2, pwmChannel, 1);
	}
#elif (_USING_PWM_NO == 0)
	PINSEL_ConfigPin (1, 2, 3);//PWM0.1
	PINSEL_ConfigPin (1, 3, 3);//PWM0.2
	PINSEL_ConfigPin (1, 5, 3);//PWM0.3
	PINSEL_ConfigPin (1, 6, 3);//PWM0.4
	PINSEL_ConfigPin (1, 7, 3);//PWM0.5
	PINSEL_ConfigPin (1, 11, 3);//PWM0.6
#else
	return 0;
#endif


	/* Set match value for PWM match channel 0 = 100, update immediately */
	PWM_MatchUpdate(_USING_PWM_NO, 0, 100, PWM_MATCH_UPDATE_NOW);

	/* PWM Timer/Counter will be reset when channel 0 matching
	 * no interrupt when match
	 * no stop when match */
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(_USING_PWM_NO, &PWMMatchCfgDat);

	/* Configure each PWM channel: --------------------------------------------- */
	/* - Channel 2: Double Edge
	 * - Channel 4: Double Edge
	 * - Channel 5: Single Edge
	 * The Match register values are as follows:
	 * - MR0 = 100 (PWM rate)
	 * - MR1 = 41, MR2 = 78 (PWM2 output)
	 * - MR3 = 53, MR4 = 27 (PWM4 output)
	 * - MR5 = 65 (PWM5 output)
	 * PWM Duty on each PWM channel:
	 * - Channel 2: Set by match 1, Reset by match 2.
	 * - Channel 4: Set by match 3, Reset by match 4.
	 * - Channel 5: Set by match 0, Reset by match 5.
	 */

	/* Edge setting ------------------------------------ */
	PWM_ChannelConfig(_USING_PWM_NO, 2, PWM_CHANNEL_DUAL_EDGE);
	PWM_ChannelConfig(_USING_PWM_NO, 4, PWM_CHANNEL_DUAL_EDGE);
	PWM_ChannelConfig(_USING_PWM_NO, 5, PWM_CHANNEL_SINGLE_EDGE);

	/* Match value setting ------------------------------------ */
	PWM_MatchUpdate(_USING_PWM_NO, 1, 41, PWM_MATCH_UPDATE_NOW);
	PWM_MatchUpdate(_USING_PWM_NO, 2, 78, PWM_MATCH_UPDATE_NOW);
	PWM_MatchUpdate(_USING_PWM_NO, 3, 53, PWM_MATCH_UPDATE_NOW);
	PWM_MatchUpdate(_USING_PWM_NO, 4, 27, PWM_MATCH_UPDATE_NOW);
	PWM_MatchUpdate(_USING_PWM_NO, 5, 65, PWM_MATCH_UPDATE_NOW);


	/* Match option setting ------------------------------------ */
	for (pwmChannel = 1; pwmChannel < 6; pwmChannel++)
	{
		/* Configure match option */
		PWMMatchCfgDat.IntOnMatch = DISABLE;
		PWMMatchCfgDat.MatchChannel = pwmChannel;
		PWMMatchCfgDat.ResetOnMatch = DISABLE;
		PWMMatchCfgDat.StopOnMatch = DISABLE;

		PWM_ConfigMatch(PWM_1, &PWMMatchCfgDat);
	}

	/* Enable PWM Channel Output ------------------------------------ */
	/* Channel 2 */
	PWM_ChannelCmd(_USING_PWM_NO, 2, ENABLE);

	/* Channel 4 */
	PWM_ChannelCmd(_USING_PWM_NO, 4, ENABLE);

	/* Channel 5 */
	PWM_ChannelCmd(_USING_PWM_NO, 5, ENABLE);

	/* Reset and Start counter */
	PWM_ResetCounter(_USING_PWM_NO);

	PWM_CounterCmd(_USING_PWM_NO, ENABLE);

	/* Start PWM now */
	PWM_Cmd(_USING_PWM_NO, ENABLE);

    /* Loop forever */
    while(1);
}
예제 #17
0
파일: pwm.c 프로젝트: jnugen/robus
void pwm_update(int value)
{
	PWM_MatchUpdate(LPC_PWM1, EN_PWM_CHN, value, PWM_MATCH_UPDATE_NOW);
}
/*********************************************************************//**
 * @brief		c_entry: Main PWM program body
 * @param[in]	None
 * @return 		None
 **********************************************************************/
void c_entry(void)
{
	uint8_t pwmChannel, channelVal;
	PWM_TIMERCFG_Type PWMCfgDat;
	PWM_MATCHCFG_Type PWMMatchCfgDat;

	/* PWM block section -------------------------------------------- */
	/* Initialize PWM peripheral, timer mode
	 * PWM prescale value = 1 (absolute value - tick value) */
	PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
	PWMCfgDat.PrescaleValue = 1;
	PWM_Init(_USING_PWM_NO, PWM_MODE_TIMER, (void *) &PWMCfgDat);

	// Initialize PWM pin connect
#if (_USING_PWM_NO == 1)
	for (pwmChannel = 0; pwmChannel <= 6; pwmChannel++)
	{
		PINSEL_ConfigPin (2, pwmChannel, 1);
	}
#elif (_USING_PWM_NO == 0)
	PINSEL_ConfigPin (1, 2, 3);//PWM0.1
	PINSEL_ConfigPin (1, 3, 3);//PWM0.2
	PINSEL_ConfigPin (1, 5, 3);//PWM0.3
	PINSEL_ConfigPin (1, 6, 3);//PWM0.4
	PINSEL_ConfigPin (1, 7, 3);//PWM0.5
	PINSEL_ConfigPin (1, 11, 3);//PWM0.6
#else
	return 0;
#endif


	/* Set match value for PWM match channel 0 = 256, update immediately */
	PWM_MatchUpdate(_USING_PWM_NO, 0, 256, PWM_MATCH_UPDATE_NOW);

	/* PWM Timer/Counter will be reset when channel 0 matching
	 * Enable interrupt when match
	 * no stop when match */
	PWMMatchCfgDat.IntOnMatch = ENABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(_USING_PWM_NO, &PWMMatchCfgDat);

	/* Configure each PWM channel: --------------------------------------------- */
	/* - Single edge
	 * - PWM Duty on each PWM channel determined by
	 * the match on channel 0 to the match of that match channel.
	 * Example: PWM Duty on PWM channel 1 determined by
	 * the match on channel 0 to the match of match channel 1.
	 */

	/* Configure PWM channel edge option
	 * Note: PWM Channel 1 is in single mode as default state and
	 * can not be changed to double edge mode */
	for (pwmChannel = 2; pwmChannel < 7; pwmChannel++)
	{
		PWM_ChannelConfig(_USING_PWM_NO, pwmChannel, PWM_CHANNEL_SINGLE_EDGE);
	}

	/* Setting interrupt for PWM ---------------------------------------------- */
    /* Disable PWM interrupt */
#if (_USING_PWM_NO == 1)
    NVIC_DisableIRQ(PWM1_IRQn);

	/* preemption = 1, sub-priority = 1 */
    NVIC_SetPriority(PWM1_IRQn, ((0x01<<3)|0x01));
#elif (_USING_PWM_NO == 0)
	NVIC_DisableIRQ(PWM0_IRQn);

	/* preemption = 1, sub-priority = 1 */
    NVIC_SetPriority(PWM0_IRQn, ((0x01<<3)|0x01));
#endif

	/* Configure match value for each match channel */
	channelVal = 10;
	for (pwmChannel = 1; pwmChannel < 7; pwmChannel++)
	{
		/* Set up match value */
		PWM_MatchUpdate(_USING_PWM_NO, pwmChannel, channelVal, PWM_MATCH_UPDATE_NOW);

		/* Configure match option */
		PWMMatchCfgDat.IntOnMatch = DISABLE;
		PWMMatchCfgDat.MatchChannel = pwmChannel;
		PWMMatchCfgDat.ResetOnMatch = DISABLE;
		PWMMatchCfgDat.StopOnMatch = DISABLE;
		PWM_ConfigMatch(_USING_PWM_NO, &PWMMatchCfgDat);

		/* Enable PWM Channel Output */
		PWM_ChannelCmd(_USING_PWM_NO, pwmChannel, ENABLE);

		/* Increase match value by 10 */
		channelVal += 30;
	}

    /* Enable PWM interrupt */
#if (_USING_PWM_NO == 1)
	NVIC_EnableIRQ(PWM1_IRQn);
#elif (_USING_PWM_NO == 0)
	NVIC_EnableIRQ(PWM0_IRQn);
#endif

	/* Reset and Start counter */
	PWM_ResetCounter(_USING_PWM_NO);

	PWM_CounterCmd(_USING_PWM_NO, ENABLE);

	/* Start PWM now */
	PWM_Cmd(_USING_PWM_NO, ENABLE);

	// Update PWM1.1 every 0x1000 match interrupt
	while (1)
	{
		if (match_cnt >= 0x1000)
		{
			match_cnt = 0;
			long_duty++;
			if (long_duty >= 256)
			{
				// Reset duty
				long_duty = 0;
			}

			// Update PWM1.1 duty
			PWM_MatchUpdate(_USING_PWM_NO, 1, long_duty, PWM_MATCH_UPDATE_NOW);
		}
	}
}
예제 #19
0
/*********************************************************************//**
 * @brief	Main PWM program body
 **********************************************************************/
int c_entry(void)
{
	uint8_t temp, temp2;
	PWM_TIMERCFG_Type PWMCfgDat;
	PWM_MATCHCFG_Type PWMMatchCfgDat;
	PINSEL_CFG_Type PinCfg;

	// DeInit NVIC and SCBNVIC
	NVIC_DeInit();
	NVIC_SCBDeInit();

	/* Configure the NVIC Preemption Priority Bits:
	 * two (2) bits of preemption priority, six (6) bits of sub-priority.
	 * Since the Number of Bits used for Priority Levels is five (5), so the
	 * actual bit number of sub-priority is three (3)
	 */
	NVIC_SetPriorityGrouping(0x05);

	//  Set Vector table offset value
#if (__RAM_MODE__==1)
	NVIC_SetVTOR(0x10000000);
#else
	NVIC_SetVTOR(0x00000000);
#endif

	/* Initialize debug */
	debug_frmwrk_init();

	// print welcome screen
	print_menu();

	/* PWM block section -------------------------------------------- */
	/* Initialize PWM peripheral, timer mode
	 * PWM prescale value = 1 (absolute value - tick value) */
	PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
	PWMCfgDat.PrescaleValue = 1;
	PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat);

	/*
	 * Initialize PWM pin connect
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 2;
	for (temp = 0; temp <= 6; temp++){
		PinCfg.Pinnum = temp;
		PINSEL_ConfigPin(&PinCfg);
	}


	/* Set match value for PWM match channel 0 = 256, update immediately */
	PWM_MatchUpdate(LPC_PWM1, 0, 256, PWM_MATCH_UPDATE_NOW);
	/* PWM Timer/Counter will be reset when channel 0 matching
	 * no interrupt when match
	 * no stop when match */
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);

	/* Configure each PWM channel: --------------------------------------------- */
	/* - Single edge
	 * - PWM Duty on each PWM channel determined by
	 * the match on channel 0 to the match of that match channel.
	 * Example: PWM Duty on PWM channel 1 determined by
	 * the match on channel 0 to the match of match channel 1.
	 */

	/* Configure PWM channel edge option
	 * Note: PWM Channel 1 is in single mode as default state and
	 * can not be changed to double edge mode */
	for (temp = 2; temp < 7; temp++)
	{
		PWM_ChannelConfig(LPC_PWM1, temp, PWM_CHANNEL_SINGLE_EDGE);
	}


	/* Configure match value for each match channel */
	temp2 = 10;
	for (temp = 1; temp < 7; temp++)
	{
		/* Set up match value */
		PWM_MatchUpdate(LPC_PWM1, temp, temp2, PWM_MATCH_UPDATE_NOW);
		/* Configure match option */
		PWMMatchCfgDat.IntOnMatch = DISABLE;
		PWMMatchCfgDat.MatchChannel = temp;
		PWMMatchCfgDat.ResetOnMatch = DISABLE;
		PWMMatchCfgDat.StopOnMatch = DISABLE;
		PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);
		/* Enable PWM Channel Output */
		PWM_ChannelCmd(LPC_PWM1, temp, ENABLE);
		/* Increase match value by 10 */
		temp2 += 10;
	}

	/* Reset and Start counter */
	PWM_ResetCounter(LPC_PWM1);
	PWM_CounterCmd(LPC_PWM1, ENABLE);

	/* Start PWM now */
	PWM_Cmd(LPC_PWM1, ENABLE);

    /* Loop forever */
    while(1);
    return 1;
}
/*********************************************************************//**
 * @brief		c_entry: Main PWM program body
 * @param[in]	None
 * @return 		int
 **********************************************************************/
int c_entry(void)
{
	uint8_t temp, temp2;
	PWM_TIMERCFG_Type PWMCfgDat;
	PWM_MATCHCFG_Type PWMMatchCfgDat;
	PINSEL_CFG_Type PinCfg;

	/* PWM block section -------------------------------------------- */
	/* Initialize PWM peripheral, timer mode
	 * PWM prescale value = 1 (absolute value - tick value) */
	PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
	PWMCfgDat.PrescaleValue = 1;
	PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat);

	/*
	 * Initialize PWM pin connect
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 2;
	for (temp = 0; temp <= 6; temp++){
		PinCfg.Pinnum = temp;
		PINSEL_ConfigPin(&PinCfg);
	}


	/* Set match value for PWM match channel 0 = 256, update immediately */
	PWM_MatchUpdate(LPC_PWM1, 0, 256, PWM_MATCH_UPDATE_NOW);
	/* PWM Timer/Counter will be reset when channel 0 matching
	 * no interrupt when match
	 * no stop when match */
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);

	/* Configure each PWM channel: --------------------------------------------- */
	/* - Single edge
	 * - PWM Duty on each PWM channel determined by
	 * the match on channel 0 to the match of that match channel.
	 * Example: PWM Duty on PWM channel 1 determined by
	 * the match on channel 0 to the match of match channel 1.
	 */

	/* Configure PWM channel edge option
	 * Note: PWM Channel 1 is in single mode as default state and
	 * can not be changed to double edge mode */
	for (temp = 2; temp < 7; temp++)
	{
		PWM_ChannelConfig(LPC_PWM1, temp, PWM_CHANNEL_SINGLE_EDGE);
	}


	/* Configure match value for each match channel */
	temp2 = 10;
	for (temp = 1; temp < 7; temp++)
	{
		/* Set up match value */
		PWM_MatchUpdate(LPC_PWM1, temp, temp2, PWM_MATCH_UPDATE_NOW);
		/* Configure match option */
		PWMMatchCfgDat.IntOnMatch = DISABLE;
		PWMMatchCfgDat.MatchChannel = temp;
		PWMMatchCfgDat.ResetOnMatch = DISABLE;
		PWMMatchCfgDat.StopOnMatch = DISABLE;
		PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);
		/* Enable PWM Channel Output */
		PWM_ChannelCmd(LPC_PWM1, temp, ENABLE);
		/* Increase match value by 10 */
		temp2 += 10;
	}

	/* Reset and Start counter */
	PWM_ResetCounter(LPC_PWM1);
	PWM_CounterCmd(LPC_PWM1, ENABLE);

	/* Start PWM now */
	PWM_Cmd(LPC_PWM1, ENABLE);

    /* Loop forever */
    while(1);
    return 1;
}
예제 #21
0
파일: PWM.cpp 프로젝트: koson/OpenOBC
void PWM::setDutyCycle(float dutyCycle)
{
	this->dutyCycle = dutyCycle;
	uint32_t pwmclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_PWM1);
	PWM_MatchUpdate(this->peripheral, this->channel, this->dutyCycle * (pwmclk / this->frequency), PWM_MATCH_UPDATE_NEXT_RST);
}
예제 #22
0
/*********************************************************************//**
 * @brief		c_entry: Main PWM program body
 * @param[in]	None
 * @return 		int
 **********************************************************************/
int c_entry(void)
{
	uint8_t temp, temp2;
	PWM_TIMERCFG_Type PWMCfgDat;
	PWM_MATCHCFG_Type PWMMatchCfgDat;
	PINSEL_CFG_Type PinCfg;

	/* PWM block section -------------------------------------------- */
	/* Initialize PWM peripheral, timer mode
	 * PWM prescale value = 1 (absolute value - tick value) */
	PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
	PWMCfgDat.PrescaleValue = 1;
	PWM_Init(LPC_PWM1, PWM_MODE_TIMER, (void *) &PWMCfgDat);

	/*
	 * Initialize PWM pin connect
	 */
	PinCfg.Funcnum = 1;
	PinCfg.OpenDrain = 0;
	PinCfg.Pinmode = 0;
	PinCfg.Portnum = 2;
	for (temp = 0; temp <= 6; temp++){
		PinCfg.Pinnum = temp;
		PINSEL_ConfigPin(&PinCfg);
	}


	/* Set match value for PWM match channel 0 = 256, update immediately */
	PWM_MatchUpdate(LPC_PWM1, 0, 256, PWM_MATCH_UPDATE_NOW);
	/* PWM Timer/Counter will be reset when channel 0 matching
	 * Enable interrupt when match
	 * no stop when match */
	PWMMatchCfgDat.IntOnMatch = ENABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);

	/* Configure each PWM channel: --------------------------------------------- */
	/* - Single edge
	 * - PWM Duty on each PWM channel determined by
	 * the match on channel 0 to the match of that match channel.
	 * Example: PWM Duty on PWM channel 1 determined by
	 * the match on channel 0 to the match of match channel 1.
	 */

	/* Configure PWM channel edge option
	 * Note: PWM Channel 1 is in single mode as default state and
	 * can not be changed to double edge mode */
	for (temp = 2; temp < 7; temp++)
	{
		PWM_ChannelConfig(LPC_PWM1, temp, PWM_CHANNEL_SINGLE_EDGE);
	}

	/* Setting interrupt for PWM ---------------------------------------------- */
    /* Disable PWM interrupt */
    NVIC_DisableIRQ(PWM1_IRQn);
    /* preemption = 1, sub-priority = 1 */
    NVIC_SetPriority(PWM1_IRQn, ((0x01<<3)|0x01));

	/* Configure match value for each match channel */
	temp2 = 10;
	for (temp = 1; temp < 7; temp++)
	{
		/* Set up match value */
		PWM_MatchUpdate(LPC_PWM1, temp, temp2, PWM_MATCH_UPDATE_NOW);
		/* Configure match option */
		PWMMatchCfgDat.IntOnMatch = DISABLE;
		PWMMatchCfgDat.MatchChannel = temp;
		PWMMatchCfgDat.ResetOnMatch = DISABLE;
		PWMMatchCfgDat.StopOnMatch = DISABLE;
		PWM_ConfigMatch(LPC_PWM1, &PWMMatchCfgDat);
		/* Enable PWM Channel Output */
		PWM_ChannelCmd(LPC_PWM1, temp, ENABLE);
		/* Increase match value by 10 */
		temp2 += 10;
	}

    /* Enable PWM interrupt */
    NVIC_EnableIRQ(PWM1_IRQn);

	/* Reset and Start counter */
	PWM_ResetCounter(LPC_PWM1);
	PWM_CounterCmd(LPC_PWM1, ENABLE);

	/* Start PWM now */
	PWM_Cmd(LPC_PWM1, ENABLE);

	// Update PWM1.1 every 0x1000 match interrupt
	while (1)
	{
		if (match_cnt >= 0x1000) {
			match_cnt = 0;
			long_duty++;
			if (long_duty >= 256) {
				// Reset duty
				long_duty = 0;
				// Print info
				//UART_Puts_(uartdev, "PWM1.1 is reset!");
			}

			// Update PWM1.1 duty
			PWM_MatchUpdate(LPC_PWM1, 1, long_duty, PWM_MATCH_UPDATE_NOW);
		}
	}
    return 1;
}
예제 #23
0
파일: PWM.cpp 프로젝트: koson/OpenOBC
PWM::PWM(uint8_t port, uint8_t pin, float dutyCycle, float frequency)
{
	this->port = port;
	this->pin = pin;
	this->dutyCycle = dutyCycle;
	this->frequency = frequency;

	PINSEL_CFG_Type PinCfg;
	if(port == 1 && pin == 18)
	{
		PinCfg.Funcnum = 2;
		this->channel = 1;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 1 && pin == 20)
	{
		PinCfg.Funcnum = 2;
		this->channel = 2;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 1 && pin == 21)
	{
		PinCfg.Funcnum = 2;
		this->channel = 3;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 1 && pin == 23)
	{
		PinCfg.Funcnum = 2;
		this->channel = 4;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 1 && pin == 24)
	{
		PinCfg.Funcnum = 2;
		this->channel = 5;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 1 && pin == 26)
	{
		PinCfg.Funcnum = 2;
		this->channel = 6;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 2 && pin == 0)
	{
		PinCfg.Funcnum = 1;
		this->channel = 1;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 2 && pin == 1)
	{
		PinCfg.Funcnum = 1;
		this->channel = 2;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 2 && pin == 2)
	{
		PinCfg.Funcnum = 1;
		this->channel = 3;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 2 && pin == 3)
	{
		PinCfg.Funcnum = 1;
		this->channel = 4;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 2 && pin == 4)
	{
		PinCfg.Funcnum = 1;
		this->channel = 5;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 2 && pin == 5)
	{
		PinCfg.Funcnum = 1;
		this->channel = 6;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 3 && pin == 25)
	{
		PinCfg.Funcnum = 3;
		this->channel = 2;
		this->peripheral = LPC_PWM1;
	}
	else if(port == 3 && pin == 26)
	{
		PinCfg.Funcnum = 3;
		this->channel = 3;
		this->peripheral = LPC_PWM1;
	}
	else
	{
		//invalid port/pin specified
// 		check_failed((uint8_t *)__FILE__, __LINE__);
		while(1);
	}

	PinCfg.Portnum = port;
	PinCfg.Pinnum = pin;
	PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL;
	PinCfg.Pinmode = PINSEL_PINMODE_TRISTATE;
	PINSEL_ConfigPin(&PinCfg);


	if(isInitialized)
	{
		uint32_t pwmclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_PWM1);

		/* Set match value for PWM match channel 0 and reset on match to set the period for all channels */
		PWM_MatchUpdate(this->peripheral, 0, pwmclk / this->frequency, PWM_MATCH_UPDATE_NOW);
		PWM_MATCHCFG_Type PWMMatchCfgDat;
		PWMMatchCfgDat.IntOnMatch = DISABLE;
		PWMMatchCfgDat.MatchChannel = 0;
		PWMMatchCfgDat.ResetOnMatch = ENABLE;
		PWMMatchCfgDat.StopOnMatch = DISABLE;
		PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat);


		/* Configure each PWM channel: --------------------------------------------- */
		/* - Single edge
		 * - PWM Duty on each PWM channel determined by
		 * the match on channel 0 to the match of that match channel.
		 * Example: PWM Duty on PWM channel 1 determined by
		 * the match on channel 0 to the match of match channel 1.
		 */
		PWM_ChannelConfig(this->peripheral, this->channel, PWM_CHANNEL_SINGLE_EDGE);


		/* Set up match value */
		PWM_MatchUpdate(this->peripheral, this->channel, this->dutyCycle * (pwmclk / this->frequency), PWM_MATCH_UPDATE_NOW);
		PWMMatchCfgDat.IntOnMatch = DISABLE;
		PWMMatchCfgDat.MatchChannel = this->channel;
		PWMMatchCfgDat.ResetOnMatch = DISABLE;
		PWMMatchCfgDat.StopOnMatch = DISABLE;
		PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat);

		/* Enable PWM Channel Output */
		PWM_ChannelCmd(this->peripheral, this->channel, ENABLE);
		return;
	}
	isInitialized = true;

	
	/* PWM block section -------------------------------------------- */
	/* Initialize PWM peripheral, timer mode
	 * PWM prescale value = 1 (absolute value - tick value) */
	PWM_TIMERCFG_Type PWMCfgDat;
	PWMCfgDat.PrescaleOption = PWM_TIMER_PRESCALE_TICKVAL;
	PWMCfgDat.PrescaleValue = 1;
	PWM_Init(this->peripheral, PWM_MODE_TIMER, (void*)&PWMCfgDat);

	uint32_t pwmclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_PWM1);

	/* Set match value for PWM match channel 0 and reset on match to set the period for all channels */
	PWM_MatchUpdate(this->peripheral, 0, pwmclk / this->frequency, PWM_MATCH_UPDATE_NOW);
	PWM_MATCHCFG_Type PWMMatchCfgDat;
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = 0;
	PWMMatchCfgDat.ResetOnMatch = ENABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat);


	/* Configure each PWM channel: --------------------------------------------- */
	/* - Single edge
	 * - PWM Duty on each PWM channel determined by
	 * the match on channel 0 to the match of that match channel.
	 * Example: PWM Duty on PWM channel 1 determined by
	 * the match on channel 0 to the match of match channel 1.
	 */
	PWM_ChannelConfig(this->peripheral, this->channel, PWM_CHANNEL_SINGLE_EDGE);


	/* Set up match value */
	PWM_MatchUpdate(this->peripheral, this->channel, this->dutyCycle * (pwmclk / this->frequency), PWM_MATCH_UPDATE_NOW);
	PWMMatchCfgDat.IntOnMatch = DISABLE;
	PWMMatchCfgDat.MatchChannel = this->channel;
	PWMMatchCfgDat.ResetOnMatch = DISABLE;
	PWMMatchCfgDat.StopOnMatch = DISABLE;
	PWM_ConfigMatch(this->peripheral, &PWMMatchCfgDat);
	
	/* Enable PWM Channel Output */
	PWM_ChannelCmd(this->peripheral, this->channel, ENABLE);
	
	/* Reset and Start counter */
	PWM_ResetCounter(this->peripheral);
	PWM_CounterCmd(this->peripheral, ENABLE);
	
	/* Start PWM now */
	PWM_Cmd(this->peripheral, ENABLE);
}
예제 #24
0
/*
Pre-Reqs:SetupPWM()
Desc: Sets PWM's match registers
Inputs: uint8_t match_channel - match register to update
		uint32_t match_value - value to update the match register with
*/
void SetRawPWM(uint8_t match_channel, uint32_t match_value){
	PWM_Cmd(LPC_PWM1, DISABLE);
	PWM_MatchUpdate(LPC_PWM1, match_channel, match_value, PWM_MATCH_UPDATE_NOW);
	PWM_Cmd(LPC_PWM1, ENABLE);
}