Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This file is part of the libopencm3 project.
- *
- * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
- * Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
- * Modified by Fernando Cortes <fermando.corcam@gmail.com>
- * modified by Guillermo Rivera <memogrg@gmail.com>
- *
- * This library is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <libopencm3/stm32/rcc.h>
- #include <libopencm3/stm32/gpio.h>
- #include <libopencm3/stm32/timer.h>
- #include <libopencm3/stm32/flash.h>
- /*
- A basic routine to adjust system clock settings to get SYSCLK
- to 64 MHz, and have AHB buses at 64 MHz, and APB Bus at 32 MHz (its max speed)
- Copied from:
- https://github.com/libopencm3/libopencm3-examples/blob/master/examples/stm32/f3/stm32f3-discovery/adc/adc.c
- */
- static void set_system_clock(void)
- {
- rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_64MHZ]);
- }
- static void setup_timer3_pwm(void)
- {
- rcc_periph_clock_enable(RCC_GPIOC);
- gpio_set_output_options(GPIOC, GPIO_OTYPE_PP,
- GPIO_OSPEED_50MHZ, GPIO8 | GPIO9);
- gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8 | GPIO9);
- gpio_set_af(GPIOC, GPIO_AF2, GPIO8 | GPIO9);
- timer_reset(TIM3);
- rcc_periph_clock_enable(RCC_TIM3);
- timer_continuous_mode(TIM3);
- timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1,
- TIM_CR1_DIR_UP);
- timer_set_oc_mode(TIM3, TIM_OC3, TIM_OCM_PWM1);
- timer_set_oc_mode(TIM3, TIM_OC4, TIM_OCM_PWM1);
- timer_set_period(TIM3, 32000);
- timer_enable_preload(TIM3);
- timer_enable_oc_preload(TIM3, TIM_OC3);
- timer_enable_oc_preload(TIM3, TIM_OC4);
- timer_set_oc_polarity_high(TIM3, TIM_OC3);
- timer_enable_oc_output(TIM3, TIM_OC3);
- timer_enable_oc_output(TIM3, TIM_OC4);
- timer_enable_counter(TIM3);
- timer_enable_update_event(TIM3);
- timer_generate_event(TIM3, TIM_EGR_UG);
- }
- int main(void)
- {
- set_system_clock();
- setup_timer3_pwm();
- while (1)
- {
- if(timer_get_flag(TIM3, TIM_SR_UIF))
- {
- timer_clear_flag(TIM3, TIM_SR_UIF);
- timer_set_oc_value(TIM3, TIM_OC3, 16000);
- timer_set_oc_value(TIM3, TIM_OC4, 24000);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement