Interfacing Programs
Interfacing Programs
AIM: Write a C code to interface stepper motor and rotate it in clockwise and
anti-clockwise direction.
Theory:
A stepper motor is an electromechanical device it converts electrical power into mechanical
power. Also, it is a brushless, synchronous electric motor that can divide a full rotation into an
expansive number of steps. The motor’s position can be controlled accurately without any
feedback mechanism, as long as the motor is carefully sized to the application. Stepper motors are
similar to switched reluctance motors. The stepper motor uses the theory of operation for magnets
to make the motor shaft turn a precise distance when a pulse of electricity is provided.
The construction of a stepper motor is fairly related to a DC motor. It includes a permanent
magnet like Rotor which is in the middle & it will turn once force acts on it. This rotor is enclosed
through a no. of the stator which is wound through a magnetic coil all over it. The stator is
arranged near to rotor so that magnetic fields within the stators can control the movement of the
rotor.
The construction of a stepper motor is fairly related to a DC motor. It includes a permanent
magnet like Rotor which is in the middle & it will turn once force acts on it. This rotor is enclosed
through a no. of the stator which is wound through a magnetic coil all over it. The stator is
arranged near to rotor so that magnetic fields within the stators can control the movement of the
rotor.
The stepper motor working principle is Electro-Magnetism. It includes a rotor which is made
with a permanent magnet whereas a stator is with electromagnets. Once the supply is provided to
the winding of the stator then the magnetic field will be developed within the stator. Now rotor in
the motor will start to move with the rotating magnetic field of the stator.
Driving Techniques
Stepper motor driving techniques can be possible with some special circuits due to their complex
design. There are several methods to drive this motor, some of them are discussed below by
taking an example of a four-phase stepper motor.
Single Excitation Mode
The basic method of driving a stepper motor is a single excitation mode. It is an old method and
not used much at present but one has to know about this technique. In this technique every phase
otherwise stator next to each other will be triggered one by one alternatively with a special circuit.
This will magnetize & demagnetize the stator to move the rotor forward.
Full Step Drive
In this technique, two stators are activated at a time instead of one in a very less time period. This
technique results in high torque & allows the motor to drive the high load.
Half Step Drive
This technique is fairly related to the Full step drive because the two stators will be arranged next
to each other so that it will be activated first whereas the third one will be activated after that. This
kind of cycle for switching two stators first &after that third stator will drive the motor. This
technique will result in improved resolution of the stepper motor while decreasing the torque.
Micro Stepping
This technique is most frequently used due to its accuracy. The variable step current will supply
by the stepper motor driver circuit toward stator coils within the form of a sinusoidal waveform.
The accuracy of every step can be enhanced by this small step current. This technique is
extensively used because it provides high accuracy as well as decreases operating noise to a large
extent.
Interfacing Diagram:
Note:
Rotor has 50 teeth and stator has 48 teeth.
Step angle= ¼(angular pitch)
Angular pitch= 3600/50 teeth
Imp: Step angle= ¼(360/50)= 1.8 degree.
Program
Note: The text in blue colour indicates the comment
#include <LPC17xx.H>
void clock_wise(void); // declaration of function to turn stepper clockwise
void anti_clock_wise(void); // declaration of function to turn stepper anti-clockwise
unsigned long int var1;
unsigned int i=0,j=0,k=0;
int main(void)
{
LPC_PINCON->PINSEL4 = 0x00000000; //P2.0 to P2.3 as GPIO, this line can be replaced with
LPC_PINCON->PINSEL4 & = 0xFFFFFF00
LPC_GPIO2->FIODIR| = 0x0000000F; //P2.0 to P2.3 as OUTPUT pin
void anti_clock_wise(void)
{
var1=0x00000008; // lower nibble is 1000 at the initial stage energising coil D first
for(i=0;i<=3;i++) //coil has to energise in the order of DCBA, So for-loop 4 times
{
LPC_GPIO2->FIOCLR = 0x0000000F; // lower nibble is 0000 EVERYTIME before energising
coil DCBA
LPC_GPIO2->FIOSET = var1; //1000, 0100, 0010, 0001 is sent in the lower nibble of port 2 to
energise coil DCBA in order
var1=var1>>1; // RIGHT shift by 1 bit to energise next coil
for(k=0;k<15000;k++); // delay for hardware to move a step size of 1.8 degree anti-clockwise
} // end of for loop
} //end of anti-clockwise function
AIM: Write a C code to interface DAC and generate Triangular and Square Wave.
Interfacing Diagram:
Note:
• n-bit DAC that generates a maximum output voltage of Vref volts has a step size or
resolution = (Vref / 2 n )
• 8-bit DAC that generates a maximum output voltage of 5 volts has a step size or resolution
=(5V / 2 8 ) = 19.5 mV.
Triangle Wave
#include <LPC17xx.H>
int main()
{
unsigned long int temp=0x00000000;
unsigned int i=0;
LPC_PINCON->PINSEL0 &= 0xFF0000FF; //P0.4 to P0.11 as GPIO
LPC_GPIO0->FIODIR |= 0x00000FF0; //P0.4 to P0.11 as output pin
while(1) // forever loop
{
for(i=0;i!=0xFE;i++) //Perform codes inside the for loop from 0 to FE h with inc by 1
For ramp with increasing slope to get from 0V upto 5 V
{
temp=i; // copy i to temp variable
temp = temp << 4; // shift left 4 times to ensure the 8 bits appear in P0.4 to P0.11
LPC_GPIO0->FIOPIN =temp; // content of temp is put into port 0, IMP: We should not
use FIOSET in this case
}
for(i=0xFF;i!=0;i--) //Perform codes inside the for loop from FF h to 0 with Dec by 1
For ramp with decreasing slope to get from 5V to 0 V
{
temp=i; // copy i to temp variable
temp = temp << 4; // shift left 4 times to ensure the 8 bits appear in P0.4 to P0.11
LPC_GPIO0->FIOPIN =temp; // content of temp is put into port 0, IMP: We should not
use FIOSET in this case
}
} // end of while loop
} //end of main