Micro Processor Lab 2 Manual
Micro Processor Lab 2 Manual
Lab 2 Introduction to Programming in Assembly for AVR Microcontrollers and Basic Input/Output Operations using Assembly
Lab-2 Introduction to Programming in Assembly for AVR Microcontrollers and Basic Input/Output Operations using Assembly
Objective:
Introduction to programming in assembly for AVR microcontrollers basic input/output operations using assembly
Software:
CodeVisionAVR
CodeVisionAVR
Follow the steps described in last lab to create a new project and source file and add source file to the project. Write the following code in your c file and build the project (Note: From here onwards any example of code would be italicized).
#include <mega16.h> void main(void) { #asm #endasm } //including header file for atmega16 //main function for c program //indicator to assembler that Assembly program will begin from here //indicator to assembler that Assembly program will end here
Lab-2 Introduction to Programming in Assembly for AVR Microcontrollers and Basic Input/Output Operations using Assembly
Now we can use MyPreferredRegister instead of R16 and use it to access R16 wherever needed. Semicolon ; is always succeeded by comments in other words you can insert comments after semicolon in assembly. Consider an Example
LDI MyPreferredRegister, 150 ; will load 150 to R16 where LDI is Load Immediate
Note: First Register is always the destination register where the result is written. LDI works only for R16 to R31, not for R0 to R15 (This restriction applies to several other commands as well, some of them are mentioned below) but CLR (setting contents of a register to zero) works for all registers from R0 to R31 e.g. CLR MyPreferredRegister.
ANDI Rx,K ORI Rx,K CPI Rx,K SBR Rx,M CBR Rx,0b00110000 SER Rx SUBI Rx,K ; Bit-And of register Rx with a constant value K ; Bit-And of register Rx with a constant value K ; Compare the content of the register Rx with a constant value K ; Set all bits in register Rx to one, that are one in the constant mask M ; Clear bits 4 & 5 (Clear bits that are set in constant mask) ; Set all bits in register Rx to one (equal to LDI Rx,255) ; Subtract the constant K from the content of register Rx and store the result in register Rx
Program Run:
One can make labels anywhere by succeeding a label by colon (:). Labels are essential to assembly programming. Consider an example
Line 1 Line 2 RJMP Start Line 4 Line 5 Start: ; any line written in assembly language for sequential execution ; any line written in assembly language for sequential execution ; jump to a label start ; any line written in assembly language for sequential execution ; any line written in assembly language for sequential execution ; the label start
Note that RJMP will always jump to start (there are other applications where RJMP is useful), RCALL is another such example of unconditional jump/branch. Hence there is no advantage in writing Lines 4 & 5 in the code. In particular situations we want the jumps to be associated with certain conditions and such jumps are called branches.
COMSATS Institute of Information Technology Lab Instructor: Anum Ali
Lab-2 Introduction to Programming in Assembly for AVR Microcontrollers and Basic Input/Output Operations using Assembly
; any line written in assembly language for sequential execution ; conditional jump that jumps if zero flag is not set ; any line written in assembly language for sequential execution ; any line written in assembly language for sequential execution ; any line written in assembly language for sequential execution
The command BRNE is (Branch if Not Equal) and it checks the zero flag. If it is not set to zero it will branch to start otherwise line 3 and 4 will execute first. The command opposite to BRNE is BREQ (Branch if Equal).
NOP ; A command that does no operation
This command will do nothing and just waste one clock cycle of microcontroller (No Operation) One can make functions using MACROS and Subroutines.
.MACRO Delay1 NOP NOP NOP NOP .ENDMACRO ; Start of a MACRO named Delay1 ; 4 NOP commands within a MACRO
; End of a MACRO
Then one can use it in function by simple writing Delay1 in assembly program. The purpose of a function can also be fulfilled by writing a subroutine.
SBRC R1,7 CPSE R1,R2 SBIC PINB,0 LDI R16,10 LOOP: DEC R16 BRNE LOOP ; Skip the next instruction if bit 7 is 0 ; Compare R1 and R2, skip if equal ; Skip if Bit 0 on port B is 0 ; Load 10 in R16 ; Define a Label entitled Loop ; Decrement in contents of R16 ; Branch to loop if contents of R16 not equal to zero
Calculations:
COM R1 NEG R1 LSL R1 LSR R1 ; ones compliment ; twos compliment ; shift left ;shift right
Lab-2 Introduction to Programming in Assembly for AVR Microcontrollers and Basic Input/Output Operations using Assembly ASR R1 ROR R1 SWAP R1 ADD R2,R4 ADC R1,R3 SUB R2,R4 SBC R1,R3 CP R2,R4 CPC R1,R3 TST R1 ; shift right for signed numbers ; rotate ; swaps lower and upper nibble ;add ; add with carry ; subtract ; subtract with carry ; compare ; compare with carry ; Checking whether R1 is zero or not
Note: Underlined commands are particularly useful for 16 bit operations as registers are 8 bit and addition, subtraction and comparisons of 16 bit numbers have to take carry in consideration from lower byte.
Ports:
There are three registers associate with every input output port of a ATmega16. PIN, PORT and DDR Registers. DDRx register decides whether a port or a specific bit associated with x port (in our case A,B,C,D) will be used as input or output with every bit set to 1 used as output. If the port or bit is used as output the contents of PORTx register will make it high or low. PINx register is used to read input from a specific x port if the port is set to be used as input.
.equ .equ .equ .equ .equ .equ .equ .equ .equ .equ .equ .equ PORTA DDRA PINA PORTB DDRB PINB PORTC DDRC PINC PORTD DDRD PIND =$1b =$1a =$19 =$18 =$17 =$16 =$15 =$14 =$13 =$12 =$11 =$10
Note: We use these definitions for our own convenience (They map port addresses to names e.g. 1b to PORT1, equ means equals). Some commands to use ports and their pins are mentioned below SBI DDRB, 0 ; bit0 of DDRB will be set to one hence making it an output pin
Lab-2 Introduction to Programming in Assembly for AVR Microcontrollers and Basic Input/Output Operations using Assembly CBI DDRB, 0 ; bit0 of DDRB will be cleared hence making it an input pin
; bit0 of PORTB will be set to one hence making it a high pin ; bit0 of PORTB will be cleared hence making it a low pin
Note: For now when you select any pin to be used as input select PORT of that specific to be 0. (Detailed discussion of such usage will come in later labs) e.g.
CBI DDRC,3 CBI PORTC,3 Another Example .DEF MyPreferredRegister = R16 LDI MyPreferredRegister, 0b00100000 OUT MCUCR, MyPreferredRegister IN MyPreferredRegister, MCUCR ; Assigning name to R16 ;Load decimal 32 in MyPreferredRegister ; Write the contents of MyPreferredRegister to MCUCR ; Read the contents of MCUCR in MyPreferredRegister ; Clear 3rd bit of c ports DDR register to use it as input ; Clear 3rd bit of c ports PORT register to make it low
Note: As the structure suggests setting the 6th bit will sleep the microcontroller (SE - Sleep)
Lab Task:
1) Write a code that detects whether upper nibble of a byte is equal to a lower nibble. If they are equal set the 3rd bit of DDRA and PORTA registers. Otherwise set the 2nd bit of DDRA and PORTA registers.
Lab-2 Introduction to Programming in Assembly for AVR Microcontrollers and Basic Input/Output Operations using Assembly
1) Write a program that takes input from PORTD using 8 switches and outputs that at PORTA using 8 LEDs. Also output the lower nibble of input at 4 MCBs of PORTC and upper nibble of input at 4 LCBs of input. Extract the four centered bits of input and show them at LCBs of PORTB. (LEDS are to be used for every output including output at PORTA, PORTC and PORTB, and switches will be set to take input only at the beginning of simulation, Open switch means zero input hence it should display zero output. See the sample figure below that shows how to connect switches and LEDs)
2) Write a code that detects whether a number is palindrome or not. If they are equal set the 5th bit of DDRC and PORTC registers. Otherwise set the 3rd bit of DDRC and PORTC registers. Definition of Palindrome in binary sense: Palindrome is a number that is read same bitwise whether you read it from MSB to LSB or from LSB to MSB Note: The essence of programming lies in writing efficient codes, hence students using commands efficiently and producing compact codes will receive more credit.