8051 Interface
8051 Interface
STEPPER MOTOR
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
#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
k++;
}
}
}
For 900 rotations, the no. of times the sequence must be repeated is
X = 900 / 7.20 = 12.5 ≈ 12d
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
k++;
}
}
}
For 1800 rotations, the no. of times the sequence must be repeated is
X = 1800 / 7.20 = 25d
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.
For a given output voltage the input to the DAC should be equal to,
(D7-D0) BCD = Vout / resolution of DAC.
#include <reg51.h>
unsigned char temp = 0x00;
void main ()
{
while(1)
{
P0 = temp; // output count to Port 0
temp++; // increment count
}
}
#include <reg51.h>
unsigned char i = 0x00;
void main ()
{
while(1)
{
for(i=0; i<= 0x66; i++)
P0 = i; // output count to Port 0
}
}
#include <reg51.h>
unsigned char temp = 0xff;
void main ()
{
while(1)
{
P0 = temp; // output count to Port 0
temp--; // increment count
}
}
#include <reg51.h>
unsigned char i = 0xcd;
void main ()
{
while(1)
{
for(i=0xcd; i>0 ; i--)
P0 = i; // output count to Port 0
}
}
#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++);
}
}
#include <reg51.h>
void delay(unsigned int);
void main(void)
{
while(1)
{
P0=0x00;
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
}
}
#include <reg51.h>
void delay(unsigned int);
void main ()
{
while(1)
{
P0 = 0x0;
delay(80);
P0 = 0xff;
delay(80);
}
}
#include <reg51.h>
void delay(unsigned int);
void main ()
{
while(1)
{
P0 = 0x0;
delay(100);
P0 = 0xff;
delay(300);
}
}
#include <reg51.h>
void main ()
{
TMOD = 0X10;
while(1)
{
TH1 = 0XFE;
TL1 = 0X33;
TR1 = 1;
while(TF1 == 0);
P0 = ~P0;
TR1 = 0;
TF1 = 0;
}
}
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.
#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
};
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.
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.
#include <reg51.h>
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);
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.
#include <reg51.h>
void lcd_init(void);
void clr_disp(void);
void lcd_com(unsigned char);
void lcd_data(unsigned char);
sbit rs = P2^4;
sbit rw = P2^5;
sbit en = P2^6;
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);
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;
}
void clr_disp(void)
{
lcd_com(0x01);
MSDelay(5);
}