Mic practicals

Post on 15-Jan-2017

109 views 0 download

transcript

Practical

Manual

Microcontroller

Prof. Suraj R. Gaikwad

Prof. Suraj R. Gaikwad MIC

Exp: 1) To write an assembly language program for arithmetic operations. Program: 1) MOV A,#20H MOV F0,#20H ADD A,F0 RET 2) MOV A,#20H MOV F0,#10H SUB A,F0 RET 3) MOV A,#04H MOV F0,#02H MUL A,F0 RET 4) MOV A,#10H MOV F0,#02H ADD A,F0 RET Results: Addition: A = 40H Subtraction: A = 0AH Multiplication: A = 08H Division: A = 05H

Prof. Suraj R. Gaikwad MIC

Exp:2) To develop an assembly language program to transfer block of 10 bytes stored in internal data memory from location 40H to 60H. Program: ORG 0000H MOV R0,#40H MOV R1,#60H MOV R2,#0AH LOOP:MOV A,@R0 MOV @R1,A INC R0 INC R1 DJNZ R2, LOOP END

Prof. Suraj R. Gaikwad MIC

Exp:3) To develop an assembly language program to get square of numbers. Program: ORG 0000H MOV R0,#40H MOV R1,#60H MOV R2,#0AH LOOP1:MOV A,@R0 MOV B,@R0 MUL AB MOV @R1,A INC R0 INC R1 DJNZ R2,LOOP1 END Result: Input:

Output:

00H = 0D

01H = 1D

04H = 4D

09H = 9D

10H = 16D

19H = 25D

24H = 36D

31H = 49D

40H = 64D

51H = 81D

Prof. Suraj R. Gaikwad MIC

Exp:4) To develop program for arranging 10 numbers in descending order. Program: ORG 00H ARR EQU 70H L0:MOV R1,#ARR MOV R0,#ARR MOV R2,#0AH MOV R3,#00H DEC R2 L1:MOV A,@R0 MOV B,A INC R1 SUBB A,@R1 JNC L2 MOV A,B XCH A,@R1 MOV @R0,A MOV R3,#01H L2:INC R0 DJNZ R2,L1 CJNE R3,#00,L0 SJMP $ END Result: Input:

Output:

Prof. Suraj R. Gaikwad MIC

Exp:5) To develop a program to generate Square wave over port pins. Program: ORG 0000H :initialization MOV TMOD,#10H :move 10H into timer/counter mode control resister UP: MOV TL1,#0D2H :move 0D2H into timer 1 low byte MOV TH1,#0FFH :move offH into timer 1 high byte SETB TR1 :addressed TR1 is set, TR1=timer 1 run control bit WAIT: JNB TF1,WAIT :jump if addressed TR1 is 0, otherwise proceed with next instruction CLR TR1 :adressed TR1 is cleared CPL P1.5 : complement the addressed P1.5 CLR TF1 : adressed TF1 is cleared, TF1=timer 1 overflow flag SJMP UP :program branches unconditionally to address indicated END :end Result:

Prof. Suraj R. Gaikwad MIC

Exp:6) To generate Square wave using DAC. Circuit Diagram:

C Program: #include <REG51xD2.H> #include <stdio.h> sbit Amp = P3^3; /* Port line to change amplitude */ sbit Fre = P3^2; /* Port line to change frequency */ void delay(unsigned int x) /* delay routine */ { for(;x>0;x--); } main() { unsigned char on = 0x7f,off=0x00; unsigned int fre = 100; while(1)

Prof. Suraj R. Gaikwad MIC

{ if(!Amp) /* if user choice is to change amplitude */ { while(!Amp); /* wait for key release */ on+=0x08; /* Increase the amplitude */ } if(!Fre) /* if user choice is to change frequency */ { if(fre > 1000) /* if frequency exceeds 1000 reset to default */ fre = 100; while(!Fre); /* wait for key release */ fre += 50; /* Increase the frequency */ } P0=on; /* write amplitude to port */ P1=on; delay(fre); P0 = off; /* clear port */ P1 = off; delay(fre); } } Output Waveform:

Prof. Suraj R. Gaikwad MIC

Assembly Program: ORG 0000H

MOV DPTR, #8000H UP:MOV DPL,#40H

MOV DPH,#40H CLR A

MOVC A,@A+DPTR SETB P1.5

WAIT:JNB P1.5,WAIT CLR P1.5 SJMP UP

END Output Waveform:

Prof. Suraj R. Gaikwad MIC

Exp:7) To write a program to drive Stepper Motor.

Theory:

Driving Unipolar Stepper Motor with 8051:

Unipolar stepper motors can be used in three modes namely the Wave Drive, Full Drive and Half Drive mode.

Half Drive Stepping Sequence

Prof. Suraj R. Gaikwad MIC

Interfacing Using L293D:

Interfacing Using ULN2003:

Prof. Suraj R. Gaikwad MIC

Programs: Keil C Code for Wave Drive:

Prof. Suraj R. Gaikwad MIC

Keil C Code for Full Drive:

Prof. Suraj R. Gaikwad MIC

Keil C Code for Half Drive:

Prof. Suraj R. Gaikwad MIC

Result:

1) Wave drive stepper sequence:

Prof. Suraj R. Gaikwad MIC

2) Full drive stepping sequence:

Prof. Suraj R. Gaikwad MIC

3) Half wave stepping sequence:

Prof. Suraj R. Gaikwad MIC

Prof. Suraj R. Gaikwad MIC

Prof. Suraj R. Gaikwad MIC

Exp:8) To write a program to drive Stepper Motor in clockwise direction and anti-clockwise direction. Program: ORG 0000H MOV R2,#12H MOV A,#88H LOOP1: MOV P2,A RR A DJNZ R2, LOOP1 MOV R2,#12H MOV A,#11H LOOP2: MOV P2,A RL A DJNZ R2, LOOP2 END Result:

Prof. Suraj R. Gaikwad MIC

Prof. Suraj R. Gaikwad MIC

Prof. Suraj R. Gaikwad MIC

Exp:9) To write a program to read keyboard and display code. Diagram:

Program:

• Program to interface matrix keyboard 4x4 • Rows are connected to the Port pins P1.0-P1.3 & Columns are connected to

Port pins P2.0-P2.3 • Rows are grounded one by one and read columns Seven segment display is

connected at port P0 ORG 00H AJMP START ORG 13H RETI START:MOV P0,#00H MOV P2,#0FH ; Port Pins P2.0 to P2.3 i/p pins and P2.4 to P2.7 o/p pins MOV P1,#00H ; Port P0 output port REL: MOV P1,#00H ; make all rows ground to check all keys MOV A,P2 ; read port p2 to ensure that all keys released

Prof. Suraj R. Gaikwad MIC

ANL A,#0FH ; maks upper bits because they are not used CJNE A,#0FH,REL ; check till all keys released AGAIN: ACALL DELAY MOV A,P2 ; see if any key pressed or not? ANL A,#0FH ; mask upper bits CJNE A,#0FH,KPRESS ; if a is not equal to 0fh then key is pressed SJMP AGAIN ; check again if key is not pressed KPRESS: ACALL DELAY MOV A,P2 ; ANL A,#0FH ; mask unused upper bits CJNE A,#0FH,KPRESS1 ; if a is not equal to 0fh then key is pressed SJMP AGAIN ; check again if key is not pressed KPRESS1: MOV P1,#0FEH ; ground row 0 MOV A,P2 ; read all columns ANL A,#0FH ; mask unused upper bits CJNE A,0FH,R_0 ; key is pressed in first row (row 0),check columns MOV P1,#0FDH ; Ground ROW 1 MOV A,P2 ; read all columns ANL A,#0FH ; mask unused upper bits CJNE A,0FH,R_1 ;key is pressed in second row (row 1),check columns MOV P1,#0FBH ; ground row 2 MOV A,P2 ; read all columns ANL A,#0FH ; mask unused upper bits CJNE A,0FH,R_2 ;key is pressed in third row (row 2),check columns MOV P1,#0F7H ; ground row 0 MOV A,P2 ; read all columns ANL A,#0FH ; mask unused upper bits CJNE A,0FH,R_3 ;key is pressed in fourth row (row 3),check columns LJMP AGAIN R_0: MOV DPTR,#KCODE0 ;set dptr=start of row 0 SJMP CHECK_C R_1: MOV DPTR,#KCODE1 ;set dptr=start of row 1 SJMP CHECK_C R_2: MOV DPTR,#KCODE2 ;set dptr=start of row 2 SJMP CHECK_C R_3: MOV DPTR,#KCODE3 ;set dptr=start of row 3 CHECK_C: RRC A ;check whether carry occurs or not JNC GET_CODE

Prof. Suraj R. Gaikwad MIC

INC DPTR SJMP CHECK_C GET_CODE: CLR A MOVC A,@A+dptr MOV P0,A LJMP REL DELAY: MOV R7,#0FFh DLOOP: MOV R6,#0FFh D_LOOP: DJNZ R6,D_LOOP DJNZ R7,DLOOP RET Result: KCODE0: DB '0','1','2','3' ; These codes are for LCDs KCODE1: DB '4','5','6','7' ; Replace this code by seven KCODE2: DB '8','9','A','B' ; segment code as per your KCODE3: DB 'C','D','E','F' ; Circuit.