Bapatla Engineering College Dept. of Ece
Bapatla Engineering College Dept. of Ece
DEPT. OF ECE.
-------------------------------8051 microcontroller programs---------------------------------------------7).Write a program to write the data onto Ports and Read the data from Ports.
C1
30pF
U1 X1
CRYSTAL 18 XTAL2 19 XTAL1 P0.0/AD0 P0.1/AD1 P0.2/AD2 P0.3/AD3 P0.4/AD4 P0.5/AD5 P0.6/AD6 P0.7/AD7 P2.0/A8 P2.1/A9 P2.2/A10 P2.3/A11 P2.4/A12 P2.5/A13 P2.6/A14 P2.7/A15 P3.0/RXD P3.1/TXD P3.2/INT0 P3.3/INT1 P3.4/T0 P3.5/T1 P3.6/WR P3.7/RD 39 38 37 36 35 34 33 32 21 22 23 24 25 26 27 28 10 11 12 13 14 15 16 17
R2
330
D1
LED-RED
C2
30pF
R3
330
D3
LED-RED
GND
RST
R4
330
D4
LED-RED
VCC
R12 C3
10uF 10k
29 30 31
PSEN ALE EA
R5
330
D5
LED-RED
R1
1k
1 2 3 4 5 6 7 8
R16
330
D6
LED-RED
R7
330
D7
LED-RED D8
R8
330
LED-RED
Resistors R1=1k ohm, R12=10k ohm, R2, R3, R4, R5, R16, R7, R8=330 ohm Capacitors C1, C2=20pF, C3=10uF Crystal Oscillator=11.0592M Hz Program Code:#include<reg51.h> void main() { unsigned int i, j,k; while(1) { j=P0; //read data from port0
DEPT. OF ECE.
8). Interface LCD to Microprocessor & display the characters on 16x2 LCD.
LCD1
LM016L
C1
19 30pF
U1
VSS VDD VEE RS RW E 4 5 6
X1 C2
30pF CRYSTAL
1 2 3
RST
VCC
R2 C3
10uF 10k
29 30 31
PSEN ALE EA
P2.0/A8 P2.1/A9 P2.2/A10 P2.3/A11 P2.4/A12 P2.5/A13 P2.6/A14 P2.7/A15 P3.0/RXD P3.1/TXD P3.2/INT0 P3.3/INT1 P3.4/T0 P3.5/T1 P3.6/WR P3.7/RD
21 22 23 24 25 26 27 28 10 11 12 13 14 15 16 17
R1
1k
1 2 3 4 5 6 7 8
--------display the character data =Hello in LCD Resistors R1=1k ohm, R12=10k ohm Capacitors C1, C2=20pF, C3=10uF Crystal Oscillator=11.0592M Hz Program Code:#include<reg51.h> void lcdcmd(unsigned char value); void lcddata(unsigned char value); void lcdready(); void msdelay(unsigned int itime); sfr ldata =0x90; sbit rs =P2^0; sbit rw =P2^1; sbit en =P2^2; unsigned int c=0; void lcdcmd(unsigned char value) { ldata =value; rs=0; rw=0; en=1; msdelay(1);
2 K.VENKATA REDDY (Assistant Professor)
7 8 9 10 11 12 13 14
18
XTAL2
VCC
D0 D1 D2 D3 D4 D5 D6 D7
XTAL1
39 38 37 36 35 34 33 32
DEPT. OF ECE.
en=0; return; }
void lcddata(unsigned char value) { ldata=value; rs=1; rw=0; en=1; msdelay(1); en=0; return; }
void main() { while(1) { lcdcmd(0x38); // 2 line 5x7 lcd msdelay(250); lcdcmd(0x0E); //display on cursor on msdelay(250); lcdcmd(0x06); // entry mode msdelay(250); lcdcmd(0x01); // clear display msdelay(250);
3 K.VENKATA REDDY (Assistant Professor)
DEPT. OF ECE.
lcddata('H'); msdelay(250); lcddata('e'); msdelay(250); lcddata('e'); msdelay(250); lcddata('l'); msdelay(250); lcddata('l'); msdelay(250); lcddata('o'); } } 9). Data Transfer between the PC & Microcontroller using RS232 Serial Port.
C1 U1
22pF 19 XTAL1 P0.0/AD0 P0.1/AD1 P0.2/AD2 P0.3/AD3 P0.4/AD4 P0.5/AD5 P0.6/AD6 P0.7/AD7 P2.0/A8 P2.1/A9 P2.2/A10 P2.3/A11 P2.4/A12 P2.5/A13 P2.6/A14 P2.7/A15 P3.0/RXD P3.1/TXD P3.2/INT0 P3.3/INT1 P3.4/T0 P3.5/T1 P3.6/WR P3.7/RD 39 38 37 36 35 34 33 32 21 22 23 24 25 26 27 28 10 11 12 13 14 15 16 17 RXD TXD RTS CTS
X1 C2
22pF 9 CRYSTAL 18
XTAL2
RST
GND VCC
R2
10k
29 30 31
PSEN ALE EA
C3
10uF
1 2 3 4 5 6 7 8
R1
1k
#include<reg51.h> unsigned char data1; void sertx(unsigned char ch); void delay(unsigned int count) { int j=0,i=0;
4 K.VENKATA REDDY (Assistant Professor)
DEPT. OF ECE.
for(j=0;j<count;j++) { for(i=0;i<35;i++); } } void seriniti() { TMOD=0x20; //timer1 in auto reload mode TH1=0xFD; //to set baud rate in 9600
SCON=0x50; //serial communication mode } void sertx(unsigned char ch) { TR1=1; SBUF = ch; while(TI==0); TR1=0; TI=0; } void main(void) { while(1) { seriniti(); delay(1000); sertx('y'); delay(100); sertx('e'); delay(100); sertx('s'); delay(100); sertx(' '); delay(100);
}}
5 K.VENKATA REDDY (Assistant Professor)
DEPT. OF ECE.
Stepper motors can be used in various areas of your microcontroller projects such as making robots, robotic arm, automatic door lock system etc. This tutorial will explain you construction of stepper motors (unipolar and bipolar stepper motors ), basic principle, different controlling types (Half step and Full step), Interfacing Techniques (using L293D or ULN2003) and programming your microcontroller in C and assembly to control stepper motor. The UNIPOLAR STEPPER MOTOR has five or six wires and four coils (actually two coils divided by center connections on each coil). The center connections of the coils are tied together and used as the power connection. They are called unipolar steppers because power always comes in on this one pole. Circuit connections:
+12V
U2
P1.0 P1.1 P1.2 P1.3
1 2 3 4 5 6 7 1B 2B 3B 4B 5B 6B 7B COM 1C 2C 3C 4C 5C 6C 7C 9 16 15 14 13 12 11 10
+88.8
ULN2003A
Program to rotate the stepper motor 900 :Full Step Sequence In the full step sequence, two coils are energized at the same time and motor shaft rotates. The order in which coils has to be energized is given in the table below. Full Mode Sequence Step 0 1 2 3 #include <REG51.H> #define stepper P1 void delay(); 6 K.VENKATA REDDY (Assistant Professor) A 1 0 0 1 B 1 1 0 0 A\ 0 1 1 0 B\ 0 0 1 1
BAPATLA ENGINEERING COLLEGE void main(){ while(1){ stepper = 0x0C; delay(); stepper = 0x06; delay(); stepper = 0x03; delay(); stepper = 0x09; delay(); } } void delay(){ unsigned char i,j,k; for(i=0;i<6;i++) for(j=0;j<255;j++) for(k=0;k<255;k++); } Program to rotate the stepper motor 45 :Half Step Sequence
0
DEPT. OF ECE.
In Half mode step sequence, motor step angle reduces to half the angle in full mode. So the angular resolution is also increased i.e. it becomes double the angular resolution in full mode. Also in half mode sequence the number of steps gets doubled as that of full mode. Half mode is usually preferred over full mode. Table below shows the pattern of energizing the coils. Half Mode Sequence Step 0 1 2 3 4 5 A 1 0 0 0 0 0 B 1 1 1 0 0 0 A\ 0 0 1 1 1 0 B\ 0 0 0 0 1 1
DEPT. OF ECE.
6 7
1 1
0 0
0 0
1 0
#include <REG51.H> #define stepper P1 void delay(); void main(){ while(1){ stepper = 0x08; delay(); stepper = 0x0C; delay(); stepper = 0x04; delay(); stepper = 0x06; delay(); stepper = 0x02; delay(); stepper = 0x03; delay(); stepper = 0x01; delay(); stepper = 0x09; delay(); } void delay(){ unsigned char i,j,k; for(i=0;i<6;i++) for(j=0;j<255;j++) for(k=0;k<255;k++); } Step Angle:Step angle of the stepper motor is defined as the angle traversed by the motor in one step. To calculate step angle, simply divide 360 by number of steps a motor takes to complete one revolution. As we have seen that in half mode, the number of steps taken by the motor to complete one revolution gets doubled, so step angle reduces to half. 8 K.VENKATA REDDY (Assistant Professor) }
DEPT. OF ECE.
As in above examples, Stepper Motor rotating in full mode takes 4 steps to complete a revolution, So step angle can be calculated as...
and in the case of half mode step angle gets half so 45.