Pic microcontroller assembly language programing: Like we need language to communicate with each other. Language is a set of symbols by which we convery our message to others. Similarly, we communicate with microcontrollers with a language called assembly. A assembler is used to understand our instruction or you can say our symbols which we provide to microcontroller Assembly language program is basically a set of instructions stored in computer memory. Computer can only understand 1’s and 0’s. Assembly language instructions are for humans only. Computer do not understand them. So we need to converter these assembly language instructions to binary form which is understandable for computers. Assembly language commands are converted into one’s and zero’s by compiler. A data file used for storing compiled program is called an “executive file”, i.e. “HEX data file”. The name comes from the hexadecimal presentation of a data file and has a suffix of “hex” as well, for example “probe.hex”. After has been generated, the data file is loaded into the microcontroller using a programmer. Assembly language codes can be written in any editor. Let’s start with registers of pic microocntrollers to understand basic instructions of pic microcontroller
WREG register in pic microcontroller assembly language
PIC microcontroller has many registers for arithmetic and logic operations. The 8-bit WREG register is most frequently used register in PIC micro-controllers. It is often called accumulator or Working Register. It can be easily initialized with a constant. For Example:
MOVLW k // here k is any 8-bit value.
MOVLW 10
MOVLW 0x45
In the instruction ‘L” stands for literal, meaning the value is immediately present. ‘W” means WREG. So the whole instruction means move an immediate value in WREG. MOVLW 0xAA //moves 0xAA (or 0b10101010) in WREG. We will use WREG register for simple instructions like MOV and ADD. In CPU, registers are used to store information temporarily. That information could be a byte of data processed or an address pointing to the data to be fetched. The vast majority of PIC registers are 8-bit registers.
Literal addressing
Used when moving constant values. For example MOVLW 0x23, will move 23 (hex) to WREG.
Direct Addressing
Used when moving variables. For example MOVWF _myReg will move contents of myReg to WREG.
Opcode in pic microcontroller assembly language
An opcode is short for ‘Operation Code’. It is a number interpreted by your machine (uC) that represents the operation to perform. For example MOVLW is an Opcode.
Labels
A label is an identifier used to represent a line in code or section of a program. Goto statements can be used in conjunction with a Label to jump to the execution of code identified by the particular label. See Task 1 code for example.
pic microcontroller assembly language Instructions
The Following Table is taken from Page 376 of the 18F46K22 Datasheet
Pic microcontroller literal assembly language instruction
pic microcontroller control assembly language instructions
mikroC Debugger can be used to check execution of pic microcontroller assembly language instructions.
pic microcontroller assembly language example 1
Compile the following example code, see how variables change in mikroC debugger, and try to figure out what the code is trying to do. you will not be able to learn assembly language until you do not perform it yourself. I have made the comment with each code for your understanding.
unsigned short myReg1,myReg2; // two Global variable are declared
void main() { ANSELA =2; // asm{ CLRF ANSELA; // Write 0 to ANSELA CLRF ANSELB; // Write 0 to ANSELB CLRF ANSELC; // Write 0 to ANSELC CLRF ANSELD; // Write 0 to ANSELD MOVLW 0xFF; //Move 0xFF to WReg register MOVWF TRISB,0; // Move 0xFF to TRISB MOVLW 0xAA; // Move oxAA to Wreg MOVWF _myReg1,0; // Move 0xAA to myReg1 variable MOVLW 0x55; // Move ox55 to Wreg MOVWF _myReg2,0; //Move 0x55 to myReg2 variable LOOP_START: MOVF _myReg1,0,0; // Move Contents of myReg to WReg MOVWF PORTB; // move Wreg to PORTB MOVF _myReg2,0,0; // move myReg2 to Wreg MOVWF PORTB; GOTO LOOP_START; } } |
pic microcontroller assembly language example 2
The equivalent of the following C code in assembly is given below.Use mickroC debugger to verify your code.
unsigned short x=0;
unsigned short i=0; for (i= 1; i< 10; i++}{ x = x+i; } |
unsigned short x=0; unsigned short i=0; void main() { OSCCON.IRCF0=0; OSCCON.IRCF1=1; OSCCON.IRCF2=1; ANSELA =2; asm{ MOVLW 0X01 ; Loop: INCF _i,1;// i=i+1 MOVF _i,0;// move i to Wreg ADDWF _x,1; //x=x+Wreg MOVLW 9; // Wreg=10 CPFSGT _i; goto Loop }
pic microcontroller assembly language example 3
Write the equivalent of the following C code in assembly. This is used to generate delay by performing no operation instructions.
unsigned short I,J,X;
for (I= 0; I<255; I++){ for (J= 1; J<255; J++){ X = X+J; // dummy instruction to generate a delay of (256*256), do not worry about overflow } } X=I; // Set a break point here
|
unsigned short I,J,X; void main() { OSCCON.IRCF0=0; OSCCON.IRCF1=1; OSCCON.IRCF2=1; ANSELB=0; TRISB=0; asm { MOVLW 0xF0; MOVWF LATB,0; Rotate: RLNCF LATB,1,0; goto Outer Inner: INCF _J,1;// J=J+1 MOVF _J,0;// move J to Wreg ADDWF _X,1; //X=X+Wreg MOVLW 255; // Wreg=10 CPFSEQ _J; goto Inner goto Outer Outer: INCF _I,1;// i=i+1 MOVLW 255; CPFSEQ _I; goto Inner goto Rotate } }
pic microcontroller assembly language examples 4
This assembly language program rotates the bits of LATB to the left
unsigned short I,X,u,J; void DEL(); void main() { ANSELA =2; TRISB = 0x00; LATB = 0x01; asm{ CLRF ANSELA; // Write 0 to ANSELA CLRF ANSELB; // Write 0 to ANSELB CLRF ANSELC; // Write 0 to ANSELC CLRF ANSELD; // Write 0 to ANSELD RLNCF LATB,1; } }
pic microcontroller assembly language examples 5
Following code will select the memory bank and increment the value of i variable.
unsigned short i absolute 0x400; void main() { asm{ CLRF _i,BANKED; MOVLB 4; LOOP_START: INCF _i,1,BANKED; GOTO LOOP_START; } }
pic microcontroller assembly language examples 6
Write the Equivalent Assembly code for the following C snippet.
short var1 = –63 //Assign any negative value short var2 = 0; var2 = abs(var1); //The var2 must get the absolute value of var1 |
Equivalent assembly code
short var1=-10; short var2=0; void main() { asm{ MOVLW 0; MOVFF _var1,_var2; ADDWF _var1,0; BNN end NEGF _var2 end: goto end } }
pic microcontroller assembly language examples 7
Write Assembly Equivalent of the following C function.
//Global Variables
short num1, num2, maximum;
short max_of_2(){ if(num1>num2){ maximum = num1; }else{ maximum = num2; } } |
Equivalent assembly code:
short num1=10; short num2=12; short maximum=0; void main() { asm{ //call max_of_2; max_of_2: MOVFF _num1, _maximum; MOVF _num1,0; CPFSGT _num2 return MOVFF _num2,_maximum return } }
pic microcontroller assembly language examples 8
Write Assembly Equivalent of the following C function. This code finds the maximum value of the array
short array_element = 0;
short i =0; const short size_array = 8; data short My_array[size_array] = {2, 4, 5, 12, 56, 13, 1, 1} absolute 0x0060; void main() { for(i=0;i<size_array;i++){ array_element=My_array[i]; } while(1); } |
Equivalent assembly code
short array_element = 0; short i =0; const short size_array = 8; data short My_array[size_array] = {2, 4, 5, 12, 56, 13, 1, 1} absolute 0x0060; void main() { My_array[1]=2; asm{ LFSR 1, 0x0060; for: INCF _i,1; MOVFF POSTINC1,_array_element; MOVLW _size_array; CPFSEQ _i goto for end: goto end; } }
I will keep updating this post with more pic assembly language programming examples.
hai my name is ezza and im a student . i have one problem with my project, Auto Irragation system using soil moisture sensor and pic microcontroller. Im still new with the programmer and when I already write the coding and build it,then I put the coding into proteus to make sure my coding function as what I want. then when I want to burn it to PIC and hardware, the program not function and its like i never write any coding into PIC. can u help me how to make my PIC function ..
hey, I’m a student from Ethiopia. I faced some difficulities while working my project and I’m sending this comment hoping that you will help me. the project is a simple DC motor speed control using PIC 16f877 microcontroller and the problem is that I couldn’t write the assembly language. I have written the c-language but the assembly language is difficult for me so please help me
I want to write simple 4bit addition programme in pic18 , how can I write it?
Please help me
Hi Radhika, Do you mean 4 bits, as in 1 nibble addition? Or do you mean 4 digits such as 5,349