5. Assembler Programming
5.1. Basic programming examples
Examples of programs in assembler for the microcontroller 8051.
E-1. Counter in the accumulator from 0 to 99 in Hexadecimal
NAME CONTADOR_HEX
ORG 0
INICIO: MOV A,#0
REPETIR: INC A
CJNE A,#99,REPETIR
END
Flow diagram of the counter in hexadecimal.
E-2. Counter in the accumulator from 0 to 99 in BCD
NAME CONTADOR_BCD
ORG 0
INICIO: MOV A,#0
REPETIR: ADD A,#1
DA A
CJNE A,#99,REPETIR
END
Flow diagram of BCD decimal counter.
E-3. Right hand rotation of 1 bit in the accumulator.
NAME ROTACION_ACUMULADOR < br / > < br / > 0 ORG < br / > < br / > HOME: MOV A, #1 < br / > REPEAT: RR to < br / > JMP repeat < br / > < br / > END
E-4. Delay by 256 instructions DJNZ program.
The time of a delay by program does not have a fixed value, depends on the instructions that are part of the loop (the used machine cycles) and the microprocessor (CPU) clock speed.
For this example in question, the loop is performed on the DJNZ instruction, which needs two cycles machine for execution. Each machine cycle uses 12 clock periods. If the CPU frequency is 12 MHz, the time taken to perform 256 iterations is 512 μ s (microseconds).
NAME RETARDO_1
ORG 0
INICIO: MOV R0,#0FFH
RET1: DJNZ R0,RET1
END
Flow chart of a timer by program.
E-5. Nested software Delay three registers R0, R1 and R2.
In order to make software delay for longer than the second has to do with the nesting of loops. The figure shows a nested loops with registers R0, R1 and R2. In this system, each decrement of the register R1, the register R0 is decremented 256 times, and for every decrease of R2, R1 has made 256 iterations, so the final result is 2 ^ 24 = 16,777,216 iterations. The resulting time is approximately 33.554432 seconds, according to the conditions outlined in the previous example.
To determine the time have to change the bits that represent the most significant values corresponding to R2.
Wasted computing power of the CPU timings or delays in software only makes sense as the microprocessor has to serve only a process. The normal work is to use timers and interrupts for timing tasks, and leave the CPU free for other processes.
NAME RETARDO_2
ORG 0
RETARDO: MOV R2,#0FFH
RET3: MOV R1,#0FFH
RET2: MOV R0,#0FFH
RET1: DJNZ R0,RET1
DJNZ R1,RET2
DJNZ R2,RET3
END
Flow chart of a software timer with three records.