Introduction & Stepper Motor
Introduction & Stepper Motor
Conduct the following experiments on an ARM CORTEX M3 evaluation board to learn ALP
and using evaluation version of Embedded 'C' & Keil uVision-4 tool/compiler.
PART-A:
PART-B:
EMetering
Lighting
Industrial networking
Alarm systems
White goods
Motor control
LPC1768 GPIO Programming
• LPC1768 is an ARM Cortex-M3 based MCU by Phillips/NXP and has plenty of General
Purpose Input Output pins to play with. The Name of Registers, Data structures that I have
used in this guide are defined in LPC17xx.h header file. LPC17xx.h header is based
on CMSIS(Cortex Microcontroller System Interface Standard) developed by ARM
• Most of the function oriented pins on LPC176x Microcontrollers are grouped into
Ports. LPC1768 has 5 ports viz. Port 0 to 4. The associated registers for each port
are grouped into a structure with the following naming convention : LPC_GPIOx ,
where x is the port number.
What is lpc1768?
The embedded NXP LPC1768 Microcontroller in particular is
designed for prototyping all sorts of devices, especially those
including Ethernet, USB, and the flexibility of lots of peripheral
interfaces and FLASH memory. It is packaged as a small DIP
form-factor for prototyping with through-hole PCBs, stripboard
and breadboard, and includes a built-in USB FLASH
programmer.
NXP (founded by Philips) LPC1768
• The NXP (founded by Philips) LPC1768 is an ARM 32-bit Cortex-M3
Microcontroller with MPU,
• CPU clock up to 100MHz,
• 512kB on-chip Flash ROM with enhanced Flash Memory Accelerator,
• In-System Programming (ISP) and In-Application Programming (IAP),
• 64kB RAM, Nested Vectored Interrupt Controller,
• Eight channel General purpose DMA controller,
• AHB Matrix,
LPC1768 has its GPIOs divided into five ports PORT0 - PORT4, although many of them are
not physically 32bit wide. Refer the data sheet for more info. The Below registers will be
used for Configuring and using the GPIOs registers for sending and receiving the Digital
signals.
A structure LPC_GPIOn(n= 0,1,2,3) contains all the registers for required for GPIO
operation. Refer lpc17xx.h file for more info on the registers.
PINSEL:GPIOPinsSelectRegister
Almost all the LPC1768 pins are multiplexed to support more than 1
function. Every GPIO pin has a minimum of one function and max
of four functions.
The required function can be selected by configuring the PINSEL
register.
As there can be up to 4 functions associated with a GPIO pin, two
bits for each pin are available to select the function. This implies
that we need two PINSEL registers to configure a PORT pins.
By this the first 16(P0.0-P0.16) pin functions of PORT0 can be
selected by 32 bits of PINSELO register.
The remaining 16 bits(P0.16-P0.32) are configured using 32bits
of PINSEL1 register.
As mentioned earlier every pin has max of four functions.
Below table shows how to select the function for a
particular pin using two bits of the PINSEL register.
Values Direction
0 Input
1 Output
FIOSET: Fast Port Output Set Register.
This register controls the state of output pins.
Writing 1s produces highs at the corresponding port pins.
Writing 0s has no effect.
Reading this register returns the current contents
of the port output register not the physical port value.
Values FIOSET
0 No Effect
Values FIOCLR
0 No Effect
This register is used for both reading and writing data from/to the
PORT.
Output: Writing to this register places corresponding values in all
bits of the particular PORT pins.
Input: The current state of digital port pins can be read from this
register, regardless of pin direction or alternate function selection
(as long as pins are not configured as an input to ADC).
1) FIODIR : This is the GPIO direction control register. Setting a bit to 0 in this register will
configure the corresponding pin to be used as an Input while setting it to 1 will configure it as
Output.
2) FIOMASK : This gives masking mechanism for any pin i.e. it is used for Pin access control.
Setting a bit to 0 means that the corresponding pin will be affected by changes to other
registers like FIOPIN, FIOSET, FIOCLR. Writing a 1 means that the corresponding pin won’t be
affected by other registers.
3) FIOPIN : This register can be used to Read or Write values directly to the pins. Regardless of
the direction set for the particular pins it gives the current start of the GPIO pin when read.
4) FIOSET : It is used to drive an ‘output’ configured pin to Logic 1 i.e HIGH. Writing Zero does
NOT have any effect and hence it cannot be used to drive a pin to Logic 0 i.e LOW. For driving
pins LOW FIOCLR is used which is explained below.
5) FIOCLR : It is used to drive an ‘output’ configured pin to Logic 0 i.e LOW. Writing Zero does
NOT have any effect and hence it cannot be used to drive a pin to Logic 1.
• Most of the PINS of LPC176x MCU are Multiplexed i.e. these pins can be configured to
provide up to 4 different functions.
• By default, after Power-On or Reset : all pins of all ports are set as GPIO so we can
directly use them when learning GPIO usage. The different functions that any
particular pin provides can be selected by setting appropriate value in
the PINSEL register for the corresponding pin.
• Each pin on any port has 2 corresponding bits in PINSEL register. The first 16 pins (0-
15) on a given port will have a corresponding 32 bit PINSEL register and the rest 16
bits will have another register.
• For example bits 0 & 1 in PINSEL0 are used to select function for Pin 1 of Port 0, bits 2
& 3 in PINSEL0 are used to select function for PIN 2 of port 0 and so on.
What is Keil IDE software?
1. µvision IDE v4
2. Compiler for ARM (armcc)
3. MicroLib (C library)
4. Assembler for ARM (armasm)
5. Linker For ARM (armLink)
Keil also provide JTAG debugger and Evaluation Board. Keil JTAG debugger name is
ULINK. This is used to connect development board with host machine via USB.
KEIL also provide Real Time RTOS kernel. Which is royalty free kernel.
This kernel is designed for ARM and cortex-M CPUs.
Basics of Embedded C Program and Programming
Structure for Beginners
• Embedded C Programming is the soul of the processor functioning inside each and
every embedded system we come across in our daily life, such as mobile phone, washing
machine, and digital camera.
• Each processor is associated with embedded software. The first and foremost thing is the
embedded software that decides functioning of the embedded system. Embedded C
language is most frequently used to program the microcontroller.
Salient features of the language
• C language is a software designed with different keywords, data types, variables,
constants, etc.
• Embedded C is a generic term given to a programming language written in C, which is
associated with a particular hardware architecture.
• Embedded C is an extension to the C language with some additional header files. These
header files may change from controller to controller.
• The microcontroller 8051 #include<reg51.h> is used.
• The embedded system designers must know about the hardware architecture to write
programs. These programs play prominent role in monitoring and controlling external
devices. They also directly operate and use the internal architecture of the
microcontroller, such as interrupt handling, timers, serial communication and other
available features.
Differences between C and Embedded C
Variable Types and Sizes
Type Size(Bits) Range
bit 1 0,1
char 8 -128 to 127
unsigned char 8 0 to 255
int 16 -32768 to 32767
short int 16 -32768 to 32767
unsigned int 16 0 to 65535
signed int 16 -32768 to 32767
long int 32
unsigned long int 32
signed long int 32
float 32 +-1.175e-38 to +-
3.4e38
double 32 +-1.175e-38 to +-
3.4e38
27
Constants
Numerical Constants
decimal 1234
binary 0b10101011
hexadecimal 0xff
octal 0777
Character Constants
character representation Equivalent Hex Value
Bitwise Operators
Ones complement ~
Left Shift <<
Right Shift >>
AND &
Exclusive OR ^
OR |
29
Bitwise Operations
Given an unsigned char y = 0xC9
operation result
x = ~y x = 0x36
x = y <<3 x = 0x48
x = y>>4 x = 0x0C
x = y&0x3F x = 0x09
x = y^1 x = 0xC8
x = y | 0x10 x = 0xD9
30
Logical Operators
Logical operator
AND &&
OR ||
x =5 and y =2
(x && y) is true, because both are non-zero
(x & y) is false, because 00000101 bitwise AND 00000010 equal to zero
31
Division
Beware division:
• If second argument is integer, the
result will be integer (rounded):
5 / 10 0 whereas 5 / 10.0 0.5
• Division by 0 will cause a problem
32
Relational Operators
Relational Operators
Is Equal to ==
Is Not equal to !=
Less Than <
Less Than or Equal to <=
Greater than >
Greater Than or equal to >=
x = 3 and y =5
(x == y) FALSE
(x != y) TRUE
(x < y) TRUE
(x<=y) TRUE
(x>y) FALSE
(x >= y) FALSE 33
Data format
Conversion specifier Description
34
Assignment Operators
x = y assign y to x x += y assign (x+y) to x
int y; int y;
y = ++x; y = x++;
/* x == 6, y == 6 */ /* x == 6, y == 5 */
35
Program -1
int main( )
{
unsigned int a[ ] = {3,6,12,9};
int i,j;
LPC_GPIO2->FIODIR = 15;
while(1)
{
for(i=0;i<50;i++)
for (j=0;j<=3;j++)
{
LPC_GPIO2->FIOPIN = a[j];
delay(1);
}
for(i=0;i<50;i++)
for (j=3;j>=0;j--)
{
LPC_GPIO2->FIOPIN = a[j];
delay(1);
}
}
}
CIRCUIT DIAGRAM OF STEPPER MOTOR
Stepper motor:Stepper motors are the motors that move in discrete steps or convert
electrical pulses into rotatory motion. They have multiple coils(4coils) that are organized in
groups called "phases"(stators named as A,B,C and D). By energizing each phase in
sequence, the motor will rotate, one step at a time.
How a stepper motor works?
1. Stepper motors work on the principle of electromagnetism.
2. There is a soft iron or magnetic rotor shaft surrounded by the electromagnetic
stators.
3. The rotor and stator have poles which may be teethed or not depending upon
the type of stepper.
4. When the stators are energized the rotor moves to align itself along with the
stator (in case of a permanent magnet type stepper) or moves to have a
minimum gap with the stator (in case of a variable reluctance stepper). This
way the stators are energized in a sequence to rotate the stepper motor.
How Stepper Motors Work?
Stepper motors consist of a permanent magnetic rotating shaft, called the rotor, and
electromagnets on the stationary portion that surrounds the motor, called the stator.
Figure 1 illustrates one complete rotation of a stepper motor.
At position 1, we can see that the rotor is beginning at the upper electromagnet, which is
currently active (has voltage applied to it). To move the rotor clockwise (CW), the upper
electromagnet is deactivated and the right electromagnet is activated, causing the rotor to
move 90 degrees CW, aligning itself with the active magnet. This process is repeated in
the same manner at the south and west electromagnets until we once again reach the
starting position.
In the above example, we used a motor with a resolution of 90 degrees or
demonstration purposes. In reality, this would not be a very practical motor for
most applications.
The average stepper motor's resolution -- the amount of degrees rotated per
pulse -- is much higher thanthis.
For example, a motor with a resolution of 1.8 degrees would move its rotor 1.8
degrees per step, thereby requiring 200 pulses (steps) to complete a full 360
degree rotation.
Here we are using 200 pole stepper motor hence it gives 360degree/200
pole=1.8 degree per step.
So for example if we need 120 degree rotation then we have to apply
approximately 67 pulses to complete 120 degree rotation
120/1.8=66.66==67 steps approximately.
Here one cycle means 4 steps. So if we need 90 degree rotation then 90/1.8=50
steps.
Here one cycle means 4 steps. So 50/4=12.5 =~ 13. So we need 13 cycles to
rotate 90 degree.
If we want to run 180 degree then 180/1.8=100. So 100/4=25 cycles would
make a stepper motor to rotate 180 degree.