tdse-tp4_02-pwm

FIUBA - Electrónica - Taller de Sistemas Embebidos - Trabajo Práctico N°: 4 - PWM
Index Commits Files Refs README
app/src/app.c (3075B)
   1 /* Project includes. */
   2 #include "main.h"
   3 
   4 
   5 /* App includes. */
   6 #include <stdlib.h>
   7 #include "app.h"
   8 #include "logger.h"
   9 
  10 /* Application includes. */
  11 
  12 
  13 /********************** macros and definitions *******************************/
  14 
  15 #define TEST_1 (1)
  16 #define TEST_2 (2)
  17 
  18 #define TEST_NUMBER TEST_1 //TEST_1, TEST_2
  19 
  20 #define DELAY_TICKS (4)
  21 #define STEP (100)
  22 #define PERIOD (65535)
  23 
  24 /********************** external data declaration *****************************/
  25 extern TIM_HandleTypeDef htim3;
  26 
  27 /********************** external functions definition ************************/
  28 
  29 /********************** internal data declaration ****************************/
  30 uint32_t tickstart;
  31 
  32 
  33 /********************** internal data definition *****************************/
  34 
  35 /********************** internal functions definitions ***********************/
  36 void test1_tick();
  37 void test2_tick();
  38 
  39 void setPWM(TIM_HandleTypeDef timer,
  40             uint32_t channel,
  41             uint16_t period,
  42             uint16_t pulse);
  43 
  44 
  45 /********************** internal functions declaration ***********************/
  46 
  47 
  48 
  49 void app_init(void)
  50 {
  51 
  52 
  53 }
  54 
  55 void app_update(void)
  56 {
  57 #if TEST_1==TEST_NUMBER
  58     test1_tick();
  59 #endif
  60 #if TEST_2==TEST_NUMBER
  61     test2_tick();
  62 #endif
  63 
  64 }
  65 
  66 
  67 
  68 #if TEST_1==TEST_NUMBER
  69 void test1_tick() {
  70 
  71     static uint16_t active = 0;
  72     static uint16_t period=PERIOD;
  73     static bool first = true;
  74     static uint32_t tick;
  75     static int16_t step = STEP;
  76 
  77     if (first) {
  78         first = false;
  79         tick = HAL_GetTick() + DELAY_TICKS;
  80     }
  81 
  82     if (HAL_GetTick()>=tick) {
  83         setPWM(htim3, TIM_CHANNEL_1, period, active);
  84         if (step>0) {
  85             if (period-step<=active) {
  86                 step = step * -1;
  87             }
  88         }
  89         else {
  90             if (abs(step)>=active) {
  91                 step = step * -1;
  92             }
  93         }
  94         active = active + step;
  95         tick = HAL_GetTick() + DELAY_TICKS;
  96     }
  97 }
  98 #endif
  99 
 100 #if TEST_2==TEST_NUMBER
 101 void test2_tick() {
 102 
 103     static uint16_t period=PERIOD/4;
 104     static bool first = true;
 105     static uint32_t tick;
 106     static int16_t step = STEP;
 107 
 108     if (first) {
 109         first = false;
 110         tick = HAL_GetTick() + DELAY_TICKS;
 111     }
 112 
 113     if (HAL_GetTick()>=tick) {
 114         setPWM(htim3, TIM_CHANNEL_1, period, period/2);
 115         if (step>0) {
 116             if (period-step>=PERIOD/2) {
 117                 step = step * -1;
 118             }
 119         }
 120         else {
 121             if (period-step<=PERIOD/4) {
 122                 step = step * -1;
 123             }
 124         }
 125         period = period + step;
 126         tick = HAL_GetTick() + DELAY_TICKS;
 127     }
 128 }
 129 #endif
 130 
 131 void setPWM(TIM_HandleTypeDef timer,
 132             uint32_t channel,
 133             uint16_t period,
 134             uint16_t pulse) {
 135   HAL_TIM_PWM_Stop(&timer, channel);
 136   TIM_OC_InitTypeDef sConfigOC;
 137   timer.Init.Period = period;
 138   HAL_TIM_PWM_Init(&timer);
 139 
 140   sConfigOC.OCMode = TIM_OCMODE_PWM1;
 141   sConfigOC.Pulse = pulse;
 142   sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
 143   sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
 144   HAL_TIM_PWM_ConfigChannel(&timer, &sConfigOC, channel);
 145 
 146   HAL_TIM_PWM_Start(&timer,channel);
 147 }
 148 
 149 
 150 /********************** end of file ******************************************/