In this project, I started to learn the MCS-51 assembly language from my dad. First, I learned some basic MCS-51 instructions, such as "MOV", "INC", "CLR C", and "SUBB". I made a simple 7-segment display digit with machine code programming. Then, I started writing code in assembly language. The development environment used was MIDE-51, an open source program under Windows 10. HEX code can be downloaded to wire-wrap an 89C52 prototype. The download function on PC is implemented with Tera Term's "Macro 'file send'". The first assembly code I wrote was for a clock, displaying HH:MM:SS on Tera Term. The code diagram, source code, and the result are shown below.
After my dad and I were done with the first project, I took off all the wires, except the switch and the single LED were kept, and put on new ones for our second project. The installment of the new wires has been recorded in the following schematic.
The second project is a MCS-51 microprocessor hardware system, and I wired the prototype. After the wiring was done, I connected the board to Tera Term and type the machine code on Tera Term. I began with the basics, using machine code to display a numbers (0, 1, 2, 3, etc.) on our LED 7-Segment display. Shortly, I found that machine code is extremely hard to memorize. Therefore, I started to learn assembly language from my dad, which represents the machine code with a string of letters and numbers (instruction). With the MCS-51 assembly language, I began writing code and using it to debug the MCS-51 prototype. The development environment was MIDE-51 (open source program).
This is the board I made (front view). The LED display is no longer used, as I've begun to use Tera Term as the code-displayer.
This is the same board (back view). I took off all the previous wires and installed new ones in its place.
After I was done with writing machine code on Tera Term, I switched to MCS-51 assembly language, which was written on MIDE-51. I slowly progressed towards making the clock: in the process, my dad and I also made some mini projects that would aide our advancement in the understanding of assembly language.
I also wrote a code on MIDE-51 that would display a counting-up sequence on the LED 7-segment display. The 7-segment LED digit is driven by Port1(7....0)= NOT (a,b,c,d,e,f,g,PD). The code for it is below -->
org 0000h
here1: mov R7,#0
mov 30H,R7
here5: lcall display
lcall Delay2s
inc R7
mov a,r7
clr c
SUBB a,#0fh ; count to this number, 02h-10h
JZ here1 ; if R7 reaches 12, then R7 =0 if (R7==0xc
ljmp here5
;here1: mov R7,#0
; ljmp here5
org 0100h
DB 03H ;”0”
db 9FH
db 25H
db 0DH
db 99H
db 49H
db 41H
db 1FH
db 01H
db 19H
db 11H
db 0C1H
db 63H
db 85H
db 61H
db 71H
;”F”
org 0200H
Display: MOV DPTR,#0100h ; #0100 is the start address where we put the digit BYTE
MOV A,R7
MOVC A,@A+DPTR ; Look up the digit BYTE table to A
MOV P1,A ; display number on 7 segment LED diplay
RET
org 0300H
;setb P1.0
;clr p1.6
Delay2s: mov r3,#20H
here3: mov r2,#0
here2: mov r1,#0
here: djnz r1,here
djnz R2,here2
djnz r3,here3
ret
;clr p1.7
;here9: sjmp here9
END
As shown above, I also learned some of the ASCII table, which I used in the code.
org 0000h
mov 40h,#0 ;clear buffer
mov 41h,#0
mov 42h,#0
mov 43h,#0
mov 44h,#0
mov 45h,#0
;step 2 Display inital 00:00:00 of your buffer 40H-45H with putchar
step2: lcall kbhit
cjne R7,#0,readKey
; mov a,r7
;clr c
;subb a,#0
;jz display_nom
sjmp display_nom
;if no key is pressed, go to normal time display. otherwise, read keyboard, accpet numbers, and put into 40H-45H
readKey: mov r0,#40h
mov r2,#3
loopkey1: mov r3,#2
loopkey: lcall getchar
lcall putchar
mov a,r7
clr c
subb a,#'0'
mov @r0,a
inc r0
djnz r3, loopkey
mov r7,#':'
lcall putchar
djnz R2, loopkey1
mov r7,#0dh
lcall putchar
mov r7, #0ah
lcall putchar
display_nom: lcall disTime
;step 3 ;Delay 1 second
lcall Delay1s
;step 4 Increase 45H by 1 and handle all the carries, display buffers
inc 45H
mov a,45H
clr c
SUBB a,#10 ; xx:xx:x(45H) if (45H)=10, Carry in to (44H), to count from 0 to 9
JNZ next1
inc 44H ;xx:xx:(44H)x
mov 45H,#0
mov a,44H
clr c
SUBB a,#6
JNZ next1
inc 43H
mov a,43H
mov 44H,#0
clr c
SUBB a,#10
JNZ next1
inc 42H
mov 43H,#0
mov a,42H
clr c
SUBB a,#6
JNZ next1
inc 41H
mov 42H,#0
; if 40H=0 go to 41H mode9, else got to 41H mode2
mov a,40H
clr c
subb a,#0
; 40H: 0 41H: 0-9
mov a,41h
jnz mode2
clr c
subb a,#10
sjmp next3
mode2: inc 40H
mov a,40H
mov 41H,0
clr c
subb a,#3
mov a,40H
clr c
subb a,#2
mov 40H,0
jnz next1
next3: inc 40h
mov 41h,#0
mov a,40H
clr c
SUBB a,#2
mov 40h,#0
sjmp next4
next4: mov a,41H
clr c
SUBB a,#3
next1: ljmp step2 ;display_nom
disTime: ; convert number in 40h to 45H to ASCII and display
mov a,40H
add A,#'0'
mov r7,a
lcall putchar
mov a,41H
add a,#'0'
mov r7,a
lcall putchar
mov r7,#':'
lcall putchar
mov a,42H
add a,#'0'
mov r7,a
lcall putchar
mov a,43H
add a,#'0'
mov r7,a
lcall putchar
mov r7,#':'
lcall putchar
mov a,44H
add a,#'0'
mov r7,a
lcall putchar
mov a,45H
add a,#'0'
mov r7,a
lcall putchar
mov r7,#0dh
lcall putchar
mov r7,#0ah
lcall putchar
ret
justinl: lcall putchar;
inc R7
djnz r6,justinl
mov r7,#0ah
lcall putchar;
mov R7,#'A'
lcall putchar;
lcall puts
sjmp $
putchar: jnb ti,$
clr ti
mov sbuf,R7 ;the ASCII to be displayed in R7
ret
getchar: jnb ri,$ ;read char from teraterm and put in R7
mov r7,sbuf
clr ri
ret
kbhit: jnb ri,nokey ;check if key is pressed, if so, return R7=1, otherwise R7=0
mov r7,#1
ret
nokey: mov r7,#0
ret
;----------------------------------------------------
puts: mov dptr, #bufLen
movx A,@DPTR
mov R6,a
mov dptr,#disBuf
movx a, @dptr
mov R7,a
bufLen: db 4
;disBuf: db "abcd", 0ah
disBuf: db "01:23:45",0ah
Delay1s: mov r3,#10H
here3: mov r2,#0
here2: mov r1,#0
here: djnz r1,here
djnz R2,here2
djnz r3,here3
ret
END
The diagram is shown above. This is the diagram I used to design the code. After drawing and analyzing the code onto this diagram, I was able to dissect the code into smaller pieces and focus on which parts to fix when the code went wrong. This diagram was created by DIA, a program my dad introduced me to that helped with graphs and diagrams.
This is the process in which I ran my code. First, I would write it on MIDE-51 and then compiled it with assembler and created hex file and a list file. I downloaded the hex file to the prototype with Tera Term - through an option of Macro. After I ran the code, the Tera Term would then display the results.