Skip to content

Commit d25d09b

Browse files
committed
exp6
1 parent 662dd9a commit d25d09b

File tree

4 files changed

+224
-190
lines changed

4 files changed

+224
-190
lines changed

Experiment - 6/part1.asm

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,19 @@
1-
; Define ports and registers
2-
PORT_DISPLAY equ 0x01 ; Address of the port connected to 7-segment displays
3-
DELAY_COUNT equ 0xFF ; Delay count for controlling the speed
4-
5-
ORG 0x0000 ; Start address of the program
6-
7-
MainLoop
8-
mov R1, #0x01 ; Initialize register R1 with 0x01
9-
10-
; Loop through the four displays
11-
LoopDisplays
12-
mov [PORT_DISPLAY], R1 ; Output the value of R1 to the display port
13-
call Delay ; Introduce a delay to control the speed
14-
mov R1, R1 << 1 ; Shift the value in R1 to the left for the next display
15-
jz ResetLoop ; If R1 becomes zero, reset the loop
16-
17-
jmp LoopDisplays ; Repeat the loop for the next display
18-
19-
ResetLoop
20-
mov R1, #0x01 ; Reset R1 to 0x01 to restart the loop
21-
22-
jmp MainLoop ; Repeat the main loop
23-
24-
Delay
25-
mov R2, #DELAY_COUNT ; Initialize register R2 with the delay count
26-
27-
DelayLoop
28-
dec R2 ; Decrement R2
29-
jnz DelayLoop ; Continue looping until R2 becomes zero
30-
ret ; Return from the delay subroutine
1+
.data
2+
array .byte 00111111b, 00000110b, 01011011b, 01001111b, 01100110b, 01101101b, 01111101b, 00000111b, 01111111b, 01101111b
3+
lastElement
4+
.text
5+
6+
main mov #array,R10
7+
mov #1d,R11
8+
mov.b #07fh,&P1DIR
9+
mov.b #00fh,&P2DIR
10+
loop mov.b @R10+,&P2OUT
11+
mov.b R11,&P1OUT
12+
rla R11
13+
cmp R11,#010h
14+
jeq main
15+
jmp loop
16+
17+
18+
19+

Experiment - 6/part2.asm

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,32 @@
1-
; Define ports and registers
2-
PORT_DISPLAY equ 0x01 ; Address of the port connected to 7-segment displays
3-
4-
ORG 0x0000 ; Start address of the program
5-
6-
Main
7-
mov R3, #0 ; Initialize register R3 to store the BCD result
8-
9-
; Binary input value (adjust this value as needed)
10-
mov R0, #97 ; Example: binary input of 97
11-
12-
; Call BCD conversion subroutine
13-
call BCDConvert
14-
15-
; Display the BCD result on 7-segment displays
16-
mov [PORT_DISPLAY], R3 ; Output the tens digit
17-
call Delay ; Introduce a delay
18-
mov [PORT_DISPLAY], R4 ; Output the ones digit
19-
20-
; Infinite loop
21-
jmp Main
22-
23-
; BCD conversion subroutine
24-
BCDConvert
25-
mov R4, R0 ; Copy the binary input to R4
26-
mov R3, #0 ; Clear R3 for the tens digit
27-
28-
; Convert tens digit
29-
BCDLoop
30-
cmp R4, #10 ; Compare R4 with 10
31-
jl BCDDone ; If R4 is less than 10, exit the loop
32-
33-
sub R4, #10 ; Subtract 10 from R4
34-
inc R3 ; Increment the tens digit
35-
jmp BCDLoop ; Repeat the loop
36-
37-
BCDDone
38-
ret ; Return from the subroutine
39-
40-
Delay
41-
; Delay subroutine goes here
42-
ret ; Placeholder return
1+
;--------------------------------------
2+
; BCD Conversion Subroutine.
3+
; Subroutine that takes a binary input between 0x00000000b and 0x01100011b and converts it to two decimal digits.
4+
; The result will be displayed on the 7-segment displays.
5+
;------------------------------------
6+
7+
8+
BCDConversion
9+
; Input: R12 - Binary input
10+
; Output: R13 - BCD result (tens digit), R14 - BCD result (ones digit)
11+
12+
; Extract the tens digit
13+
mov.w R12, R13 ; Copy the binary input to R13
14+
rla R13 ; Rotate left through carry
15+
rla R13 ; Rotate left through carry (multiply by 4)
16+
rla R13 ; Rotate left through carry (multiply by 8)
17+
rla R13 ; Rotate left through carry (multiply by 16)
18+
19+
; Extract the ones digit
20+
mov.w R12, R14 ; Copy the binary input to R14
21+
and #000Fh, R14 ; Mask the lower nibble (ones digit)
22+
23+
; Convert the ones digit to BCD
24+
add #3, R14 ; Add 3 (to adjust for binary to BCD conversion)
25+
daa ; Decimal adjust after addition
26+
27+
; Convert the tens digit to BCD
28+
add #3, R13 ; Add 3 (to adjust for binary to BCD conversion)
29+
daa ; Decimal adjust after addition
30+
31+
; Result is in R13 (tens digit) and R14 (ones digit)
32+
ret

Experiment - 6/part3.asm

Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,74 @@
1-
; Define ports and registers
2-
PORT_DISPLAY equ 0x0200 ; Address of the port connected to 7-segment displays
1+
.data
2+
array .byte 00111111b, 00000110b, 01011011b, 01001111b, 01100110b, 01101101b, 01111101b, 00000111b, 01111111b, 01101111b
3+
lastElement
4+
seconds .word 0
5+
centiseconds .word 0
36

4-
; Timer A registers
5-
TA0CTL equ 0x0160 ; Timer A0 Control
6-
TA0CCR0 equ 0x0172 ; Timer A0 Capture/Compare 0
7-
TA0CCTL0 equ 0x0162 ; Timer A0 Capture/Compare Control 0
7+
.text
88

9-
ORG 0x0000 ; Start address of the program
9+
; ... (existing code goes here)
1010

11-
; Initialize the main program
12-
Main mov.w #WDTCTL, R2 ; Disable Watchdog Timer
13-
mov.w #WDTPW | WDTHOLD, R3
14-
mov.w #PORT_DISPLAY, R4 ; Address of the display port
15-
mov.w #0, R5 ; Clear register R5 for the BCD result
11+
BCDConversion
12+
; Input: R12 - Binary input
13+
; Output: R13 - BCD result (tens digit), R14 - BCD result (ones digit)
1614

17-
; Configure Timer A
18-
mov.w #TA0CTL, R6 ; Load the address of Timer A0 Control register
19-
mov.w #TASSEL_2 | MC_1 | ID_3, 0(R6) ; Set SMCLK as source, Up mode, and divide by 8
15+
; Extract the tens digit
16+
mov.w R12, R13 ; Copy the binary input to R13
17+
rla R13 ; Rotate left through carry
18+
rla R13 ; Rotate left through carry (multiply by 4)
19+
rla R13 ; Rotate left through carry (multiply by 8)
20+
rla R13 ; Rotate left through carry (multiply by 16)
21+
22+
; Extract the ones digit
23+
mov.w R12, R14 ; Copy the binary input to R14
24+
and #000Fh, R14 ; Mask the lower nibble (ones digit)
2025

21-
mov.w #TA0CCR0, R7 ; Load the address of Timer A0 CCR0 register
22-
mov.w #10486, 0(R7) ; Set the value for 10 ms interrupt (10486 for 1 MHz SMCLK)
26+
; Convert the ones digit to BCD
27+
add #3, R14 ; Add 3 (to adjust for binary to BCD conversion)
28+
daa ; Decimal adjust after addition
2329

24-
mov.w #TA0CCTL0, R8 ; Load the address of Timer A0 CCTL0 register
25-
mov.w #CCIE, 0(R8) ; Enable Timer A0 CCR0 interrupt
30+
; Convert the tens digit to BCD
31+
add #3, R13 ; Add 3 (to adjust for binary to BCD conversion)
32+
daa ; Decimal adjust after addition
2633

27-
; Enable global interrupts
28-
EINT
34+
; Result is in R13 (tens digit) and R14 (ones digit)
35+
ret
2936

30-
; Infinite loop
31-
MainLoop jmp MainLoop
3237

33-
; Timer A0 CCR0 interrupt service routine
34-
.sect "int09"
35-
.short TISR
38+
TISR
39+
; Timer Interrupt Service Routine
3640

37-
TISR ; Interrupt service routine code goes here
41+
; Increment centiseconds
42+
inc @seconds
3843

39-
; For demonstration purposes, toggle an output on every interrupt
40-
XOR.B #1, 0(R4) ; Toggle the least significant bit of the display port
44+
; Check if centiseconds reached 100
45+
cmp #100d, @seconds
46+
jl notReached100
4147

42-
RETI ; Return from interrupt
48+
; Centiseconds reached 100, reset to 0 and increment seconds
49+
clr @seconds
50+
inc @centiseconds
51+
52+
notReached100
53+
; Convert centiseconds to BCD and display
54+
mov @centiseconds, R12
55+
call BCDConversion
56+
mov #array, R5
57+
add R13, R5 ; Add the BCD result for tens digit to array
58+
mov #1d, &P2OUT ; Set the appropriate port value for display
59+
mov R5, &P1OUT ; Display the value
60+
61+
; ... Repeat the process for ones digit
62+
mov.w R8, R12 ; Copy the binary input to R12 (ones digit)
63+
64+
; Convert the ones digit to BCD
65+
add #3, R12 ; Add 3 (to adjust for binary to BCD conversion)
66+
daa ; Decimal adjust after addition
67+
68+
; Save the BCD result of the ones digit
69+
mov R12, @R15 ; Store the result in memory or register (adjust R15 as needed)
70+
71+
; Clear interrupt flag and enable the next interrupt
72+
bis #00001h, &TA0CCTL0
73+
74+
ret

0 commit comments

Comments
 (0)