-
Notifications
You must be signed in to change notification settings - Fork 0
/
soft_pwm.c
54 lines (44 loc) · 1.5 KB
/
soft_pwm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @file soft_pwm.h
* @author Julian Bustamante N
* @brief software pwm driver
* @version 0.1.0
* @date 2021-04-09
*
* @copyright Copyright (c) 2021
*
*/
#include "soft_pwm.h"
#include <stdlib.h>
#include <stdio.h>
static int16_t SFPWM_Rounding(float num);
void SFPWM_Init(SFPWM_data_t *obj, write_pin_fcn _write_pin, float basetiming ,float freq )
{ obj->private.write_pin = _write_pin ;
obj->private.frequency = freq;
obj->private.period = (1 / obj->private.frequency); //0.1
obj->private.basetime = basetiming; //0.001
obj->private.count = 0;
obj->private.Tcomparar = (obj->private.period *(1/obj->private.basetime));
}
void SFPWM_Update(SFPWM_data_t *obj){
++obj->private.count;
if ( SFPWM_Rounding( obj->private.Tcomparar ) == obj->private.count ){ // overflow period
obj->private.count = 0; // overflow
}
obj->statepin = ( SFPWM_GetCount(obj) < obj->private.dutyc ) ? 1 : 0 ;
if( obj->private.write_pin != NULL){
obj->private.write_pin( obj->statepin ); // 10 < 127
}
}
uint16_t SFPWM_GetCount(SFPWM_data_t *obj){
return obj->private.count;
}
uint16_t SFPWM_GetStatePin(SFPWM_data_t *obj){
return obj->statepin;
}
void SFPWM_SetDuty(SFPWM_data_t *obj , uint16_t duty){
obj->private.dutyc = SFPWM_Rounding( ( (duty*( obj->private.Tcomparar ) ) / SFPWM_MAX_PERCENTAGE ) ) ;
}
static int16_t SFPWM_Rounding(float num){
return num < 0 ? num - 0.5 : num + 0.5;
}