yasm81
Newbie level 1
Proff gave us the code and we need to modify it ... but we never learnt asembly so I don't understand the code in each step AT ALL..we have only 1 week..anyone has any idea? here is the codes
; period.asm: measures the period at pin p2.0
; by Jesus Calvino-Fraga, 2007
;
; Uses the serial port of the microcontroller to display the
; measured period. Connect the microcontroller serial port
; to the computer using HyperTerminal or any other terminal program
; configured at 115200 baud, 2 stop bits, no parity, handshake none.
;
$MOD52
org 0000H
ljmp MyProgram
org 000BH ; Interrupt vector for timer 0
ljmp Timer0ISR
org 001BH
ljmp 1234H ; Needed by CMON51
; Increment 16-bit count in R0-R1
Timer0ISR:
inc R1
cjne R1, #0, done
inc R0
done:
reti
;Converts the hex number in R0-R1 to BCD in R2-R3-R4
;Changes R0 to R5 and the accumulator.
;Takes 310 machine cycles and 28 bytes.
hex2bcd:
mov R2, #0 ; Set BCD result to 00000
mov R3, #0
mov R4, #0
mov R5, #16 ; Loop counter.
hex2bcd_L0:
mov a, R1 ; Shift R0-R1 left through carry
rlc a
mov R1, a
mov a, R0
rlc a
mov R0, a
mov a, R4 ; Perform bcd+bcd+carry using BCD numbers
addc a, R4
da a
mov R4, a
mov a, R3
addc a, R3
da a
mov R3, a
mov a, R2
addc a, R2
mov R2, a
djnz R5, hex2bcd_L0
ret
;Initializes the serial port
InitSerial:
clr TR1 ; Stop timer 1
mov a,#0FH ; Set timer 1 as 8-bit autoreload timer
anl a,TMOD
orl a,#20H
mov TMOD,a
orl PCON,#80H ; SMOD=1 -> k=2: double baudrate
mov TL1,#0FFH ; Set baud rate to 115200 for 22.1184MHz crystal
mov TH1,#0FFH
setb TR1 ; Start timer 1
mov SCON,#52H ; Serial port in mode 1, ren, txrdy, rxempty
ret
;Sends the byte in the accumulator through the serial port
putchar:
jnb TI, putchar ; Wait for the last character to transmit
clr TI
mov SBUF, a
ret
;Sends the zero terminated string pointed by DPTR through the serial port
puts:
clr a
movc a, @a+dptr
jz puts_end
lcall putchar
inc dptr
jmp puts
puts_end:
ret
;Prints the BCD number stored in R2-R3-R4
printBCD:
; Digit 5
mov a,r2
orl a,#30H
lcall putchar
; Decimal point
mov a, #'.'
lcall putchar
; Digit 4
mov a,r3
swap a
anl a,#0FH
orl a,#30H
lcall putchar
; Digit 3
mov a,r3
anl a,#0FH
orl a,#30H
lcall putchar
; Digit 2
mov a,r4
swap a
anl a,#0FH
orl a,#30H
lcall putchar
; Digit 1
mov a,r4
anl a,#0FH
orl a,#30H
lcall putchar
ret
;Initializes timer/counter 0 as a 8-bit autoreload counter
InitTimer0:
setb T0 ; Enable pin T0 as input
clr TR0 ; Stop timer 0
mov a,#0F0H
anl a,TMOD
orl a,#02H ; Set timer 0 as 8-bit autoreload timer
mov TMOD,a
mov TH0, #72 ; For 100us @ 22.11MHz: 256-((100e-6)*(22.11e6/12))
mov IE, #82H ; Enable timer 0 interrupt and global interrupts
ret
MyProgram:
mov SP, #80H ; Set the stack pointer to the begining of idata
lcall InitSerial
lcall InitTimer0
mov dptr, #Banner
lcall puts
forever:
mov TL0, #0
mov R0, #0
mov R1, #0
setb P2.0 ; Use this pin as an input
; Discard any incomplete periods
L0:
jnb P2.0, L0
L1:
jb P2.0, L1
setb TR0 ; Start counting
L2:
jnb P2.0, L2
L3:
jb P2.0, L3
clr TR0 ; Stop counting R0-R1 has the count in 100us increments
lcall hex2bcd
mov dptr, #Period
lcall puts
lcall printBCD
mov dptr, #Secs
lcall puts
jmp forever
; Some strings:
Banner:
db 0AH, 0DH
db 'Period.asm: Measures the period (0.0000s to 6.5535s) at pin p2.0'
db 0AH, 0DH
db 'By Jesus Calvino-Fraga, 2007'
db 0AH, 0DH, 0AH, 0DH, 0
Period:
db 'T='
db 0
Secs:
db 's'
db 0DH, 0
END
; period.asm: measures the period at pin p2.0
; by Jesus Calvino-Fraga, 2007
;
; Uses the serial port of the microcontroller to display the
; measured period. Connect the microcontroller serial port
; to the computer using HyperTerminal or any other terminal program
; configured at 115200 baud, 2 stop bits, no parity, handshake none.
;
$MOD52
org 0000H
ljmp MyProgram
org 000BH ; Interrupt vector for timer 0
ljmp Timer0ISR
org 001BH
ljmp 1234H ; Needed by CMON51
; Increment 16-bit count in R0-R1
Timer0ISR:
inc R1
cjne R1, #0, done
inc R0
done:
reti
;Converts the hex number in R0-R1 to BCD in R2-R3-R4
;Changes R0 to R5 and the accumulator.
;Takes 310 machine cycles and 28 bytes.
hex2bcd:
mov R2, #0 ; Set BCD result to 00000
mov R3, #0
mov R4, #0
mov R5, #16 ; Loop counter.
hex2bcd_L0:
mov a, R1 ; Shift R0-R1 left through carry
rlc a
mov R1, a
mov a, R0
rlc a
mov R0, a
mov a, R4 ; Perform bcd+bcd+carry using BCD numbers
addc a, R4
da a
mov R4, a
mov a, R3
addc a, R3
da a
mov R3, a
mov a, R2
addc a, R2
mov R2, a
djnz R5, hex2bcd_L0
ret
;Initializes the serial port
InitSerial:
clr TR1 ; Stop timer 1
mov a,#0FH ; Set timer 1 as 8-bit autoreload timer
anl a,TMOD
orl a,#20H
mov TMOD,a
orl PCON,#80H ; SMOD=1 -> k=2: double baudrate
mov TL1,#0FFH ; Set baud rate to 115200 for 22.1184MHz crystal
mov TH1,#0FFH
setb TR1 ; Start timer 1
mov SCON,#52H ; Serial port in mode 1, ren, txrdy, rxempty
ret
;Sends the byte in the accumulator through the serial port
putchar:
jnb TI, putchar ; Wait for the last character to transmit
clr TI
mov SBUF, a
ret
;Sends the zero terminated string pointed by DPTR through the serial port
puts:
clr a
movc a, @a+dptr
jz puts_end
lcall putchar
inc dptr
jmp puts
puts_end:
ret
;Prints the BCD number stored in R2-R3-R4
printBCD:
; Digit 5
mov a,r2
orl a,#30H
lcall putchar
; Decimal point
mov a, #'.'
lcall putchar
; Digit 4
mov a,r3
swap a
anl a,#0FH
orl a,#30H
lcall putchar
; Digit 3
mov a,r3
anl a,#0FH
orl a,#30H
lcall putchar
; Digit 2
mov a,r4
swap a
anl a,#0FH
orl a,#30H
lcall putchar
; Digit 1
mov a,r4
anl a,#0FH
orl a,#30H
lcall putchar
ret
;Initializes timer/counter 0 as a 8-bit autoreload counter
InitTimer0:
setb T0 ; Enable pin T0 as input
clr TR0 ; Stop timer 0
mov a,#0F0H
anl a,TMOD
orl a,#02H ; Set timer 0 as 8-bit autoreload timer
mov TMOD,a
mov TH0, #72 ; For 100us @ 22.11MHz: 256-((100e-6)*(22.11e6/12))
mov IE, #82H ; Enable timer 0 interrupt and global interrupts
ret
MyProgram:
mov SP, #80H ; Set the stack pointer to the begining of idata
lcall InitSerial
lcall InitTimer0
mov dptr, #Banner
lcall puts
forever:
mov TL0, #0
mov R0, #0
mov R1, #0
setb P2.0 ; Use this pin as an input
; Discard any incomplete periods
L0:
jnb P2.0, L0
L1:
jb P2.0, L1
setb TR0 ; Start counting
L2:
jnb P2.0, L2
L3:
jb P2.0, L3
clr TR0 ; Stop counting R0-R1 has the count in 100us increments
lcall hex2bcd
mov dptr, #Period
lcall puts
lcall printBCD
mov dptr, #Secs
lcall puts
jmp forever
; Some strings:
Banner:
db 0AH, 0DH
db 'Period.asm: Measures the period (0.0000s to 6.5535s) at pin p2.0'
db 0AH, 0DH
db 'By Jesus Calvino-Fraga, 2007'
db 0AH, 0DH, 0AH, 0DH, 0
Period:
db 'T='
db 0
Secs:
db 's'
db 0DH, 0
END