0% found this document useful (0 votes)
22 views7 pages

Lem

This document describes code for sensorless brushless DC motor control using an Arduino UNO and L6234 driver. It defines pins and registers for motor commutation and PWM control. Functions are included for BEMF sensing, motor stepping, and setting the PWM duty cycle.

Uploaded by

Drifius lembi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Lem

This document describes code for sensorless brushless DC motor control using an Arduino UNO and L6234 driver. It defines pins and registers for motor commutation and PWM control. Functions are included for BEMF sensing, motor stepping, and setting the PWM duty cycle.

Uploaded by

Drifius lembi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1 /*eONE

2 * Sensorless brushless DC (BLDC)


motor control with Arduino UNO and
3 L6234 driver.
4 * This is a free software with NO
5 WARRANTY.

6 * http://simple-circuit.com/

7 */

10 #define SPEED_UP A0

11 #define SPEED_DOWN A1

12 #define PWM_MAX_DUTY 255

13 #define PWM_MIN_DUTY 50

14 #define PWM_START_DUTY 100

15

16 byte bldc_step = 0, motor_speed;

17 unsigned int i;

18 void setup() {

19 DDRD |= 0x38; //
Configure pins 3, 4 and 5 as
20 outputs
21 PORTD = 0x00;
22 DDRB |= 0x0E; //
Configure pins 9, 10 and 11 as
23
outputs
24
PORTB = 0x31;
25
26 // Timer1 module setting: set
clock source to clkI/O / 1 (no
27 prescaling)
28 TCCR1A = 0;
29 TCCR1B = 0x01;
30 // Timer2 module setting: set
31 clock source to clkI/O / 1 (no
prescaling)
32
TCCR2A = 0;
33
TCCR2B = 0x01;
34
// Analog comparator setting
35
ACSR = 0x10; //
36 Disable and clear (flag bit)
analog comparator interrupt
37
pinMode(SPEED_UP,
38
INPUT_PULLUP);
39
pinMode(SPEED_DOWN,
40 INPUT_PULLUP);

41 }

42 // Analog comparator ISR

43 ISR (ANALOG_COMP_vect) {

44 // BEMF debounce

45 for(i = 0; i < 10; i++) {


if(bldc_step & 1){ if(!(ACSR &
46 0x20)) i -= 1; } else { if((ACSR &
0x20)) i -= 1; } } bldc_move();
47
bldc_step++; bldc_step %= 6; }
48 void bldc_move(){ // BLDC motor
commutation function
49 switch(bldc_step){ case 0:
50 AH_BL(); BEMF_C_RISING(); break;
case 1: AH_CL(); BEMF_B_FALLING();
51 break; case 2: BH_CL();
BEMF_A_RISING(); break; case 3:
52
BH_AL(); BEMF_C_FALLING(); break;
case 4: CH_AL(); BEMF_B_RISING();
53 break; case 5: CH_BL();
BEMF_A_FALLING(); break; } } void
54 loop() {
55 SET_PWM_DUTY(PWM_START_DUTY); //
Setup starting PWM with duty cycle
56 = PWM_START_DUTY i = 5000; //
Motor start while(i > 100) {
57
delayMicroseconds(i);
58
bldc_move();
59
bldc_step++;
60
bldc_step %= 6;
61
i = i - 20;
62
}
63
motor_speed = PWM_START_DUTY;
64
ACSR |= 0x08;
65
// Enable analog comparator
66 interrupt

67 while(1) {

68 while(!(digitalRead(SPEED_UP))
&& motor_speed < PWM_MAX_DUTY){
69 motor_speed++;
SET_PWM_DUTY(motor_speed);
70
delay(100); }
71 while(!(digitalRead(SPEED_DOWN))
&& motor_speed > PWM_MIN_DUTY){
72
motor_speed--;
73
SET_PWM_DUTY(motor_speed);
74
delay(100);
75
}
76
}
77
}
78

79
void BEMF_A_RISING(){
80 ADCSRB = (0 << ACME); //
Select AIN1 as comparator negative
81 input
82 ACSR |= 0x03; // Set
83 interrupt on rising edge

84 }

85 void BEMF_A_FALLING(){

86 ADCSRB = (0 << ACME); //


Select AIN1 as comparator negative
87 input
88 ACSR &= ~0x01; // Set
interrupt on falling edge
89
}
90
void BEMF_B_RISING(){
91
ADCSRA = (0 << ADEN); //
92
Disable the ADC module
93
ADCSRB = (1 << ACME);
94
ADMUX = 2; //
95 Select analog channel 2 as
comparator negative input
96
ACSR |= 0x03;
97
}
98
void BEMF_B_FALLING(){
99
ADCSRA = (0 << ADEN); //
100 Disable the ADC module
101 ADCSRB = (1 << ACME);
102 ADMUX = 2; //
Select analog channel 2 as
103
comparator negative input
104
ACSR &= ~0x01;
105
}
106
void BEMF_C_RISING(){
107 ADCSRA = (0 << ADEN); //
Disable the ADC module
108
ADCSRB = (1 << ACME);
109
ADMUX = 3; //
110 Select analog channel 3 as
111 comparator negative input

112 ACSR |= 0x03;

113 }

114 void BEMF_C_FALLING(){

115 ADCSRA = (0 << ADEN); //


Disable the ADC module
116
ADCSRB = (1 << ACME);
117
ADMUX = 3; //
118 Select analog channel 3 as
comparator negative input
119
ACSR &= ~0x01;
120
}
121

122
void AH_BL(){
123
PORTB = 0x04;
124
PORTD &= ~0x18;
125
PORTD |= 0x20;
126
TCCR1A = 0; // Turn
127
pin 11 (OC2A) PWM ON (pin 9 & pin
128 10 OFF)

129 TCCR2A = 0x81; //

130 }

131 void AH_CL(){

132 PORTB = 0x02;

133 PORTD &= ~0x18;


134 PORTD |= 0x20;

135 TCCR1A = 0; // Turn


pin 11 (OC2A) PWM ON (pin 9 & pin
10 OFF)

TCCR2A = 0x81; //

void BH_CL(){

PORTB = 0x02;

PORTD &= ~0x28;

PORTD |= 0x10;

TCCR2A = 0; // Turn
pin 10 (OC1B) PWM ON (pin 9 & pin
11 OFF)

TCCR1A = 0x21; //

void BH_AL(){

PORTB = 0x08;

PORTD &= ~0x28;

PORTD |= 0x10;

TCCR2A = 0; // Turn
pin 10 (OC1B) PWM ON (pin 9 & pin
11 OFF)

TCCR1A = 0x21; //

void CH_AL(){

PORTB = 0x08;

PORTD &= ~0x30;

PORTD |= 0x08;
TCCR2A = 0; // Turn
pin 9 (OC1A) PWM ON (pin 10 & pin
11 OFF)

TCCR1A = 0x81; //

void CH_BL(){

PORTB = 0x04;

PORTD &= ~0x30;

PORTD |= 0x08;

TCCR2A = 0; // Turn
pin 9 (OC1A) PWM ON (pin 10 & pin
11 OFF)

TCCR1A = 0x81; //

void SET_PWM_DUTY(byte duty){

if(duty < PWM_MIN_DUTY) duty =


PWM_MIN_DUTY; if(duty >
PWM_MAX_DUTY)

duty = PWM_MAX_DUTY;

OCR1A = duty; //
Set pin 9 PWM duty cycle

OCR1B = duty; //
Set pin 10 PWM duty cycle

OCR2A = duty; //
Set pin 11 PWM duty cycle

You might also like