Interfacing Examples
1) Continuosly toggles the value of port 0
Code in C
#include
void main(void){
while(1)
{
PO = 0x00 ;
PO = 0xFF ;
}
}
Code in Assembly:
loop : MOV A,#ODH
MOV A,#OFFH
MOV PO,A
JMP loop
2) Get data PO and set to P1
Code in C
#include
void main(void){
PO = 0xFF ;
P1 = 0x00 ;
while(1)
{
P1 = P0 ;
}
}
Code in Assembly:
MOV A,#OFFH
MOV PO,A
MOV A,#OOH
MOV P1,A
loop: MOV A,P0
MOV P1,A
JMP loop
3)Create 50% duty cycle on bit D of port 1
Code in C
#include
sbit mybit = P1 ^0 ;
void delay(unsigned int n){
unsigned int x,y;
x=0;
y=0;
while(x {
while(y<1275){
y++ ;
}
x++ ;
}
}
void main(void) { while(1){ mybit = ~mybit ; delay(250); } }
Assembly:
loop: SET B P1.0
LCALL DELAY
CLR P1.0
LCALL DELAY
JMP LOOP
4) Get status of switch at P1.0 and sent it to LED at P2.7
Code in C
#include
sbit in_bit =P1 ^0;
sbit out_bit = P2^7 ;
void main(void){
while(1)
{
out_bit = in_bit;
}
}
Code in Assembly:
SETB P1.0
CLR P2.7
loop: MOV C,P1.0
MOV P2.7,C
JMP loop
5)Blink 8 LEDS connected at Port 2
Code in C
#include
void main(void){
P2= 0x00 ;
while(1)
{
P2 = 0x00 ;
P2=0xFF;
}
}
Assembly:
MOV A,#OFFH
MOV P2,A
loop: MOV A,#OOH
MOV P2,A
MOV A,#0FFH
MOV P2,A
JMP loop
6) 7 segment display to show 0 to 9
Code in Assembly
MOV A,#OFFH
MOV P2,A
MOV P2,#C0
ACALL DELAY
MOV P2,#F9
ACALL DELAY
MOV P2,#A4
ACALL DELAY
MOV P2,#BD
ACALL DELAY
MOV P2,#99
ACALL DELAY
MOV P2,#92
ACALL DELAY
MOV P2,#82
ACALL DELAY
MOV P2,#F8
ACALL DELAY
MOV P2,#80
ACALL DELAY
MOV P2,#90
ACALL DELAY
Code in C
#include
void main() {
P2 = 0x00 ;
P2 = 0xC0;
delay(200);
P2 =0xF9 ;
delay(200)
p3=0xA4 ;
….
}
7) 7 Segment display to show 99 to 00
Code in C
#include
void main() {
int i,j ;
int array[10];
array[1] = 0xF9;
array[2] = 0xA4;
array[3] = 0xB0;
array[4] = 0x99;
array[5] = 0x92;
array[6] = 0x82;
array[7] = 0xF8;
array[8] = 0x80;
array[9] = 0x90;
array[0] = 0xC0;
P1 = 0x00 ;
P2 = 0x00 ;
for(i=9;i>=0;i--){
P1 =array[i];
for(j=9;j>=0;j--){
P2=array[j] ;
}
}
}
8)Sum of two 8bit BCD in RAM at 50H and 51H and store BCD sum at RAM 52H and 53H.
Code in Assembly
MOV A,50H
MOV B,51H
CLR R7
ADD A,B
DAA
JNC next
INC R7
next: MOV 52H,R7
MOV 53H,A
9) Compute precise 5-ms delay
for 5 ms;
n x 1.085 microsec = 5 ms ;
so n = 4608; 1 + 655535-4608 = 60927+1 = EE00H ;
Code in C
#include
void delay() ;
void main() {
while(1){
delay
}
}
void delay(void){ TM0D = 0x01 ; TL0=0x00; TH0 =0xEE ; TRD=1; while(TF0==0); TR0=0 ; TF0 = 0 ; }
Code in Assembly
DELAY: MOV TMOD #01
Here: MOV TL#0
MOV TH0,#0EEH
SETB TR0
again JNB TF0,again
CLR TR0
CLR TF0
Ⓒ Copyright ESign Technology 2019. A Product of ESign Technology. All Rights Reserved.