Microcontrollers
Microcontrollers
Names of Members:
• Muhammad Hamza Zahoor Reg # 398659
• Hammad Asim Reg # 432331
Loops:
Loops are one of the building blocks of logical circuits. They are imperative to reduce the complexity
of any code. Rather than rewriting a block of code, we can put it in a loop that will execute that piece
of code an number of times we tell it to.
In C++ language if we want to rewrite any piece of code we use for, while, do while, etc. meanwhile
in assembly language we use jump instruction, we have two types of jumps, conditional (a condition
being true, we jump) and unconditional (regardless of the state we will jump) jumps, where we jump
to a specific label from defined in the code.
Instructions Actions
JZ Jump if byte is zero
JNZ Jump if byte is not zero
DJNZ Decrement byte and jump if byte is not zero
CJNE A, Byte Jump if A ≠ Byte
CJNE reg, #data Jump if Byte ≠ #data
JC Jump if CY = 1
JNC Jump if CY = 0
JB Jump if bit = 1
JNB Jump if bit = 0
JBC Jump if bit = 1 clear bit
Figure 1: All the Jump instructions on the AT89C51
Nested loops:
Nested loops are also an important topic under loops and are necessary sometimes as you may need
to loop a block more than 256 times (max number that can be counted from 8 bits). Hence, we use
nested loops that can count to 256*256 times (65,536).
It is normal that we don’t normally want to repeat a block of code that many times but rather we focus
on implementing a delay with the help of these nested loops. As the clock runs at 12Mhz, we can
calculate how much time it takes for the loop to complete and using that time to do nothing, we have
successfully implemented a delay.
A point to note is that we need to put the initialization for the counter register (of the nested loop) in
the first loop, so the register can be re-initialized every time the first loop is re-run.
Look up tables:
Look up tables are a good way of reducing complexity for the controller to do, for example computing
the factorial of a given number on the ports. In order to do that, will have to traditionally have to find
all the numbers before that number and then multiply all the number together often in large lines of
code, but by utilizing the ROM, we can store the answers to the data on a chip and the number given
on the port will just act as an index to the right answer which can then be outputted.
This data is written in the code with the prefix of ORG and DB. ORG setting the data location being
used and DB being the set-byte.
CLOCK CYCLE:
DEFINATION:
In 8051-based system, the crystal oscillator has a frequency of 11.0592 MHz when C/T bit of TMOD is 0.
Each machine cycle is made up of 12 clock cycles. Hence for a single machine cycle, the frequency becomes
1/12 × 11.0529 MHz = 921.6 KHz
MACHINE CYCLE:
DEFINATION:
Machine cycle refers to a sequence of steps that a computer's central processing unit (CPU) goes through in
order to execute a single machine language instruction. It is also known as the instruction cycle.
1. Clock Frequency:
The clock frequency for the 8051 microcontroller is given as 11.0592 MHz
This means that the microcontroller executes approximately 11,059,200 cycles per second.
2. Total Clock Cycles:
From the previous example, we calculated the total number of clock cycles for the nested loops:
o Innermost Loop (R0 = 255):
Each DJNZ instruction takes 2 clock cycles.
To convert the total clock cycles into seconds, multiply the total cycles by the time per cycle (90.5
ns):
So, the total delay generated by the nested loops is approximately 46.16 ms.
Lab Tasks
Task 1
The first task was to Transfer the block of data from 20h to 30h to location 40h to 50h using loops.
Here we initialized the data ourselves between 20H to 30H, you can see the output on the memory
section on figure 2.
Task 2
The second task was to continuously scan port P0. If data is other, then 0FFh multiply it with 10 and
send it to port P1 and P2. We will need to use both a Conditional Jump (CJNE) and an unconditional
jump (SJMP). The output of this can be seen in figure 3.
Task 4
Finally, the last task was to write a program to add the following numbers and send the result to P1
and P2. The data is stored at ROM addresses starting from 250H. Here in order to move the data from
the ROM we will use MOVC instruction, in combination with A (index) and DPTR (where our data
is in ROM). This task can be seen in figure 5 (with the answers highlighted.
Task 5
Write a program to toggle all the bits of P1 every 150 ms (using nested loops).
Assume that the crystal frequency is 11.0592 MHz
Code:
Figure 7: output
Task 6
Write a program to generate a frequency of 4Hz on P1.1. Crystal Frequency is
11.0592MHz
CODE:
Figure 9: output
Task7
Write a program to generate a frequency of 80Hz on P1.6. Crystal frequency is
12MHz
CODE:
Proteus Simulation:
Conclusion:
This lab clarified some of the basic concepts of loops and lookup tables and delays in assembly
language programming, especially regarding the 8051 microcontrollers. It successfully explained the
concept and usage of these instructions using examples and explained how they can be used to reduce
computational complexity.
Page 13 of 13