|
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 |
3 | 6 |
|
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 |
8 | 8 |
|
9 | | - ORG 0x0000 ; Start address of the program |
| 9 | +; ... (existing code goes here) |
10 | 10 |
|
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) |
16 | 14 |
|
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) |
20 | 25 |
|
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 |
23 | 29 |
|
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 |
26 | 33 |
|
27 | | - ; Enable global interrupts |
28 | | - EINT |
| 34 | + ; Result is in R13 (tens digit) and R14 (ones digit) |
| 35 | + ret |
29 | 36 |
|
30 | | - ; Infinite loop |
31 | | -MainLoop jmp MainLoop |
32 | 37 |
|
33 | | -; Timer A0 CCR0 interrupt service routine |
34 | | -.sect "int09" |
35 | | -.short TISR |
| 38 | +TISR |
| 39 | + ; Timer Interrupt Service Routine |
36 | 40 |
|
37 | | -TISR ; Interrupt service routine code goes here |
| 41 | + ; Increment centiseconds |
| 42 | + inc @seconds |
38 | 43 |
|
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 |
41 | 47 |
|
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