Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

assembly code for input and output

Status
Not open for further replies.

vead

Full Member level 5
Full Member level 5
Joined
Nov 27, 2011
Messages
285
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
india
Visit site
Activity points
3,815
I am new in assembly language I want to learn how to write statement for input and output port
use port P0 for input
use port P1 for output

8 led connected to P0
8 switch connected to P1

how to write assembly statement for input port and output port?
 

You didn't mentioned which microcontroller you are using. Assuming that you are using 8051s,

Code:
MOV  P0,#0FFH
MOV  P1,#00H

Where '0FF' (zero-F-F) means 11111111 in binary & 255 in decimal. This will write 1 in every pins, thus making them input (high). 'H' after '0FF' or '00' means hexadecimal.
'00' in any format means 0. It will make every pin output (low).
If its 8051, then you have to add pull up resistor network in P0.
Older 8051 doesn't source enough current to drive LEDs directly, so if you want to drive LEDs through P0, set it to output.
 

thanks for reply can you write code for my basic understanding with comments
 

ok I am trying to write code
HTML:
    ORG 000H               ; START AT 0 LOCATION
 AGAIN:  MOV A, #FFH     ; A=FF
            MOV P0,A           ; P0 AS INPUT PORT WHERE SWITCHES  ARE CONNECTED
            SETB P0             ; SET LOGIC 1 FOR EVERY PIN OF PORT P0
            ACALL DEALY      ; WAIT
            CLR A                 ; A=000
            MOV P1,A           ; P1 AS OUTPUT PORT WHERE LED ARE CONNECTED 
            SETB P1              ;SET LOGIC 0 FOR EVERY PIN OF PORT P1 
            SJMP AGAIN         ; JUMP AGAIN KEEP DOING
            END                    ; END THE PROGRAM
 
Last edited:

In your code, moving the value of FF to A (accumulator), then moving the value of A to P0 is unnecessary. You can directly move the value FF to P0, but remember, ALWAYS put '0' (zero) before any hex value. So you write, MOV P0,#0FFH.
'SETB P0' is not a code. You can only set any PIN, not the whole port. And you don't need it after moving value of FF to that port.

There is no "DEALY" subroutine in your code. So you cannot call such. And always pay attention to the spellings. Its 'DELAY'.
Same will go with P1 port.

After making them input & output, you no need to jump back to "AGAIN" subroutine, or else you will keep looping and doing same work again & again.

So your code just to make ports input & output will be..

Code:
ORG 00H             ;Start from vector 0
MOV  P0,#0FFH    ;Moving value of FF to port 0 and making it input
MOV  P1,#00H     ;Same as above, making it output

Now if you want to control LEDs with the switches, then, as Mr. Jayant said, you have to connect LEDs to P1 port and switches to P0. You have to keep scanning P0 for switch input & turn ON or OFF its consecutive LEDs.
 

In your code, moving the value of FF to A (accumulator), then moving the value of A to P0 is unnecessary. You can directly move the value FF to P0, but remember, ALWAYS put '0' (zero) before any hex value. So you write, MOV P0,#0FFH.
'SETB P0' is not a code. You can only set any PIN, not the whole port. And you don't need it after moving value of FF to that port.

There is no "DEALY" subroutine in your code. So you cannot call such. And always pay attention to the spellings. Its 'DELAY'.

Same will go with P1 port.

After making them input & output, you no need to jump back to "AGAIN" subroutine, or else you will keep looping and doing same work again & again.

So your code just to make ports input & output will be..

Code:
ORG 00H             ;Start from vector 0
MOV  P0,#0FFH    ;Moving value of FF to port 0 and making it input
MOV  P1,#00H     ;Same as above, making it output

Now if you want to control LEDs with the switches, then, as Mr. Jayant said, you have to connect LEDs to P1 port and switches to P0. You have to keep scanning P0 for switch input & turn ON or OFF its consecutive LEDs.

thanks for help now i learned how to write statement for input and output port
can you tell me how to write statement for pin like low pin or high pin
 


Code ASM - [expand]
1
2
setb P1.0
clr P2.1



will do the job but before trying to set or clear the required PORT bit, you must have made the PORT bit as output pin. In the above code P1.0 and P2.1 are assumed to be set as output pin already.
 

ok now i am able how to write statement for port and pin and now i want to learn how to make loop for led
turn on led
wait 100ms
turn off led
how to write delay program
 

You have to use 2 loops (nested loops). The inner should give a delay of 1 ms. The out will give desired delay. If outer is set to 100 then inner loop should be executed 100 times and you will get 100 ms delay.
 

You have to use 2 loops (nested loops). The inner should give a delay of 1 ms. The out will give desired delay. If outer is set to 100 then inner loop should be executed 100 times and you will get 100 ms delay.
I have no idea how to make loop but I am trying

SETB P0.1 ; pin 1 is high then turn on led
ACALL DELAY ; wait
CLR P0.1 ; pin 1 is low then turn off led
 

One way is to use timer. And another way is to use nested loop. The advantage of using timer is that you can keep the delay work in background and do other works, as timer doesn't needs the processor to count the delay. But for simplicity, lets go for loop. Since microprocessor doesn't understand about time like humans, so no mater what you do, you have to waste instructions to consume your desired time. You cannot just tell it to wait for certain time. The 8051 will first waste 12 clock cycles to execute 1 machine cycle. So when using 12MHz clock, your resultant clock will be 1MHz. That is, the smallest time for single instruction is 1us. The following code will waste 100ms. Subroutine loop3 will waste 1ms and loop2 will multiply it to another 100 times. NOP consumes 1 machine cycle & DJNZ takes 2 machine cycle. So in loop3 subroutine the total delay for the 3 line is (1+1+2)x250us. The last line will repeat that 3 line another 100 times.

Code:
Loop1:
mov r1,#100

loop2:
mov r2,#250

loop3:
nop
nop
djnz r2,loop3

djnz r1,loop2

So, your code will be

Code:
start:
setb p1.0
acall delay
clr p1.0
.
.
.
.
.
.

delay:
mov r1,#100

loop2:
mov r2,#250

loop3:
nop
nop
djnz r2,loop3

djnz r1,loop2
 

its very good explanation for me i want learn more about assembly i know the arithmetic instruction add sub mul div I know they are use for addition subtraction division multiplication of bits or byte but i don't know really how and when we use this instruction in making project if possible can you explain with example
 

See, there is a lot of things you have to know. It will be very helpful to you if you start google your queries. You will get better explanations with examples. Refer to some tutorials, download some ebooks and check out all the MCS 51 codes you need to know from here.

Google search for 8051 tutorial

You have to learn by your own. If you get stuck somewhere you can ask question in this forum. We are always here to help.
 

for making any port as input, move high(mov P1,#0FFH) and for making it as output, there is no need to define anything. If you google the basic structure of port you find that there is a "D"flip- flop present at every pin. which Q bar is connected to a mosfet. this command MOV P1,#0FFh actually sets the flip-flop of whole port. (you can do same for a single pin by setb P1.0). this command open the mosfet. so you get a high voltage at your port. at that point same voltage is fed to internal circuitry of micro controller. so when you give ground to pin then micro controller take i/p "0", and when you give high voltage it take i/p "1". if you don't give any thing then it takes high.
 
  • Like
Reactions: vead

    vead

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top