Q) Implement a BCD Up-Down Counter on the Logic Controller Interface.
This program will act as a BCD up-down counter to count from 0 to 10 and then back.
Algorithm:
Step 1: Connect Logic Controller to PC.
Step 2: Set the 8255 using control word, initialize the control word i.e., 82H
Step 3: Store the control word in the Control register.
Step 4: Initialize the counter to 10(count to loop for displaying up counter), and AL with 00.
Step 5: Initialize the Port A address and display the contains of AL, call some delay and add 01h to AL and again display it. Loop repeats until counter becomes 0.
Step 6: Again initialize counter to 10, and AL with 00, each time subtract 01 from AL and display contents of AL, until counter becomes 0.
Step 7: Initialize the counter with 09H and AL, with 01H, display the contents of AL.
Step 8: Rotate the contains of AL once loop back and again display AL, the loop repeats until counter becomes 0.
Step 9: Stop
CODE:
This program will act as a BCD up-down counter to count from 0 to 10 and then back.
Algorithm:
Step 1: Connect Logic Controller to PC.
Step 2: Set the 8255 using control word, initialize the control word i.e., 82H
Step 3: Store the control word in the Control register.
Step 4: Initialize the counter to 10(count to loop for displaying up counter), and AL with 00.
Step 5: Initialize the Port A address and display the contains of AL, call some delay and add 01h to AL and again display it. Loop repeats until counter becomes 0.
Step 6: Again initialize counter to 10, and AL with 00, each time subtract 01 from AL and display contents of AL, until counter becomes 0.
Step 7: Initialize the counter with 09H and AL, with 01H, display the contents of AL.
Step 8: Rotate the contains of AL once loop back and again display AL, the loop repeats until counter becomes 0.
Step 9: Stop
CODE:
Explanation:
Our data segment contains three memory locations assigned with port addresses of the logical interface. Port A(PA), Port B(PB), Port C(PC) and Control registers(CR) are equated with appropriate port addresses.
Now that controller is activated, set the counter CX to 10 (to display from 00-09).
AL (assumed as count value of BCD up down counter) is loaded with value ‘0’ initially, to display it on controller.
BACK is a label.
Move the output port address to DX to display on the controller.
Display the AL content using OUT command.
Now, increment AL value by 1, using INC command.
Perform DAA (Decimal adjustment after addition).
What is DAA?
Instead of telling the theory, let me give an example.
Consider the decimal value 10. It’s binary is actually 00001010. That is, AH=0000 AL=1010.
After DAA, content of AL will be altered as, first digit’s binary will be in AH and second digit’s binary will be in AL. (then it will be helpful to analyze the digits in the logic controller).
After DAA, call some delay ( to introduce some time gap between each count display)
Then loop to label BACK (CX will be decreased by 1 after LOOP).
So loop will run 10 times, displaying from 00-10 on the controller.
Now you have to display from 10-00
Move output port address to DX. Display the content of DL using OUT command (which for the first iteration, is 10).
Decrease AL by 1 using DEC instruction.
Perform DAS (Decimal adjustment after subtraction), same work as DAA but that was after addition, this is after subtraction.
Call some delay and decrease CX by 1 and if CX is not zero jump back to BACK1.
And delay procedure is same.
Code ends and so does label START.
Output:
Seen in logic controller. (see H/W basics for more info in setting up Controllers)
ASSUME CS:CODE,DS:DATAProgramming model selected contains one data segment(DS) and one code segment (CS).
DATA SEGMENT
CR EQU 0e803H
PA EQU 0e800H
PB EQU 0e801H
PC EQU 0e802H
DATA ENDS
Our data segment contains three memory locations assigned with port addresses of the logical interface. Port A(PA), Port B(PB), Port C(PC) and Control registers(CR) are equated with appropriate port addresses.
CODE SEGMENTCode segment starts with initializing of data segment at the line 3 and 4 in the snippet.
START:
MOV AX,DATA
MOV DS,AX
MOV DX,CRActivate the logic controller by activating control word using these commands. (see the previous article for its explanation if you do not know).
MOV AL,80H
OUT DX,AL
Now that controller is activated, set the counter CX to 10 (to display from 00-09).
MOV CX,10Counter is set to 10.
MOV AL,0H
BACK:
MOV DX,PA
OUT DX,AL
INC AL
DAA
CALL DELAY
LOOP BACK
AL (assumed as count value of BCD up down counter) is loaded with value ‘0’ initially, to display it on controller.
BACK is a label.
Move the output port address to DX to display on the controller.
Display the AL content using OUT command.
Now, increment AL value by 1, using INC command.
Perform DAA (Decimal adjustment after addition).
What is DAA?
Instead of telling the theory, let me give an example.
Consider the decimal value 10. It’s binary is actually 00001010. That is, AH=0000 AL=1010.
After DAA, content of AL will be altered as, first digit’s binary will be in AH and second digit’s binary will be in AL. (then it will be helpful to analyze the digits in the logic controller).
After DAA, call some delay ( to introduce some time gap between each count display)
Then loop to label BACK (CX will be decreased by 1 after LOOP).
So loop will run 10 times, displaying from 00-10 on the controller.
Now you have to display from 10-00
Again set the counter to 10.
MOV CX,10
BACK1:
MOV DX,PA
OUT DX,AL
DEC AL
DAS
CALL DELAY
DEC CX
JNZ BACK1
Move output port address to DX. Display the content of DL using OUT command (which for the first iteration, is 10).
Decrease AL by 1 using DEC instruction.
Perform DAS (Decimal adjustment after subtraction), same work as DAA but that was after addition, this is after subtraction.
Call some delay and decrease CX by 1 and if CX is not zero jump back to BACK1.
MOV AH,4CHTerminate program.
INT 21H
DELAY PROC
PUSH BX
PUSH DX
MOV DX,0FFFFH
UP2: MOV BX,0FFFFH
UP1: NOP
DEC BX
JNZ UP1
DEC DX
JNZ UP2
POP DX
POP BX
RET
DELAY ENDP
CODE ENDS
END START
And delay procedure is same.
Code ends and so does label START.
Output:
Seen in logic controller. (see H/W basics for more info in setting up Controllers)
Share your views about this article!