-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUARTInt.PIC16.ex.asm
155 lines (106 loc) · 3.61 KB
/
UARTInt.PIC16.ex.asm
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
;############################################################################
;# TITLE "USART General Purpose Library Checking Software"
;#
;#
;# Program: PIC16Tst.ASM
;# Version: 1.0
;# Revision Date:
;# Author: Gaurang Kavaiya
;#
;#
;# Program demonstrates use of USART general purpose library module.
;############################################################################
list p=16f877A
include <P16f877A.INC>
include "UARTInt.inc"
UARTTstRAM UDATA
ISR_STAT RES 02 ;For saving STATUS value
#define ISR_PCLATH ISR_STAT+1 ;For saving PCLATH
;If device has shared RAM
UARTTstShr UDATA_SHR
ISR_W RES 01 ;For Saving W reg. value
;If device does not have shared RAM then reserve one location in each bank
;UART_Shr UDATA 0x7f
;ISR_W RES 01
;UART_Shr_1 UDATA 0x7f
;ISR_W_1 RES 01
;UART_Shr_2 UDATA 0xff
;ISR_W_2 RES 01
;This is not required for most of the devices as bank
;UART_Shr_3 UDATA 0x17f
;ISR_W_3 RES 01
;UART_Shr_4 UDATA 0x1ff
;ISR_W_3 RES 01
STARThere CODE 0x00
goto START
INTserv CODE 0x04 ;
nop ;this is necessary because the Linker file only allows 1 instruction
;if we use the goto instruction, the code might go to the wrong location
;depending on PCLATH. So using a nop allows the code to go to the next
;location. This is important for context saving.
;Else for better INT Latency
; movwf ISR_W ;If INT latency is critical
InteruptServiceLocation CODE 0x05
ISRoutine
;context savings (very important)
movwf ISR_W ;save Wreg in ISR_W, If NOP is used above
swapf STATUS,W
banksel ISR_STAT
movwf ISR_STAT ;put STATUSreg (swapped) into ISR_STAT
movf PCLATH,W
movwf ISR_PCLATH ;put PCLATH into ISR_PCLATH
;call the interrupt function
pagesel UARTIntISR
call UARTIntISR ;Call general purpose RTC interrupt service routine
;restore context
banksel ISR_STAT
movf ISR_PCLATH,W ;put ISR_PCLATH back into PCLATH
movwf PCLATH
swapf ISR_STAT,W
movwf STATUS ;swap ISR_STAT back into STATUSreg
swapf ISR_W,f ;swap ISR_W into itself
swapf ISR_W,W ;swap ISR_W into Wreg.
retfie
Main CODE
START
;Define the required TRIS and PORT settings here
;Make sure that UART pins are defined as i/p
pagesel UARTIntInit
call UARTIntInit
;Display Hello
movlw 'H'
Pagesel UARTIntPutCh
call UARTIntPutCh
movlw 'E'
call UARTIntPutCh
movlw 'L'
call UARTIntPutCh
movlw 'L'
call UARTIntPutCh
movlw 'O'
call UARTIntPutCh
;Change the pre-claculated baudrate, If required
; mSetUARTBaud .9600
banksel vUARTIntStatus
WaitRxData
;Check if Receive buffer is full
btfss vUARTIntStatus,UARTIntRxBufFul
goto WaitRxData
;If receive buffer is full then read the data
ReadAgain
Pagesel UARTIntGetCh
call UARTIntGetCh
;check if Tx buffer is empty
banksel vUARTIntStatus
WaitForTxBufEmpty
btfsc vUARTIntStatus,UARTIntTxBufFul
goto WaitForTxBufEmpty
;Echo back the received data
Pagesel UARTIntPutCh
call UARTIntPutCh
;Check if Rx Buffer is empty. If not keep reading it.
banksel vUARTIntStatus
btfss vUARTIntStatus,UARTIntRxBufEmpty
goto ReadAgain
goto WaitRxData
END