====== Set_up.c ====== void clock_setup(void) //this sets and starts the global clock on the microcontroller to 168MHz { rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); } void gpio_setup(void) // Enable GPIO clock and sets the leds to output push-pull mode { /* Enable GPIOC clock. */ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN); /* Set GPIOs (in GPIO port D) to 'output push-pull'. */ gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT,GPIO_PUPD_NONE, GPIO12); // green LED gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT,GPIO_PUPD_NONE, GPIO13); // orange led gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT,GPIO_PUPD_NONE, GPIO14); // red led gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT,GPIO_PUPD_NONE, GPIO15); // blue led } void exti_setup(void) //sets the external interruption to be used { /* Enable GPIOA clock. */ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN); /* Enable EXTI0 interrupt. */ nvic_enable_irq(NVIC_EXTI0_IRQ); /*enable update interrupt*/ nvic_enable_irq(NVIC_TIM1_UP_TIM10_IRQ); /* Set GPIO0 (in GPIO port A) to 'input open-drain'. */ gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO0); //PA0 is connected to the user button, so this enables the use of this button gpio_set_af(GPIOA, GPIO_AF0, GPIO0); //this sets the GPIOA0(user button) AF function to first function. /* Configure the EXTI subsystem. */ exti_select_source(EXTI0, GPIOA); //associates EXTI0 to block GPIOA exti_direction = FALLING; //sets exti direction to Falling, this is just for comparison exti_set_trigger(EXTI0, EXTI_TRIGGER_FALLING); //this one sets the trigger direction for EXTI0 to falling, so it occur when the button is pressed DOWN exti_enable_request(EXTI0); //This enables EXTI0 to be called. } void tim_setup(void) { /* Enable TIM1 clock. */ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_TIM1EN); //F4 clock for TIM 1 is APB2. TIM1 port is E /*Enable AF clock*/ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPEEN);//this enables another clock for alternative function. /* * Set TIM1 channel output pins to * 'output alternate function push-pull'. */ gpio_mode_setup(GPIOE, GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO9 | GPIO11 | GPIO13); //Associates Pins 09, 11 and 13 to TIM1 /*TIM1 CH1, CH2 and CH3 as alternate functions*/ gpio_set_af(GPIOE, GPIO_AF1, GPIO9 | GPIO11 | GPIO13); //Then sets function that the pin will be acomplishing for TIM1: GIO9: CH1, GPIO11: CH2, //GPIO13: CH3 /* * Set TIM1 complementary channel output pins to * 'output alternate function push-pull'. */ gpio_mode_setup(GPIOE, GPIO_MODE_AF,GPIO_PUPD_NONE,GPIO8 | GPIO10 | GPIO12); //Associates pins 08, 10 and 12 to TIM1 /*TIM1 CH1N, CH2N and CH3N as alternate functions*/ gpio_set_af(GPIOE, GPIO_AF1, GPIO8 | GPIO10 | GPIO12); //then sets the functions that the pins will be acompliching for TIM1: GIO8: CH1N, GPIO10: CH2N, //GPIO12: CH3N /* Enable TIM1 commutation interrupt. */ nvic_enable_irq(NVIC_TIM1_TRG_COM_TIM11_IRQ); /* Reset TIM1 peripheral. */ timer_reset(TIM1); /* Timer global mode: * - No divider * - Alignment, center with mode 3* see manual for details * - Direction up */ timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_3, TIM_CR1_DIR_UP); /* Reset prescaler value for TIM1. */ timer_set_prescaler(TIM1, prescale); /* Reset repetition counter value for TIM1. */ timer_set_repetition_counter(TIM1, repetition_counter_value); /* Enable preload on TIM1. */ timer_enable_preload(TIM1); /* Sets TIM1 Continuous mode. */ timer_continuous_mode(TIM1); /* Sets the Period for TIM1 PWM, named ARR. (64kHz see "pwm_6step_experiment.h")*/ timer_set_period(TIM1,pwm_period_ARR); /* Configure break and deadtime. */ timer_set_deadtime(TIM1, deadtime_percentage*pwm_period_ARR); //sets the deadtime value as a percentage of the wave period timer_set_enabled_off_state_in_idle_mode(TIM1); timer_set_enabled_off_state_in_run_mode(TIM1); timer_disable_break(TIM1); //disables emergency stop function, break timer_set_break_polarity_high(TIM1); timer_disable_break_automatic_output(TIM1); timer_set_break_lock(TIM1, TIM_BDTR_LOCK_OFF); /* -- OC1 and OC1N configuration -- */ /* Disable outputs. */ timer_disable_oc_output(TIM1, TIM_OC1); timer_disable_oc_output(TIM1, TIM_OC1N); /* Configure global mode of line 1. */ timer_disable_oc_clear(TIM1, TIM_OC1); timer_enable_oc_preload(TIM1, TIM_OC1); timer_set_oc_slow_mode(TIM1, TIM_OC1); timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_PWM1); /* Configure OC1. */ timer_set_oc_polarity_high(TIM1, TIM_OC1); timer_set_oc_idle_state_set(TIM1, TIM_OC1); /* Configure OC1N. */ timer_set_oc_polarity_high(TIM1, TIM_OC1N); timer_set_oc_idle_state_set(TIM1, TIM_OC1N); /* Set the initial capture compare value for OC1. */ timer_set_oc_value(TIM1, TIM_OC1, 1000);//this will be changed later to: initial_duty_cycle*pwm_period_ARR /* Reenable outputs. */ timer_enable_oc_output(TIM1, TIM_OC1); timer_enable_oc_output(TIM1, TIM_OC1N); /* -- OC2 and OC2N configuration -- */ /* Disable outputs. */ timer_disable_oc_output(TIM1, TIM_OC2); timer_disable_oc_output(TIM1, TIM_OC2N); /* Configure global mode of line 2. */ timer_disable_oc_clear(TIM1, TIM_OC2); timer_enable_oc_preload(TIM1, TIM_OC2); timer_set_oc_slow_mode(TIM1, TIM_OC2); timer_set_oc_mode(TIM1, TIM_OC2, TIM_OCM_PWM1); /* Configure OC2. */ timer_set_oc_polarity_high(TIM1, TIM_OC2); timer_set_oc_idle_state_set(TIM1, TIM_OC2); /* Configure OC2N. */ timer_set_oc_polarity_high(TIM1, TIM_OC2N); timer_set_oc_idle_state_set(TIM1, TIM_OC2N); /* Set the capture compare value for OC2. */ timer_set_oc_value(TIM1, TIM_OC2, 1000);//initial_duty_cycle*pwm_period_ARR); /* Reenable outputs. */ timer_enable_oc_output(TIM1, TIM_OC2); timer_enable_oc_output(TIM1, TIM_OC2N); /* -- OC3 and OC3N configuration -- */ /* Disable outputs. */ timer_disable_oc_output(TIM1, TIM_OC3); timer_disable_oc_output(TIM1, TIM_OC3N); /* Configure global mode of line 3. */ timer_disable_oc_clear(TIM1, TIM_OC3); timer_enable_oc_preload(TIM1, TIM_OC3); timer_set_oc_slow_mode(TIM1, TIM_OC3); timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_PWM1); /* Configure OC3. */ timer_set_oc_polarity_high(TIM1, TIM_OC3); timer_set_oc_idle_state_set(TIM1, TIM_OC3); /* Configure OC3N. */ timer_set_oc_polarity_high(TIM1, TIM_OC3N); timer_set_oc_idle_state_set(TIM1, TIM_OC3N); /* Set the capture compare value for OC3. */ timer_set_oc_value(TIM1, TIM_OC3, 1000);//initial_duty_cycle*pwm_period_ARR);//100); /* Reenable outputs. */ timer_enable_oc_output(TIM1, TIM_OC3); timer_enable_oc_output(TIM1, TIM_OC3N); /* ---- */ /* ARR reload enable. */ timer_enable_preload(TIM1); // once the configuration is done and the lines are enabled again, we reload the TIM1 /* * Enable preload of complementary channel configurations and * update on COM event. */ //timer_enable_preload_complementry_enable_bits(TIM1); timer_disable_preload_complementry_enable_bits(TIM1); /* Enable outputs in the break subsystem. */ timer_enable_break_main_output(TIM1); /* Counter enable. */ timer_enable_counter(TIM1); /* Enable commutation interrupt. */ timer_enable_irq(TIM1, TIM_DIER_COMIE); /* Enable capture/compare interrupt. */ timer_enable_irq(TIM1, TIM_DIER_CC1IE); nvic_enable_irq(NVIC_TIM1_CC_IRQ); } Go back to [[teaching:ie0117:proyectos:2012:i:final:stm32f4-discovery:controlador|Motor Control]] [[ie0117_proyectos:final:stm32f4-discovery:|INDEX]]