Esempio n. 1
0
static void pwm_timer_init(pwm_callback update_callback) {
  /** timer config **/
  rcc_periph_clock_enable(RCC_TIM2);
  timer_reset(TIM2);

  timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
  timer_set_prescaler(TIM2, 0);

  timer_set_oc_mode(TIM2, TIM_OC1, TIM_OCM_PWM1);
  timer_set_oc_mode(TIM2, TIM_OC2, TIM_OCM_PWM2);

  timer_enable_oc_output(TIM2, TIM_OC1);
  timer_enable_oc_output(TIM2, TIM_OC2);

  timer_set_period(TIM2, 1024-1);

  timer_set_oc_value(TIM2, TIM_OC1, 0);
  timer_set_oc_value(TIM2, TIM_OC2, 0);

  timer_enable_counter(TIM2);

  if ( update_callback != 0 ) {
    pwm_update_callback = update_callback;
    nvic_enable_irq(NVIC_TIM2_IRQ);
    timer_enable_irq(TIM2, TIM_DIER_UIE);
  }
}
Esempio n. 2
0
/** Set pulse widths from actuator values, assumed to be in us
 */
void actuators_pwm_commit(void) {
#ifdef PWM_SERVO_0
  timer_set_oc_value(PWM_SERVO_0_TIMER, PWM_SERVO_0_OC, actuators_pwm_values[PWM_SERVO_0]);
#endif
#ifdef PWM_SERVO_1
  timer_set_oc_value(PWM_SERVO_1_TIMER, PWM_SERVO_1_OC, actuators_pwm_values[PWM_SERVO_1]);
#endif
#ifdef PWM_SERVO_2
  timer_set_oc_value(PWM_SERVO_2_TIMER, PWM_SERVO_2_OC, actuators_pwm_values[PWM_SERVO_2]);
#endif
#ifdef PWM_SERVO_3
  timer_set_oc_value(PWM_SERVO_3_TIMER, PWM_SERVO_3_OC, actuators_pwm_values[PWM_SERVO_3]);
#endif
#ifdef PWM_SERVO_4
  timer_set_oc_value(PWM_SERVO_4_TIMER, PWM_SERVO_4_OC, actuators_pwm_values[PWM_SERVO_4]);
#endif
#ifdef PWM_SERVO_5
  timer_set_oc_value(PWM_SERVO_5_TIMER, PWM_SERVO_5_OC, actuators_pwm_values[PWM_SERVO_5]);
#endif
#ifdef PWM_SERVO_6
  timer_set_oc_value(PWM_SERVO_6_TIMER, PWM_SERVO_6_OC, actuators_pwm_values[PWM_SERVO_6]);
#endif
#ifdef PWM_SERVO_7
  timer_set_oc_value(PWM_SERVO_7_TIMER, PWM_SERVO_7_OC, actuators_pwm_values[PWM_SERVO_7]);
#endif
#ifdef PWM_SERVO_8
  timer_set_oc_value(PWM_SERVO_8_TIMER, PWM_SERVO_8_OC, actuators_pwm_values[PWM_SERVO_8]);
#endif
#ifdef PWM_SERVO_9
  timer_set_oc_value(PWM_SERVO_9_TIMER, PWM_SERVO_9_OC, actuators_pwm_values[PWM_SERVO_9]);
#endif

}
void set_dual_pwm_timer_s_oc(uint32_t oc_value, uint32_t oc_value2)
{

#if DUAL_PWM_USE_TIM5
  timer_set_oc_value(DUAL_PWM_SERVO_5_TIMER, DUAL_PWM_SERVO_5_OC, oc_value);
  timer_set_oc_value(DUAL_PWM_SERVO_6_TIMER, DUAL_PWM_SERVO_6_OC, oc_value2);
#endif
}
Esempio n. 4
0
static void platform_init_pwm() {

  gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO6);
  gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO7);
  gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8);
  gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9);
  gpio_set_af(GPIOC, GPIO_AF2, GPIO6);
  gpio_set_af(GPIOC, GPIO_AF2, GPIO7);
  gpio_set_af(GPIOC, GPIO_AF2, GPIO8);
  gpio_set_af(GPIOC, GPIO_AF2, GPIO9);

  timer_reset(TIM3);
  timer_disable_oc_output(TIM3, TIM_OC1);
  timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
  /* 72ish Hz, close to 60 */
  timer_set_period(TIM3, 65535);
  timer_set_prescaler(TIM3, 16);
  timer_disable_preload(TIM3);
  timer_continuous_mode(TIM3);
  /* Setup output compare registers */
  timer_ic_set_input(TIM3,  TIM_IC1, TIM_IC_OUT);
  timer_disable_oc_clear(TIM3, TIM_OC1);
  timer_disable_oc_preload(TIM3, TIM_OC1);
  timer_set_oc_slow_mode(TIM3, TIM_OC1);
  timer_set_oc_mode(TIM3, TIM_OC1, TIM_OCM_PWM1);
  timer_set_oc_value(TIM3, TIM_OC1, 0);
  timer_set_oc_polarity_high(TIM3, TIM_OC1);
  timer_enable_oc_output(TIM3, TIM_OC1);

  timer_ic_set_input(TIM3,  TIM_IC2, TIM_IC_OUT);
  timer_disable_oc_clear(TIM3, TIM_OC2);
  timer_disable_oc_preload(TIM3, TIM_OC2);
  timer_set_oc_slow_mode(TIM3, TIM_OC2);
  timer_set_oc_mode(TIM3, TIM_OC2, TIM_OCM_PWM1);
  timer_set_oc_value(TIM3, TIM_OC2, 0);
  timer_set_oc_polarity_high(TIM3, TIM_OC2);
  timer_enable_oc_output(TIM3, TIM_OC2);

  timer_ic_set_input(TIM3,  TIM_IC3, TIM_IC_OUT);
  timer_disable_oc_clear(TIM3, TIM_OC3);
  timer_disable_oc_preload(TIM3, TIM_OC3);
  timer_set_oc_slow_mode(TIM3, TIM_OC3);
  timer_set_oc_mode(TIM3, TIM_OC3, TIM_OCM_PWM1);
  timer_set_oc_value(TIM3, TIM_OC3, 0);
  timer_set_oc_polarity_high(TIM3, TIM_OC3);
  timer_enable_oc_output(TIM3, TIM_OC3);

  timer_ic_set_input(TIM3,  TIM_IC4, TIM_IC_OUT);
  timer_disable_oc_clear(TIM3, TIM_OC4);
  timer_disable_oc_preload(TIM3, TIM_OC4);
  timer_set_oc_slow_mode(TIM3, TIM_OC4);
  timer_set_oc_mode(TIM3, TIM_OC4, TIM_OCM_PWM1);
  timer_set_oc_value(TIM3, TIM_OC4, 0);
  timer_set_oc_polarity_high(TIM3, TIM_OC4);
  timer_enable_oc_output(TIM3, TIM_OC4);
  timer_enable_counter(TIM3);

}
Esempio n. 5
0
void  SVM_voltage_switch_inverter_VSI(float duty_A,float duty_B,float duty_C,bool shutdown)
{
  //DTC-SVM switching selection
  float Attenuation;

  if (shutdown==false)
  {
    Attenuation=1.0f;

    //-------------SA: S1 and S4------------------------------------
    timer_set_oc_mode      (TIM1, TIM_OC1, TIM_OCM_PWM1);
    timer_enable_oc_output (TIM1, TIM_OC1 );  //S1
    timer_enable_oc_output (TIM1, TIM_OC1N);  //S4}

    //-------------SB: S3 and S6------------------------------------
    timer_set_oc_mode      (TIM1, TIM_OC2, TIM_OCM_PWM1);
    timer_enable_oc_output (TIM1, TIM_OC2 );  //S3
    timer_enable_oc_output (TIM1, TIM_OC2N);  //S6


    //-------------SC: S5 and S2-------------------------------------
    timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_PWM1);
    timer_enable_oc_output (TIM1, TIM_OC3 );  //S5 on
    timer_enable_oc_output (TIM1, TIM_OC3N);  //S2 off
  }  

  else
  {
    Attenuation=0.0f;
    duty_A=0.0f;
    duty_B=0.0f;
    duty_C=0.0f;

    //-------------SA: S1 and S4------------------------------------
    timer_set_oc_mode      (TIM1, TIM_OC1, TIM_OCM_PWM1);
    timer_disable_oc_output (TIM1, TIM_OC1 );  //S1
    timer_disable_oc_output (TIM1, TIM_OC1N);  //S4}

    //-------------SB: S3 and S6------------------------------------
    timer_set_oc_mode      (TIM1, TIM_OC2, TIM_OCM_PWM1);
    timer_disable_oc_output (TIM1, TIM_OC2 );  //S3
    timer_disable_oc_output (TIM1, TIM_OC2N);  //S6


    //-------------SC: S5 and S2-------------------------------------
    timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_PWM1);
    timer_disable_oc_output (TIM1, TIM_OC3 );  //S5 on
    timer_disable_oc_output (TIM1, TIM_OC3N);  //S2 off
  }  

  //Set the capture compare value for OC1.
  timer_set_oc_value(TIM1, TIM_OC1, duty_A*Attenuation*PWM_PERIOD_ARR);
  //Set the capture compare value for OC1.
  timer_set_oc_value(TIM1, TIM_OC2, duty_B*Attenuation*PWM_PERIOD_ARR);
  //Set the capture compare value for OC1.
  timer_set_oc_value(TIM1, TIM_OC3, duty_C*Attenuation*PWM_PERIOD_ARR);
}
Esempio n. 6
0
int output_speed(int channel, int speed) {
	check_channel(channel);
	check_speed(speed);
	if (channel == 0) {
		timer_set_oc_value(TIM2, TIM_OC2, speed*20);
	} else {
		timer_set_oc_value(TIM2, TIM_OC1, speed*20);
	}
	return 0;
}
Esempio n. 7
0
void set_pwm(int output, float value) {
  int ival = value * 65535;
  switch (output) {
    case 1:
      return timer_set_oc_value(TIM3, TIM_OC1, ival);
    case 2:
      return timer_set_oc_value(TIM3, TIM_OC2, ival);
    case 3:
      return timer_set_oc_value(TIM3, TIM_OC3, ival);
    case 4:
      return timer_set_oc_value(TIM3, TIM_OC4, ival);
  }
}
Esempio n. 8
0
void motor_control_pwm(int left, int right)
{
	//set direction
	if (left == 0)
	{
		gpio_clear(GPIOC, MOTOR_DIR_L_1 | MOTOR_DIR_L_2);

	}
	else if (left < 0)
	{
		gpio_set  (GPIOC, MOTOR_DIR_L_1);
		gpio_clear(GPIOC, MOTOR_DIR_L_2);
		left = -left;
	}
	else
	{
		gpio_set  (GPIOC, MOTOR_DIR_L_2);
		gpio_clear(GPIOC, MOTOR_DIR_L_1);
	}

	//set direction
	if (right == 0)
	{
		gpio_clear(GPIOC, MOTOR_DIR_R_1 | MOTOR_DIR_R_2);

	}
	else if (right < 0)
	{
		gpio_set  (GPIOC, MOTOR_DIR_R_1);
		gpio_clear(GPIOC, MOTOR_DIR_R_2);
		right = -right;
	}
	else
	{
		gpio_set  (GPIOC, MOTOR_DIR_R_2);
		gpio_clear(GPIOC, MOTOR_DIR_R_1);
	}

	// calculate compare registers
	motor_front_left  = get_oc_value( left  , MOTOR_RESOLUTION);
	motor_rear_left   = get_oc_value( left  , MOTOR_RESOLUTION);
	motor_front_rigth = get_oc_value( right , MOTOR_RESOLUTION);
	motor_rear_rigth  = get_oc_value( right , MOTOR_RESOLUTION);

	// Set the capture compare value
	timer_set_oc_value(TIM3, MOTOR_PWM_FRONT_LEFT,  motor_front_left);
	timer_set_oc_value(TIM3, MOTOR_PWM_REAR_LEFT,   motor_rear_left);
	timer_set_oc_value(TIM3, MOTOR_PWM_FRONT_RIGHT, motor_front_rigth);
	timer_set_oc_value(TIM3, MOTOR_PWM_REAR_RIGHT,  motor_rear_rigth);

}
void tim2_isr(void)
{
	if (timer_get_flag(TIM2, TIM_SR_CC1IF)) {

		/* Clear compare interrupt flag. */
		timer_clear_flag(TIM2, TIM_SR_CC1IF);

		/*
		 * Get current timer value to calculate next
		 * compare register value.
		 */
		compare_time = timer_get_counter(TIM2);

		/* Calculate and set the next compare value. */
		frequency = frequency_sequence[frequency_sel++];
		new_time = compare_time + frequency;

		timer_set_oc_value(TIM2, TIM_OC1, new_time);
		if (frequency_sel == 18)
			frequency_sel = 0;

		/* Toggle LED to indicate compare event. */
		gpio_toggle(GPIOC, GPIO12);
	}
}
Esempio n. 10
0
void usa_rxb(uint8_t ch)
{
    (void)ch;
    pwm[3] = 1;
    timer_set_oc_value(TIM5, TIM_OC4, pwm[3]);
    run = true;
}
Esempio n. 11
0
/******************************************************************************
Initializes the timer, turn on the interrupt and put the interrupt time to zero
INPUT	void
OUTPUT	void

The timer is set to roll over at 0.5 second from an 8us clock, and the output
compare 1 is used to trigger an alarm. This is set progressively by the
CanFestival stack.
******************************************************************************/
void initTimer(void)
{
    /* Set alarm back to zero */
    timerAlarm = 0;
    /* Enable TIM3 clock. */
    rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_TIM3EN);
    /* Enable TIM3 interrupt. */
    nvic_enable_irq(NVIC_TIM3_IRQ);
    timer_reset(TIM3);
    timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT,
                   TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
    /* Set prescaler to give 8us clock */
    timer_set_prescaler(TIM3, 576);
    /* Set the period as 0.5 second */
    timer_set_period(TIM3, TIMEVAL_MAX);
    /* Disable physical pin outputs. */
    timer_disable_oc_output(TIM3, TIM_OC1 | TIM_OC2 | TIM_OC3 | TIM_OC4);
    /* Configure global mode of output channel 1, disabling the output. */
    timer_disable_oc_clear(TIM3, TIM_OC1);
    timer_disable_oc_preload(TIM3, TIM_OC1);
    timer_set_oc_slow_mode(TIM3, TIM_OC1);
    timer_set_oc_mode(TIM3, TIM_OC1, TIM_OCM_FROZEN);
    /* Set the initial compare value for OC1. */
    timer_set_oc_value(TIM3, TIM_OC1, timerAlarm);
    /* Continous counting mode. */
    timer_continuous_mode(TIM3);
    /* ARR reload disable. */
    timer_disable_preload(TIM3);
    /* Counter enable. */
    timer_enable_counter(TIM3);
    /* Enable compare match interrupt. */
    timer_enable_irq(TIM3, TIM_DIER_CC1IE);
}
Esempio n. 12
0
static void pwm_setup(void) {
	/* Configure GPIOs: OUT=PA7 */
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
	    GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_TIM3_CH2 );

	timer_reset(TIM3);

	timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);

	timer_disable_oc_output(TIM3, TIM_OC2);
	timer_set_oc_mode(TIM3, TIM_OC2, TIM_OCM_PWM1);
	timer_disable_oc_clear(TIM3, TIM_OC2);
	timer_set_oc_value(TIM3, TIM_OC2, 0);
	timer_enable_oc_preload(TIM3, TIM_OC2);
	timer_set_oc_polarity_high(TIM3, TIM_OC2);
	timer_enable_oc_output(TIM3, TIM_OC2);

	timer_set_dma_on_update_event(TIM3);
	timer_enable_irq(TIM3, TIM_DIER_UDE); // in fact, enable DMA on update

	timer_enable_preload(TIM3);
	timer_continuous_mode(TIM3);
	timer_set_period(TIM3, WSP);

	timer_enable_counter(TIM3);
}
Esempio n. 13
0
void BeepInit(void)
{
    rcc_peripheral_enable_clock(&RCC_APB1ENR, BEEP_RCC_APB1ENR_TIMEN);
    gpio_set_mode(BEEP_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, BEEP_PIN);

    timer_set_mode(BEEP_TIM, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
    
    /* Period */
    timer_set_period(BEEP_TIM, 65535);

    /* Prescaler */
    timer_set_prescaler(BEEP_TIM, 5);
    timer_generate_event(BEEP_TIM, TIM_EGR_UG);

    /* ---- */
    /* Output compare 1 mode and preload */
    timer_set_oc_mode(BEEP_TIM, BEEP_TIM_OC, TIM_OCM_PWM1);
    timer_enable_oc_preload(BEEP_TIM, BEEP_TIM_OC);

    /* Polarity and state */
    timer_set_oc_polarity_low(BEEP_TIM, BEEP_TIM_OC);
    timer_enable_oc_output(BEEP_TIM, BEEP_TIM_OC);

    /* Capture compare value */
    timer_set_oc_value(BEEP_TIM, BEEP_TIM_OC, 0x8000);
    /* ---- */
    /* ARR reload enable */
    timer_enable_preload(BEEP_TIM);
}
Esempio n. 14
0
//naplni strukturu daty z retezce
void protokol(char *buf) //buf = 0 pro pøepsat  nastaveni, 1 pro spustit a 2 pro prepsat a spustit
{
    if(buf[0]==0)
    {
        Parametry.Cas_Nas=buf[3]*256+buf[4]; //uloz cas mereni
        Parametry.Napeti=buf[1]*256+buf[2];  //uloz merici napeti
        Parametry.set_param=1;  //nastav ze se maji zmenit parametry po domereni
        Parametry.start_mer=0;  // mereni se nema hend spustit
    }
    else if(buf[0]==1)
    {
        pwm[3] = 1; //vyresetuj mereni
        timer_set_oc_value(TIM5, TIM_OC4, pwm[3]); // nahraj do timeru 1
        run=true; //spust mereni
    }
    else if(buf[0]==2)
    {
        Parametry.Cas_Nas=buf[3]*256+buf[4]; //uloz do struktury cas
        Parametry.Napeti=buf[1]*256+buf[2]; //uloz do struktury merici napeti
        Parametry.set_param=1; //uloz priznak ze se maji zmenit parametry
        Parametry.start_mer=1; //uloz priznak ze se hned ma spustit nove mereni
        set_mer(); //zavolej fci, ktera prenese data ze struktury do patricnych periferii a promenych
    }
    else
    {
        run=false; //zastav mereni
    }

}
Esempio n. 15
0
void enable_test_trigger(trigger_type trig, unsigned int rpm) {
  if (trig != FORD_TFI) {
    return;
  }

  timeval_t t = time_from_rpm_diff(rpm, 45);

  /* Set up TIM5 as 32bit clock */
  rcc_periph_clock_enable(RCC_TIM5);
  gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO0);
  gpio_set_af(GPIOA, GPIO_AF2, GPIO0);
  timer_reset(TIM5);
  timer_disable_oc_output(TIM5, TIM_OC1);
  timer_set_mode(TIM5, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
  timer_set_period(TIM5, (unsigned int)t);
  timer_set_prescaler(TIM5, 0);
  timer_disable_preload(TIM5);
  timer_continuous_mode(TIM5);
  /* Setup output compare registers */
  timer_ic_set_input(TIM5,  TIM_IC1, TIM_IC_OUT);
  timer_disable_oc_clear(TIM5, TIM_OC1);
  timer_disable_oc_preload(TIM5, TIM_OC1);
  timer_set_oc_slow_mode(TIM5, TIM_OC1);
  timer_set_oc_mode(TIM5, TIM_OC1, TIM_OCM_TOGGLE);
  timer_set_oc_value(TIM5, TIM_OC1, t);
  timer_set_oc_polarity_high(TIM5, TIM_OC1);
  timer_enable_oc_output(TIM5, TIM_OC1);
  timer_enable_counter(TIM5);
}
Esempio n. 16
0
void pwm_set_duty(uint32_t timer, uint8_t channel, float dc) {
  assert(timer == TIM1 ||
         timer == TIM2 ||
         timer == TIM3 ||
         timer == TIM4 ||
         timer == TIM5 ||
         timer == TIM6 ||
         timer == TIM7 ||
         timer == TIM8);
  assert(channel == TIM_OC1 ||
         channel == TIM_OC2 ||
         channel == TIM_OC3 ||
         channel == TIM_OC4);
  uint32_t value;
  if (dc > 1.0) {
    dc = 1.0;
  } else if (dc < 0.0) {
    dc = 0.0;
  }

  value = lround(dc*PWM_PERIOD);

  //eprintf("Duty Cycle Set to %.2f%% [%lu of %lu] \n", dc*100.0,
  //value, (uint32_t)PWM_PERIOD);
  timer_set_oc_value(timer, channel, value);  // sets TIMx_CCRx
}
Esempio n. 17
0
int main(void)
{
    int i;

    rcc_clock_setup_in_hse_8mhz_out_72mhz();
    rcc_periph_clock_enable(RCC_GPIOC);
    rcc_periph_clock_enable(RCC_TIM3);
    
    rcc_periph_clock_enable(RCC_AFIO);
    gpio_primary_remap(0,AFIO_MAPR_TIM3_REMAP_FULL_REMAP );
    gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
            GPIO_CNF_OUTPUT_ALTFN_PUSHPULL , GPIO9);

    timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE,
               TIM_CR1_DIR_UP);
    
    timer_set_prescaler(TIM3,36);
    timer_set_period(TIM3, 1000);
    timer_set_oc_mode(TIM3, TIM_OC4, TIM_OCM_PWM1);
    timer_enable_oc_output(TIM3, TIM_OC4);
   // timer_enable_preload(TIM3);
    //timer_continuous_mode(TIM3);
    timer_enable_counter(TIM3);
    
    


#if defined(ENABLE_SEMIHOSTING) && (ENABLE_SEMIHOSTING) 
    initialise_monitor_handles();
#endif
    uint16_t a = 0;
    while (1)
    {
        for(i=0; i< 1000; i++){
        timer_set_oc_value(TIM3, TIM_OC4, i);
        delay();
        }
        for(i=999; i >=0 ; i--){
        timer_set_oc_value(TIM3, TIM_OC4, i);
        delay();
        }       
        //gpio_toggle(GPIOC, GPIO9); /* LED on/off */
    //printf("hello world\n");
        
        
    }
}
static void tim_setup(void)
{
	/* Enable TIM2 clock. */
	rcc_periph_clock_enable(RCC_TIM2);

	/* Enable TIM2 interrupt. */
	nvic_enable_irq(NVIC_TIM2_IRQ);

	/* Reset TIM2 peripheral. */
	timer_reset(TIM2);

	/* Timer global mode:
	 * - No divider
	 * - Alignment edge
	 * - Direction up
	 */
	timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT,
		       TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);

	/* Reset prescaler value. */
	timer_set_prescaler(TIM2, 36000);

	/* Enable preload. */
	timer_disable_preload(TIM2);

	/* Continous mode. */
	timer_continuous_mode(TIM2);

	/* Period (36kHz). */
	timer_set_period(TIM2, 65535);

	/* Disable outputs. */
	timer_disable_oc_output(TIM2, TIM_OC1);
	timer_disable_oc_output(TIM2, TIM_OC2);
	timer_disable_oc_output(TIM2, TIM_OC3);
	timer_disable_oc_output(TIM2, TIM_OC4);

	/* -- OC1 configuration -- */

	/* Configure global mode of line 1. */
	timer_disable_oc_clear(TIM2, TIM_OC1);
	timer_disable_oc_preload(TIM2, TIM_OC1);
	timer_set_oc_slow_mode(TIM2, TIM_OC1);
	timer_set_oc_mode(TIM2, TIM_OC1, TIM_OCM_FROZEN);

	/* Set the capture compare value for OC1. */
	timer_set_oc_value(TIM2, TIM_OC1, 1000);

	/* ---- */

	/* ARR reload enable. */
	timer_disable_preload(TIM2);

	/* Counter enable. */
	timer_enable_counter(TIM2);

	/* Enable commutation interrupt. */
	timer_enable_irq(TIM2, TIM_DIER_CC1IE);
}
Esempio n. 19
0
/**
 * Update our last capture time
 *
 */
void comm_tim_update_capture(void)
{
	comm_tim_data.last_capture_time = timer_get_counter(TIM2);
	timer_set_oc_value(TIM2, TIM_OC1,
			   comm_tim_data.last_capture_time + comm_tim_data.freq);

	OFF(DP_EXT_SCL);
}
Esempio n. 20
0
/* set pulse widths from actuator values, assumed to be in us */
void actuators_pwm_commit(void) {
  timer_set_oc_value(TIM3, TIM_OC1, actuators_pwm_values[0]);
  timer_set_oc_value(TIM3, TIM_OC2, actuators_pwm_values[1]);
  timer_set_oc_value(TIM3, TIM_OC3, actuators_pwm_values[2]);
  timer_set_oc_value(TIM3, TIM_OC4, actuators_pwm_values[3]);

#if USE_SERVOS_7AND8
  timer_set_oc_value(TIM4, TIM_OC1, actuators_pwm_values[6]);
  timer_set_oc_value(TIM4, TIM_OC2, actuators_pwm_values[7]);
#endif
#if REMAP_SERVOS_5AND6
  timer_set_oc_value(TIM5, TIM_OC1, actuators_pwm_values[4]);
  timer_set_oc_value(TIM5, TIM_OC2, actuators_pwm_values[5]);
#else
  timer_set_oc_value(TIM4, TIM_OC3, actuators_pwm_values[4]);
  timer_set_oc_value(TIM4, TIM_OC4, actuators_pwm_values[5]);
#endif

}
Esempio n. 21
0
void hbridge_init() {
  // M- bridge
  // A9 - pin 21 - PWM2A - HIN
  // B0 - pin 15 - PWM2B - \LIN
  
  // M+ bridge
  // A8 - pin 20 - PWM1A - HIN
  // A7 - pin 14 - PWM1B - \LIN
  
  rcc_peripheral_enable_clock(&RCC_APB2ENR,
    RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN |
    RCC_APB2ENR_AFIOEN | RCC_APB2ENR_TIM1EN);
  
  AFIO_MAPR |= AFIO_MAPR_TIM1_REMAP_PARTIAL_REMAP;
  gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
                GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO7 | GPIO8 | GPIO9);
  gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
                GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO0);
  
  timer_reset(TIM1);
  timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1,
                 TIM_CR1_DIR_UP);
  timer_set_period(TIM1, half_period_ticks);
  timer_set_prescaler(TIM1, 9); // / 10
  
  timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM2);
  timer_set_oc_polarity_high(TIM1, TIM_OC1);
  timer_set_oc_polarity_low(TIM1, TIM_OC1N);
  timer_enable_oc_output(TIM1, TIM_OC1);
  timer_enable_oc_output(TIM1, TIM_OC1N);
  timer_set_oc_value(TIM1, TIM_OC1, half_period_ticks);
  
  timer_set_oc_mode(TIM1, TIM_OC2, TIM_OCM_PWM2);
  timer_set_oc_polarity_high(TIM1, TIM_OC2);
  timer_set_oc_polarity_low(TIM1, TIM_OC2N);
  timer_enable_oc_output(TIM1, TIM_OC2);
  timer_enable_oc_output(TIM1, TIM_OC2N);
  timer_set_oc_value(TIM1, TIM_OC2, half_period_ticks);
  
  timer_enable_break_main_output(TIM1);
  timer_enable_counter(TIM1);
}
Esempio n. 22
0
/**
 * Update our last capture time and curr time
 *
 */
void comm_tim_update_capture_and_time(void)
{
	comm_tim_data.last_capture_time = timer_get_counter(TIM2);
	timer_set_oc_value(TIM2, TIM_OC1,
			comm_tim_data.last_capture_time + comm_tim_data.freq);

	comm_tim_data.prev_time = comm_tim_state.next_prev_time;
	comm_tim_data.curr_time = comm_tim_data.last_capture_time;
	comm_tim_state.next_prev_time = comm_tim_data.last_capture_time;
	comm_tim_data.update_count = comm_tim_state.update_count;
	comm_tim_state.update_count = 0;
}
Esempio n. 23
0
/******************************************************************************
Set the timer for the next alarm.
INPUT	value TIMEVAL (unsigned long) 0...TIMEVAL_MAX
OUTPUT	void
******************************************************************************/
void setTimer(TIMEVAL value)
{
    /* Add the desired time to the timer interrupt time for the next alarm */
    /* NOTE: This is computing (timerAlarm + value) % TIMEVAL_MAX, but so that it can
    handle possible (timerAlarm + value) beyond the integer range of TIMEVAL */
    /* Just make certain that value is in range */
    if (value > TIMEVAL_MAX) value = TIMEVAL_MAX;
    if (timerAlarm < TIMEVAL_MAX - value)
        timerAlarm += value;
    else
        timerAlarm -= TIMEVAL_MAX - value;
    timer_set_oc_value(TIM3, TIM_OC1, (unsigned long) timerAlarm);
}
Esempio n. 24
0
void sound_set_frequency(uint32_t freq) {
    uint32_t prescaler, period;

    if (freq <= 200) {
        // switch off pwm
        timer_disable_oc_output(TIM1, TIM_OC1);
        // return;
    }

    // reset TIMx peripheral
    timer_reset(TIM1);

    // roughly factor into 16-bit
    period    = (rcc_timer_frequency / 1) / freq;
    prescaler = (period / 65536) + 1;
    period    = (period / prescaler);

    // Set the timers global mode to:
    // - use no divider
    // - alignment edge
    // - count direction up
    timer_set_mode(TIM1,
                   TIM_CR1_CKD_CK_INT,
                   TIM_CR1_CMS_EDGE,
                   TIM_CR1_DIR_UP);

    timer_set_prescaler(TIM1, prescaler - 1);
    timer_set_repetition_counter(TIM1, 0);

    timer_enable_preload(TIM1);
    timer_continuous_mode(TIM1);
    timer_set_period(TIM1, period - 1);

    // start with disabled pwm output
    timer_disable_oc_output(TIM1, TIM_OC1);

    // NOTE: on advanced timers as TIM1 we have
    //       to break the main output, otherwise
    //       no pwm output signal will be present on pin
    timer_enable_break_main_output(TIM1);

    // configure output mode
    timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM1);
    // set period for 50/50 duty cycle
    timer_set_oc_value(TIM1, TIM_OC1, period / 2);
    // enable pwm output
    timer_enable_oc_output(TIM1, TIM_OC1);

    // start timer
    timer_enable_counter(TIM1);
}
Esempio n. 25
0
/**
 * Set the DSM timer to interrupt
 * @param[in] us The time in microseconds divided by 10
 */
void timer_dsm_set(u16 us) {
#if DEBUG && !DSM_RECEIVER && !DSM_MITM
	u16 new_t = (us*2 + timer_get_counter(TIMER_DSM)) & 65535;
#else
	u16 new_t = (us + timer_get_counter(TIMER_DSM)) & 65535;
#endif

	// Update the timer compare value 1
	timer_set_oc_value(TIMER_DSM, TIM_OC1, new_t);

	// Clear the interrupt flag and enable the interrupt of compare 1
	timer_clear_flag(TIMER_DSM, TIM_SR_CC1IF);
	timer_enable_irq(TIMER_DSM, TIM_DIER_CC1IE);
}
Esempio n. 26
0
void tim3_isr(void)
{
    if (timer_interrupt_source(TIM3,TIM_SR_UIF)) {
        timer_clear_flag(TIM3,TIM_SR_UIF);
        timer_set_oc_value(TIM3, TIM_OC1, PULSE);
        timer_disable_oc_output(TIM3,TIM_OC2);
        timer_disable_oc_output(TIM3,TIM_OC4);
        //timer_disable_irq(TIM3,TIM_OC2);
        //timer_disable_irq(TIM3,TIM_OC4);
        timer_enable_irq(TIM3,TIM_DIER_CC1IE);
        //gpio_set(GPIOA,GPIO1);
    }
    if ( timer_interrupt_source(TIM3,TIM_SR_CC1IF)) {
        timer_clear_flag(TIM3,TIM_SR_CC1IF);
        //if (PERIOD >= TIM3_CCR1) {
        //gpio_clear(GPIOA,GPIO1);
        timer_enable_oc_output(TIM3, TIM_OC2);
        //timer_enable_irq(TIM3,TIM_DIER_CC2IE);
       // }

    }
    if(timer_interrupt_source(TIM3,TIM_SR_CC2IF)) {
        timer_clear_flag(TIM3,TIM_SR_CC2IF);
        timer_set_oc_value(TIM3, TIM_OC1, PULSE*4);
        //timer_enable_oc_output(TIM3,TIM_OC1);
        timer_disable_oc_output(TIM3,TIM_OC2);
        //timer_disable_irq(TIM3,TIM_DIER_CC2IE);
    }
    if(timer_interrupt_source(TIM3,TIM_SR_CC3IF)) {
        timer_clear_flag(TIM3,TIM_SR_CC3IF);
        timer_enable_oc_output(TIM3,TIM_OC4);
        //timer_enable_irq(TIM3,TIM_DIER_CC4IE);
    }
    if(timer_interrupt_source(TIM3,TIM_SR_CC4IF)) {
        //timer_disable_oc_output(TIM3,TIM_OC4);
    }
}
Esempio n. 27
0
void PulseWidth::enable_output(uint8 channel)
{
    if(timer_peripheral == TIM2)
    {
        if(channel >= 4)
            return;

        //rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);

        gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
                      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
                      (1 << channel));
    }
    else if(timer_peripheral == TIM3)
    {
        switch(channel)
        {
        case 0:
        case 1:
        {
            //rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
            gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
                          GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
                          (1 << (channel+6)));
        }
        break;
        case 2:
        case 3:
        {
            //rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
            gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
                          GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
                          (1 << (channel-2)));
        }
        break;
        }
    }
    else
        return;

    tim_oc_id tid = static_cast<tim_oc_id>((channel%4)*2);

    timer_disable_oc_output(timer_peripheral, tid);
    timer_set_oc_mode(timer_peripheral, tid, TIM_OCM_PWM1);
    timer_set_oc_value(timer_peripheral, tid, 0);
    timer_enable_oc_output(timer_peripheral, tid);
}
Esempio n. 28
0
/*
                                --- FTFM (with edits) ---
Bullet points are the steps needed.

Pulse width modulation mode allows you to generate a signal with a frequency determined
by the value of the TIMx_ARR register (the period) and a duty cycle determined by the value of the
TIMx_CCRx register (the output compare value).
  -- Set TIMx_ARR to desired frequency
  -- Set TIMx_CCRx to desired duty cycle

The PWM mode can be selected independently on each channel (one PWM per OCx
output) by writing 110 (PWM mode 1) or ‘111 (PWM mode 2) in the OCxM bits in the
TIMx_CCMRx register.
  -- Write PWM Mode 1 or PWM Mode 2 to OCxM bits in TIMx_CCMRx register

You must enable the corresponding preload register by setting the
OCxPE bit in the TIMx_CCMRx register, and eventually the auto-reload preload register by
setting the ARPE bit in the TIMx_CR1 register.
  -- Set corresponding OCxPE bit in TIMx_CCMRx register
  -- Set ARPE bit in TIMx_CR1

As the preload registers are transferred to the shadow registers only when an update event
occurs, before starting the counter, you have to initialize all the registers by setting the UG
bit in the TIMx_EGR register.
  -- set UG bit in TIMx_EGR register

OCx polarity is software programmable using the CCxP bit in the TIMx_CCER register. It
can be programmed as active high or active low. OCx output is enabled by the CCxE bit in
the TIMx_CCER register. Refer to the TIMx_CCERx register description for more details.
  -- set desired polarity in TIMx_CCER
  -- set CCxE bit in TIMx_CCER  (enable output)
*/
static void
pwm_init(uint32_t timer,
         uint8_t channel,
         uint32_t period) {
  // Convert channel number to internal rep
  enum tim_oc_id chan;
  switch (channel) {
    case 1:   chan = TIM_OC1; break;
    case 2:   chan = TIM_OC2; break;
    case 3:   chan = TIM_OC3; break;
    case 4:   chan = TIM_OC4; break;
    default: assert(false); chan = -1; break;
  }

  // Timer Base Configuration
  // timer_reset(timer);
  timer_set_mode(timer, TIM_CR1_CKD_CK_INT, // clock division
                        TIM_CR1_CMS_EDGE,   // Center-aligned mode selection
                        TIM_CR1_DIR_UP);    // TIMx_CR1 DIR: Direction
  timer_continuous_mode(timer);             // Disables TIM_CR1_OPM (One pulse mode)
  timer_set_period(timer, period);                    // Sets TIMx_ARR
  timer_set_prescaler(timer, 1);               // Adjusts speed of timer
  timer_set_clock_division(timer, 0);            // Adjusts speed of timer
  timer_set_master_mode(timer, TIM_CR2_MMS_UPDATE);   // Master Mode Selection
  timer_enable_preload(timer);                        // Set ARPE bit in TIMx_CR1

  // Channel-specific settings
  timer_set_oc_value(timer, chan, 0);             // sets TIMx_CCRx
  timer_set_oc_mode(timer, chan, TIM_OCM_PWM1);   // Sets PWM Mode 1
  timer_enable_oc_preload(timer, chan);           // Sets OCxPE in TIMx_CCMRx
  timer_set_oc_polarity_high(timer, chan);        // set desired polarity in TIMx_CCER
  timer_enable_oc_output(timer, chan);             // set CCxE bit in TIMx_CCER  (enable output)

  // Initialize all counters in the register
  switch (timer) {
    case TIM1:  TIM1_EGR |= TIM_EGR_UG; break;
    case TIM2:  TIM2_EGR |= TIM_EGR_UG; break;
    case TIM3:  TIM3_EGR |= TIM_EGR_UG; break;
    case TIM4:  TIM4_EGR |= TIM_EGR_UG; break;
    case TIM5:  TIM5_EGR |= TIM_EGR_UG; break;
    case TIM6:  TIM6_EGR |= TIM_EGR_UG; break;
    case TIM7:  TIM7_EGR |= TIM_EGR_UG; break;
    case TIM8:  TIM8_EGR |= TIM_EGR_UG; break;
    default: assert(false); break;
    }
}
Esempio n. 29
0
void adc_finish(uint16_t values[])
{
    if (!run)
        return;

    fwrite(&pwm[3],1,2,us2);
    fwrite(&values[0],1,2,us2);
    fwrite(&values[1],1,2,us2);
    fwrite(&values[2],1,2,us2);

    // increment TIM5/OC4
    pwm[3] = (pwm[3] + 1) & 0xFFF;

    if (pwm[3] == 0)
        run = false;

    timer_set_oc_value(TIM5, TIM_OC4, pwm[3]);
}
Esempio n. 30
0
void set_mer(void) //prenese parametry ze struktury do promenych, a podle hodnoty ve strukture spusti enbo enspusti mereni
{
    set_napeti(Parametry.Napeti); //zavolej fci ktera nastavi vystupni anpeti
    nas_koef=Parametry.Cas_Nas; //uloz merici cas
    pwm[0]=Parametry.Cas_Nas*Parametry.default_pwm[0]; //do pole s parametry citacu uloz hodnoty
    pwm[1]=Parametry.Cas_Nas*Parametry.default_pwm[1];
    pwm[2]=Parametry.Cas_Nas*Parametry.default_pwm[2];
    pwm_init(); // volanim teto fce nahraju hodnoty do citacu
    Parametry.set_param=0; //zakazani dalsiho volani teto funkce kdyz se nezmenily parametry mereni
    if(Parametry.start_mer==1)
    {
        Parametry.start_mer=0; //zablokovani dalsiho spusteni mereno
        pwm[3] = 1; //reset citace od kereho se odviji spusteni ADC
        timer_set_oc_value(TIM5, TIM_OC4, pwm[3]);
        run=true;//spusteni mereni

    }


}