Skip to content

Commit b7266ff

Browse files
authored
Create atmega32u4_mcu.cpp
add support for atmega32u4
1 parent 3d9fee8 commit b7266ff

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
2+
#include "../hardware_api.h"
3+
4+
#if defined(__AVR_ATmega32U4__)
5+
6+
// set pwm frequency to 32KHz
7+
void _pinHighFrequency(const int pin){
8+
// High PWM frequency
9+
// reference: http://r6500.blogspot.com/2014/12/fast-pwm-on-arduino-leonardo.html
10+
if ( pin == 6 || pin == 13 ) {
11+
TCCR4A=0x82;
12+
TCCR4B=2; // TCCR4B=2 32khz; TCCR4B=1 64khz; TTCR4B=3 16khz
13+
TCCR4C|=0x09;
14+
TCCR4D=0;
15+
}
16+
if ( pin == 5 ){
17+
TCCR3B=1;
18+
}
19+
20+
if (pin == 9 || pin == 10 ){
21+
TCCR1B = ((TCCR1B & 0b11111000) | 0x01); // set prescaler to 1
22+
}
23+
24+
if (pin == 3 || pin == 11 ) {
25+
TCCR0B = ((TCCR0B & 0b11111000) | 0x01); // 62kHZ
26+
//TCCR0B = ((TCCR0B & 0b11111000) | 0x02); // 7.8 khz
27+
}
28+
}
29+
30+
31+
// function setting the high pwm frequency to the supplied pins
32+
// - Stepper motor - 2PWM setting
33+
// - hardware speciffic
34+
// supports Arudino/ATmega328
35+
void _configure2PWM(long pwm_frequency,const int pinA, const int pinB) {
36+
// High PWM frequency
37+
// - always max 32kHz
38+
_pinHighFrequency(pinA);
39+
_pinHighFrequency(pinB);
40+
}
41+
42+
// function setting the high pwm frequency to the supplied pins
43+
// - BLDC motor - 3PWM setting
44+
// - hardware speciffic
45+
// supports Arudino/ATmega328
46+
void _configure3PWM(long pwm_frequency,const int pinA, const int pinB, const int pinC) {
47+
// High PWM frequency
48+
// - always max 32kHz
49+
_pinHighFrequency(pinA);
50+
_pinHighFrequency(pinB);
51+
_pinHighFrequency(pinC);
52+
}
53+
54+
// function setting the pwm duty cycle to the hardware
55+
// - Stepper motor - 2PWM setting
56+
// - hardware speciffic
57+
void _writeDutyCycle2PWM(float dc_a, float dc_b, int pinA, int pinB){
58+
// transform duty cycle from [0,1] to [0,255]
59+
analogWrite(pinA, 255.0*dc_a);
60+
analogWrite(pinB, 255.0*dc_b);
61+
}
62+
63+
// function setting the pwm duty cycle to the hardware
64+
// - BLDC motor - 3PWM setting
65+
// - hardware speciffic
66+
void _writeDutyCycle3PWM(float dc_a, float dc_b, float dc_c, int pinA, int pinB, int pinC){
67+
// transform duty cycle from [0,1] to [0,255]
68+
analogWrite(pinA, 255.0*dc_a);
69+
analogWrite(pinB, 255.0*dc_b);
70+
analogWrite(pinC, 255.0*dc_c);
71+
}
72+
73+
// function setting the high pwm frequency to the supplied pins
74+
// - Stepper motor - 4PWM setting
75+
// - hardware speciffic
76+
// supports Arudino/ATmega328
77+
void _configure4PWM(long pwm_frequency,const int pin1A, const int pin1B, const int pin2A, const int pin2B) {
78+
// High PWM frequency
79+
// - always max 32kHz
80+
_pinHighFrequency(pin1A);
81+
_pinHighFrequency(pin1B);
82+
_pinHighFrequency(pin2A);
83+
_pinHighFrequency(pin2B);
84+
}
85+
86+
// function setting the pwm duty cycle to the hardware
87+
// - Stepper motor - 4PWM setting
88+
// - hardware speciffic
89+
void _writeDutyCycle4PWM(float dc_1a, float dc_1b, float dc_2a, float dc_2b, int pin1A, int pin1B, int pin2A, int pin2B){
90+
// transform duty cycle from [0,1] to [0,255]
91+
analogWrite(pin1A, 255.0*dc_1a);
92+
analogWrite(pin1B, 255.0*dc_1b);
93+
analogWrite(pin2A, 255.0*dc_2a);
94+
analogWrite(pin2B, 255.0*dc_2b);
95+
}
96+
97+
98+
99+
// Configuring PWM frequency, resolution and alignment
100+
// - BLDC driver - 6PWM setting
101+
// - hardware specific
102+
// supports Arudino/ATmega328
103+
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) {
104+
return -1;
105+
}
106+
107+
// function setting the
108+
void _setPwmPair(int pinH, int pinL, float val, int dead_time)
109+
{
110+
int pwm_h = _constrain(val-dead_time/2,0,255);
111+
int pwm_l = _constrain(val+dead_time/2,0,255);
112+
113+
analogWrite(pinH, pwm_h);
114+
if(pwm_l == 255 || pwm_l == 0)
115+
digitalWrite(pinL, pwm_l ? LOW : HIGH);
116+
else
117+
analogWrite(pinL, pwm_l);
118+
}
119+
120+
// Function setting the duty cycle to the pwm pin (ex. analogWrite())
121+
// - BLDC driver - 6PWM setting
122+
// - hardware specific
123+
// supports Arudino/ATmega328
124+
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){
125+
_setPwmPair(pinA_h, pinA_l, dc_a*255.0, dead_zone*255.0);
126+
_setPwmPair(pinB_h, pinB_l, dc_b*255.0, dead_zone*255.0);
127+
_setPwmPair(pinC_h, pinC_l, dc_c*255.0, dead_zone*255.0);
128+
}
129+
130+
#endif

0 commit comments

Comments
 (0)