SimpleFOClibrary  2.1
atmega2560_mcu.cpp
Go to the documentation of this file.
1 #include "../hardware_api.h"
2 
3 #if defined(__AVR_ATmega2560__)
4 
5 // set pwm frequency to 32KHz
6 void _pinHighFrequency(const int pin){
7  // High PWM frequency
8  // https://sites.google.com/site/qeewiki/books/avr-guide/timers-on-the-atmega328
9  // https://forum.arduino.cc/index.php?topic=72092.0
10  if (pin == 13 || pin == 4 ) {
11  TCCR0A = ((TCCR0A & 0b11111100) | 0x01); // configure the pwm phase-corrected mode
12  TCCR0B = ((TCCR0B & 0b11110000) | 0x01); // set prescaler to 1
13  }
14  else if (pin == 12 || pin == 11 )
15  TCCR1B = ((TCCR1B & 0b11111000) | 0x01); // set prescaler to 1
16  else if (pin == 10 || pin == 9 )
17  TCCR2B = ((TCCR2B & 0b11111000) | 0x01); // set prescaler to 1
18  else if (pin == 5 || pin == 3 || pin == 2)
19  TCCR3B = ((TCCR2B & 0b11111000) | 0x01); // set prescaler to 1
20  else if (pin == 8 || pin == 7 || pin == 6)
21  TCCR4B = ((TCCR4B & 0b11111000) | 0x01); // set prescaler to 1
22  else if (pin == 44 || pin == 45 || pin == 46)
23  TCCR5B = ((TCCR5B & 0b11111000) | 0x01); // set prescaler to 1
24 
25 }
26 
27 
28 // function setting the high pwm frequency to the supplied pins
29 // - Stepper motor - 2PWM setting
30 // - hardware speciffic
31 void _configure2PWM(long pwm_frequency,const int pinA, const int pinB) {
32  // High PWM frequency
33  // - always max 32kHz
34  _pinHighFrequency(pinA);
35  _pinHighFrequency(pinB);
36 }
37 
38 // function setting the high pwm frequency to the supplied pins
39 // - BLDC motor - 3PWM setting
40 // - hardware speciffic
41 void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC) {
42  // High PWM frequency
43  // - always max 32kHz
44  _pinHighFrequency(pinA);
45  _pinHighFrequency(pinB);
46  _pinHighFrequency(pinC);
47 }
48 
49 // function setting the pwm duty cycle to the hardware
50 // - Stepper motor - 2PWM setting
51 // - hardware speciffic
52 void _writeDutyCycle2PWM(float dc_a, float dc_b, int pinA, int pinB){
53  // transform duty cycle from [0,1] to [0,255]
54  analogWrite(pinA, 255.0*dc_a);
55  analogWrite(pinB, 255.0*dc_b);
56 }
57 
58 // function setting the pwm duty cycle to the hardware
59 // - BLDC motor - 3PWM setting
60 // - hardware speciffic
61 void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, int pinA, int pinB, int pinC){
62  // transform duty cycle from [0,1] to [0,255]
63  analogWrite(pinA, 255.0*dc_a);
64  analogWrite(pinB, 255.0*dc_b);
65  analogWrite(pinC, 255.0*dc_c);
66 }
67 
68 // function setting the high pwm frequency to the supplied pins
69 // - Stepper motor - 4PWM setting
70 // - hardware speciffic
71 void _configure4PWM(long pwm_frequency,const int pin1A, const int pin1B, const int pin2A, const int pin2B) {
72  // High PWM frequency
73  // - always max 32kHz
74  _pinHighFrequency(pin1A);
75  _pinHighFrequency(pin1B);
76  _pinHighFrequency(pin2A);
77  _pinHighFrequency(pin2B);
78 }
79 
80 // function setting the pwm duty cycle to the hardware
81 // - Stepper motor - 4PWM setting
82 // - hardware speciffic
83 void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, int pin1A, int pin1B, int pin2A, int pin2B){
84  // transform duty cycle from [0,1] to [0,255]
85  analogWrite(pin1A, 255.0*dc_1a);
86  analogWrite(pin1B, 255.0*dc_1b);
87  analogWrite(pin2A, 255.0*dc_2a);
88  analogWrite(pin2B, 255.0*dc_2b);
89 }
90 
91 
92 // function configuring pair of high-low side pwm channels, 32khz frequency and center aligned pwm
93 // supports Arudino/ATmega2560
94 int _configureComplementaryPair(int pinH, int pinL) {
95  if( (pinH == 4 && pinL == 13 ) || (pinH == 13 && pinL == 4 ) ){
96  // configure the pwm phase-corrected mode
97  TCCR0A = ((TCCR0A & 0b11111100) | 0x01);
98  // configure complementary pwm on low side
99  if(pinH == 13 ) TCCR0A = 0b10110000 | (TCCR0A & 0b00001111) ;
100  else TCCR0A = 0b11100000 | (TCCR0A & 0b00001111) ;
101  // set prescaler to 1 - 32kHz freq
102  TCCR0B = ((TCCR0B & 0b11110000) | 0x01);
103  }else if( (pinH == 11 && pinL == 12 ) || (pinH == 12 && pinL == 11 ) ){
104  // set prescaler to 1 - 32kHz freq
105  TCCR1B = ((TCCR1B & 0b11111000) | 0x01);
106  // configure complementary pwm on low side
107  if(pinH == 11 ) TCCR1A = 0b10110000 | (TCCR1A & 0b00001111) ;
108  else TCCR1A = 0b11100000 | (TCCR1A & 0b00001111) ;
109  }else if((pinH == 10 && pinL == 9 ) || (pinH == 9 && pinL == 10 ) ){
110  // set prescaler to 1 - 32kHz freq
111  TCCR2B = ((TCCR2B & 0b11111000) | 0x01);
112  // configure complementary pwm on low side
113  if(pinH == 10 ) TCCR2A = 0b10110000 | (TCCR2A & 0b00001111) ;
114  else TCCR2A = 0b11100000 | (TCCR2A & 0b00001111) ;
115  }else{
116  return -1;
117  }
118  return 0;
119 }
120 
121 // Configuring PWM frequency, resolution and alignment
122 // - BLDC driver - 6PWM setting
123 // - hardware specific
124 // supports Arudino/ATmega328
125 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) {
126  // High PWM frequency
127  // - always max 32kHz
128  int ret_flag = 0;
129  ret_flag += _configureComplementaryPair(pinA_h, pinA_l);
130  ret_flag += _configureComplementaryPair(pinB_h, pinB_l);
131  ret_flag += _configureComplementaryPair(pinC_h, pinC_l);
132  return ret_flag; // returns -1 if not well configured
133 }
134 
135 // function setting the
136 void _setPwmPair(int pinH, int pinL, float val, int dead_time)
137 {
138  int pwm_h = _constrain(val-dead_time/2,0,255);
139  int pwm_l = _constrain(val+dead_time/2,0,255);
140 
141  analogWrite(pinH, pwm_h);
142  if(pwm_l == 255 || pwm_l == 0)
143  digitalWrite(pinL, pwm_l ? LOW : HIGH);
144  else
145  analogWrite(pinL, pwm_l);
146 }
147 
148 // Function setting the duty cycle to the pwm pin (ex. analogWrite())
149 // - BLDC driver - 6PWM setting
150 // - hardware specific
151 // supports Arudino/ATmega328
152 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){
153  _setPwmPair(pinA_h, pinA_l, dc_a*255.0, dead_zone*255.0);
154  _setPwmPair(pinB_h, pinB_l, dc_b*255.0, dead_zone*255.0);
155  _setPwmPair(pinC_h, pinC_l, dc_c*255.0, dead_zone*255.0);
156 }
157 
158 #endif
_configure3PWM
void _configure3PWM(long pwm_frequency, const int pinA, const int pinB, const int pinC)
Definition: drivers/hardware_specific/generic_mcu.cpp:31
_configure4PWM
void _configure4PWM(long pwm_frequency, const int pin1A, const int pin1B, const int pin2A, const int pin2B)
Definition: drivers/hardware_specific/generic_mcu.cpp:40
_constrain
#define _constrain(amt, low, high)
Definition: foc_utils.h:9
_writeDutyCycle3PWM
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, int pinA, int pinB, int pinC)
Definition: drivers/hardware_specific/generic_mcu.cpp:64
_writeDutyCycle2PWM
void _writeDutyCycle2PWM(float dc_a, float dc_b, int pinA, int pinB)
Definition: drivers/hardware_specific/generic_mcu.cpp:55
_writeDutyCycle4PWM
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, int pin1A, int pin1B, int pin2A, int pin2B)
Definition: drivers/hardware_specific/generic_mcu.cpp:74
_writeDutyCycle6PWM
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)
Definition: drivers/hardware_specific/generic_mcu.cpp:86
_configure6PWM
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)
Definition: drivers/hardware_specific/generic_mcu.cpp:47
_configure2PWM
void _configure2PWM(long pwm_frequency, const int pinA, const int pinB)
Definition: drivers/hardware_specific/generic_mcu.cpp:23