1 #include "../hardware_api.h"
3 #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega328PB__)
6 void _pinHighFrequency(
const int pin){
9 if (pin == 5 || pin == 6 ) {
10 TCCR0A = ((TCCR0A & 0b11111100) | 0x01);
11 TCCR0B = ((TCCR0B & 0b11110000) | 0x01);
13 if (pin == 9 || pin == 10 )
14 TCCR1B = ((TCCR1B & 0b11111000) | 0x01);
15 if (pin == 3 || pin == 11)
16 TCCR2B = ((TCCR2B & 0b11111000) | 0x01);
25 void _configure2PWM(
long pwm_frequency,
const int pinA,
const int pinB) {
28 _pinHighFrequency(pinA);
29 _pinHighFrequency(pinB);
36 void _configure3PWM(
long pwm_frequency,
const int pinA,
const int pinB,
const int pinC) {
39 _pinHighFrequency(pinA);
40 _pinHighFrequency(pinB);
41 _pinHighFrequency(pinC);
49 analogWrite(pinA, 255.0*dc_a);
50 analogWrite(pinB, 255.0*dc_b);
58 analogWrite(pinA, 255.0*dc_a);
59 analogWrite(pinB, 255.0*dc_b);
60 analogWrite(pinC, 255.0*dc_c);
67 void _configure4PWM(
long pwm_frequency,
const int pin1A,
const int pin1B,
const int pin2A,
const int pin2B) {
70 _pinHighFrequency(pin1A);
71 _pinHighFrequency(pin1B);
72 _pinHighFrequency(pin2A);
73 _pinHighFrequency(pin2B);
79 void _writeDutyCycle4PWM(
float dc_1a,
float dc_1b,
float dc_2a,
float dc_2b,
int pin1A,
int pin1B,
int pin2A,
int pin2B){
81 analogWrite(pin1A, 255.0*dc_1a);
82 analogWrite(pin1B, 255.0*dc_1b);
83 analogWrite(pin2A, 255.0*dc_2a);
84 analogWrite(pin2B, 255.0*dc_2b);
89 int _configureComplementaryPair(
int pinH,
int pinL) {
90 if( (pinH == 5 && pinL == 6 ) || (pinH == 6 && pinL == 5 ) ){
92 TCCR0A = ((TCCR0A & 0b11111100) | 0x01);
94 if(pinH == 6 ) TCCR0A = 0b10110000 | (TCCR0A & 0b00001111) ;
95 else TCCR0A = 0b11100000 | (TCCR0A & 0b00001111) ;
97 TCCR0B = ((TCCR0B & 0b11110000) | 0x01);
98 }
else if( (pinH == 9 && pinL == 10 ) || (pinH == 10 && pinL == 9 ) ){
100 TCCR1B = ((TCCR1B & 0b11111000) | 0x01);
102 if(pinH == 9 ) TCCR1A = 0b10110000 | (TCCR1A & 0b00001111) ;
103 else TCCR1A = 0b11100000 | (TCCR1A & 0b00001111) ;
104 }
else if((pinH == 3 && pinL == 11 ) || (pinH == 11 && pinL == 3 ) ){
106 TCCR2B = ((TCCR2B & 0b11111000) | 0x01);
108 if(pinH == 11 ) TCCR2A = 0b10110000 | (TCCR2A & 0b00001111) ;
109 else TCCR2A = 0b11100000 | (TCCR2A & 0b00001111) ;
120 int _configure6PWM(
long pwm_frequency,
float dead_zone,
const int pinA_h,
const int pinA_l,
const int pinB_h,
const int pinB_l,
const int pinC_h,
const int pinC_l) {
124 ret_flag += _configureComplementaryPair(pinA_h, pinA_l);
125 ret_flag += _configureComplementaryPair(pinB_h, pinB_l);
126 ret_flag += _configureComplementaryPair(pinC_h, pinC_l);
131 void _setPwmPair(
int pinH,
int pinL,
float val,
int dead_time)
133 int pwm_h =
_constrain(val-dead_time/2,0,255);
134 int pwm_l =
_constrain(val+dead_time/2,0,255);
136 analogWrite(pinH, pwm_h);
137 if(pwm_l == 255 || pwm_l == 0)
138 digitalWrite(pinL, pwm_l ? LOW : HIGH);
140 analogWrite(pinL, pwm_l);
147 void _writeDutyCycle6PWM(
float dc_a,
float dc_b,
float dc_c,
float dead_zone,
int pinA_h,
int pinA_l,
int pinB_h,
int pinB_l,
int pinC_h,
int pinC_l){
148 _setPwmPair(pinA_h, pinA_l, dc_a*255.0, dead_zone*255.0);
149 _setPwmPair(pinB_h, pinB_l, dc_b*255.0, dead_zone*255.0);
150 _setPwmPair(pinC_h, pinC_l, dc_c*255.0, dead_zone*255.0);