8051 asm & intrf manual_new1

Download 8051 Asm & Intrf Manual_New1

Post on 18-Nov-2014

6.751 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

MICROCONTROLLERS LAB

IVth SEM EC

R.N.SHETTY INSTITUTE OF TECHNOLOGY Channasandra, Bangalore-560061

MICROCONTROLLER LABORATORY MANUAL (06ESL47)

DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING 2008

8051 MANUAL

RNSIT

1

MICROCONTROLLERS LAB

IVth SEM EC

VTU DRAFT SYLLABUSSUBJECT: MICROCONTROLLERS LAB EXAM HOURS: 3 (Common to EE, EC, IT, TC, BM and ML) EXAM MARKS: 50

I. PROGRAMMING1. Data Transfer - Block move, Exchange, Sorting, Finding largest element in an array 2. Arithmetic Instructions - Addition/subtraction, multiplication and division, square, Cube (16 bits Arithmetic operations bit addressable) 3. Counters 4. Boolean & Logical Instructions (Bit manipulations) 5. Conditional CALL & RETURN 6. Code conversion: BCD ASCII; ASCII Decimal; Decimal - ASCII; HEX Decimal and Decimal - HEX 7. Programs to generate delay, Programs using serial port and on-Chip timer /counter

II. INTERFACINGWrite C programs to interface 8051 chip to interfacing modules to develop single chip solutions 8. Simple Calculator using 6 digit seven-segment display and Hex Keyboard interface to 8051 9. Alphanumeric LCD panel and Hex keypad input interface to 8051 10. External ADC and Temperature control interface to 8051 11. Generate different waveforms Sine, Square, Triangular, Ramp etc. using DAC interface to 8051; change the frequency and amplitude 12. Stepper and DC motor control interface to 8051 13. Elevator interface to 8051

8051 MANUAL

RNSIT

2

MICROCONTROLLERS LAB

IVth SEM EC

IntroductionPROCESSOR used is Atmel AT89C51ED2 - micro controller that has 64Kbytes of onchip program memory. It is a version of 8051 with enhanced features. AT 89C51ED2 operates at 11.0592 MHz PROCESSOR FEATURES ON-CHIP MEMORY: CODE MEMORY: 64K Bytes of flash. DATA MEMORY: 256 Bytes of RAM, 1792 Bytes of XRAM, 2K Bytes of EEPROM. ON-CHIP PERIPHERALS 3 16-bit Timers/Counters,Watch Dog Timer,Programmable Counter Array (PCA) on Port1 i.e. PWM and Capture & Compare, SPI (Serial Peripheral Interface) on Port1,Full duplex enhanced UART. INTERRUPTS Nine sources of interrupt (both external and internal). Two External interrupts INT0 and INT1 are provided with push button switches; these can also be used as general-purpose switches. I/O (Port) Lines Four 10-pin connectors for all the 32 I/O lines. P0, P1 and P2 Port lines are available on a 26-pin connector, 16X2 LCD & SERIAL I/O are also available.

8051 MANUAL

RNSIT

3

MICROCONTROLLERS LAB

IVth SEM EC

Creating and compiling a Vision2 project1. Double Click on the Vision3 icon on the desktop. 2. Close any previous projects that were opened using Project->Close. 3. Start Project New Project, and select the CPU from the device database (DatabaseAtmel- AT89C51ED2). (Select AT89C51ED2 or AT89C51RD2 as per the board).On clicking OK, the following option is displayed. Choose Yes.

4. Create a source file (using File->New), type in the assembly or C program and save this (filename.asm/ filename.c) and add this source file to the project using either one of the following two methods. (i) Project-Components,Environmentand Books->addfiles-> browse to the required file -> OK OR (ii) right click on the Source Group in the Project Window and the Add Files to Group

option. 5. Set the Target options using -> Project Options for Target opens the Vision2 Options for Target Target configuration dialog. Set the Xtal frequency as 11.0592 Mhz, and also the Options for Target Debug use either Simulator / Keil Monitor- 51 driver.

If Keil Monitor- 51 driver is used click on Settings -> COM Port settings select the COM Port to which the board is connected and select the baud rate as 19200 or 9600 (recommended). Enable Serial Interrupt option if the user application is not using onchip UART, to stop program execution. 6. Build the project; using Project -> Build Project. Vision translates all the user application and links. Any errors in the code are indicated by Target not created in the Build window, along with the error line. Debug the errors. After an error free build, goto Debug mode

8051 MANUAL

RNSIT

4

MICROCONTROLLERS LAB

IVth SEM EC

7. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog. Or by clicking in the icon. 8.The program is run using the Debug-Run command & halted using Debug-Stop Running. Also the (reset, run, halt) icons can be used. Additional icons are

(step, step over, step into, run till cursor). 9. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led status, etc. If it is a part A program, the appropriate memory window is opened using View -> memory window (for data RAM & XRAM locations), Watch window (for timer program), serial window, etc. Note: To access data RAM area type address as D:0020h. Similarly to access the DPTR region (XRAM-present on chip in AT89C51ED2) say 9000h location type in X:09000H.

8051 MANUAL

RNSIT

5

MICROCONTROLLERS LAB

IVth SEM EC

1. DATA TRANSFER INSTRUCTIONS1) Write an assembly language program to transfer n =10 bytes of data from location 8035h to location 8041h (without overlap). ORG 0000H SJMP 30H ORG 30H MOV DPH,#80H MOV R0,#35H //source address MOV R1,#41H //destination address MOV R3,#05H //count BACK: MOV DPL, r0 MOVX A,@dptr MOV DPL, R1 MOVX @dptr,A INC R0 INC R1 DJNZ R3, BACK HERE: SJMP HERE END RESULT: Before Execution: 10 locations X:8035h are filled up with data.

After Execution: 10 locations X:8041h are filled up with data from 8035h.

Algorithm1. Initialize registers to hold count data & also the source & destination addresses. 2. Get data from source location into accumulator and transfer to the destination location. 3. Decrement the count register and repeat step 2 till count is zero. Note: For data transfer with overlap start transferring data from the last location of source array to the last location of the destination array.

8051 MANUAL

RNSIT

6

MICROCONTROLLERS LAB

IVth SEM EC

2) ASSEMBLY LANGUAGE PROGRAM TO EXCHANGE A BLOCK OF DATA. Write an assembly language program to exchange n = 5 bytes of data at location 0027h and at location 0041h. ORG 00H SJMP 30H ORG 30H MOV R0,#27H //source address MOV R1,#41H //destination address MOV R3,#05H //count BACK: MOVX A,@r0 MOV r2,a MOVX a,@r1 MOVX @r0,a MOV a, r2 MOVX @r1,a INC R0 INC R1 DJNZ R3, BACK HERE: SJMP HERE END Aliter using XCH command. ORG 0000H SJMP 30H ORG 30H MOV R0,#27H //source address MOV R1,#41H //destination address MOV R3,#05H //count BACK: MOVX A,@r0 MOV r2,a MOVX a,@r1 XCH a, r2 MOVX @r1,a XCH a, r2 MOVX @r0,a INC R0 INC R1 DJNZ R3, BACK HERE: SJMP HERE END RESULT: Before Execution: 5 locations at X:0027h & X:0041h are filled up with data.

8051 MANUAL

RNSIT

7

MICROCONTROLLERS LAB

IVth SEM EC

After Execution: The data at X:8027h & X:8041h are exchanged.

Algorithm1. Initialize registers to hold count data (array size) & also the source & destination addresses. 2. Get data from source location into accumulator and save in a register. 3. Get data from the destination location into accumulator. 4. Exchange the data at the two memory locations. 5. Decrement the count register and repeat from step 2 to 4 till count is zero. 3) ASSEMBLY LANGUAGE PROGRAM TO SORT NUMBERS. //BUBBLE SORT PROGRAM Write an assembly language program to sort an array of n= 6 bytes of data in ascending order stored from location 8035h.(use bubble sort algorithm) ORG 0000H SJMP 30H ORG 30H MOV R0,#05 L1: MOV dptr, #9000h MOV A,R0 MOV R1,A L2: MOVX a, @dptr MOV B, A INC dptr MOVX a, @dptr CLR C MOV R2, A SUBB A, B JC NOEXCHG MOV A,B MOVX @dptr,a DEC DPL MOV a,R2 MOVX @dptr,a INC DPTR NOEXCHG: DJNZ R1,L2 DJNZ R0,L1 here: SJMP here END

//count n-1 -ARRAY SIZE-n- Pass Counter //array stored from address 9000h //initialize exchange counter //GET NUMBER FROM ARRAY //& STORE IN B //next number in the array //reset borrow flag //STORE IN R2 //2nd - 1st no.no compare instruction in 8051 // JNC - FOR ASCENDING ORDER //EXHANGE THE 2 NOES IN THE ARRAY //DEC dptr-INSTRUCTION NOT PTRESENT

//decrement compare counter //decrement pass counter

Algorithm1. Store the elements of the array from the address 9000h 2. Initialize a pass counter with array size-1 count (for number of passes). 8051 MANUAL RNSIT 8

MICROCONTROLLERS LAB

IVth SEM EC

3. Load compare counter with pass counter contents & initialize DPTR to point to the start address of the array (here 9000h). 4. Store the current and the next array elements pointed by DPTR in registers B and r2 respectively. 5. Subtract the next element from the current element. 6. If the carry flag is set (for ascending order) then exchange the 2 numbers in the array. 7. Decrement the compare counter and repeat through step 4 until the counter becomes 0. 8. Decrement the pass counter and repeat through step 3 until the counter becomes 0. RESULT: Before Execution:Unsorted Array at 9000h

After Execution: Sorted Array (Descending order) at 9000h

4) Write an assembly language program to find the largest element in a given string of n = 6 bytes at location 4000h. Store the largest element at location 4062h. ORG 0000H SJMP 30H ORG 30H MOV R3,#6 //length of the array MOV DPTR,#4000H //starting address of the array MOVX A,@DPTR MOV r1,a NEXTBYTE: INC DPTR MOVX A,@DPTR CLR C //reset borrow flag MOV R2,A //next number in the array SUBB A,R1 //OTHER Num - PREVIOUS LARGEST no. JC skip // JNC for smallest element MOV A,r2 //UPDATE larger number in r1 MOV R1,A skip:DJNZ R3,NEXTBYTE MOV DPL, #62