Вы находитесь на странице: 1из 36

REAL LIFE CASE STUDIES FOR BRUSHLESS DC

MOTOR CONTROL IN E-BICYCLE


I. Brushless DC Motor Control
Figure 1 shows a permanent magnet DC brushless machine (BLDC) fitted into rear
wheel of an electronic bicycle system. BLDC has many applications. A BLDC is rare-
earth based permanent magnet based motor. BLDC is also called synchronous DC motor.
A motor has the stator and rotor. BLDC motor windings are at the stator (static) portion
of the motor. The rotor portion contains the permanent magnets.
The BLDC advantages are variable speed, silent operation, high efficiency and due
to no brushes, a long life. BLDC gives higher power to weight ratio and higher torque to
inertia ratio. A BLDC has (i) trapezoidal type wave shape of their induced emf, (ii)
square flux density, (iii) square wave stator current and (iv) switching stator field. Figure
2 shows three windings of the BLDC. It shows three coils, A, B and C and their ends,
PA, PB and PC are connected to power transistor amplifiers made from MOSFETs or



Figure 1
A brushless DC motor fitted in an electric bicycle
2 Embedded Systems
similar drivers.
The switching and commutating currents flow in windings. Table 1 gives these
currents for six distinct BLDC motor phases, 1 to 6. These phases can be assumed to six
distinct angles of motor shaft, 0
o
, 60
o
, 120
o
, 180
o
, 240
o
, 300
o
and 360
o
. The windings are
directly attached to the amplifier power electronics. It requires an electronic controller
and Hall sensors for the position. Assume that a Hall sensor is placed such that it gives
high signal (1), when rotor permanent magnet North Pole passes nearby to the stator
winding and low signal (0) when South.
Transistors or power MOSFETs are used to switch a current I to one pair of motor
poles for change from one phase to another. The switched transistor pair in a phase
depends on the feedbacks from three Hall sensors. The feedback enables closed loop
control of the motor.
Table 1
Requirements of switching currents in the coils A, B and C












Figure 2
Current directions in three windings A, B and C of BLDC at six instances, 1, 2, 3, 4, 5 and 6
C
B
A
1
2
3
6
5
5
PB
PA
PC
1
4
3
2
4
6
Real Life Case Studies 3
Switching
current
Commutatin
g
current
EMF in
the coils
zero crossing Angle

Path in
Figure 2
Phase
I
B
I
A
E
AB
+ve to -ve 0
o
1
+ I
A
+I
C
E
CA
-ve to + ve 60
o
1 2
I
C
I
B
E
BC
+ve to -ve 120
o
3
+ I
B
+ I
A
E
AB
-ve to + ve 180
o
3 4
I
A
I
C
E
CA
-ve to + ve 240
o
5
+ I
C
+ I
B
E
BC
+ve to -ve 300
o
5 6
I
B
I
A
E
AB
-ve to + ve 360
o
1

Figure 3 shows the required switching currents in the coils at different time periods.
Figure 4 shows the connection of six power transistors (Q0 to Q5) to the coil ends, PA,
PB and PC. +DC is applied to each power transistor Q1, Q3 and Q5 collector is applied
at n-end. DC is applied to each power transistor Q0, Q2 and Q4 emitter is applied at p-
end.


















Figure 3
The coil currents at different time intervals in the coils A, B and C shown in Figure 2
B
A
C
t1 t2 t3 t4 t5 t6 t1 t2 t3 t4 t5 t6 t1 t2 t3
1 3 5 1 3 5
60
o

120
o

180
o

240
o

300
o

360
o

4 Embedded Systems
Each transistor Q0 to Q5 collector and emitter junctions connect to n- and p-ends of
diodes D0 to D5. This prevents induced EMFs from damaging the Qs. Q0 to Q5 six
transistor bases connect to PWM0, PWM1, PWM2, PWM3, PWM4 and PWM5 outputs
at the outputs of 6 IGBT driver transistors. [Refer Section 1.3.7 for understanding PWM
concept.]
Each IGBT input is switched ON-OFF by the port outputs through the
optocouplers. Port output can be used to control the switch on and transfer of the PWM
outputs to the emitters of the Qs and Port P0 inputs for the Hall sensor inputs. System
program the will control the switching PWM currents.
[PWM means pulse width modulated output. Assume that there is a pulse of 0.4 ms
period (repetition intervals) (base frequency = 2.5 kHz). If duration is 0.04 ms for logic 1
and 0.36 ms for 0, its pulse width or duty cycle is said to be 10%. Now if these PWM
pulses are used for input to a dc motor, the average current, I over 1 second will be 10 %
of the peak current and thus speed will be around 10% of the maximum settable speed.
The I is proportional to the pulse width duration with respect to pulse repetition
intervals.]









Figure 4
Connections of six power transistors (Q0 to Q5) to three coil ends, PA, PB and PC in Figure 2
To PA
To PC
To PB
DC+
DC
PWM0
PWM1
PWM2
PWM3
PWM4
PWM5
Q0
Q1
Q3 Q5
Q4
Q2
Real Life Case Studies 5
When there is forward rotation the phase currents are generated as per Figure 2.
Table 2 gives the sequences to be followed as per the Hall sensor inputs from coil A, B
and C, respectively.

Table 2
Control of PWM outputs using P1 ports switched ON-OFF for forward rotation
Hall
Sensor Input
PWM output switched to
emitter of
Phase Sequence
C B A First Q Second Q
Phase
Curren
t in A
Phase
Current
in B
Phase
Curren
t in C
1F 1 (0
o
) 1 0 0 Q5 (PWM5) Q2 (PWM2) off +
2F 2 (60
o
) 1 1 0 Q5 (PWM5) Q0 (PWM0) off +
3F 3 (120
o
) 1 1 1 Q3 (PWM5) Q0 (PWM0) + off
4F 4 (180
o
) 0 1 1 Q3 (PWM5) Q4 (PWM4) off +
5F 5(240
o
) 0 0 1 Q1 (PWM1) Q4 (PWM4) + off
6F 6(300
o
) 0 0 0 Q1 (PWM1) Q2 (PWM2) + off

When there is reverse rotation the phase currents are generated in opposite
sequences to that shown in Figure 2. Port P1 outputs in an 8051 based system can control
the switching and transfer the currents to the emitters as per the PWM outputs. Table 3
gives the phases, sequences and transistor pairs, which are switched ON during reverse
rotation. During reverse rotation the switching sequences to be followed according to the
Hall sensor inputs obtained fromz coil A, B and C, respectively.





6 Embedded Systems
Table 3
Control of PWM outputs using P1 ports switched ON-OFF for reverse rotation
Hall
Sensor Input
PWM output given to
emitter
Phase Sequence
C B A of First Q Second Q
Phase
Current
in A
Phase
Current
in B
Phase
Current
in C
1R 1 (0
o
) 0 1 1 Q5 (PWM5) Q2 (PWM2) off +
2R 6(300
o
) 1 1 1 Q1 (PWM1) Q2 (PWM2) + off
3R 5(240
o
) 1 1 0 Q1 (PWM1) Q4 (PWM4) + off
4R 4 (180
o
) 1 0 0 Q3 (PWM5) Q4 (PWM4) off +
5R 3 (120
o
) 0 0 0 Q3 (PWM5) Q0 (PWM0) + off
6R 2 (60
o
) 0 0 1 Q5 (PWM5) Q0 (PWM0) off +
II. Case Study Electronic Controller for BLDC Motor using an 8051
based system
Objective of the case study is design of an electronic controller for a BLDC Motor
in an electric bicycle using an 8051 based system.
A program embeds into the system. The system is programmed for the close loop
control of BLDC is done as follows. The current is switched to next pair of coils
(transistors sending current to the coils) as per the three sensor input change according to
Table 2 for forward rotation and Table 3 according to backward rotation.
The feedback from a sensor is also used to compute the present speed to control the
new value of winding current sent to a pair of coils. When the motor rotates, a Hall
sensor feedback shows the change in the current direction. The number of transitions
from 0 to 1 and 1 to 0 enables the feedback of present speed. If present speed is less, the
winding current is increased and if more than the winding current is decreased. During
the running of the motor, this loop auto synchronizes the winding currents with the motor
rotational speed.

Real Life Case Studies 7
III. Requirements, Design Metric and System Functioning
Assume that there is a BLDC motor for used in electric bicycle. Table 2 and 3 gave
the required outputs for the given inputs for forward and reverse movement, respectively.
Table 4 gives the requirements, design metrics and system functioning of the electronic
controller for BLDC system.
Table 4
Requirements, design metrics and system functioning of the electronic controller for BLDC
system
Requirement Description
Purpose Electronically control a three-phase BLDC motor based system using six PWM outputs,
motor protection t for excessive current and speed control as per user preset speed
Inputs (i) Three inputs at three port bits Key0, Key1 and Key2 from a speed up and down push
buttons interface at port bit 0, bit 1 and bit 2
(ii) Key3 input to reverse or forward move the motor when 0 or 1, respectively, at port bit
3
(iii) Key4 input to Off or on the motor when 0 or 1, respectively, at port bit 3
(iv) Three Hall sensors inputs S0, S1, S2 at 1s or 0s as per Table 2 or 3 at port bit 5, bit 6
and bit 7. [The Hall sensor is positioned in the motor assembly such that it gives high
(logic 1) when North Pole passes nearby during rotation and 0 when south.]
(v) ADC0809 interface 8-bit inputs after conversion [Sections 1.3.7 and 2.2.6]
(vi) End of conversion, EOC signal at INT0 from ADC0809 interface
(vii) Interrupt at INT1from a motor Hall sensor in BLDC. Number of INT1 interrupts in a
timeout period of a timer enables calculation of rotation period
Events (i) Switch Key4 to run the BLDC motor or switch off the BLDC motor
(ii) Forward/reverse switch Key3 changing for clockwise and anticlockwise motion of the
BLDC motor.
(iii) Pressing speed up or down push buttons.
System
outputs
(i) If run switch Key4 is ON, then generate PWM outputs for a pair of transistors as per
the speed control inputs, Key0, Key1 and Key2. If switch Key4 for run is ON, switching
ON-OFF outputs to Q0, , to Q5 six power transistors as per Table 2 if forward motion
set by Key3 input = 1 and Table 3 if reverse motion set by Key3 input = 1.
(ii) If switch Key4 for run is OFF, then all PWM outputs switched OFF (= 0s) 0% duty
cycle.
(iii) ADC0809 interface bits channel select, A2, A1, A0, and chip select at four port bit
outputs.
(iv) One port bit output for start of conversion, SOC by ADC
(iv) One port bit output for LED to show the motor status with flashing frequency
proportional to motor present_speed
Functions of
the system
Given separately after the Table 4
8 Embedded Systems
Design
metrics
(i) Use 8051 microcontroller based system
(ii) The motor has three phases with three Hall sensors for enabling the close loop
electronic control. Use six PWM outputs for currents in the six Qs for motor control.
(iii) Preset the BLDC set_speed two push-button switches and push buttons interface
circuit.
(iv) Closed loop control for auto-synchronization mode starts after 1.8 s (150 time 12 ms)
(v) Use Hall sensor input change as interrupt to find motor speed.
(vi) Both forward and reverse control and ON-OFF control.
(vii) Use BLDC 24V motor 5.8 A Maximum permissible current.
(viii) Use an ADC for reading current every 12 ms. The protection from high currents in
the winding using an ADC 0809 control check is after every 24 ms and if average I
exceeds the I_max, then all, IGBTs and Q transistors are gradually reduced currents every
24 ms.
(ix) Flashing LED output with flashing frequency proportional to present_speed
(x) One hour run at full speed without recharging the battery.
Test and
validation
conditions
Each program module tested individually as well as after system integration for different
speeds and for forward and reverse motion for one hour continuous run without need of
recharging.

IV. Functioning of the system
1. The six outputs are for PWM outputs as per Table 2 or 3.
2. Hall sensor inputs are the input port bits. Table 2 shows that a Hall sensor
input changes from 1 to 0 after every 180
o
rotation. 1 to 0 transitions occurs
when North Pole of rotor permanent magnet starts facing the South Pole of the
stator winding from North earlier. Therefore, one of the Hall sensors S0 is used
as INT1 interrupt.
3. Protection from the high current in winding is done by using an ADC 0809 to
measure the winding current and if average I exceeds the I_max, then all six
witching transistors are OFF by slowly reducing current (the duty cycle of
PWM pulses). A shunt resistance of 0.1 or 0.22 is put in the path of the
supply to the windings. The analog voltage developed across it is converted by
ADC after a start of conversion at a port pin output to it and ADC outputs bits
are available as port input along with an end of conversion EOC interrupt at
INT0 after a conversion interval. [Sections 1.3.7 and 2.2.6]
4. The ADC0809 post conversion inputs are at an 8-bit input Port. The ADC0809
end of conversion signal is at INT0 pin port input P3 pin 2. When the EOC
interrupts the system through INT0 at P3^2 the ADC data bits are read at the
port inputs.
5. The ADC0809 start of conversion SOC, chip select CS and channel select A2,
A1 and A0 signals are at port output bits, P1^6, P3^4, P3^5, P3^6 and P3^7,
Real Life Case Studies 9
respectively. A function for control of present speed function presumes that
there is maximum current in windings when I_max = 0x80 and The presentI
variable is used such that ADC output is between ADC output is between
0x00 and 0x7F.
6. A motor status indicating LED interfaces at port output bit, P1^7
7. The Keys, Key0 to Key4 are set to run the motor forward or backwards at one
of the 8 speeds. Both forward and reverse control and ON OFF control are
done using two switches Key3 and Key4, respectively at the centre of the cycle
handle and two push button switches PButton1 and PButton2 attached to
bicycle handle. One can have PButton1L and PButton2L and their replicas
PButton1R and PButton2R attached to bicycle handle on the left and right
sides both. This is to facilitate use of buttons by left and right hander persons,
easily.
8. The system can run at different speeds preset_speed. The speed is set by either
of two pairs of pushbuttons, PButton1 and PButton2 one for decrementing
the speed and other for increasing the speed provide for use by the cyclist. The
inputs from the PButton1 and PButton2 connect an interfacing circuit,
PBuInterface. PBuInterface consists of the up/down counter and encoder. The
encoder generates the Key0, Key1 and Key2 inputs, which are as per the initial
preset_speed.
9. The forward or reverse motion is controlled by a switch for Key3 input and
ON-PFF by a switch for Key4 input, respectively.
10. The 8051 given system, if does not have six PWM generators, then the outputs
are generated using a timer. The 8051 given system has two timers, T1 and T0.
11. T1 is used in mode 2 and programmed for 9600 baud rate for the serial
interface. The interface to the computer for system is required for function
testing, simulation and for downloading programs hex files. [Section 13.4]
12. The serial interface of 8051 connects the computer COM port through an
UART. The interface is meant for system functioning tests and for
downloading programs hex files. A hyper terminal window shows the outputs
of 8051 on the computer screen. The serial interface of 8051 initiates every 12
s the printing of ADC results.
13. T0 in mode 2 programmed for 0.025 ms interrupts is used as real time clock
ticking after every 0.025 ms.
14. Six software timers SWT1 to SWT6 are used, which executes the service codes
or routine. The service codes execution initiates after the preset numbers of
inputs are given to each of them from the real time clock interrupts. Five get
inputs every 0.025 ms and one SWT4 every 12 ms.
15. Two software timers are used as the given system does not have six PWM
generators. Each PWM output level 1 and level0 is generated using two
10 Embedded Systems
software timer SWT1 and SWT0, respectively. The SWT1 and SWT0 get
inputs on interrupts from T0 running in mode 2 auto re-load mode. The SWT1
and SWT0 time intervals for timeouts are as per the PWM output logic 1 and
logic 0 intervals. The pulse period = total of intervals for 1and 0 and is as per
fixed frequency of PWM pulses to the BLDC interface. The interval for 1 is
proportional to the set_speed. The interval for 0 = total PWM pulse period
minus the interval for 1.
16. Table 2 is used for the forward motion by switching ON and OFF the destined
transistors on the SWT1 and SWT0 timeouts. Table 3 is used for the reverse
motion by switching ON and OFF the destined transistors on the SWT1 and
SWT0 timeouts.
17. A software timer SWT2 is programmed for 12 ms timeout and is used as
auxiliary long period real time clock ticking after every 12 ms.
18. SWT2 clock ticking initiates number of actions after every 12 ms. A service
routine does this. It gives count input to SWT4. It also initiates an SOC pulse.
Motor windings current is checked by sending SOC (start of conversion
negative going pulse) to ADC0809 interface. The EOC interrupt at INT0 pin is
used to measure the windings current and if it exceeds at preset I_max, the
PWM outputs are switched off to all transistors.
19. The service routine on SWT2 timeouts in even cycle (at each 24 ms) initiates
calculation of present rotations in 100 s and the present_speed on 8 point scale
in every even cycle of SWT2 clock tick, which means after every 24 ms.
20. A software timer SWT3 is programmed for timeouts after a set interval, which
initiates the flashing LEDs. The flashing frequency of LED for motor status is
controlled by SWT3 timeout intervals. The flashing interval is as per the
present_speed.
21. Software timer SWT3 timeout interval is set as per the present_speed. The
present_speed depends on the number of times the Hall sensor inputs change
during motor rotation.
22. A software timer SWT4 timeouts after a preset interval of 12 s to initiate the
serial interface and printing of ADC channel 0 results on a computer hyper
terminal. SWT4 is a software timer chained to the SWT2 timeout service
routine for the count inputs.
23. SWT5 is used in delay function. A delay function is used to provide wait for
specified period in the program. A software timer SWT5 timeouts after a
preset interval = delayperiod, which is given as input in units of ms. SWT5 is a
software timer depends on the T0 interrupts for the count inputs.
V. Hardware Architecture
Real Life Case Studies 11
Figure 5 shows the system architecture. Figure 6 shows the 8051 based hardware.
A PC is used as development host. (Chapter 13 Section 13.2) The PC connects to
the system using RS232C COM1 port, operated at 9600 baud. The system has
P89C51RD2 microcontroller of 8051 family. Prefix 89 means, flash memory is used for
the internal ROM. Total flash memory (internal plus external) for the program memory in
the system is 64KB. The system includes the followings: (i) A reset switch to restart the
system after erasing the previous program and downloading a program into flash from PC
using a PC at the host using a software module at the host. An LED shows the system
ON. (ii) Port bits connects to the interfaces through jumper sockets (iii) interfacing circuit
for connection SI port pins (pins 0 and 3 of port P3, P3^0 and P3^1) to the computer
COM port RxD and TxD RS232C voltage level pins) through one pair of jumper sockets
using a ribbon cable and (iv) three interfacing circuits, one for the phase of BLDC motor,
second for ADC I_max protection and third for push buttons connect to Port other pins
through three pair of jumper sockets using three ribbon cables. There are other LEDs also













Figure 5
RS232C
System
Flash
Memory
RAM
UART
Monitor
Memory
Philips
P89C51RD2
micro-
controller
RS232C
COM2

System Keypad
System Display
PC

Port P1
Port P0
For software test and debug
Development host with
Integrated
Development Environment Keil
Port P3
Port P2
SI Port
pins
Interface
circuit to
RS232C
COM 1 por
RS232C
COM1

8 Opto-couplers OC0 to OC7
BLDC Interface using 6 IGBTs
and six power transistors
3 Hall sensor
outputs to
P0^5, P0^6
and P0^7 port
P0 inputs
through
jumper
sockets

P1 eight
outputs
from
P1pins
using Jumper
sockets at P1
and interface
12 Embedded Systems
for use by user program.
1. System has 5 inputs Key0 to Key4 with 1-0 states reflecting the preset speed,
forward/reverse movement and on-off. These inputs are at P0^0, P0^1, P0^2,
P0^3 and P0^4 when BLDC interfacing circuit is connected by jumper sockets
to Port P0.
2. System has 3 inputs from Hall sensors at the port P0 inputs, at P0^5, P0^6
and P0^7 when BLDC interfacing circuit is connected by jumper sockets to
Port P0.
3. Pins 0 and 1 of port P3, P3^0 and P3^1 connect to the computer COM port
RxD and TxD RS232C voltage level pins using another set of jumper sockets
for port P3.
4. Pins 5, 6 and 7 of port P3, P3^5, P3^6 and P3^7 connect the A2, A1 and A0.
Pin4 of port P3, P3^4, channel select pins of ADC0809 interface.
5. Pin 6 P1^6 connects the start of conversion to the ADC0809 interface.





Figure 6
8051 Microcontroller and hardware
Microcontroller
INT0
Serial
Interface
ADC
BLDC
SI RAM Flash
Memory/ROM
CPU
From Computer
Computer
Hall Sensor
S0
Program
download
Motor Status
LED
OCs, IGBTs
and Qs
INT1
Hall Sensor
S1
Hall Sensor
S2
Display
Real Life Case Studies 13
6. Pin 7 P1^7 connects the LED interface for motor status indications by flashing
glows at rate depending on present speed.
7. Refer Tables 2 and 3. A pair of transistors conducts and remaining four are
OFF as per the Hall sensor inputs at a given instance. At an instance only two
transistors conduct and remaining four are OFF. Port P1 six outputs P1^0,
P1^1, P1^2, P1^3, P0^4 and P1^5 connect to the six power transistors in
BLDC interface through jumper sockets between P0 and BLDC interface
BLDC interfacing circuit consists of 6 optocouplers and 6 IGBTs to give input
to six power transistors. The programmed operations are as per table 2 and 3.
For example, if P1^5 and P1^2 are on and on and give PWM output phase 1F
and the transistors Q5 and Q2 switches ON in circuit of Figure 4.
8. System has 8 bit ADC channel 0 inputs from ADC0809 interface. The
interface is used in Example 1 to protect the motor excessive current in the
winding over I_max.. ADC output is between 0x00 and 0x7F when at
successive periods of 24 ms present I in the windings is less than till presentI
< I_Max. When presentI is >= I_max, then the bldc current is switched off by
successively decreasing the currents in the windings every 24 ms.
9. Assume that given BLDC has I_Max of 5.8 A and shunt resistance used in
series with the winding is 0.22 . For 5 A, the V = 1.10 V. If voltage reference
+ with ADC is 2.55V and is GND, then 8-bit ADC resolution per bit is 10
mV. At 5 A, the ADC bits after conversion will be 0x6E. If 5.8 A is present,
then V = 1.28 V. The I is I_max and ADC output is 0x80. If 6 A is present,
then V = 1.32 V. The I is be more than 0x80. The output is 0x84. [Sections
1.3.7 and 2.2.6]

14 Embedded Systems
Figure 7 shows ports P0, P1, P2 and P3 in 8051 and IO port interfacing circuits for
the BLDC, Hall sensor, keys and ADC0809 interfaces shown by blue, green, orange
and purple text boxes, respectively. There is no internal ADC present in given 8051
system, therefore, the external ADC0809 interface is therefore used in the system. INT0
service routine is used to read ADC channel 0 and check, switch OFF all BLDC coil
inputs if present I > set I max in the coil windings and switch ON BLDC coil inputs as
per Table 2 or 3 if present I < set I max in the coil windings. Only two timers are
available in given 8051 system. Hence there is need of six software timers.(Section 3.6)






















Figure 7
IO ports P0, P1, P2 and P3 in 8051 and IO port interfacing
circuits for the BLDC, Hall sensor, keys and ADC0809 interfaces shown by blue, green, orange and
purple text boxes, respectively
P1.0
OC0
P1.1
OC1
OC2
P1.6
OC3
P1.7
P0.5
Hall
Sensors
S0,
S1,
S2

Q0, , Q5
Six IGBTs
IGBT0 to
IGBT5
Port P2
P1.2
P1.3
P1.4
P1.5
V
SS
V
cc

OC4
OC5
SOC
EOC
P0.6
P3.0
P3.1
Key0, , Key4
P0.0 to P0.4
ADC0809 Output
bits after conversion
P0.7
UART
PC COM1
P3.4 to
P3.7
CS and ADC
Ch_select, and
total 4 bits
Key4
Key3
Key0, Key1 and Key2
PBuInterface
Interface for
set_speed
P3.2
P3.3
INT0
INT1
LED
Real Life Case Studies 15
V.1 PWM
There is none PWM output present in given 8051 system. Two software timers
SWT1, and SWT0 (Section VI) are used, which get count inputs on T0 interrupt every
0.025ms. PWM pulse level is set = 1 using SWT1 timeout service codes.. PWM pulse
level is reset to 0 using SWT1 timeout service codes
The PWM frequency is set in Example 1 equal to 2.5 kHz. The number swt1
timeouts + swt1 timeouts = swt10period = 16. There are 40 T0 interrupts per ms. Thus
every 0.4 ms, there is PWM pulse.
VI Software Timers
SWT1, SWT0, SWT2, SWT3 and SWT4 are used for the given port interfaces.
Sixth SWT5 is used by a delay function in the program.
Section VII will give a state diagram of the software timer functions used for
BLDC running. T0 in auto-reload continuously ticking every 0.025 is used as real time
clock of the BLDC running system. The SWT count inputs are from timeout T0 interrupt
periods, which is auto loaded for interrupts after each 0.025 ms interval. The software
timer functions get inputs using real time clock ticks (interrupts). The SWT timeout
intervals are in units of number of SWT count inputs.
Let set speed be on 8 point scale. The KEY0, KEY1 and KEY2 are therefore as per
preset_speed in the Example 1. SWT1 is set for time out after number of T0 interrupts =
2* (KEY0 + KEY1 *2 + KEY3 * 4) initially. Later if present speed is found to be not
equal to preset speed or I_max is reached in the windings then SWT1 is set as per new
set_speed.
From the set_speed, SWT1set and SWT2set are computed for PWM pulse at 1 and
0, respectively. Due to increase or decrease of SWT1set, there is increase or decrease in
16 Embedded Systems
the PWM duty cycle, therefore, the winding current. The new set speed is altered in two
functions, one for control of speed and other for protection of winding currents.
Assume PWM base frequency = 2.5 kHz. After every 0.4 ms, new PWM cycle
starts. Therefore, the SWT0 is set for time out after number of T0 interrupts =
swt10period swt1set, because there are total swt10period interrupts in each PWM cycle
of 1 and 0.
SWT2 is set for the execution of service codes after each timeout. The timeout is
programmed for interval = time taken in number of T0 interrupts = 480 when number of
interrupts = 40 per ms. SWT2 routine gives input to SWT4. Also the ADC0809 interrupts
are after 12 ms timeout of SWT2. ADC interrupt is after the start of conversion pulse
generated by SWT2 timeout service routine. An EOC signal at end of conversion results
in INT0 interrupt.
Assume that shunt resistance in the winding path is such that for I_max the ADC
channel 0 output is 0x80 in the Example 1. The protect function bldcProtect ( ) protects
by reducing the set_speed every 12 ms and thus reduces the PWM duty cycle (ratio of
periods of SWT1 and SWT0 timeouts). The PWM duty cycle decrement also decrements
the average winding current.
SWT2 time overflow service function at even cycle that is after each 24 ms
interval is used to reset number of Hall sensor interrupts = 0 in even cycle and is used to
find the cycle present speed. The present speed on 8 point scale is calculated as follows.
Let cycle maximum speed Sp_max be set at 24 km/hr. It means Sp_max = 24000/3600 m
per s = 6.666 m per s. If rolling circumference of the wheel = 2.0 m, then rotate frequency
at Sp_max = 6.666/2 per s = 3.333 per s.

Real Life Case Studies 17
Assume that present speed is set as 8, for rotate frequency 333.3 per 100 s. The 100
interval is chosen as the integral number of variables are used in Example 1 and no
floating point variable is used.
1. Present speed is set as 7, for rotate frequency per 100 s = 380.9.
2. Present speed is set as 6, for rotate frequency per 100 s = 444.4.
3. Present speed is set as 5, for rotate frequency per 100 s = 533.2.
4. Present speed is set as 4, for rotate frequency per 100 s = 666.6.
5. Present speed is set as 3, for rotate frequency per 100 s = 888.8.
6. Present speed is set as 2, for rotate frequency per 100 s = 1333.2
7. Present speed is set as 1, for rotate frequency per 100 s = 2666.4.
SWT3 is set for flash frequency between 0.64/s and 8.3/s for present speed between
1 and 7. The SWT3 is set for time out after 40* (900 120* present_speed) interrupts of
T0, where present speed is on 8 point scale. If there are forty T0 interrupts/s per ms then
SWT3 Interrupts = 40* (900 120* 7) = 40* 60, when present_speed = 7. It means
interrupt interval = 60 ms, which means about 1000/120 = about 8.3 flashes per sec. The
motor status LED glows every 60ms and off every 60 ms.
SWT3 Interrupts = 40* (900 120* 1) = 40* 60 interrupts of T0 real time clock
ticks, when present_speed = 1. It means service interval interval = 780 ms, which
means 1000/1560 = about 0.64 flashes per sec. The motor status LED glows every 780
ms and off every 780 ms.
SWT4 service codes start at every 1000
th
ADC cycle that is after each 12 s
interval in Example 1 and is used to print ADC channel 0 results on computer.
SWT5 is used in delay function. for wait for delayperiod ms. The delayperiod is
input for the delay in ms. It is used in a delay function with input for preset delay in ms.
18 Embedded Systems




Figure 8
State diagram for T0 and Software Timers
Start
T0 Run
True
Set Period over
False
SWT4
End
True
Return

Increment Count
SWT1 and PWM = 1

Interrupt 0.025 m; Run T0_ISR
Run
Run
ADC Conversion start

SWT3
True
True
SWT2
software timer
SWT5
SWT1
False
Increment Count SWT0
and PWM = 0

wait
Over
Run
SWT0
False
Count
input
Count
input
Count
input
delaydelayperiod
ms ( ) wait
True
Count input
Count
input
Count input
every 12 ms
Call
ADC EOC Interrupt
every 12 ms
Reset SWT4
and Serial
Output ADC
data on
Computer
Hyper
terminal
Present I >
I_max
Read ADC Data
Call BLDC Protect
Function
T0 Stop
False
Increment
SWT4
Reset SWT1 when counts
SWT1 = SWT1Set
SWT2 service function when counts SWT2 =
SWT2Set and Reset SWT2
Reset SWT0 on when
counts SWT0 = SWT0Set
Reset SWT3 on when
counts > SWT3Set
Run
Real Life Case Studies 19
VII. State Diagrams
The state diagram for clarity of the software timer functions used during BLDC
running is shown in Figure 8. Figure 9 shows the state diagram for clarity of SWT2
service and set speed functions.
VIII. IDE Software
Software Vision3 integrated development environment (IDE) of Keil Software.
Vision3 also has simulator and debugger. Vision3 runs on the PC during testing and
simulation stages and for results of 8051 system output on hyper terminal window. There
is a serial communication based simulator in software Vision3. Vision3 has provision
for selection of system microcontroller device. Vision3 has provision for enabling user
to write a program in C, then check for errors and compile using Keil compiler for the
8051 based system using the selected microcontroller device.
When a COM port receives the characters at PC, then and display the received
characters on a hyper terminal (a window set to display characters received at the COM
port) The IDE also enables preparation of hex file for the specified system. It enables user
to download the hex file. The hex file is the final ROM image (Section 1.4.1) into the
system flash memory.
IX. Programs

Consider the program in Example 1. Figure 10 shows the declaration of variables
and functions and the main function and preprocessor commands in Example 1. Figure 11
Variables in the swt service codes, bldc functions and remaining functions of the
Example. Following program enables BLDC motor forward and backward run at selected
one of the 8 speeds. The P1 outputs are sent to 6 opto-couplers, which control the 6
IGBTs. Hall sensors inputs are the port P0 higher 3 bits. Five keys set the speed, BLDC
20 Embedded Systems
system run and forward/backward motor movement. Their inputs are at port P0 lower 5
bits (Figure 6).
Example 1




Figure 9
State diagram for SWT2 Service and set speed Functions
Real Life Case Studies 21
/* Program for the PWM outputs at port P2 for BLDC currents in the coils as per Hall
sensor inputs and Tables 2 and 3, 10-bit ADC0809 interface data at port P2 for BLDC
excessive current control, BLDC control by set_speed, forward-reverse and run inputs, and
the serial communication of data from the system to PC RS232C COM port for display as
text characters */
/* Modules BLDC, ADC0809, movement controls and PWM */
/*--------------------------------------------------------------------------------------------------------*/
1.
/* include files to define 8051 special function registers and stdio*/
# include <stdio.h>
# include <reg51.h>
# include <ctype> /* include header file for functions for text character type data */
2.
/*-----------------------------------------------------------------------------------------------------*/
/*---------------- Specify BLDC Interface Circuit input Bits at the port ---------------*/
/* Define KEYS port P1bits */
# define INPUTS P0
/*--------------------------------------------------------------------------------------------------------*/
/*---------------- Specify Bits P0 at the port PButton Interface Circuit --------------*/
sbit Key0 P0^0
sbit Key1 P0^1

22 Embedded Systems
sbit Key2 P0^2
/*--------------------------------------------------------------------------------------------------------*/
/*---------------- Specify Bits at the port P0 for Forward/reverse switche ---------- ------*/
sbit Key3 P0^3
/*---------------- Specify Bits at the port P0 for - on/off switch ---------------------------*/
sbit Key4 P0^4
/*-------------------------------------------------------------------------------------------------------*/
/* Specify BLDC Interface Circuit Hall Sensor input port P0 bits */
sbit S0 P0^5
sbit S1 P0^6
sbit S2 P0^7
/*-------------------------------------------------------------------------------------------------------*/
/*---------------- Specify BLDC Interface Circuit output Bits at the port ------*/
/* Define Optocoupler six output port P1 bits */
























Figure 10
Declaration of variables and functions and the main function and preprocessor commands in Example 1
Include three header files for the 8051 special function registers
Specify port P0 bits for 5 key inputs and 3 Hall Sensor inputs, port P1 output bits
6 optocouplers, two P1 output bits for SOC and LED, and one input bit for EOC
Initialize Ports P0 and P2 for inputs by writing 0xFF, P1 and set at Initial states.
Initialize P3.2 and P3.3 = 1 and 1
Main Function

Specific initial set_speed, I_Max, ADC_bit8 and bit 9.
Set SOC conversion pulse 0 level period in terms of 3 time looping in for-loop

call and Initialize parameters in bldccOff ( )
Run BLDC program if switch Key. Enable interrupts EA = 1, T0 run, T0 interrupts

BLDC protect if presentI > I_Max
Specify 3 P3 bits for ADC channel select and one for ADC chip select
Specify port P2 input bits for ADC and P3.0 and P3.1 for serial communication to
PC for downloading program and using Output at computer screen
Declare functions for BLDC, timer PWM functions and variables, for rotation
counter, speed measurement and delayperiod between successive reception and
transmission.
Set SWT4 maximum count for period. Set ADC channel 0 A2, A1 and A0 =0
Real Life Case Studies 23
sbit pwm0 P1^0
sbit pwm1 P1^1
sbit pwm P1^2
sbit pwm P1^3
sbit pwm P1^4
sbit pwm P1^5
/*-------------------------------------------------------------------------------------------------------*/
/*Specify motor status reflecting LED interfaces P1.7 */
sbit lednew = P1^7;
/*--------------------------------------------------------------------------------------------------------*/
/*---------------- Specify ADC0809 Interface Circuit Bits at the ports ---------------------*/
/* Specify ADC start of conversion pin interfaces P1.6 */
sbit SOC = P1^6; /*Specify motor status reflecting LED interfaces P1.7 */
sbit lednew = P1^7;
/*--------------------------------------------------------------------------------------------------------*/
/*---------------- Specify ADC0809 Interface Circuit Bits at the ports ---------------------*/
/* Specify ADC start of conversion pin interfaces P1.6 */
sbit SOC = P1^6;
/* Specify the ADC bit0, , bit 7 interfaced to port P2.0, , P2.7, respectively. End of
conversion instance of the ADC generates interrupt INT0 at pin P3.2. Start of conversion
instance is sent to ADC by output negative going pulse for about 3 instructions with in a
for-loop in period about 6 s at P1.6. Chip select is using P3^4*/
/*Specify ADC chip select pin interfaces P3.3 */
sbit CS = P3^4;
/*Specify ADC end of conversion interface to port bit ; .2 for Interrupt INT0 */
sbit EOC = P3^2;
/*Specify Hall sensor interface to port bit P0 interrupt ar P3.2 for Interrupt INT1 */
sbit S0Intr = P3^3;

24 Embedded Systems





































Figure11
Variables in the swt service codes, bldc functions and remaining functions of Example 1
BLDC Functions
bldcoff () to send 0s to all transistors and initial conditions set before start again
Serial Communication initializing Function

Initialize serial port for mode 1 asynchronous communication
Initialize timer T1 for auto reload repeatedly and for 9600 baud rate in 11.0592
MHz crystal based 8051 system. When SI required; Run timer T1, Enable ES and
ET1. Initialize timer T0 for auto reload every 0.025 ms
SWT1 and SWT2 timeouts control the PWM output levels = 1 and 0, respectively
Delay Function A Waiting function delayperiod input in ms

Uses a software timer SWT5, count inputs from T0 interrupts every 0.025 ms
Software timer inputs- SWT1, SWT2, SWT3, SWT5 count inputs on T0 interrupts.
The SWT1, SWT0, SWT2, SWT3 and SWT5 timeouts and service routine statements
execute.
Timer T0 ISR

ADC using INT0 ISR
SWT2 service routine (i) starts ADC conversion each 12 ms, (ii) starts find speed
every 24 ms, (iii) control speed if present speed is not equal to set speed. It also
gives input to SWT4 for 12 s time out.
SWT4 transmission of adcresults to computer print out, if present I > I maximum
permitted in coils then call bldcProtect
setspeed ( ) initializes as per KEY0, 1 and 2 first and then reset to new values later
if present speed (found from Hall sensor interrupts every 24 ms is higher or lower
bldcforward ( ) and bldcreverse ( ) for outputs to Oc0 to OC5 as per the Table 2
or Table 3. PWM base frequency depends on 16 T0 Interrupts period
Present speed change also changes LED flash frequency using
setLEDFlashFrequency
Find present speed and then control present speed
bldcProtect function. Slowly reduces the coil currents by reducing set speed
Hall sensor S0 number of inputs are used every 24 ms to find present speed.
INT 1 ISR A function to count number of Hall sensor input transitions
d l i d i t i
Real Life Case Studies 25

/* Specify Port P3 pins P3.5, P3.6 and P3.7 as channel address select pins A2, A1 and A0 at the
ADC. */

sbit A2 = P3^5;
sbit A1 = P3^6;
sbit A0 = P3^7;
/*--------------------------------------------------------------------------------------------------------*/

3.
/*-------------------- Declare global variables -----------------*/
unsigned int numT0Intr; /* Variable to reset count and to increment on T0 interrupt; */
unsigned int numS0Intr; /* Variable to reset count of number of Sensor 0 interrupt and to
increment on INT1 interrupts */
unsigned int rotFreq100s; /* Variable to find number of rotations per 100 s from number of INT1
interrupts */
/* sensorInput = Hall sensor input = S0 + S1 * 2 + S3 *4; preset_speed is a variable to set
speed as per the PButton1 and PButton2 Interface. preset_speed is as per the inputs at
Key0, Key 1 and Key2. During the running of the motor, present_speed is a variable. The
present speed is computed from the number of times the Hall sensor S0 input changes,
which means number of interrupts at INT1 input. */
unsigned int sensorInput; preset_speed, set_speed, present_speed;
/* present windings current and maximum permissible windings current and I_max */
/*controlStart = initial time, which increments on each SWT2 timeout; controlStartTime =
BLDC control functions preset start period in units of SWT2 timeout intervals */
unsigned int controlStart, control StartTime;
unsigned char controlON; /* ControlON = 0 intially. After controlstarttime it becomes 1*/
unsigned int presentI; I_max;
unsigned int swt0set, swt1set, swt2set, swt3set, swt4set, swt5set, swt10period;
/* char swt0set, swt1set, swt2set, swt3set, swt4set, swt5set and swt10freq variables to set
SWT1, SWT0, SWT2, SWT3, SWT4 and SWT5 timeout intervals and PWM = 1 and = 0
cycle period in units of number of SWT count inputs. The SWT count inputs are from
timeout T0 interrupt periods, which is preloaded for interrupts after 1 ms. */
unsigned char socnewpulsedelay; /* socnewpulsedelay to set interval rom start of ADC
conversion pulse*/
unsigned char pwmnew; /* pwmnew variable either 1 or 0 as per current PWM output level, */
unsigned char adc8bitsdata; /* variables for 8-bits ADC data */
unsigned char adcLower8bits (void); /* ADC data Receiving function */
unsigned int adcdata; /* variable for all 10-bits ADC data */
int ADC_bit8 ; /* 8-th (msb bt one) bit of 10 bit ADC */
int ADC_bit9; /* 9-th bit (msb) of 10 bit ADC used here*/
unsigned char swt2SRCycle;
26 Embedded Systems
/* When controlON = 1; variable to find if ADC cycle is even , then reset numS0Intr = 0
and if swt2SRCycle = 0 then find present_speed from numS0Intr */
unsigned int delayperiod; /* Declare a wait function delaydelayperiodms ( ) given delayperiod in
ms*/
unsigned char waitover ; /* A variable reset to 0 on wait start in wait function
delaydelayperiodms ( ) function and set to 1 on wait over */
unsigned char bldcon; /* variable for BLDC On status */
/*--------------------------------------------------------------------------------------------------------*/
4.
/*------------------------------- Declare BLDC Functions ------------ -------------------*/
void controlPresentSpeed (void) ; /* declare function for Control of Present speed in even SWT2
interrupt cycle after 24 ms*/
void setLEDFlashperiod (void); /*Set LED flash period as per the presentSpeed */
/* PWM duty cycle, thus initial preset_speed and set_speed as per inputs from PBuInterface and
later after SWT2 timeout and control becomes ON after controlstarttime as per the new
set_Speed value --- */
void setSpeed (void );
void bldcOff (void); /*declare function to switch off BLDC inputs*/
void bldcRun (void) ; /*declare function to switch off BLDC inputs*/
void bldcProtect (void ); /* /*declare function to protect BLDC inputs after finding presentI */
void bldcforward (void ); /* declare function for forward movement of bldc */
void bldcreverse (void ); /* declare function for reverse movement of bldc */
/* INT1 Service routine for interrupts from Hall Sensor. This function to find present speed
is called on swt2 interrupt. */
void INT1_ISR (void); void INT1_ISR (void);
/*--------------------------------------------------------------------------------------------------------*/
5.
/*------------------- Declare Functions for SWTs ------------ -----------------------------*/
void softwaretimerinterrupts (void); /* declare function for giving input to software timers and
call software timer ISRs if set time for each is over */
/*---------------------------------------------------------------------------------------------*/
6.
/* Service routines for timeouts of SWT1, SWT0, SWT2 and SWT3, which get inputs from time
T0 0.025 ms interrupts */
/* Service routine to initiate ADC conversion and also give input to SWT4 * /
unsigned swt2SR (void );
/* SWT1 Service routine to make pulse level of PWM outputs = 1 executes within the
softwaretimerinterrupts () function itself * /
/* SWT0 Service routine to make pulse level of PWM outputs = 0 executes within the
softwaretimerinterrupts () function itself */.
/* SWT3 Service routine to call LED flashing to ON if Off and OFF if ON executes within
the softwaretimerinterrupts ( ) function itself */.
/* SWT4 count inputs are from timeout of SWT2 interrupt periods after every 12 s*/
Real Life Case Studies 27
/* SWT5 count inputs are from timeout of T0 every 0.025 ms. SWT5 timeout is used in
delaydelayperiodms function itself */
*-------------------------------------------------------------------------------------------------------*/
7.
/* ------------------ serial interface initialization mode 1 and T1-------------------------------
------ programmed for 9600 baud and T0 programmed for 1 ms interrupts. Both T1----
--------------------------and T0 run in mode 2--------------------------------------------------- */
void SI_InitM1T0M2T1M2 (void);
/* -----ADC Result and Hyper terminal Display on PC for user interaction -----------*/
unsigned char [ ] adcresult /* String for ADC result as ASCII text characters */
/* ------------- Declare delay routine for delayperiod ms delay ---------------- */
-------------using T0 programmed mode2 to auto reload after 0.025 ms ----- */
unsigned char delaydelayperiodms (void);
/*--------------------------------------------------------------------------------------------------------*/
8.
/*----------------------------------------------- MAIN FUNCTION ---------------------*/
void main (void)
{
/* To enable functioning as inputs, initialize the P0 and P2 bits = 1s and P3^2 EOC = 1 for
enabling it to read BLDC PButton interface, BLDC Hall sensor interfacing circuit at P0 and ADC
converted bits at P2 on EOC is interrupt INT0 bit and S0Intr is interrupt INT0 bit */
P0 = 0xFF; P2 = 0xFF; EOC= 1; S0Intr = 1;
/* Initialize swt4set parameter for printing adc ch0 results after 12 s interval */
swt4set = 1000; /
/* ----------------Initialize Motor Protection from excess current using --------------------------
--------
-------------- ADC Channel bits A2-A1-A0 = 000 to enable read from channel 0 of
ADC0809------
----------- and Set SOC pulse interval = about 6 microsecond -------------- ---*/
A2 = 0;
A1 = 0;
A0 = 0;
socnewpulsedelay = 3; /* socnewpulsedelay set to 3 for-loop delay interval in swt2SR */
controlStart = 0; /* Initial value of close loop control start time = 0 */
ControlON = 0;
controlStartTime = 150; /* controlStartTime= 1.8 ms, a preset value of close loop control start
interval in units of SWT2 timeout intervals. */
int ADC_bit8 = 0; /* Only 8-bit of 10 bit ADC used here*/
int ADC_bit9= 0; /* Only 8-bit of 10 bit ADC used here*/
/* Function to initialize ISR for serial port SI of 8051 to mode 1 and baud rate to 9600
using timer mode 2 at T1 and initialize Timer T0 Mode 2 auto reload every 0.025 ms */
SI_InitM1T0M2T1M2 ();
ES =1; /* Start Serial Interface by enable ES Interrupts and Running T1 for 9600 baud rate /*

28 Embedded Systems
TR1 = 1; /* Run Timer 1 */
/*-------------------INITIALIZE BLDC FUNCTION PARAMETERS ----------------*/
swt10period = 16; /* Set PWM pulse period = 16 * 0.025 ms = 0.4.ms. PWM base frequency =
2.5 kHz */
if (controlON = 0) then {preset_speed = (KEY0 + KEY1 *2 + KEY3 * 4); set_speed =
preset_speed; };
setSpeed ( ); /* set SWT1 and SWT0 timeout intervals for 1 and 0 PWM level */
setLEDFlashperiod (); /*Set LED flash period as per the present_speed */
I_max = 0x80 ; /* Let Maximum Permissible Winding Current is such that ADC gives the
output 0x80 and above if present I > = I_max */
swt2set = 480; /* Set number of T0 interrupts for executing SWT2 timeout service routine for
ADC start after 480 * 0.025 ms = 12 ms*/
/*----------------------------------------------------------------------------------------------------------------*/
while (1) {
presentI = I_max; /* Presume that Present winding currents = I_max to initialize bldcoff
function in while loop */
while (presentI >= I_max) {bldcOff ( );} /*switch off BLDC inputs and set initial values for the
run time variables */

if (onoff = = 1){
if (presentI < I_max) { bldcRun ( );};
} else{bldcProtect ( );}; /* BLDC motor Protect Function */;



}/* end of the infinite while loop */

} /* end of the main program */
/*----------------------------------------------------------------------------------------------------------*/
9.
/*----------------------- BLDC MOTOR OFF FUNCTION ------------------------------------*/
void bldcOff (void) { /*declare function to switch off BLDC inputs and set initial values before
setting the run time variables for BLDC running */
pwm0 = 0;
pwm1 = 0;
pwm2 = 0;
pwm3 = 0;
pwm4 = 0;
pwm5 = 0;
pwm new = 0;
present_speed = 0;
presentI = 0;
EA = 0; /* Disable all interrupts */
lednew = 0; /* LED OFF*/
Real Life Case Studies 29
pwmnew = 0; ;/* PWM PULSE LEVEL 0 */
socnewpulsedelay =0; /* A
numT0Intr = 0;
numS0Intr = 0;
swt2SRCycle = 0;
SOC = 1; /* SOC output at P1^6 = 1*/
EX1= 0; /* Disable INT1 from Hall Sensor */
EX0= 0; EOC = 1; /* Disable EOC INT0 input at P3^2 */
ET0 = 0; TR0 =0 / *Disable Timer 0 interrupts and T0 running*/
swt1 =0; /* Count-input of SWT1 = 0 */
swt0 = 0; /* Count-input of SWT0 = 0 */
swt 2 =0; /* Count-input of SWT2 = 0 */
swt3 =0; /* Count-input of SWT3 = 0 */
swt4 = 0;
bldcon = 0; /* BLDC On status = 0 */

} /* end of the bldcoff function */
/*----------------------------------------------------------------------------------------------------------*/
10.
/*----------------------- BLDC MOTOR RUN FUNCTION ------------------------------------*/
void bldcRun (void) { /*declare function to control the BLDC coil inputs by starting real time
clock ticks (interrupts) using time T0 and set initial values for the variables for running and
control of BLDC */
bldcon = 1; /* BLDC On status = 0 */
lednew = 1; /* LED ON*/
EA= 1; /* Enable all interrupts */
TR0 = 1; ;/* Run Timer 0 */
ET0 = 1; ;/* enable timer 0 interrupt service routine run */
swt2SRCycle = 0; /* Reset number of SWT2 service routine cycle = 0 so that it becomes odd at
the first SWT2 timeout service routine run */
};
/*---------------------------------------------------------------------------------------------------------*/
11.
/*---------PWM DUTY CYCLE PRESET AS PER PBUINTERFACE INPUTS -------*/
--------- AT KEY0, KEY 1 AND KEY 2 INITIALLY AND LATER AS PER NEW SET_SPEED
*/
void setSpeed (void ) {
if (set_speed > 8) set_speed = 8;
if (set_speed < = 8} {swt1set = 2 * set_speed; /* Set number of T0 interrupts for executing
SWT1 timeout service routine*/
swt0set = swt10period swt1set; /* Set number of T0 interrupts for executing SWT0 timeout
service routine for PWM level = 0 */
};
}/* End of set speed function */
30 Embedded Systems
/*----------------------------------------------------------------------------------------------------------*/
12.
/* --- Function to ADJSUT LED flash period as per the present_speed on 8 point scale- */
/* Set number of T0 interrupts for executing SWT3 timeout service routine */
void setLEDFlashperiod (void ) {
/* See text for calculation. (1) The motor status LED glows every 60ms and off every 60
ms, when present_speed = 7. (2) The LED glows every 780 ms and off every 780 ms when
present_speed = 1.
*/
if (present_speed ! = 0 | | present_speed < 8) {
swt3set = 40* (900 120* present_speed);
} else {swt3set = 0; lednew = 0;}
}
/*----------------------------------------------------------------------------------------------------------*/
13.
/*-------------- BLDC MOTOR FORWARD MOVEMENT FUNCTION ----------------*/
void bldcforward (void) { /* function to forward movement as per table 2 BLDC inputs */
{ sensorInput = S0 + S1 *2 + S2* 4;
if (sensorInput = = 1) {pwm5 =pwmnew; pwm2 = pwmnew;};
if (sensorInput = = 3) {pwm5 =pwmnew; pwm0 = pwmnew;};
if (sensorInput = = 7) {pwm3 =pwmnew; pwm0 = pwmnew;};
if (sensorInput = = 6) {pwm3 =pwmnew; pwm4 = pwmnew;};
if (sensorInput = = 4) {pwm1 =pwmnew; pwm4 = pwmnew;};
if(sensorInput = = 0) {pwm1 =pwmnew; pwm2 = pwmnew;};
} /* end of the bldforward function */
/*----------------------------------------------------------------------------------------------------------*/

14.
/*-------------- BLDC MOTOR REVERSE MOVEMENT FUNCTION ----------------*/
void bldcreverse (void) { /* function to reverse movement as per table 2 BLDC inputs */
{ sensorInput = S0 + S1 *2 + S2* 4;
if (sensorInput = = 6) {pwm5 =pwmnew; pwm2 = pwmnew;};
if (sensorInput = = 7) {pwm1 =pwmnew; pwm2 = pwmnew;};
if (sensorInput = = 3) {pwm1 =pwmnew; pwm4 = pwmnew;};
if (sensorInput = = 1) {pwm3 =pwmnew; pwm4 = pwmnew;};
if (sensorInput = = 0) {pwm3 =pwmnew; pwm0 = pwmnew;};
if(sensorInput = = 4) {pwm5 =pwmnew; pwm0 = pwmnew;};
}
/*---------------------------------------------------------------------------------------------------- */
15.
/*--------------------------------- TIMER T0 INTERRUPT ---------------------------------*/
void T0_ISR ( ) interrupt 1 {/* T0 Interrupt Service Routine *
if (onoff = = 1 && bldcon = = 1) {
numT0Intr ++; /* Increment T0 Interrupts Count by 1 */
Real Life Case Studies 31
TR0 = 1; /* run T0 */
EA = 1; ET0 = 1; /* Enable T0 interrupts again */
softwaretimerinterrupts ( ); /* Give swt Inputs and Increment counts in SWTs */
} else {TR0 = 0; ET0 = 0;};
}
/*---------------------------------------------------------------------------------------------------- */
16.
/*----------FUNCTION FOR GIVING COUNT INPUTS TO SOFTWARE TIMERS ---------
----------------- AND EXECUTE SOFTWARE TIMER SERVICE ROUTINES ------------
------------------- IF THEIR SET TIMEOUT PERIOD OVER -------------------------- ---*/
Software Timer Interrupts TIMER T0 INTERRUPT --------------*/
void softwaretimerinterrupts (void ) {
/* Count input to SWT1, 0, 2, 3 and 5 from T0 interrupt */
(if pwmnew == 1) {swt1 ++;}; /* During level 1 increment SWT1*/
(if pwmnew == 0){swt0++;}; /* During level 1 increment SWT0*/
swt2++;
swt3++;
swt5 ++;
/* execute the software timer service codes if set time is over and reset the software timer
count to 0 after the timeout */
/* Execute swt1SR Service codes to make pulse level of PWM outputs = 1 * /
if (swt1 > = swt1set & & swt1set != 0)
{
if (key4 = = 1)
{ /* PWM 1 interval over, so make pwmnew 0 and reset SWT0*/
pwmnew = 0; /* PWM 1 interval over, so make pwmnew 0 */
swt0 = 0;
if (KEY3 ==1){bldcforward ( ); } else {bldcreverse ( ); };
};
};
/* Execute swt0SR Service codes to make pulse level of PWM outputs = 0 * /
if (swt0 > = swt0set & & swt0set != 0)
{ /* PWM 0 interval over, so make pwmnew 1 and reset SWT1*/
pwmnew = 1; /* PWM 1 interval over, so make pwmnew 0 */
swt1 = 0;
if (KEY3 ==1){bldcforward ( ); } else {bldcreverse ( ); };
};
/* Call service function swt2SR */
if (swt2 > = swt2set & & swt2set != 0) {swt2SR (); swt2 = 0; */};
/* Execute Service routine to flash LED ON if Off and OFF if ON * /
if (swt3 > = swt3set && swt3set != 0)
{
if (lednew = = 0) {lednew = 1;} else if (lednew = = 1) {lednew = 0;};
swt3 = 0;
32 Embedded Systems
} else lednew =0;
} /* end of SWT Service routines */
/*-----------------------------------------------------------------------------------------------------*/
17.
/*---SWT2 Service Routine for finding speed between two SWT2Interrupts, ----------
-----for initiating ADC start of conversion and ADC Read on INT0 interrupt, ----------
----- for SWT4++ timer input and closed loop control and protection of BLDC -----*/
void swt2SR (void) {
swt4++; /* Increment SWT4 count every SWT2 interrupt */
if (controlON = = 0) {controlStart + +; };
if (controlStart = = ControlStartTime) {controlON =1;};
/* Start Hall Sensor Interrupts */
/* Start ADC conversion */
CS = 0;
SOC =1;
for (int i = 0; i< socnewpulsedelay; i++) { SOC = 0; P3^2 = 1; EOC =1;};
socnewpulsedelay =0; SOC =1; EX0 = 1; EA = 1;
/* Find Present speed in odd cycle */
/* Start close loop control */
if (controlStart > = ControlStartTime 2) )
{ EX1 =1; /*enable S0 Interrupts service at INT1 */
if (swt2SRCycle == 0) {
swt2SRCycle = 1; /* Specify next cycle odd */
if (controlON ==1) {findPresentSpeed ( ); };/* Present speed in even cycle*/
}
else if (swt2SRCycle = = 1)
{swt2SRCycle = = 0; /* Specify next cycle even */
};


} /* End of SWT2 service function */
/*---------------------------------------------------------------------------------------------*/
18.
/*--------------------FUNCTION TO FIND PRESENT SPEED--------------------------*/
void findPresentSpeed (void ){/* Find Present speed in odd cycle*/
/* Total number of T0 interrupts for Speed Finding and ADC0809 = 480 */
/* Variable to find if swt2SRCycle is odd, then reset numS0Intr and if swt2SRCycle = 0
then find present_speed from numS0Intr */
rotFreq100s = 8333 /numS0Intr;
/* present rotation frequency for 100 s =100 s divided time for 1 ADC SWT2 = 0.024 ms
and again divided by 2 as there is interrupt INT1 after every 180 degree rotation */
if (rotFreq100s < 357 ) {present_speed = 8;};
if (rotFreq100s > 357 && rotFreq100s <= 413 ) {present_speed = 7; };
if (rotFreq100s > 413 && rotFreq100s <= 490 ) {present_speed = 6; };
Real Life Case Studies 33
if (rotFreq100s > 490 && rotFreq100s <= 600 ) {present_speed = 5; };
if (rotFreq100s > 600 && rotFreq100s <= 778 ) {present_speed = 4; };
if (rotFreq100s > 778 && rotFreq100s <= 1111 ) {present_speed = 3; };
if (rotFreq100s > 1111 && rotFreq100s <= 2000) {present_speed = 2; };
if (rotFreq100s > 2000 && rotFreq100s <= 4000 ) {present_speed = 1; };
if (rotFreq100s > 4000 ) {present_speed = 0; }; /* Refer text to find present speed on 8
point scale from rotate speed */
};
numS0Intr = 0; /* Reset to 0 number of S0 sensor input interrupts at INT1*/
swt2SRCycle = 0; /* Specify next cycle as even */
/* Enable INT1interrupts for finding present speed*/.
*/
/* Call control present speed if present_speed is not as preset_speed */
if (present_speed != preset_speed ) {controlPresentSpeed (); setLEDFlashPeriod ( ); }; /*
End of find present speed function */
/*---------------------------------------------------------------------------------------------*/
19.
/*-----FUNCTION TO CONTROLPRESENT SPEED TOWARDS set_speed -----*/
void controlPresentSpeed (void) {/* Control Present speed in odd SWT2 interrupt cycle after
24 ms*/
if (present_speed > preset_speed || present_speed = = 8) {set_speed - -; setSpeed ( ); }
else if (present_speed < preset_speed && present_speed <8) {set_speed ++; setSpeed ( );
};
};
}; /* End of control present speed function */
/*---------------------------------------------------------------------------------------------*/
20.
/*--------------------FUNCTION TO PROTECT BLDC --------------------------*/

void bldcProtect (void ){
/* Decrease current gradually after every 12 ms as long present_speed reduction does not
reduce does not presentI below I_max */
If (bldcOn = = 1 & & presentI >= I_max && set_speed > = 1) {set_speed - -; }
If (set_speed = = 0) {bldcOff ( );} else setSpeed ( );};
}
/*---------------------------------------------------------------------------------------------*/
21.
/*----------- INT1 Service Routine for Sensing the Hall Sensor ----------------*/
void INT1_ISR (void) interrupt 2 {
numS0Intr ++;
EX1 = 1; /* Enable EX1 next interrupt interrupts */
}
/*---------------------------------------------------------------------------------------------*/
22.
34 Embedded Systems
/*------------ ISR Function for ADC Interfaces Current Reading at Channel 0 --------
------------------ Using EOC at INT0 INTERRUPT AFTER SOC AT T AT P1.6 ---
----- AND CALCUCLATING PRESENT I AND CALL BLDC PROTECTION-- -------*/
void INT0_ISR ( ) interrupt 0 {/* INT0 Interrupt Service Routine for interrupt when EX0 =
1*/
/* Find adc8bitsdata and adc bit 8 and bit 9 highest two bits and calculate adcdata */
adc8bitsdata = P2;
adcdata = adc8bitsdata + ADC_bit8 * 256 + ADC_bit9 * 512;
presentI = adc8bitsdata;
if (presentI > = I_max) {bldcProtect ( ); };
if (swt4 > swt4set && swt4set != 0) /* If 12 s over after 1000 SWT2 interrupts */
{ TI = 1;
adcresult = toascii (adcdata); /* Convert adcdata to ASCII coded string */
printf ( ADC Channel 0 Output = %3bd C, adcresult);}
printf (-------------- ADC Interfaces Current Reading at Channel 0 ------------------------
--\n\n );
swt4 = 0; ES =1; TR1 = 1; TI = 1; /* Reset swt4 for next cycle*/
}
CS = 1; EOC =1; EX0= 0; /* Deselect ADC0809 chip and Disable INT0 interrupts run till
next SWT2 timeout */
}

23.
/*----------- INITIALIZE SERIAL INTERFACE PORT ----------------------
------------ TIMER MODE 2 T1 FOR BAUD RATE 9600 AND START ----
--------IMER T0 FOR MODE 2 ONE MS INTERVAL INTERRPTS ------------- */
void SI_InitM1T0M2T1M2 (void) {
{
/* Function to initialize serial port SI of 8051 to mode 1 and baud rate to 9600 using timer
T1*/
/* 0x50 = 01010000 SCON^7, , SCO^0 are SM0, SM1, SM2, REN, TB8, RB8, TI and
RI, respectively
SM0= 0 and SM1 = 1 for mode 1, start, 8 data bits and stop bit total 10 UART bits
SM2 = 0
REN = 1, receiver enable
TB8 = 0 as not used in mode 1
RB8 = 0 as not used in mode 1
TI = when read initial value of transmitter complete interrupt flag = 0
RI = when read initial value of receiver complete interrupt flag = 0
TI = 1 when initialize serial transmitter service routine
*/
SCON = 0x50;
/*--------------------------------------------------------------------------------------------------*/
Real Life Case Studies 35
/* Initialize timer T1 to 8-bit mode 2 timer (auto-reload from TH1 into TL1 after each
timer overflow interrupt */
/* timer T0 loads and runs in mode 0 by function T0_start (t0set, t0reset) in 13-bit mode 0
timer, */
0x20 = 00100000 TMOD^7, , TMOD^1 are Gate1, C/T1, M1, M0, Gate0, C/T0, M1,
M0, respectively
Gate1= 0 as external pin gate control of timer 1 start not used
C/T1 = 0 as external pin input (counter) not used
M1, M0 = 10 for mode 2, eight bit timer TL1 with auto-reload from TH1 after each
overflow of T1
Gate0, C/T0, M1, M0 = 0, 0, 0 and 0 as T0 used as follows:
Gate0= 0 as external pin gate control of timer 1 start not used
C/T0 = 0 as external pin input (counter) not used
M1, M0 = 10 for mode 2, 8 bit timer TL0 with auto reload TH0 after each overflow of T1
*/
TMOD = 0x22;
/* For overflow after each (0x100 0xFD) = 3 timer ticks. Mode 1 has serial clock pre-
scaling factor
of 32. Time for 3 serial clock ticks = 3 32 12/11.0592 s for 11.0592 MHz crystal in
8051 system. Baud rate = 1000000 11.0592/ (3 32 12) = 9600 */
TH1 = 0xFD; /* Note T1 Runs when TCON^6 TR1 to run timer T1 is set to 1 in main
function */
/* For overflow after each (0x100 0xE9) = 23 timer T0 ticks. Time for 23 timer ticks =
23 12/11.0592 s for 11.0592 MHz crystal in 8051 system = 0.025 ms. T0 interrupt rate
= 40000 per s */
TH0 = 0xE9; /* Note T0 Runs when TCON^4 TR0 to run timer T0 is set to 1 in main
function */
} /* end of the serial, T0 and t1 initialization function */
/*-------------------------------------------------------------------------------------------------*/
24.
/* --------------------------- Delay function for delay of delayperiod ms ------ --------*/
unsigned char delaydelayperiodms (void)
{ waitover = 0; /* Function returns waitover = 0 during wait and waitover = 1 on return*/ -
swt5 = 0; /* swt5 increments on each T0 interrupt in function softwaretimeinterrupts ( ) */
swt5set = 40 * delayperiod;
while (swt5 < swt5set; {;};
waitover =1 /* end of wait for delayperiod ms in for-loop for delay. swt5 increments every
0.025 ms */
}/* end of function */
/*---------------------------------------------------------------------------------------------------*/

36 Embedded Systems
X. Method
Install Vision3 integrated development environment (IDE) of Keil Software. Now
run Vision3. First create and edit a new project file BLDCControl.C with C program of
Example 1.
Now compile the program. If no errors then a hex file BLDCControl.hex will be
created. Now, start the program for flash programming. After erasing the previous
program, the new hex file is down loaded.
XI. Results
Results are simulated on the host.

Вам также может понравиться