|
| 1 | +; Define ports and registers |
| 2 | +PORT_DISPLAY equ 0x0200 ; Address of the port connected to 7-segment displays |
| 3 | +PORT_BUTTONS equ 0x0210 ; Address of the port connected to buttons |
| 4 | + |
| 5 | +; Timer A registers |
| 6 | +TA0CTL equ 0x0160 ; Timer A0 Control |
| 7 | +TA0CCR0 equ 0x0172 ; Timer A0 Capture/Compare 0 |
| 8 | +TA0CCTL0 equ 0x0162 ; Timer A0 Capture/Compare Control 0 |
| 9 | + |
| 10 | +; Button registers |
| 11 | +BUTTON_STOP equ BIT0 ; Stop button (P1.0) |
| 12 | +BUTTON_START equ BIT1 ; Start button (P1.1) |
| 13 | +BUTTON_RESET equ BIT2 ; Reset button (P1.2) |
| 14 | + |
| 15 | +; Variables |
| 16 | +SECONDS equ 0x0204 ; Seconds variable |
| 17 | +CENTISECONDS equ 0x0205 ; Centiseconds variable |
| 18 | +TIMER_RUNNING equ 0x0206 ; Flag to indicate if the timer is running (1 if running, 0 if stopped) |
| 19 | + |
| 20 | + ORG 0x0000 ; Start address of the program |
| 21 | + |
| 22 | +; Initialize the main program |
| 23 | +Main mov.w #WDTCTL, R2 ; Disable Watchdog Timer |
| 24 | + mov.w #WDTPW | WDTHOLD, R3 |
| 25 | + mov.w #PORT_DISPLAY, R4 ; Address of the display port |
| 26 | + mov.w #PORT_BUTTONS, R9 ; Address of the buttons port |
| 27 | + mov.b #0, TIMER_RUNNING ; Initialize timer running flag |
| 28 | + |
| 29 | + ; Configure Timer A |
| 30 | + mov.w #TA0CTL, R6 ; Load the address of Timer A0 Control register |
| 31 | + mov.w #TASSEL_2 | MC_1 | ID_3, 0(R6) ; Set SMCLK as source, Up mode, and divide by 8 |
| 32 | + |
| 33 | + mov.w #TA0CCR0, R7 ; Load the address of Timer A0 CCR0 register |
| 34 | + mov.w #10486, 0(R7) ; Set the value for 10 ms interrupt (10486 for 1 MHz SMCLK) |
| 35 | + |
| 36 | + mov.w #TA0CCTL0, R8 ; Load the address of Timer A0 CCTL0 register |
| 37 | + mov.w #CCIE, 0(R8) ; Enable Timer A0 CCR0 interrupt |
| 38 | + |
| 39 | + ; Configure buttons as inputs with pull-up resistors |
| 40 | + mov.w #BUTTON_STOP | BUTTON_START | BUTTON_RESET, 0(R9) ; Set pins as inputs |
| 41 | + mov.w #BUTTON_STOP | BUTTON_START | BUTTON_RESET, R10 ; Load button mask to R10 |
| 42 | + |
| 43 | + ; Enable global interrupts |
| 44 | + EINT |
| 45 | + |
| 46 | + ; Infinite loop |
| 47 | +MainLoop mov.b 0(R9), R11 ; Read buttons status |
| 48 | + |
| 49 | + ; Check Stop button |
| 50 | + bit.b #BUTTON_STOP, R11 |
| 51 | + jnz StopPressed |
| 52 | + |
| 53 | + ; Check Start button |
| 54 | + bit.b #BUTTON_START, R11 |
| 55 | + jnz StartPressed |
| 56 | + |
| 57 | + ; Check Reset button |
| 58 | + bit.b #BUTTON_RESET, R11 |
| 59 | + jnz ResetPressed |
| 60 | + |
| 61 | + jmp MainLoop |
| 62 | + |
| 63 | +StopPressed mov.b #0, TIMER_RUNNING ; Stop the timer |
| 64 | + jmp MainLoop |
| 65 | + |
| 66 | +StartPressed mov.b #1, TIMER_RUNNING ; Start the timer |
| 67 | + jmp MainLoop |
| 68 | + |
| 69 | +ResetPressed mov.b #0, TIMER_RUNNING ; Stop the timer |
| 70 | + clr.b SECONDS ; Clear the seconds variable |
| 71 | + clr.b CENTISECONDS ; Clear the centiseconds variable |
| 72 | + jmp MainLoop |
| 73 | + |
| 74 | +; Timer A0 CCR0 interrupt service routine |
| 75 | +.sect "int09" |
| 76 | +.short TISR |
| 77 | + |
| 78 | +TISR mov.b TIMER_RUNNING, R12 ; Check if the timer is running |
| 79 | + cmp.b #1, R12 ; If timer is running, proceed with counting |
| 80 | + jeq CountTime |
| 81 | + |
| 82 | + RETI ; If timer is not running, return from interrupt |
| 83 | + |
| 84 | +CountTime inc.b CENTISECONDS ; Increment centiseconds |
| 85 | + |
| 86 | + ; Check if one second has passed |
| 87 | + cmp.b #100, CENTISECONDS |
| 88 | + jne SkipSecond |
| 89 | + |
| 90 | + clr.b CENTISECONDS ; Reset centiseconds |
| 91 | + inc.b SECONDS ; Increment seconds |
| 92 | + |
| 93 | +SkipSecond RETI ; Return from interrupt |
0 commit comments