From 5285f1678426081e13e20ff3393a7a8a049c2fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 6 Jan 2016 20:10:34 +0100 Subject: [PATCH] improvements --- capture.c | 21 +++++++++++++++++++++ capture.h | 2 ++ init.c | 36 ++++++++++++++++++++++++++++++++++++ init.h | 1 + main.c | 37 +++++++++++++++++++++++++++++++++++-- 5 files changed, 95 insertions(+), 2 deletions(-) diff --git a/capture.c b/capture.c index aeff424..1b78fee 100644 --- a/capture.c +++ b/capture.c @@ -1,5 +1,6 @@ #include "capture.h" #include "utils/timebase.h" +#include uint16_t adc_measure(uint8_t channel) { @@ -54,3 +55,23 @@ float measure_temp(void) return ((80.0f / (ts_cal2 - ts_cal1)) * (ts_data - ts_cal1) + 30.0f); } + + +void pwm2_set_frequency(float hz) +{ + float core_clk = 16000000 / 2.0f; // prescaller + + float cnt = core_clk / hz; + + uint32_t div = (uint32_t)cnt; + + if (hz < 150) { + div /= 8; + TIM2_PSC = 15; + } else { + TIM2_PSC = 1; + } + + TIM2_ARR = div; // sets frequency + TIM2_CCR2 = TIM2_ARR/2; // duty cycle +} diff --git a/capture.h b/capture.h index 9549e1e..0c6c8c1 100644 --- a/capture.h +++ b/capture.h @@ -6,3 +6,5 @@ float measure_temp(void); float measure_angle(void); float measure_resistance(void); float measure_exposure(void); + +void pwm2_set_frequency(float hz); diff --git a/init.c b/init.c index 26e765f..ed0d5e6 100644 --- a/init.c +++ b/init.c @@ -141,6 +141,42 @@ void init_pwm1(void) } +// pwm with variable frequency +void init_pwm2(void) +{ + // enable clock for the timer + RCC_APB1ENR |= RCC_APB1ENR_TIM2EN; + + // using timer 2, channel 2 - PA1 + gpio_set_af(GPIOA, BIT1, AF1); + + patch_register(TIM2_CCMR1, TIM_CCMR1_OC2M, TIM_OCM_PWM1); // set PWM1 mode + + TIM2_CCMR1 |= TIM_CCMR1_OC2PE; // preload enable + TIM2_CR1 |= TIM_CR1_ARPE; // auto reload is buffered + TIM2_CCER |= TIM_CCER_CC2E; // enable output compare (PWM output) + + patch_register(TIM2_CR1, TIM_CR1_CMS, TIM_CMS_EDGE); // centering mode + patch_register(TIM2_CR1, TIM_CR1_DIR, 0); // count upwards only + + // frequency set to 16 kHz + + /* + TIM2_PSC = 32; // prescaller + TIM2_ARR = 1000; // sets frequency + TIM2_CCR2 = 500; // duty cycle + */ + + TIM2_PSC = 1; // prescaller + TIM2_ARR = 1600; // sets frequency + TIM2_CCR2 = 500; // duty cycle + + // generate update event to latch the value registers + TIM2_EGR |= TIM_EGR_UG; + + TIM2_CR1 |= TIM_CR1_CEN; // enable timer. +} + diff --git a/init.h b/init.h index a8d62f6..afc1b3e 100644 --- a/init.h +++ b/init.h @@ -8,3 +8,4 @@ void init_systick(void); void init_adc(void); void init_dac(void); void init_pwm1(void); +void init_pwm2(void); diff --git a/main.c b/main.c index 6cd7387..1d211ba 100644 --- a/main.c +++ b/main.c @@ -15,8 +15,10 @@ static bool gate_closed = false; static uint32_t gate_cnt = 0; -float exposure_out, exposure_accum; -uint32_t exposure_cnt; +static float exposure_out, exposure_accum; +static uint32_t exposure_cnt; + +static uint32_t pwm2_f = 50; /** IRQ */ void USART2_IRQHandler(void) @@ -32,6 +34,8 @@ void USART2_IRQHandler(void) // handle incoming char. char c = usart_rx_char(USART2); + uint32_t pwm2_f_old = pwm2_f; + switch (c) { case 'g': // nulovat pocitadlo preruseni gate_cnt = 0; @@ -53,10 +57,36 @@ void USART2_IRQHandler(void) if (TIM3_CCR1 > 0) TIM3_CCR1 -= 50; break; + case 'f': // zvysit PWM stridu + if (pwm2_f < 20000) pwm2_f += 10; + break; + + case 's': // snizit PWM stridu + if (pwm2_f > 20) pwm2_f -= 10; + break; + + case 'F': // zvysit PWM stridu + if (pwm2_f < 20000-100) pwm2_f += 100; + break; + + case 'S': // snizit PWM stridu + if (pwm2_f > 100) { + pwm2_f -= 100; + } else { + pwm2_f = 20; + } + + if (pwm2_f < 20) pwm2_f = 20; + + break; + default: break; } + if (pwm2_f_old != pwm2_f) { + pwm2_set_frequency(pwm2_f); + } USART2_SR ^= USART_SR_RXNE; } @@ -88,6 +118,9 @@ void SystemInit(void) init_adc(); init_dac(); init_pwm1(); + init_pwm2(); + + pwm2_set_frequency(50); register_periodic_task(green_toggle, 1000); // indicate running state