0% found this document useful (0 votes)
2 views21 pages

8051 Interface

The document provides an overview of stepper motors and their interface with the 8051 microcontroller, detailing their operation, programming for rotation in both clockwise and anti-clockwise directions, and calculations for steps per revolution. It also discusses the DAC0808, an 8-bit Digital-to-Analog Converter, including its interfacing with the 8051, output voltage range, and various programming examples for generating waveforms. Additionally, it covers sine wave generation and LCD display initialization, emphasizing the practical applications of these components in digital signal processing and control systems.

Uploaded by

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

8051 Interface

The document provides an overview of stepper motors and their interface with the 8051 microcontroller, detailing their operation, programming for rotation in both clockwise and anti-clockwise directions, and calculations for steps per revolution. It also discusses the DAC0808, an 8-bit Digital-to-Analog Converter, including its interfacing with the 8051, output voltage range, and various programming examples for generating waveforms. Additionally, it covers sine wave generation and LCD display initialization, emphasizing the practical applications of these components in digital signal processing and control systems.

Uploaded by

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

ESD-I 2025

STEPPER MOTOR

The stepper motor interface to 8051 is shown in figure 1. A Stepper motor


translates electrical pulses into mechanical movement. A conventional motor shaft runs
freely, whereas the stepper motor shaft moves in a fixed increment and hence the shaft
position can be controlled precisely. Stepper motors are used for position control
applications such as dot matrix printers, disk drives, robotics, etc.

The stepper motor consists of a permanent magnet rotor (shaft) surrounded by a


stator as shown in figure 2. Generally the stator has 4 windings that are paired with a
center-tapped common as shown in figure 3. The center tap allows a change of current
direction in each of the two coils, hence changing the direction of polarity in the stator
poles which intern leads to a change in the direction of rotor rotation.

Stepper motor interface to 8051:

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 1


ESD-I 2025
Permanent magnet stepper motor Stator winding configuration

Normal 4-step sequence

Number of teeth in a rotor = 50


The no. of steps for one complete revolution = 4 steps * 50 rotor teeth
= 200 steps/revolution
Therefore
Step angle = 3600 / 200 steps/revolution
= 1.80 per step
For 4-step sequence rotation angle is = 1.80 * 4 = 7.20

For 900 rotations, the no. of times the sequence must be repeated is
X = 900 / 7.20 = 12.5 ≈ 12d
For 1800 rotations, the no. of times the sequence must be repeated is
X = 1800 / 7.20 = 25d

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 2


ESD-I 2025
PROGRAMS:

1. Write a program to rotate the stepper motor in Clockwise direction continuously


#include <reg51.h>
void delay(void);
void main(void)
{
while(1)
{
P2=0x07; // output 0x07 to port P2
delay(); // generate delay

P2=0x0b; // output 0x0b to port P2


delay(); // generate delay

P2=0x0d; // output 0x0d to port P2


delay(); // generate delay
P2=0x0e; // output 0x0e to port P2
delay(); // generate delay
}
}

// function to generate delay


void delay(void)
{
unsigned int i;
for(i=0;i<=30000;i++);
}

2. Write a program to rotate the stepper motor 900 clockwise direction

#include <reg51.h>
void delay(void);
void main(void)
{
unsigned int k = 0;
while(1)
{
if(k < 12) // (1.8 * 4) * (count ) = 900
{
P2=0x07; // output 0x07 to port P2
delay(); // generate delay

P2=0x0b; // output 0x0b to port P2


delay(); // generate delay

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 3


ESD-I 2025
P2=0x0d; // output 0x0d to port P2
delay(); // generate delay

P2=0x0e; // output 0x0e to port P2


delay(); // generate delay

k++;
}
}
}

// function to generate delay


void delay(void)
{
unsigned int i;
for(i=0;i<=30000;i++);
}

For 900 rotations, the no. of times the sequence must be repeated is
X = 900 / 7.20 = 12.5 ≈ 12d

3. Write a program to rotate the stepper motor in Anti Clockwise direction


continuously
#include <reg51.h>
void delay(void);
void main(void)
{
while(1)
{
P2=0x0e; // output 0x0e to port P2
delay(); // generate delay

P2=0x0d; // output 0x0d to port P2


delay(); // generate delay

P2=0x0b; // output 0x0b to port P2


delay(); // generate delay

P2=0x07; // output 0x07 to port P2


delay(); // generate delay
}
}

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 4


ESD-I 2025
// function to generate delay

void delay(void)
{
unsigned int i;
for(i=0;i<=30000;i++);
}

4. Write a program to rotate the stepper motor 1800 Anti Clockwise direction

#include <reg51.h>
void delay(void);
void main(void)
{
unsigned int k = 0;
while(1)
{
if(k < 25) // (1.8 * 4) * (count ) = 1800
{
P2=0x0e; // output 0x0e to port P2
delay(); // generate delay

P2=0x0d; // output 0x0d to port P2


delay(); // generate delay

P2=0x0b; // output 0x0b to port P2


delay(); // generate delay

P2=0x07; // output 0x07 to port P2


delay(); // generate delay

k++;
}
}
}

// function to generate delay


void delay(void)
{
unsigned int i;
for(i=0;i<=30000;i++);
}

For 1800 rotations, the no. of times the sequence must be repeated is
X = 1800 / 7.20 = 25d

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 5


ESD-I 2025

DAC
The DAC0808 is an 8-bit Digital-to-Analog Converter (DAC) that converts digital
binary values into corresponding analog voltage or current. It's widely used in digital
signal processing, instrumentation, and control systems where digital outputs need to be
converted into analog signals.

DAC INTERFACE TO 8051


The DAC is an interface unit that can translate digital information into
corresponding analog signal. It accepts a digital or binary word as its input and output an
analog voltage or current.
The commonly used 8 – bit DAC is DAC0808. In the lab we used DUAL DAC
module interfaced to P0 and P1 of 8051 microcontroller.
DAC0808 output voltage range is, 0 (0x00) – 10(0xff) volts

DAC interface to 8051


Resolution of DAC = Vmax / 2n = 10v / 28 = 39.06mV

For a given output voltage the input to the DAC should be equal to,
(D7-D0) BCD = Vout / resolution of DAC.

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 6


ESD-I 2025
1. Write a program to generate rising saw tooth waveform

#include <reg51.h>
unsigned char temp = 0x00;
void main ()
{
while(1)
{
P0 = temp; // output count to Port 0
temp++; // increment count

}
}

2. Write a program to generate 4v rising saw tooth waveform

(D7-D0) BCD = Vout / resolution of DAC = 4v / 39.06mv


= 102d or 66h

#include <reg51.h>
unsigned char i = 0x00;
void main ()
{
while(1)
{
for(i=0; i<= 0x66; i++)
P0 = i; // output count to Port 0
}
}

3. Write a program to generate falling saw tooth waveform

#include <reg51.h>
unsigned char temp = 0xff;
void main ()
{
while(1)
{
P0 = temp; // output count to Port 0
temp--; // increment count

}
}

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 7


ESD-I 2025
4. Write a program to generate 8v falling saw tooth waveform

(D7-D0) BCD = Vout / resolution of DAC = 8v / 39.06mv


= 205d or CDh

#include <reg51.h>
unsigned char i = 0xcd;
void main ()
{
while(1)
{
for(i=0xcd; i>0 ; i--)
P0 = i; // output count to Port 0
}
}

5. Write a program to generate triangular waveform

#include <reg51.h>
unsigned char i, j;
void main ()
{
while(1)
{
for(i=0; i<= 0xff; i++)
P0 = i; // output count to Port 0
for(j=0; j<8; j++);
for(i=0xff; i<= 0; i--)
P0 = i; // output count to Port 0
for(j=0; j<8; j++);
}
}

6. Write a program to generate staircase waveform with a step size of 2v

(D7-D0) BCD = Vout / resolution of DAC = 2v / 39.06mv


= 51d or 33h

#include <reg51.h>
void delay(unsigned int);
void main(void)
{
while(1)
{
P0=0x00;
delay(120); // generate 2v step delay

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 8


ESD-I 2025
P0=0x33;
delay(120); // generate 2v step delay
P0=0x66;
delay(120); // generate 2v step delay

P0=0x99;
delay(120); // generate 2v step delay

P0=0xcc;
delay(120); // generate 2v step delay

P0=0xff;
delay(120); // generate 2v step delay
}
}

// function to generate delay


void delay(unsigned int step)
{
unsigned int i;
for(i=0; i<=step; i++);
}

7. Write a program to generate square waveform with 50% duty cycle

#include <reg51.h>
void delay(unsigned int);
void main ()
{
while(1)
{
P0 = 0x0;
delay(80);
P0 = 0xff;
delay(80);
}
}

// function to generate delay


void delay(unsigned int value)
{
unsigned int i;
for(i=0; i<=value; i++);
}

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 9


ESD-I 2025

8. Write a program to generate square waveform with 75% duty cycle

#include <reg51.h>
void delay(unsigned int);
void main ()
{
while(1)
{
P0 = 0x0;
delay(100);
P0 = 0xff;
delay(300);
}
}

// function to generate delay


void delay(unsigned int value)
{
unsigned int i;
for(i=0; i<=value; i++);
}

9. Write a C program to generate square waveform of frequency f = 1KHz. Use


timer1 in mode1.

#include <reg51.h>
void main ()
{
TMOD = 0X10;
while(1)
{
TH1 = 0XFE;
TL1 = 0X33;
TR1 = 1;
while(TF1 == 0);
P0 = ~P0;
TR1 = 0;
TF1 = 0;
}
}

Note: Do not select 6clk/cycle option in flash magic

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 10


ESD-I 2025

Sinewave Generation:
Generally a sine wave starts at 0v with positive half (00 – 1800) and a negative half (1800
– 3600) with +Vm as positive peak and –Vm as negative peak. The value of sine wave at
any point V(t) = Vmsinθ.
The DAC interfacing to 8051 setup produces an output voltage Vout in the range of 0 –
10v, no negative values. Hence the range 0 – 10v is divided into 2 portions.
0 – 5v as negative half cycle
5 – 10v as positive half cycle.
Therefore,
V(t) = Vout = 5 + 5sinθ

Waveforms:

A look-up table is used in his example to generate a sine wave. The values in the table
represent the magnitude of the sine of angle between 00 and 3600.

Angle, θ sinθ Vout = 5 + 5sinθ DAC input =


in degree’s Vout/39.06mV
0 0 5 128
10
20
30
40 0.643 8.214 210
50
60 0.866 9.33 238
70

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 11


ESD-I 2025
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
280
290
300
310
320
330
340
350
360

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 12


ESD-I 2025
1. Write a Program to generate sine waveform

#include <reg51.h>
unsigned char sine_tab [ ]=
{ 128, 150, 171, 192, 210, 226, 238, 248, 254, 255, 254, 248, 238, 226, 210,
171, 150, 128, 105, 84, 64, 45, 30, 17, 8, 2, 0, 2, 8, 17, 30, 45, 64, 84, 105,
128
};

unsigned int count;


void main (void)
{
while(1)
{
for(count=0; count<=36; count++)
P0 = sine_tab[count];
}
}

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 13


ESD-I 2025

LCD Display:
LCD displays are DOT MATRIX displays where display elements in the form of
dots are arranged as rows and columns (5x7 matrixes). By switching ON some dots and
OFF some dots, any character can be formed. Inside the LCD there is a ROM which
stored the required dot matrix code for corresponding ASCII input.

To display data on LCD the following steps are executed.

1. Initialize the LCD with set of command words. The command words are sent on the
D0 – D7 data lines with RS = 0, R/W = 0 and a HIGH to LOW pulse on the enable ‘E’
pin of LCD.
2. The ASCII value of the character to be displayed is sent on the D0-D7 data lines with
RS = 1 (data register), R/W = 0 and a HIGH to LOW pulse on the ‘E’ pin of LCD.

Interfacing of LCD to 8051

LCD pin functions

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 14


ESD-I 2025

LCD command words

1. Write a Program to Display the given Message on LCD Screen

#include <reg51.h>

sfr ldata = 0x80;


sbit rs=P2^4;
sbit rw=P2^5;
sbit en=P2^6;
void lcddata(unsigned char value);
void lcdcmd(unsigned char value);
void MSDelay( unsigned int itime);

void main()
{
lcdcmd(0x38); // set function 8 bits with 5 X 7 matrix
MSDelay(250);
lcdcmd(0x0e); // display on with cursor not blinking
MSDelay(250);
lcdcmd(0x01); // clear display
MSDelay(250);
lcdcmd(0x06); // shift cursor right upon display at location
MSDelay(250);
lcdcmd(0x80); // set cursor at leftmost postion in lcd
MSDelay(250);

lcddata('G'); // output 'G'


MSDelay(250);

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 15


ESD-I 2025
lcddata('M'); // output 'M'
MSDelay(250);
lcddata('I'); // output 'I'
MSDelay(250);
lcddata('T); // output 'T'
MSDelay(250);

lcdcmd(0xC0); // move cursor to 9th postion addres 0xc0


MSDelay(250);
lcddata('E'); //output 'E'
MSDelay(250);
lcddata('C'); //output 'C'
MSDelay(250);
here: goto here;
}

void lcdcmd(unsigned char value)


{
ldata = value;
rs=0;
rw=0;
en=1;
MSDelay(1);
en=0;
return;
}

void lcddata(unsigned char value)


{
ldata = value;
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
return;
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for(i=0; i<itime; i++)
for(j=0; j<1275; j++);
}

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 16


ESD-I 2025

LCD Key Pressed:

At the lowest level keyboards are organized in a matrix of rows and columns as
shown in figure. The rows are connected to an output port and the columns are connected
to an input port. If no key has been pressed, reading the input port will yield 1’s for all
columns since they are all connected to high (VCC). If all rows are grounded and a key is
pressed, one of the columns will have 0 since the key pressed provides the path to ground.
It is the function of microcontroller to scan the keyboard continuously to detect and
identify the key pressed.

The 4x4 keypad interface to 8051

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 17


ESD-I 2025
Flowchart:

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 18


ESD-I 2025
2. Write a Program to Display the value of the key pressed

#include <reg51.h>

void lcd_init(void);
void clr_disp(void);
void lcd_com(unsigned char);
void lcd_data(unsigned char);

void display(unsigned char);


void MSDelay(unsigned char);

sbit rs = P2^4;
sbit rw = P2^5;
sbit en = P2^6;

sfr COL = 0xa0; //P2


sfr ROW = 0x90; //P1
sfr LCD = 0x80; //P0

code unsigned char keypad[4][4] = {'0', '1', '2', '3',


'4', '5', '6', '7',
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F'};
idata unsigned char rowloc, colloc;

void main(void)
{
lcd_init();
MSDelay(5);
COL = 0x0f;
while(1)
{
do
{
ROW = 0x00;
colloc = COL;
colloc &= 0x0f;
}while(colloc != 0x0f);

do
{
do
{
MSDelay(5);
colloc = COL;
colloc &= 0x0f;
}while(colloc == 0x0f);
MSDelay(5);

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 19


ESD-I 2025
colloc = COL;
colloc &= 0x0f;
}while(colloc == 0x0f);

while(1)
{
ROW = 0xfe;
colloc = COL;
colloc &= 0x0f;
if(colloc != 0x0f)
{
rowloc = 0;
break;
}
ROW = 0xfd;
colloc = COL;
colloc &= 0x0f;
if(colloc != 0x0f)
{
rowloc = 1;
break;
}
ROW = 0xfb;
colloc = COL;
colloc &= 0x0f;
if(colloc != 0x0f)
{
rowloc = 2;
break;
}
ROW = 0xf7;
colloc = COL;
colloc &= 0x0f;
rowloc = 3;
break;
}

//check column and send result to lcd


if(colloc == 0x0e)
display(keypad[rowloc][0]);
else if(colloc == 0x0d)
display(keypad[rowloc][1]);
else if(colloc == 0x0b)
display(keypad[rowloc][2]);
else
display(keypad[rowloc][3]);
}
}

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 20


ESD-I 2025
void lcd_init(void)
{
lcd_com(0x38); // initialize LCD 2 lines 5x7 matrix
MSDelay(5);
lcd_com(0x0f); // Display ON Cursor ON
MSDelay(5);
lcd_com(0x06); // Shift cursor right
MSDelay(5);
clr_disp();
}

void clr_disp(void)
{
lcd_com(0x01);
MSDelay(5);
}

void display(unsigned char result)


{
lcd_com(0x80);
MSDelay(5);
lcd_data(result);
MSDelay(5);
}

void lcd_com(unsigned char temp)


{
LCD = temp;
rs = 0;
rw = 0;
en = 1;
MSDelay(5);
en = 0;
}

void lcd_data(unsigned char temp)


{
LCD = temp;
rs = 1;
rw = 0;
en = 1;
MSDelay(5);
en = 0;
}
void MSDelay(char r)
{
int r1;
for(r1 = 0; r1 > r; r1++);
}

Harisha G C, Assistant Professor, GMU, DVG, harisha0710@gmail.com 21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy