improvements

master
Ondřej Hruška 8 years ago
parent 253064d39c
commit 5285f16784
  1. 21
      capture.c
  2. 2
      capture.h
  3. 36
      init.c
  4. 1
      init.h
  5. 37
      main.c

@ -1,5 +1,6 @@
#include "capture.h"
#include "utils/timebase.h"
#include <math.h>
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
}

@ -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);

@ -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.
}

@ -8,3 +8,4 @@ void init_systick(void);
void init_adc(void);
void init_dac(void);
void init_pwm1(void);
void init_pwm2(void);

@ -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

Loading…
Cancel
Save