Controlling AC Motor Using Arduino Microcontroller
Controlling AC Motor Using Arduino Microcontroller
Huskie Commons
2014
Recommended Citation
Nannuri, Nithesh Reddy, "Controlling AC motor using Arduino microcontroller" (2014). Graduate Research
Theses & Dissertations. 1960.
https://huskiecommons.lib.niu.edu/allgraduate-thesesdissertations/1960
This Dissertation/Thesis is brought to you for free and open access by the Graduate Research & Artistry at Huskie
Commons. It has been accepted for inclusion in Graduate Research Theses & Dissertations by an authorized
administrator of Huskie Commons. For more information, please contact jschumacher@niu.edu.
ABSTRACT
Space vector modulation (SVM) is a technique used for generating alternating current
waveforms to control pulse width modulation signals (PWM). It provides better results of PWM
trigonometric functions of sine, cosine, magnitude and phase using bit shift, addition and
multiplication operations.
This thesis implements SVM with Arduino microcontroller using CORDIC algorithm.
This algorithm is used to calculate the PWM timing signals which are used to control the motor.
Comparison of the time taken to calculate sinusoidal signal using Arduino and CORDIC
DEKALB, ILLINOIS
DECEMBER 2014
BY
MASTER OF SCIENCE
Thesis Director:
Dr. Donald S Zinger
ACKNOWLEDGEMENTS
I would like to express my sincere gratitude to Dr. Donald S. Zinger for his continuous
support and guidance in this thesis work as well as throughout my graduate study.
I would like to thank Dr. Martin Kocanda and Dr. Peng-Yung Woo for serving as
I would like to thank my family for their unconditional love, continuous support,
enduring patience and inspiring words. Finally, I would like to thank my friends and everyone
who has directly or indirectly helped me for their cooperation in completing the thesis.
TABLE OF CONTENTS
Page
LIST OF TABLES…………………………………………………………… v
LIST OF FIGURES………………………………………………………….. vi
Chapter
1. INTRODUCTION ……………………………………………. 1
4. CORDIC ……………………………………………………………. 27
5. RESULTS ……………………………………………………………… 36
6. CONCLUSION ……………………………………………………… 39
REFERENCES ………………………………………………………. 40
Table Page
Figure Page
INTRODUCTION
Three-phase induction motors are widely used in many applications and several methods
are available to control the speed and torque of the motor. By varying the load, speed of the
motors can be controlled and energy can be saved. There are a few techniques involved in
controlling the speed of the motor. They are classified as scalar control and vector control. [1]
In scalar control method speed can be varied by changing the supply frequency which
results in change of impedances. This change of impedances might increase the current or
decrease it. If the current is small, torque of the motor decreases. Frequency and impedances are
directly proportional and if frequency decreases, coils can be burned or saturation can occur in
the iron of coils. To avoid this both voltage and frequency are varied at the same time,
Field-oriented control is one of the methods of vector control. Direct torque control
(DTC) and direct self-control methods also work with vectors. The field-oriented control works
with the principle of rotating vectors in a complex coordinate system. Here magnitude and
phase are controlled with change in current. With this field components are uncoupled,
2
which can be used to control current and flux independently. Induction motor loses its
complexity and high performance can be realized [2] by maintaining 900 electrical angles
This control technique can be carried out by applying system and coordinate
transformations to the basic equations of the motor. Alternating and sinusoidal quantities
become non alternating quantities. Magnitude and phase of supply voltage or current can be
Scalar control method is cheap and simple compared with field-oriented control. It
controls magnitude of voltages and frequency instead of controlling phase and magnitude of
currents. On the other hand, field-oriented control method controls the current and operates with
fast responses.
pins out of which six pins can be used as PWM outputs, six analog inputs, a 16 MHz ceramic
resonator, a USB connection, a power jack, an ICSP header and a reset button. It contains
everything needed to support microcontroller. Simply connect it to a computer via USB or else
Arduino UNO can be programmed with Arduino software. The open-source Arduino
environment makes it easy to write code and upload it to I/O board through USB. It runs on all
3
platforms like Windows, Mac OSX and LINUX. CORDIC algorithm and Space vector
modulation are implemented by using Arduino microcontroller. The switching times are
Space vector modulation is used for controlling pulse width modulation (PWM). This is
the one of the best methods available for PWM signals. It is used for the creation of AC
waveforms. Usually SVM works well if all the three output voltages of the inverter on the
There are eight switching states for the inverter at any instant of time. These states are
represented by stationary vectors, out of which six vectors are active vectors and the remaining
two vectors are zero vectors. The output at any instant of time is given by the reference vector
Vref, which can be synthesized by three stationary vectors. The on and off times of the inverter
switch are calculated and the switching sequence is chosen to minimize the number of
switching.
1.5 CORDIC:
to compute trigonometric and hyperbolic functions using bit shift, addition operations by
eliminating multiplication operations. This can be operated in vector mode and rotating mode.
The angle whose cosine or sine has to be found is stored in an accumulator and each step is
obtained by direction of rotation of the vector, based on the sign of residual angle.
4
‘x’ is started with the value of constant 1/K and is obtained by the product of cosine
values. At each iteration ‘x’ and ‘y’ values are updated. ‘z’ values are updated to zero to make
error as low as possible and at certain iteration ‘z’ value becomes zero. At the end, ‘x’ value
gives cosine of the angle and ‘y’ value gives sine of that particular angle. The sign of ‘d’ depends
on angle of rotation.
algorithm. The second chapter deals with the Arduino UNO board, discussing the digital pins,
power supply and the microcontroller associated with it. The features of Arduino and its software
The third chapter deals with the space vector modulation. The representation of reference
vector, how the switching states are represented and the calculation of switching times are
discussed there. The selection of switching sequence is important to reduce harmonic effects and
The fourth chapter includes the calculation of sine and cosine of a particular angle using
CORDIC algorithm eliminating the need for multiplication. The fifth chapter contains the results
obtained from Arduino software and the waveforms associated with it. Conclusions and future
ARDUINO UNO
Arduino can sense the environment by receiving input from a variety of sensors and can
affect its surroundings by controlling lights, motors and other actuators. The microcontroller on
the board is programmed using the Arduino Programming Language and the Arduino
development environment. Arduino projects can be stand-alone or they can communicate with
Arduino UNO is based on ATmega328. It has 14 digital input/output pins of which six can
be used as PWM outputs. It has six analog inputs, a 16 MHz ceramic resonator, a USB
connection, a power jack, an ICSP header, and a reset button. It contains everything needed to
support the microcontroller and is connected to the computer using USB cable or AC-DC adapter
or battery to get started. [3] Figure [1] shows an Arduino UNO microcontroller board.
7
ATmega328
Microcontroller
5V
Operating voltage
7V - 12V
Input voltage
6V – 12V
Output voltage ( limits)
16 ( 6 PWM pins)
Digital I/O pins
6
Analog input pins
40 mA
DC current per I/O pin
50 mA
DC current for 3.3V pin
32 KB
Flash Memory
2 KB
SRAM
1 KB
EEPROM
16 MHz
Clock Speed
9
The Arduino can be powered via USB connection or external power supply. Power
source is selected automatically. External power can come from AC-DC supply or battery. The
board can be operated on an external supply of 6V- 20V. The recommended voltage is 7V- 12V.
If the board is supplied with less than 7V, it will be unstable, and if it is supplied with more than
12V, the voltage regulator is overheated and may damage the board.
VIN: The board is supplied with 5V through USB or from an external battery.
5V: This pin outputs a regulated 5V from the regulator onto the board. Power can be supplied
with DC jack (7V – 12V), USB connector (5V), or the VIN pin on the board.
IOREF: Provides the voltage reference with which microcontroller operates. Selects the
appropriate power source or enables voltage translators on the output working with 5V or
3.3V.[3]
Each pin in the board can be used as input or output pin using pinMode( ), digitalWrite( )
Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data.
These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL
Serial chip.
LED: 13. There is a built-in LED connected to digital pin 13. When the pin is HIGH
value, the LED is on; when the pin is LOW, it's off.
The UNO has six analog inputs, labeled A0 through A5, each of which provides 10 bits
TWI: A4 or SDA pin and A5 or SCL pin. Support TWI communication using the Wire
library.
Reset. Bring this line LOW to reset the microcontroller. Typically used to add a reset
2.4 Communication:
Arduino UNO has several ways of communication with computers, other Arduinos or
from other microcontrollers. ATmega328 provides UART TTL serial communication, which is
available on digital pins 0 (RX) and 1 (TX). Arduino software has serial monitor which allows
11
simple data to be sent to and from Arduino board. RX and TX LEDs will flash while data is
Arduino UNO can be programmed with Arduino software. It comes with a boot loader
that allows uploading the new code without any external hardware programmer. Arduino
software uses Arduino C language which is similar to C++. Once the coding is over, it is
uploaded to the board through USB. While uploading TX and RX, LEDs will flash. Choose the
board as Arduino UNO and select serial port from serial port menu. This is likely to be COM3 or
higher.
Arduino UNO has reset button in the board. When it is pressed, the board will reset.
Instead of doing so, UNO is designed in a way that allows it to reset by software running on a
connected computer.
2.6 ATMega328:
has an on-chip 2-cycle multiplier. High-endurance non-volatile memory segments include 32K
bytes of internal SRAM. The write/erase cycles are set to 10,000 Flash/100,000 EEPROM.
One 16-bit timer/counter with separate prescaler, compare Mode, and capture mode
It has 23 programmable I/O lines. The operating voltage is in between 1.8V – 5.5 V and the
architecture. Atmega328 achieves 1 MIPS per MHz by executing powerful instructions allowing
the system designer to optimize power consumption versus processing speed. [5] See Figure 2.
13
The AVR core combines a rich instruction set with 32 general-purpose working registers.
All the 32 registers are directly connected to the arithmetic logic unit (ALU), allowing two
independent registers to be accessed in one single instruction executed in one clock cycle. The
is a powerful microcontroller that provides a highly flexible and cost-effective solution to many
Arduino UNO has its own software. It is similar to ‘C’ language. Code has to be written
in the software provided and uploaded the code to the board which is connected to the computer
using USB cable. When using development software, selection of Arduino UNO board and serial
In recent years, Space vector modulation (SVM) is used for controlling three-phase PWM
inverters. This is the one of the best available methods for PWM signals. It is an advanced
control mechanism that generates three-phase AC voltages of the desired magnitude and
frequency at the output of the inverter. To implement SVM a reference signal Vref is sampled
with frequency fs (fs = 1/Ts) [6]. For generating switching patterns the output voltages of the
inverter are taken into account. The vectorial representation was first presented in the
The voltage source inverter is shown in the Figure 4. A voltage source inverter can have
only eight switching states because the input lines are never shorted and the output current is
always continuous. These eight switching states are shown in the Figure 5. These states are
represented by stationary vectors, out of which six vectors are active vectors and the remaining
Each of the terminals ‘A’, ‘B’ and ‘C’ has only two levels with respect to ‘N’, Vg and 0 where Vg
is the DC bus voltage. Therefore the switching state for each terminal can be denoted by ‘P’ and
‘N’. Here state ‘P’ represents when upper switch is ON and the output voltage of the inverter
becomes +Vg. ‘N’ represents the state when lower switch is ON and the voltage will be zero.
Each of the eight switching states can be therefore denoted with three terms consisting of either
‘P’ or ‘N’.
17
inverter
18
topology V1 (PNN) which is shown in the Figure 5, the line voltages VAB, VBC and VCA are given
by
VAB = Vg
VBC = 0 and
VCA= -Vg
This can be represented in the Figure 6, where line voltages are displaced in 1200. The A, B and
C are connected to either positive or negative DC rail. In PNN, phase A is connected to positive
DC rail and phases B and C are connected to negative DC rail as shown in the Figure [6].[10]
VBC
VAB = Vg -VCA
VBC = 0 Vg
VCA= -Vg
V1 (PNN)
VCA -Vg
VAB
Figure 6: Representation of V1 (PNN) in , plane
19
Accordingly, similar non-zero vectors (V1 – V6) can be shown in positions as shown in
the Figure 7. The area enclosed by two adjacent vectors within the hexagon is defined as a
j
V3 V2
2
3 1
V1
V4
5 4 6
5
V5 V6
Figure 7: Non-zero voltage vectors in , plane
The output line voltages generated by topologies V0 (PPP) and V7 (NNN) are as follows:
VAB = 0
VBC = 0 and
VCA = 0
These vectors are called zero vectors and hence the magnitude of these voltage vectors is zero.
They assume their position at the origin in the plane as shown in Figure 8. [10]
20
VAB = 0
VBC = 0 VBC
VCA = 0
V7, V8
VCA VAB
The switching states of the inverter are represented by six active vectors V1 – V6 and two
where V AO (t), V BO (t) and V CO (t) are load phase voltages. So, if any of the two voltages are
given, the third one can be calculated by using the above equation [9].
The three phase voltages can be transformed into two phase variables in plane as follows:
VAO(t)
V(t) 1 1/2 1/2
= 2/3
V(t) VBO(t) (3.2)
0 √3/2 √3/2
VCO(t)
21
Instead of analyzing the whole system, one could analyze looking at each phase.
Any space vector can be expressed in terms of two phase voltages in plane as
2 j2∏ j4∏
𝑣̅ (t) = * VAO (t)*ej0 + VBO (t)*e 3
+ VCO (t) * e 3
(3.4)
3
The load voltages can be obtained by substituting eq (3.4) in to eq (3.3) and denoted by 𝑉̅ 2:
2
𝑉̅ 2 = *Vg* e j∏3 (3.5)
3
Similarly, all six vectors can be obtained by the following equation [9]:
2
𝑉̅ n = *Vg* e j(𝑛−1)∏ where n= 1 to 6 (3.6)
3 3
See Table 2.
22
𝑉̅ 0 PPP S1,S3,S5 𝑉̅ 0 = 0
𝑉̅ 7 NNN S4,S6,S2 𝑉̅ 7 = 0
𝑉̅ 1 PNN S1,S6,S2 2
𝑉̅ 1 = *Vg* e j0
3
𝑉̅ 2 PPN S1,S3,S2 2
𝑉̅ 2 = *Vg* e j∏3
3
𝑉̅ 3 NPN S4,S3,S2 2
𝑉̅ 3 = *Vg* e j2∏
3 3
𝑉̅ 4 NPP S4,S3,S5 2
𝑉̅ 4 = *Vg* e j3∏
3 3
𝑉̅ 5 NNP S4,S6,S5 2
𝑉̅ 5 = *Vg* e j4∏
3 3
𝑉̅ 6 PNP S1,S6,S5 2
𝑉̅ 6 = *Vg* e j5∏
3 3
The reference vector can be generated by zero-vector and the vector enclosing a sector.
The reference vector rotates at an angular velocity of 2∏ times the fundamental frequency of the
23
inverter output voltage. The inverter output voltage would have rotated one complete cycle if the
reference voltage completes one complete revolution thereby creating an AC waveform [11].
By rotating the reference vector ̅𝑉 s around space vector diagram, modulation can be
achieved. By adding all the vectors with in one switching period of Ts, modulation can be
achieved. Both maximum deviation of current for switching states and cycle time should be
small to achieve the required PWM [12]. Vector diagram for calculating duty cycles is as shown
in Figure 9.
𝑉̅ 2
𝑉̅ s
T2
θ
𝑉̅ 0 𝑉̅ 1
T1
Figure 9: Generating vector 𝑉̅ s from 𝑉̅ 1, 𝑉̅ 2 and 𝑉̅ 7
Duty cycle can be calculated from above. If the vector is in sector 1, the output reference
𝑇𝑠 𝑇1 𝑇1+𝑇2 𝑇𝑠
∫0 ̅
𝑉𝑠 dt = ∫
0
𝑉̅ 1dt+ ∫
𝑇1
𝑉̅ 2 dt+∫
𝑇1+𝑇2
𝑉̅0 dt (3.7)
For high switching frequencies, reference vector is constant during time Ts; vectors 𝑉̅ 1
𝑉̅s ̣∙ Ts = 𝑉̅ 1 ∙ T1 + 𝑉̅ 2 ∙ T2 (3.8)
cos θ 2 1 cos θ
𝑉̅ s * Ts * = T1* V * 2
+ T2* 3 Vg
sin θ 3 g (3.10)
0 sin θ
1
̅𝑉 s * Ts *cos θ = T1* 2 Vg + T2* 2 Vg *
3 3 2
√3
̅𝑉 s * Ts *sin θ = T2* 2 Vg * (3.11)
3 2
̅𝑠
√3∗𝑇𝑠∗ 𝑉
T1 = sin (60 – θ)
𝑉𝑔
̅𝑠
√3∗𝑇𝑠∗ 𝑉
T2 = sin (θ)
𝑉𝑔
T0 = Ts - T1 – T2 (3.12)
With the help of eq (3.12) we can calculate switching times in all the sectors and a
multiple of angle 600 has to be subtracted from the actual angle so that the resultant lies in the
first sector.
25
The switching times are calculated using Arduino UNO microcontroller using CORDIC
algorithm and these times are used to generate PWM signals which can drive the inverter and
Several variations in SVM can be obtained varying null vector from ̅𝑉0 to ̅𝑉 7 providing
vectors in the seven-segment switching sequence. One of such configuration is shown in the
Figure 10.
𝑉̅ 0 𝑉̅ 1 𝑉̅ 2 𝑉̅ 0 𝑉̅ 2 𝑉̅ 1 𝑉̅ 0
VAN
Vg
VTs0
VBN
Vg
0
VTs
VCN Vg
VTs0
T0 /4 T1 /2 T2 /2 T0 /2 T1/2 T2 /2 T0 /4
Ts
In the above sequence, all the duty cycles T0, T1 and T2 sum up to get the sampling period Ts. The
switching frequency will be equal to sampling frequency when the inverter switch turns on and
off per sampling frequency. Moving from one sector to other sector doesn’t need any switching
[9].
This sequence is used to generate the PWM signals (Table 3) and is fed as input to the
motor to make it run. In this thesis switching times are generated using Arduino microcontroller
CORDIC
calculating trigonometric functions, magnitude and phase using simple lookup tables, bit shifts,
addition and subtraction operations avoiding multiplications. CORDIC is also used to calculate
CORDIC revolves around the idea of rotation. Compared to other approaches, this is
highly recommended when hardware multiplier is unavailable like microcontroller. Table lookup
methods and power series are faster than CORDIC when hardware multiplier is available.
This algorithm is based on rotation of a vector in a plane (Figure11) [14]. Let 𝑅̅ be the
vector with initial points as (X0, Y0). If the vector is rotated by certain angle θ, let the new
endpoints after rotation be (Xn, Yn). The algebraic representation of the vector is expressed as:
The magnitude of the vector is assumed to be constant. So, the implementation of CORDIC
involves four multiplications and two addition operations and also evaluation of cos θ and sin θ.
The multiplications can be eliminated by restricting the angles of rotation to powers of 2 [11].
Yn (Xn, Yn)
(X0, Y0)
Y0
x
Xn X0
Angle ‘θ’ can be decomposed into smaller angles and the sum of all angles equals to the
original angle:
θ = ∑∞
𝑖=0 𝜃 i (4.2)
This makes sure that angle ‘θ’ will be equal to the product of all rotations by addition of all the
Now the angle θi is chosen as multiples of tan-1(2-i), so that angle tan θi becomes di * 2-i
(di takes values +1 or -1). This guarantees that multiplications can be done using shift operations.
The original angle is broken down to microangles and hence the relation:
Rotation (θ) = ∏∞
𝑖=0 𝑅𝑜𝑡𝑎𝑡𝑖𝑜𝑛 (θi) (4.6)
K = ∏∞
𝑖=0 𝐾 i (4.7)
K = ∏∞
𝑖=0 𝑐𝑜𝑠 (θi)
K = ∏∞
𝑖=0 𝑐𝑜𝑠 (tan (2 ))
-1 -i
(4.8)
K = 0.60725293………….
30
Even though the angular rotation is done with positive or negative sign, K value remains
constant.
Third variable is used to keep track of total rotation to store angle θ after the
decomposition:
Zn = Z0 – di* θi
The values of tan-1(2-i) are stored in the lookup table. The complete set of three equations is
given as follows:
where di = ±1.
Equation (4.10) requires addition operation, bit shift operations and look up table. The
general implementation of CORDIC is shown in Figure 12. The main aim is to eliminate Zn and
the direction of rotation determines the sign of di. At iteration ‘i’ (0< i <n-1) the coordinates
Initially vector is rotated by an angle ‘θ’. At each iteration, the direction of rotation is
Let the desired angle be 200, that is, Zi = 200. For each iteration if Zi > 00 subtract the
iteration angle from Zi or else if Zi < 00 add the current iteration angle to Zi and make appropriate
0 1 45 20 -45 25
Table 4 Continued.
Continued on following page 33
…… …… …… …… …… ……
19 …… …… 0.000195 …… ……
With the help of the above implementation, it took almost 20 iterations to calculate angle
of 200. Irrespective of direction of rotation, cos θi (cos θi = cos (-θi)) becomes constant. So as Zi
Zn = 0 (4.12)
X (0) and Y (0) are chosen as 1/K and 0 respectively to eliminate the K value. The final values
are:
Xn = cos θ
Yn = sin θ (4.13)
Using the Arduino we calculated the angles instead of using long lookup tables which
consume a lot of time and also memory. Usually around 15 iterations would give the accurate
34
results and is used for most of the applications. This algorithm is used to compute trigonometric
functions without the use of multiplications. The algorithm converges as long as the rotation
θmax = ∑∞
𝑖=0 𝑡𝑎𝑛 (2 ) = 99. 88 .
-1 -i 0
In this thesis CORDIC is used for computing sine waves for calculating switching pulse
The CORDIC algorithm is used to calculate the duty cycles of stationary vectors. Steps
1. Start
10. Stop
35
Start
Yes No
Zi >0
di = 1 di = -1
No
End of
iterations
Yes
Stop
CHAPTER-5
RESULTS
Using Arduino SVM is implemented using CORDIC algorithm. The results of using
In the Figure 14, CORDIC was implemented for angle 300. ‘x’ value gives the cosine
value and ‘y’ value gives the sine of angle 300. To justify the use of the CORDIC algorithm, a
comparison of the time used in calculating the sine with the CORDIC algorithm to the standard
sine function was done. To get an accurate measure of the calculation time, 27 iterations of the
CORDIC algorithm was completed. The time taken to complete one-iteration is 1.92 μs which is
less than traditional method of implementing sine wave. The time taken for implementing sine
wave for different angles using traditional method was 4.90 μs.
37
Figure 15 shows the pulse width modulated signals for sectors 1-6 which are operated at
a frequency of 50Hz.
38
CONCLUSION
Switching cycles of the inverter are generated and space vector modulation has been
implemented to calculate the switching times for different frequencies. The pulse width
modulated signals were generated using CORDIC algorithm and then fed as input to the motor.
CORDIC algorithm takes less time in computing the sine values compared to any other
methods which are used to generate sine values. It helps in reducing size of the lookup table and
The work can be extended to control motor speed using V/Hz method and, before
changing the speed of the motor, check its limits. Implementation of space vector modulation
using unbalanced condition (V AO (t) + V BO (t) + VCO (t) ≠ 0) can also be tried.
REFERENCES
[1] G.KohlRusz., and D.Fodor., 2011, “Comparison of Scalar and vector control strategies
265-270.
[2] J.M.D. Murphy., and F.G.Turnbull, 1998, “Power Electronic control of AC motors,”
[4] Ray Andarka., Feb 22-24, 1998, “A Survey of CORDIC algorithms for FPGA based
http://www.atmel.com/dyn/resources/prod_documents/doc8161.pdf
[6] Asma, Naik R.L., and Jangamshetti Suresh., 2012, “Implementation of Space Vector
[7] Park, R.H., “Two-Reaction Theory of Synchronous Machines,” AIEEE Trans. No.
[8] Kron, G., 1942, “The Application of Tensors to the Analysis of Rotating Electrical
[9] Bin Wu., 2006, “High Power Converters and AC Drives,” IEEE Press, John Wiley &
Sons.
[10] Notes for space vector modulation for three phase inverters is available [online] at
“http://scholar.lib.vt.edu/theses/available/etd-2798-1216/unrestricted/chap2.pdf
[11] Heinz Willi Van Der Broeck., Hans-Christoph skudelny., and Georg Viktor
Stanke., January /February 1988, “Analysis and Realization of Pulse width Modulator
No. 1.
[14] Milos D. Ercegovac., and Thomas Lang., 2004, “Digital Arithmetic Morgan
Kauffman Edition”.
APPENDIX
CODE LISTING
CORDIC IMPLEMENTATION:
long cordic_lookup [ ] =
{
0x20000000L,
0x12E4051EL,
0x09FB385BL,
0x051111D4L,
0x028B0D43L,
0x0145D7E1L,
0x00A2F61EL,
0x00517C55L,
0x0028BE53L,
0x00145F2FL,
0x000A2F98L,
0x000517CCL,
0x00028BE6L,
0x000145F3L,
0x0000A2FAL,
0x0000517DL,
0x000028BEL,
0x0000145FL,
0x00000A30L,
43
0x00000518L,
0x0000028CL,
0x00000146L,
0x000000A3L,
0x00000051L,
0x00000029L,
0x00000014L,
0x0000000AL,
0x00000005L
};
#define ITERS 27
void setup ()
{
Serial.begin (57600) ;
long elapsed = micros () ;
for (long i = 0 ; i < ITERS ; i++)
elapsed = micros () - elapsed ;
Serial.print ("time taken for ") ;
Serial.print (ITERS) ;
Serial.print (" iterations = ") ;
Serial.print (elapsed) ;
Serial.println ("us") ;
Serial.print (elapsed / ITERS) ;
Serial.println (" us/iter") ;
test_cordic (0x20000000L, true) ;
}
void test_cordic (long angle, boolean printres)
{
long xx = 607252935L ;
44
long yy = 0L ;
for (int i = 0 ; i <= 27 ; i++)
{
long zi = cordic_lookup [i] ;
long xnew = yy >> i ;
long ynew = -xx >> i ;
if (angle < 0L)
{
angle += zi ;
xx += xnew ;
yy += ynew ;
}
else
{
angle -= zi ;
xx -= xnew ;
yy -= ynew ;
}
}
if (!printres)
return ;
Serial.print ("angle=") ;
Serial.print ("30") ;
Serial.print (" end x = 0.") ;
Serial.print (xx) ;
Serial.print (" end y = 0.") ;
Serial.println (yy) ; }
void loop (){
}
45
long cordic_lookup [ ] =
{
0x20000000L,
0x12E4051EL,
0x09FB385BL,
0x051111D4L,
0x028B0D43L,
0x0145D7E1L,
0x00A2F61EL,
0x00517C55L,
0x0028BE53L,
0x00145F2FL,
0x000A2F98L,
0x000517CCL,
0x00028BE6L,
0x000145F3L,
0x0000A2FAL,
0x0000517DL,
0x000028BEL,
0x0000145FL,
0x00000A30L,
0x00000518L,
0x0000028CL,
0x00000146L,
0x000000A3L,
0x00000051L,
0x00000029L,
46
0x00000014L,
0x0000000AL,
0x00000005L
};
int ledPin1 = 9;
int ledPin2 = 10;
int ledPin3 = 3;
int val_a = 0;
int val_b = 0;
int val_c = 0;
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
void loop()
int dt;
{
int t1 =0;
47
int t2 = millis();
dt = t2-t1;
t1=t2;
return;
}
const float pi = 3.14;
float k= dt*0.1*pi+k;
int n;
float rad = 60 * pi/180.0;
if (k>rad);
{
k= k-rad;
n= n+1;
}
float Ts = 255;
float T1;
float T2;
float T0;
T1 = sqrt(3)*Ts*sin(rad*n-k);
T2 = sqrt(3)*Ts*sin(k-(n-1)*rad);
T0 = Ts-T1-T2;
val_a = T1+T2+(T0/2);
val_b = T2+(T0/2);
val_c = T0/2;
48
analogWrite(ledPin1, val_a);
analogWrite(ledPin2, val_b);
analogWrite(ledPin3, val_c);
}
//Calculation of sine values using CORDIC//
void test_cordic (long aa, boolean printres)
{
long xx = 607252935L ;
long yy = 0L ;
for (int i = 0 ; i <= 27 ; i++)
{
long zi = cordic_lookup [i] ;
long tx = yy >> i ;
long ty = -xx >> i ;
if (aa < 0L)
{
aa += zi ;
xx += tx ;
yy += ty ;
}
else
{
aa -= zi ;
xx -= tx ;
yy -= ty ;
}
}
if (!printres)
return ;
49
Serial.print ("angle=") ;
Serial.print (" end y = 0.") ;
Serial.println (yy) ;
}