MC Practicals 2
MC Practicals 2
ORG 0000H
MOV R7,#05H // Initialise Counter for 5 Numbers
MOV R0,#50H //Initialise Source Memory Location
MOV R1,#90H //Initialise Destination Memory Location
LOOP: MOV A,@R0 // transfer first number to Accumulator from Source Memory Location
MOV @R1,A // transfer A content toDestination Memory Location
INC R0 // increment Source Memory Location
INC R1 // increment Destination Memory Location
DJNZ R7, LOOP // decrement counter, check 0 , if not jump to label LOOP
NOP
END
2 Internal to external
ORG 0000H
MOV R2,#0AH // Initialise Counter for 5 Numbers
MOV R0,#30H //Initialise Source Memory Location
MOV DPTR,#1000H //Initialise Destination Memory Location
LOOP: MOV A,@R0 // transfer first number to Accumulator from Source Memory
Location
MOVX @DPTR,A // transfer A content toDestination Memory Location
INC R0 // increment Source Memory Location
INC DPTR // increment Destination Memory Location
DJNZ R2, LOOP // decrement counter, check 0 , if not jump to label LOOP
NOP
END
3 Program for Toggle bits of Port Using Unknown Timer.
#include<reg51.h>
void Delay(void);
void main (void)
{
while(1) // infinite loop
{
P3 = 0x00; // LED ON
Delay();
P3 = 0xff; // LED OFF
Delay();
}
}
void Delay(void)
{
int i, j;
for(i=0;i<10;i++)
{
for(j=0;j<10000;j++)
{
}
}
}
4 Blink LED connected port P2.3
#include<reg51.h>
void Delay(void);
void main (void)
{
while(1) // infinite loop
{
P2 = 0x00; // LED ON
Delay();
P2 = 0xff; // LED OFF
Delay();
}
}
void Delay(void)
{
int i, j;
for(i=0;i<10;i++)
{
for(j=0;j<10000;j++)
{
}
}
}
5 Genrate Square wave on 8051
ORG 0000H
MOV P1,#00H
REPEAT: CALL SQUARWAVE ; //GENERATE SQUARE WAVE
JMP REPEAT;
SQUARWAVE: MOV P3,#0FFH
CALL DELAY2
MOV P3,#00H
CALL DELAY2
RET
DELAY1: MOV R0,#10
DEL2: MOV R1,#250
DEL1: MOV R2,#250
DJNZ R2,$
DJNZ R1,DEL1
DJNZ R0,DEL2
RET
ORG 0000H
MOV P1,#00H
REPEAT: CALL TRIWAVE ; //GENERATE TRIANGULAR WAVE
JMP REPEAT;
TRIWAVE: MOV R7,#00H
TRIWAVE1: MOV P2,R7
INC R7
CJNE R7,#0FFH,TRIWAVE1
MOV R7,#0FFH
TRIWAVE2: MOV P2,R7
DJNZ R7,TRIWAVE2
RET
DELAY1: MOV R0,#10
DEL2: MOV R1,#250
DEL1: MOV R2,#250
DJNZ R2,$
DJNZ R1,DEL1
DJNZ R0,DEL2
RET
#include<reg51.h>
void main( )
{
unsigned char no_code[
]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //Array for hex
values (0-9) for common anode 7 segment
int k;
while(1)
{
for(k=0;k<10;k++) // loop to display 0 to 9
{
P2 = no_code [k];
msdelay (100);
}
}
}
10 Dc Motor SW1 & SW2
#include<reg51.h>
sbit switch1=P2^0;
sbit switch2=P2^1;
sbit clk=P3^0;
sbit anticlk=P3^1;
Void main()
{
switch1=switch2=1; //making P2.0 and P2.1 as inputs
switch1=switch2=0;
clk=anticlk=0;
while(1)
{
if((switch1))
clk=1;
else if((switch2))
anticlk=1;
else
P3=0x00;
}
}
11 Interface 16x2 LCD Display
// LCD16x2 ,8 bit 8051 interface
#include <reg51.h>
#define lcd_data P3 /* P3 port as data port */
sbit rs=P2^0; /* Register select pin */
sbit rw=P2^1; /* Read/Write pin */
sbit en=P2^2; /* Enable pin */
// function defining
void lcd_init (); /* LCD Initialize function */
void cmd (unsigned char a); /* LCD16x2 command function */
void dat (unsigned char b); /* LCD data write function */
void show (unsigned char *s);
void lcd_delay ();
void lcd_init () /* LCD Initialize function */
{
P1=0x00;
cmd(0x38); /* Initialization of 16X2 LCD in 8bit mode */
cmd(0x0e); // Display On cursor blinking
cmd(0x01); /* Clear display */
cmd(0x06); /* Auto Increment cursor */
cmd(0x0c); /* Display ON Cursor OFF */
cmd(0x80); /* Cursor at home position */
}
void cmd (unsigned char a) /* LCD16x2 command function */
{
lcd_data=a;
rs=0; /* command reg. */
rw=0; /* Write operation */
en=1;
lcd_delay ();
en=0;
}
void dat (unsigned char b) /* LCD data write function */
{
lcd_data=b;
rs=1; /*Data reg.*/
rw=0; /* Write operation*/
en=1;
lcd_delay ();
en=0;
}
void show (unsigned char *s)
{
while(*s)
{
dat(*s++);
}
}
void lcd_delay ()
{
unsigned int lcd_delay;
for(lcd_delay=0;lcd_delay<=6000;lcd_delay++);
}
int main()
{
unsigned int j;
lcd_init(); /* initialization of LCD*/
while(1)
{
cmd(0x80); // force cursor to begining of 1st line
show("India");
cmd(0xc0); // force cursor to begining of 2st line
show("Welcome");
for(j=0; j<60000; j++);
cmd(0x01); // clear display screen
for(j=0; j<60000; j++);
}
}