8051 microcontroller has 40 Pins. It has 4 I/O ports (P0, P1, P2, P3) with 8 pins each.
Pins 1 to 8 [PORT 1] − This port doesn’t serve any other functions. It is internally pulled up, bi-directional I/O port for connecting only input or output devices.
Pin 9 − It is a RESET pin, which is used to reset the microcontroller to its initial values.
Pins 10 to 17 [PORT 3] − These pins are known as Port 3. This port serves some functions like external interrupts, timer input, control signals, serial communication signals RxD and TxD, etc.
Pins 18 & 19 − These pins are used for interfacing an external crystal to get the system clock.
Pin 20 − This pin is connected to ground.
Pins 21 to 28 [PORT 2] − These pins are known as Port 2. It serves as I/O port. Higher order address bus signals are also multiplexed using this port.
Pin 29 − This is PSEN pin which stands for Program Store Enable. It is used to read a signal from the external program memory.
Pin 30 − This is EA pin which stands for External Access input. It is used to enable/disable the external memory interfacing.
Pin 31 − This is ALE pin which stands for Address Latch Enable. It is used to demultiplex the address-data signal of port.
Pins 32 to 39 [PORT 0] − These pins are known as Port 0. It serves as I/O port. Lower order address and data bus signals are multiplexed using this port.
Pin 40 − This pin is used to provide power supply to the circuit.
Reset PIN / Pin 9 – This PIN is attached to a 10 microfarad capacitor, when the microcontroller boots up the capacitor is not charged, its a open circuit and reset pin is considered to be grounded. When this happens the Program counter register is set to 0000H so program control moves to starting point. After that the capacitor is charged so the reset pin receives a value of 1 and is deactivated. We can reset the microcontroller at any time by pressing the Switch S1 which will discharge the capacitor and send a zero to reset pin again.
The values for capacitor and resistor are chosen in such a way as to match the machine cycle.
When we attach a external memory to our controller (be it a ROM or a RAM) we need to use the other pins (29,30,31)
EA PIN (PIN 30) – External Access – If we connect a external ROM/Code memory then this PIN is grounded, otherwise this has to be connected to VCC to have a value of 1.
RD – PIN 17 (P3.7) and WR – PIN 16 (P3.6) are used for access data from data memory (RAM)
Connecting external memory
When we connect external memory, lets say 8 bit 64KB, we have 2^16 addresses, for this we need 16 address lines, and we need 8 datalines.
Port 0 and Port 2 are used to send address and data to the external memory
Port 0 will send 8 lower bits of address information, however we will also use Port 0 for data lines.
For higher 8 bits of address we will use port 2.
The pins on Port 0, the pin ids 39 to 32 are multiplexed address/data lines, which means that they will send address and data both at different time interval.
In the 1 phase of machine cycle, address will flow over multiplexed lines, and for remaining part of machine cycle, the data will flow over the multiplexed lines.
PSEN – Program Store Enable – will be active if EA pin is 0 (means the external memory is connected.) This connects to Chip Select and Read line, to signal the code memory about the intention to read.
ALE – Address Latch Enable – Demultiplex the address data lines. It will be high (0) for 1 T state in the beginning machine cycle.
Connecting a external memory to the microcontroller
We need an IC 74L3373 to de-multiplex the data/address lines. This IC contains 8 D flip flops, and 8 tri state inverters. This IC is connected to the Address data lines (AD0 to AD7) coming from port 0.
The whole connection looks like this.
The D flip flop attached here is a level triggered flip flop which means the value of Q is changed during the time when the clock signal is high. There are positive edge triggered and negative edge triggered flip flops which change the output value only when the clock state transition is happening and the value is determined based on the state of the input at that instance.
The Q inverse output from the flip flop is fed to the tri state inverter, A tri state inverter relies on an enable signal to generate output. Only when the enable flag is OFF/0 then the output is negative of input. When Enable input becomes 1, then the inverter behaves as a open circuit.
In the beginning of the machine cycle, the ALE (Address Latch Enable) is 1, so the D flip flop will operate and pass the value to the address lines A0-A7 from Port 0.
Once the address information is sent to memory, the ALE changes to 0, which turns off the D flip flops, so any changes in AD0 to AD7 is not sent to memory A0-A7 and the data flows to and from between data lines d0-d7 in memory and the microcontroller.
As we see in this case the PSEN is connected to chip select and Read pins, implying that this memory is active and we are reading from this memory. This works in case of a code memory.
In case we connect a data memory to the microcontroller, we will connect the read and write pins from memory to the P3.7 (Pin 17) and P3.6 (Pin 16) respectively.
Assembly language
In late 1980s the Therac-25 radio therapy machine was developed which used high energy electrons instead of Cobalt-60 to create a high energy beam to treat tumors.
This machine was a collaboration of AECL – Atomic Energy of Canada Limited and CGR Compagnie générale de radiologie from France.
The software for the Therac-25 was developed by one person over several years using PDP-11 assembly language. Well it was not tested at all except by developer and the result was some patients lost their lives because of huge amount of radiation received.
By nature Assembly language is hard to read and debug.
Addressing modes
Instructions requires operands to be processed, and there are multiple ways to pass the information to the instruction through the operand
Immediate addressing – We are sending the data directly to the operand (eg. #30H – we are sendin a value 30H)
Direct addressing – We are sending the data stored at the given location to the operand (eg. 30H – we are sending value stored at 30H to the operand)
Register direct addressing – We use the register name instead of memory address. (eg R0, R1 .. R7 or A or B – we are sending value stored at register to the operand)
Register indirect addressing – We use the value stored at register name to refer to the memory address. (eg @R0, @R1 only – we are sending value stored at memory address stored in register to the operand)
Variable definition
DB – Define Byte – allocates 1 byte
DW – Define Word – allocates 2 bytes
MOV – instruction moves the value from source to destination, Syntax is MOV DEST, SRC. the SRC DEST are referred using addressing modes
INC – Increments the value defined by the operand
DEC – Decrements the value defined by the operand
Loops
LJMP – Long jump instruction uses 3 bytes (1 for instruction name, and 2 bytes for the address). Instead of address the user can use a label also.
Example LJMP 20H will send control to memory location 20H
Example LJMP AHEAD will send control to line where AHEAD lable is defined as AHEAD: INSTRUCTION
SJMP – Short jump instruction uses 2 bytes (1 for instruction name, and 1 bytes for the relative address). The 1 byte is a signed value between -127 and 127
JC – Jump Carry – is a conditional short jump which will jump only if carry flag is 1.
JNC – Jump NO Carry – is a conditional short jump which will jump only if carry flag is 0.
JZ – Jump Zero – is a conditional short jump which will jump only if content of accumulator (A register) is 0.
JNZ – Jump Not Zero – is a conditional short jump which will jump only if content of accumulator (A register) is not 0.
JB – Jump Bit – (JB 20H 2H) is a conditional short jump which will jump only if content of bit mentioned in first operand is 1
JBC – Jump Bit Clear – does the same thing as JB above, and will also clear the bit mentioned in the operand
DJNZ – Decrease Jump Not Zero – The bit refered here can be a direct addess or a bank register. It will decrease the content of the bit refered in operand and then jump. It will not jump if the value of the bit becomes 0 after decreasing
Lets take a basic example, we have to move 5 bytes of data stored starting from 50H to 70H
One way to do it is
ORG 0000H ; Starting location of program
MOV 70H, 50H; move content from 50H to 70H
MOV 71H, 51H; move content from 51H to 71H
MOV 72H, 52H; move content from 52H to 72H
MOV 73H, 53H; move content from 53H to 73H
MOV 74H, 54H; move content from 54H to 74H
END
Other way to write this using loops is
ORG 0000H ; Starting location of program
MOV R0, #50H ; set source starting address in R0
MOV R1, #70H ; set destination starting address in R1
MOV R2, #05H ; this is a counter which will cound downwards
BACK: MOV A, @R0 ; Move data from Source byte to accumulator
MOV @R1, A ; Take the accumulator value and put it in destination byte
INC R0 ; Increase the address pointer in R0 source register
INC R1 ; Increase the address pointer in R1 destination register
DNJZ R2, BACK ; This will jump back 5 times decreasing R2 every time
END
Cheers!! – Amit Tomar