1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
**************************************************************************
*
* Title: LED Light Dimmer
*
* Objective: CMPEN 472 Homework 3
*
* Revision: V1.0
*
* Date: Feb. 5, 2025
*
* Programmer: Jacob McDonnell
*
* Company: The Pennsylvania State University
* Department of Computer Science and Engineering
*
* Algorithm: Simple Parallel I/O use and time delay-loop demo
*
* Register Use: A: LED Light on/off state and Switch 1 on/off state
* X: Delay loop counter
*
* Memory Use: RAM Locations from $3000 for data,
* RAM Locations from $3100 for program
*
* Input: Parameters hard-coded in the program - PORTB
* Switch 1 at PORTB bit 0
* Switch 2 at PORTB bit 1
* Switch 3 at PORTB bit 2
* Switch 4 at PORTB bit 3
*
* Output: LED 1 at PORTB bit 4
* LED 2 at PORTB bit 5
* LED 3 at PORTB bit 6
* LED 4 at PORTB bit 7
*
* Observation: This program will dim the LED4 to either 5% when the
* switch is not pressed and 15% when the switch is pressed.
*
* Note: ON CSM-12C128 board,
* Switch 1 is at PORTB bit 0, and
* LED 4 is at PORTB bit 7.
*
* Comments: This program is developed and simulated using CodeWarrior
* development software and targeted for Axion
* Manufacturing's CSM-12C128 board running at 24MHz.
*
**************************************************************************
* Parameter Declearation Section
*
* Export Symbols
xdef pgstart ; export 'pgstart' symbol
absentry pgstart ; for assembly entry point
* Symbols and Macros
PORTA equ $0000 ; i/o port A addresses
DDRA equ $0002 ; data direction register for PORTA
PORTB equ $0001 ; i/o port B addresses
DDRB equ $0003 ; data direction register for PORTB
**************************************************************************
* Data Section: address used [ $3000 to $30FF ] RAM Memory
*
org $3000 ; Reserved RAM memory starting address
; for Data for CMPEN 472 class
Counter dc.w $0039 ; X register count number for time Delay
; loop for 10 useconds
; The work to calculate this number is in
; the comments for the delay10usec subroutine.
ONN dc.b $0005 ; Counter for how long LED should be on for
OFF dc.b $005f ; Counter for how long LED should be off for
*
**************************************************************************
* Program Section: address used [ $3100 to $3FFF ] RAM Memory
*
org $3100 ; Program start address, in RAM
pgstart lds #$3100 ; initialize the stack pointer
ldaa #%11110001 ; LED 1,2,3,4 at PORTB bit 4,5,6,7
staa DDRB ; set PORTB bit 4,5,6,7 as output
ldaa #%01010000
staa PORTB ; Turn on only LEDs 1 & 3 on PORTB
mainLoop
ldaa PORTB ; check bit 0 of PORTB, switch 1
anda #%00000001 ; if 0, run blink LED4 5% light level
bne p25LED4 ; if 1, run blink LED4 25% light level
p05LED4
ldaa #5 ; load 5 into accumulator A
staa ONN ; store A in counter ONN
ldaa #95 ; load 95 into accumulator A
staa OFF ; store A in counter OFF
jsr dimmer ; jump to dimmer subroutine
bra mainLoop ; loop to mainLoop to check switch
p25LED4
ldaa #25 ; load 25 into accumulator A
staa ONN ; store A in counter ONN
ldaa #75 ; load 75 into accumulator A
staa OFF ; store A in counter OFF
jsr dimmer ; jump to dimmer subroutine
bra mainLoop ; loop to mainLoop to check switch
**************************************************************************
* Subroutine Section: address used [ $3100 to $3FFF ] RAM Memory
*
;*************************************************************************
; dimmer subroutine
;
; This subroutine will dim LED4 to a given level
;
; Input: Two 1 byte counters, ONN and OFF, for how many times
; LED4 should be on and off for.
; Output: LED4 dimmed to a given level, wasted cycles
; Registers in use: A accumulator to counter number of times looped
; Memory locations in use: Two bytes ONN and OFF used to dim the LED4 to a given level
;
; Comments: This subroutine requires delay10usec subroutine
;
dimmer
bset PORTB,%10000000 ; Turn LED4 on
psha ; Save A to the stack
onDelay ldaa ONN ; Load counter ONN into A
beq skipToOff ; if zero jump to loop2 for off
jsr delay10usec ; delay for 10 microseconds
dec ONN ; decrement counter ONN by 1
bra onDelay ; jump back to loop1 always
skipToOff bclr PORTB,%10000000 ; Turn off LED4
offDelay ldaa OFF ; load counter OFF into A
beq doneLoop ; if 0, skip loop
jsr delay10usec ; delay 10 microseconds
dec OFF ; decrement counter OFF by 1
bra offDelay ; jump back to loop2 always
doneLoop pula ; restore A from the stack
rts ; return to caller
;*************************************************************************
; delay10usec subroutine
;
; This subroutine causes a 10 usec. delay
;
; Input: a 16bit count number in 'Counter'
; Output: time delay, cpu cycle wasted
; Registers in use: X register, as counter
; Memory locations in use: a 16bit input number at 'Counter'
;
; Comments: Code relies on counter being $39 to be exactly 10 usec work is below
; Given: freq = 24MHz = 24000000 sec = 10 usec = 0.00001
; freq = cycles / seconds
;
; cycles = freq * seconds = 24000000Hz * 0.00001 = 240
;
; This sub routine is 12 + 4 * 'Counter' cycles long, solving for 'Counter'
; the result is found to be 57.
;
delay10usec
pshx ; Save register x to the stack
ldx Counter ; load counter into register x
innerLoop dex ; decrement register x by 1
bne innerLoop ; loop while register x is not 0
pulx ; restore register x from the stack
nop ; extra nop to make exactly 10 usec
rts ; return to caller
end ; last line of the file
|