0% found this document useful (0 votes)
181 views5 pages

PIC12F752 UART Communication Code

1. The document defines constants and variables for use in a PIC12F752 microcontroller program for UART communication. 2. It contains the main program loop which initializes ports and timers for UART communication at 9600 baud and 40Hz sampling. 3. Interrupt service routines handle transmission and reception of data over UART using Timer1 and Timer2 for bit timing. Data is received on PORTA and transmitted from PORTA.

Uploaded by

api-397709136
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
181 views5 pages

PIC12F752 UART Communication Code

1. The document defines constants and variables for use in a PIC12F752 microcontroller program for UART communication. 2. It contains the main program loop which initializes ports and timers for UART communication at 9600 baud and 40Hz sampling. 3. Interrupt service routines handle transmission and reception of data over UART using Timer1 and Timer2 for bit timing. Data is received on PORTA and transmitted from PORTA.

Uploaded by

api-397709136
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

1 list P=PIC12F752, r=dec

2 #include "[Link]"
3 __config(_CP_OFF & _WDTE_OFF & _PWRTE_ON & _FOSC0_INT & _MCLRE_OFF)
4
5 ;
6 ; definitions
7 ;
8 WREG_TEMP equ 0x70
9 STATUS_TEMP equ 0x71
10 PCLATH_TEMP equ 0x72
11 RX_FuelTank_DR equ 0x73
12 TX_FuelTank_DR equ 0x74
13 TX_Tiva_DR equ 0x75
14 UART_Counter equ 0x76
15 UART_Active equ 0x77
16 UART_ActiveLine equ 0x78
17
18 RX_FuelTank equ 0
19 TX_FuelTank equ 1
20 TX_Tiva equ 2
21 DataMask equ b'00110000'
22 Timer1LVal equ 0xB0
23 Timer1HVal equ 0x3C
24 QueryByte equ 0xAA
25
26 org 0
27 goto Main
28 org 4
29 goto ISR
30 org 5
31
32 ;
33 ; tables
34 ;
35
36 ;
37 ; main
38 ;
39 Main:
40 banksel OSCCON ;set Fosc to 8MHz (11)
41 bsf OSCCON,4
42 bsf OSCCON,5
43 banksel PORTA ;clear all values
44 clrf PORTA
45 banksel LATA
46 clrf LATA
47 banksel ANSELA ;select digital function for RA<5:0>
48 clrf ANSELA
49 banksel TRISA ;set RA<2:1> as outputs, RA0 as input
50 movlw b'00000001'
51 movwf TRISA
52 banksel INTCON ;set GIE and PEIE
53 movlw b'11000000'
54 movwf INTCON
55 banksel IOCAN ;enable IOC negative edge on RX pin RA0
56 movlw b'00000001'
57 movwf IOCAN
58 banksel PIE1 ;enable Timer2 interrupt
59 bsf PIE1,TMR2IE
60 banksel PR2 ;load PR2 to 103 for 9600 baud
61 movlw 103
62 movwf PR2
63 banksel T2CON ;prescaler 1:2, Timer2 off, postscaler 1:1
64 movlw b'00001000'
65 movwf T2CON
66 banksel PIE1 ;enable Timer1 interrupt
67 bsf PIE1,TMR1IE
68 banksel TMR1L ;load Timer1 with 15536 (-50000) for 40Hz
rate
69 movlw Timer1LVal
70 movwf TMR1L
71 movlw Timer1HVal
72 movwf TMR1H
73 banksel T1CON ;prescaler 1:1, Timer1 on, use Fosc/4
74 movlw b'00000001'
75 movwf T1CON
76 banksel LATA ;TX pins idle high
77 bsf LATA,TX_FuelTank
78 bsf LATA,TX_Tiva
79 bcf UART_Active,0 ;clear UART active flag
80
81 Loop:
82 goto $ ;looplooplooplooplooplooplooplooploop
83
84 ;
85 ; ISRs
86 ;
87 ISR:
88 call Push
89 StartQueryISR:
90 banksel PIR1
91 btfss PIR1,TMR1IF ;skip goto if flag is set
92 goto StartRXISR
93 bcf PIR1,TMR1IF ;clear interrupt flag
94 call StartQuery ;start the query write
95 goto EndISR
96 StartRXISR:
97 banksel IOCAF
98 btfss IOCAF,RX_FuelTank ;skip goto if flag is set
99 goto TMR2ISR
100 bcf IOCAF,RX_FuelTank ;clear interrupt flag
101 call StartReceive
102 goto EndISR
103 TMR2ISR:
104 banksel PIR1
105 btfss PIR1,TMR2IF ;skip goto if flag is set
106 goto EndISR
107 banksel PIR1
108 bcf PIR1,TMR2IF ;clear interrupt flag
109 movf UART_ActiveLine,W ;switch based on active line
110 decf UART_Counter ;decrement counter
111 btfss STATUS,Z ;if clear, continue, else, end UART
112 call ActiveLineContinueSwitch
113 btfsc STATUS,Z
114 call ActiveLineEndSwitch
115 goto EndISR
116 ActiveLineContinueSwitch:
117 addwf PCL,F ;switch-case between active line responses
118 goto ContinueReceive ;RX_FuelTank
119 goto ContinueQuery ;TX_FuelTank
120 NOP ;TX_Tiva
121 ActiveLineEndSwitch:
122 addwf PCL,F ;switch-case between active line responses
123 goto EndReceive ;RX_FuelTank
124 goto EndQuery ;TX_FuelTank
125 NOP ;TX_Tiva
126 EndISR:
127 call Pop
128 retfie
129
130 ;
131 ; subroutines
132 ;
133 StartQuery:
134 call DisableIOC ;disable IOC until end of query
135 call DisableTimer1 ;disable and reset Timer1
136 call EnableTimer1 ;reenable Timer1
137 movlw QueryByte ;move query byte into data register
138 movwf TX_FuelTank_DR
139 movlw 9 ;load counter to 9 (8 data bits)
140 movwf UART_Counter
141 bsf UART_Active,0 ;set UART active flag
142 movlw TX_FuelTank ;set UART active line
143 movwf UART_ActiveLine
144 banksel LATA
145 bcf LATA,TX_FuelTank ;clear tx pin (start bit)
146 call EnableTimer2 ;enable Timer2
147 return
148 ContinueQuery:
149 btfss TX_FuelTank_DR,0 ;write LSB to output
150 call Clear_TX_FuelTank
151 btfsc TX_FuelTank_DR,0
152 call Set_TX_FuelTank
153 RRF TX_FuelTank_DR,F ;shift right 1 bit to prepare next xmit
154 return
155 EndQuery:
156 call Set_TX_FuelTank ;set tx pin (stop bit)
157 call DisableTimer2 ;disable Timer2
158 call EnableIOC ;enable IOC
159 return
160 StartReceive:
161 call DisableIOC ;disable IOC until end of receive
162 banksel LATA
163 movlw 9 ;load counter to 9 (8 data bits)
164 movwf UART_Counter
165 bsf UART_Active,0 ;set UART active flag
166 movlw RX_FuelTank ;set UART active line
167 movwf UART_ActiveLine
168 banksel LATA ;set TX_Tiva line low (start bit)
169 bcf LATA,TX_Tiva
170 banksel PR2 ;load PR2 with 1x bit time to center on data
bits
171 movlw 103
172 movwf PR2
173 call EnableTimer2 ;enable Timer2
174 return
175 ContinueReceive:
176 banksel PORTA ;write value of LSB to output line
177 btfss PORTA,RX_FuelTank
178 bcf PORTA,TX_Tiva
179 btfsc PORTA,RX_FuelTank
180 bsf PORTA,TX_Tiva
181 return
182 EndReceive:
183 call DisableTimer2 ;disable Timer2
184 call EnableIOC ;enable IOC
185 banksel LATA ;set TX_Tiva line high (stop bit)
186 bsf LATA,TX_Tiva
187 return
188 Clear_TX_FuelTank:
189 banksel LATA
190 bcf LATA,TX_FuelTank
191 return
192 Set_TX_FuelTank:
193 banksel LATA
194 bsf LATA,TX_FuelTank
195 return
196 EnableTimer2:
197 banksel T2CON ;turn on Timer2
198 bsf T2CON,TMR2ON
199 return
200 DisableTimer2:
201 banksel T2CON ;turn off Timer2
202 bcf T2CON,TMR2ON
203 banksel TMR2 ;reset counter to 0
204 clrf TMR2
205 return
206 EnableTimer1:
207 banksel T1CON ;turn on Timer1
208 bsf T1CON,TMR1ON
209 return
210 DisableTimer1:
211 banksel T1CON ;turn off Timer1
212 bcf T1CON,TMR1ON
213 banksel TMR1L ;reset Timer1 counter
214 movlw Timer1LVal
215 movwf TMR1L
216 movlw Timer1HVal
217 movwf TMR1H
218 return
219 EnableIOC:
220 banksel IOCAN ;enable IOC negative edge on RX pin RA0
221 bsf IOCAN,0
222 banksel INTCON ;enable IOC enable
223 bsf INTCON,IOCIE
224 return
225 DisableIOC:
226 banksel IOCAN ;disable IOC negative edge on RX pin RA0
227 bcf IOCAN,0
228 banksel INTCON ;disable IOC enable
229 bcf INTCON,IOCIE
230 return
231 Push:
232 banksel WREG_TEMP ;save W in WREG_TEMP
233 movwf WREG_TEMP
234 movf STATUS,W ;save STATUS in STATUS_TEMP
235 clrf STATUS
236 movwf STATUS_TEMP
237 movf PCLATH,W ;save PCLATH in PCLATH_TEMP
238 movwf PCLATH_TEMP
239 clrf PCLATH
240 return
241 Pop:
242 banksel PCLATH_TEMP ;restore PCLATH_TEMP contents to PCLATH
243 movf PCLATH_TEMP,W
244 movwf PCLATH
245 movf STATUS_TEMP,W ;restore STATUS_TEMP contents to STATUS
246 movwf STATUS
247 swapf WREG_TEMP,F ;restore WREG_TEMP contents to W
248 swapf WREG_TEMP,W
249 return
250 End

You might also like