Maker Pro
Maker Pro

assembly program for 8051

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
I want to learn how to write statement for input and output pins
2 LED connected to PINS P01 and P02
2 SWITCH connected to PINS P11 and P12

how to write statement for input and output pin in assembly
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
12,613
Joined
Nov 17, 2011
Messages
12,613
If you want to write assembly language code for any microcontroller, you need to get acquainted with the mnemonics and the syntax of the particular microcontroller. You will find all that in the manual of the controller.
You need to get that manual. It is my opinion that you will not come far without it.

The 8051 uses MOV instructions to move data from and to ports. Here is a tutorial.
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
thanks for reply i have read this, in this tutorial shown how to make port as input but not shown how to make port as output
i have read some where to make port as input we write
MOV A; #FFh
MOV P1; A
If it is correct how to make statement for output port
 

brevor

Apr 9, 2013
87
Joined
Apr 9, 2013
Messages
87
With an 8051 you dont "make a statement for an output port" it is quasi bidirectional. You need to read the databook concerning port reads and writes.
It would be MOV P1,A
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
The standard 8051's digital I/O ports are "quasi-bidirectional" and are not explicitly configured for either input or output. There is no data direction register or tri-state control register as there is on most other microcontrollers.

Each port has an output register and an input register. When you set a bit in the output register to 1, the corresponding pin goes high but is only pulled high weakly (this is called a "weak pullup"). External circuitry (such as a pushbutton connected from the pin to GND/VSS) can then pull that pin low, and the pin state can be read on the input register.

When you set a bit in the output register to 0, the corresponding pin goes low with a strong pull-down, and cannot be used for input. For this reason, pins that are used for output functions are normally arranged to be active low, and loads (e.g. LEDs) are normally connected from VCC to the pin.

Edit: Port 0 (if present) does not have weak pullups.

Edit2: The read-only input register and the write-only output register are actually accessed through the same location in the SFR (special function register) address space. The port registers are directly bit-addressable using the 8051's bit-processing instructions (CLR bit to drive the pin low, SETB bit to set it high so it can be used for input, and MOV C,bit to read the state of the pin).
 
Last edited:

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
ok i am trying to write code please correct me if i am wrong
MOV A, #FFH ; A=FF
MOV P0,A ; PORT AS INPUT PORT
MOV P1, A ; PORT AS OUTPUT PORT
 

vead

Nov 27, 2011
473
Joined
Nov 27, 2011
Messages
473
You're wrong. Do you understand my explanation in post #5? If not, you need to learn more about digital electronics and binary logic. Then you should be able to understand the 8051's I/O port architecture. Here's another reasonably good 8051 tutorial I found: http://www.mikroe.com/products/view/267/architecture-and-programming-of-8051-mcu-s/

this statement is correct or wrong
HTML:
 ORG 00H             ;Start from 000
MOV  P0,#0FFH              ; load value FFH in to P0 as input port  
MOV  P1,#00H                ; load value 00H into P1 as output port
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
this statement is correct or wrong
HTML:
 ORG 00H             ;Start from 000
MOV  P0,#0FFH              ; load value FFH in to P0 as input port  
MOV  P1,#00H                ; load value 00H into P1 as output port
Wrong.

1. It's better to use the 8051's bit manipulation instructions on the ports instead of reading and writing whole 8-bit bytes from/to them. The syntax for referencing bits in SFRs is different for different assemblers. What assembler are you using?

2. You haven't shown a schematic, but from your description, I don't think you've connected the switch and the LED(s) correctly. Post a schematic.

3. You have limited value as a hardware or software engineer if you are not able to (or worse, don't even try to) answer your questions through your own research and prior knowledge. The information you need is all freely accessible on the internet. Start by learning about the following topics (Google them): MOSFET switch, open-drain output, weak pullup, active-low, quasi-bidirectional. (These terms all come from the 8051 data sheet and tutorials. Whenever you find unfamiliar terms in a data sheet, Google them. D'oh!) Then read the 8051 tutorials and the data sheet (mainly the data sheet) and make sure you understand everything in there. If you don't understand something... you know what do do, right?
 
Last edited:
Top