1 	list  P=PIC16F690
 2 #include "p16F690.inc"
 3 	__config( _HS_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF & _MCLRE_ON & _IESO_ON  & _FCMEN_ON);
 4 
 5 ;	__config(_CP_OFF & _WDT_OFF &  _PWRTE_ON & _HS_OSC)
 6 	errorlevel -302
 7 ; Using a high frequency external oscillator
 8 
 9 ; **************************************
10 ;Variable Definition
11 ;
12 ; Interrupt Storage
13 W_TEMP			equ	0x70
14 STATUS_TEMP		equ 0x71
15 
16 ; Timer / Output Capture Storage  
17 debounce_count	equ 0x30
18 press_ok		equ 0x31
19 
20 timehi			equ 0x32
21 timelo			equ 0x33
22 periodhi		equ 0xFF	;value
23 periodlo		equ 0xFF	;value
24 
25 ; DISPLAY STORAGE
26 TestByte		equ 0x34
27 
28 ; Receive Storage
29 cur_byte		equ 0x39	
30 byte_counter	equ 0x40
31 
32 byte_start		equ 0x02	;value
33 
34 calc_CK			equ 0x46
35 CK				equ 0x47
36 
37 byte_last		equ 0x03 	;value
38 
39 popped_balloon	equ 0x53	;value
40 happy_balloon	equ 0x5F	;value
41 ; **************************************
42 
43 	org 0					;set program counter to 0
44 	GOTO	Main
45 	org 4					;Sets program counter to 4 for interrupts
46 	GOTO	InterruptResp
47 
48 
49 
50 ;********************************************************************** 
51 Main:	
52 
53 ; Set up Ports
54 	Banksel	TRISC				; Bank 1
55 	MOVLW	0x00				; All outputs
56 	MOVWF 	TRISC
57 	CLRF	TRISB				; All Tris B is output except...
58 	BSF		TRISB,5  			; EUSART Set the recieve to input 
59 	BSF		TRISB,6				; Send Buttion to an input
60 
61 
62 	banksel	PORTC
63 	BCF		PORTB,4				; clear test bit to low
64 	MOVLW	0xFF				; set port c to hi
65 	MOVWF	PORTC
66 
67 ; Set ports to Digital only	
68 	Banksel ANSEL				; Bank 2
69 	CLRF	ANSEL
70 	CLRF	ANSELH
71 
72 ; EUSART Initialization (BAUD RATE)
73 	Banksel BAUDCTL 			; Bank 1
74 	BCF		BAUDCTL, BRG16
75 	BCF		TXSTA,	BRGH
76 	Banksel	SPBRG				; Bank 1
77 	MOVLW	0x20				; Baud rate "X" varible = 25
78 	MOVWF	SPBRG
79 	CLRF	SPBRGH
80 
81 ; EUSART Initialization (Transmission)
82 	Banksel	TXSTA				; Bank 1
83 	BCF		TXSTA,SYNC
84 
85 		
86 ; EUSART Initialization (Receive)
87 ; Serial port enabled, 9 bit recieve, continuous recieve, address detect
88 	Banksel	RCSTA				; Bank 0
89 	BSF		RCSTA,SPEN
90 	BSF		RCSTA, CREN
91 	Banksel	PIE1
92 	BSF		PIE1, RCIE			; Enable recieve interupt
93 	Banksel	PIR1
94 	MOVF	PIR1,F
95 	Banksel	RCREG
96 	MOVF	RCREG,W				;Clear receive interrupt
97 
98 
99 
100 ;; Setup Debounce timer
101 ;	;Set up timer : TMR1 on, internal clock, Prescale value is /8 
102 	banksel T1CON	
103 	BSF		T1CON, 4			; Prescale values
104 	BSF		T1CON, 5
105 	BSF		T1CON, TMR1ON
106 
107 
108 ; Output Compare 
109 	banksel CCP1CON
110 	MOVLW	0x0A
111 	MOVWF	CCP1CON
112 	;IORWF	CCP1CON,F 			; Set the compare to generate sotfware intterupt
113 	BCF		PIR1,CCP1IF
114 	; O/C Interrupt
115 	banksel	PIE1
116 	BSF		PIE1,CCP1IE		;Enable TMR1 O/C interrupt
117 
118 ; Enable interrupts 
119 ; Enable global and peripheral interrupts
120 	Banksel	INTCON				; All banks
121 	BSF		INTCON,	PEIE 
122 	BSF		INTCON,	GIE
123 
124 
125 ; Set bank 0 addresses to 0 
126 
127 
128 ClearAll:
129 	CLRF	0x30
130 	CLRF	0x31
131 	CLRF	0x32
132 	CLRF	0x33
133 	CLRF	0x34
134 	CLRF	0x35
135 	CLRF	0x36
136 	CLRF	0x37
137 	CLRF	0x38
138 	CLRF	0x39
139 	CLRF	0x40	
140 	CLRF	0x41
141 	CLRF	0x42
142 	CLRF	0x40
143 	CLRF	0x46
144 	CLRF	0x47
145 	
146 Initialization:
147 
148 	
149 ;	; Set Query bit to lo
150 ;	Banksel	PORTC
151 ;	BCF		PORTC,0				; Clear RC0 to query the balloon monitor
152 ;	
153 ;
154 ;	; Set up O/C timer to go off in "period" amout of time	
155 ;	banksel	TMR1H
156 ;	MOVF	TMR1H,W			; Store the current time (hi bit)
157 ;	MOVWF	timehi
158 ;	MOVF	TMR1L,W			; Store the current time (lo bit)
159 ;	MOVWF	timelo
160 ;	ADDLW	periodlo
161 ;	MOVWF	CCPR1L			; Set the low bit of the O/C value
162 ;	MOVF	timehi,W
163 ;	BTFSC	STATUS,C
164 ;	ADDLW	0x01			; Account for low bit roll over
165 ;	ADDLW	periodhi
166 ;	MOVWF	CCPR1H			; Set the hi bit of the O/C value
167 ;
168 ;	; O/C Interrupt
169 ;	banksel	PIE1
170 ;	BSF		PIE1,CCP1IE		;Enable TMR1 O/C interrupt
171 
172 	;RETURN
173 
174 	
175 Loop: 
176 
177 	; Poll for send button
178 ;	Banksel	PORTB
179 ;	BTFSS	PORTB , 6 			; If buttion is pressed (LO)
180 ;	CALL 	ButtonPress			; send a message
181 
182 
183 ;	Banksel	PIR1
184 ;	BTFSC	PIR1,RCIF				; If receive flag is set	
185 ;	Call 	Receive	
186 
187 
188 		
189 	GOTO	Loop
190 	
191 Test:						;Test code to toggle bit 4
192 	MOVLW	0x10			; Mask for bit 4
193 	Banksel	PORTB
194 ;	IORWF	PORTB,F
195 	XORWF	PORTB,F
196 	RETURN
197 
198 
199 ;********************************************************************
200 ;INTERRUPT RESPONSE CODE
201 
202 InterruptResp:
203 	MOVWF	W_TEMP 
204 	SWAPF	STATUS, W
205 	MOVWF	STATUS_TEMP
206 ISR:
207 	Banksel PIR1					; Bank 1
208 	
209 	BTFSC	PIR1, CCP1IF
210 	Call 	Compare
211 
212 Eusart:	
213 
214 
215 	BTFSC	PIR1,RCIF				; If receive flag is set	
216 	Call 	Receive	
217 
218 			
219 Back:
220 	SWAPF 	STATUS_TEMP, W
221 	MOVWF	STATUS
222 	SWAPF	W_TEMP, F
223 	SWAPF	W_TEMP, W	
224 	RETFIE						; Return from interrupt
225 
226 ;----------
227 Compare:	
228 	BCF		PIR1, CCP1IF
229 
230 	; Set Query bit to hi
231 	Banksel	PORTC
232 
233 	MOVLW	0x01
234 	XORWF	PORTC,F
235 ;	BSF		PORTC,0				; Set RC0 to query the balloon monitor
236 
237 	; Set up O/C timer to go off in "period" amout of time	
238 	banksel	TMR1H
239 	MOVF	TMR1H,W			; Store the current time (hi bit)
240 	MOVWF	timehi
241 	MOVF	TMR1L,W			; Store the current time (lo bit)
242 	MOVWF	timelo
243 	ADDLW	periodlo
244 	MOVWF	CCPR1L			; Set the low bit of the O/C value
245 	MOVF	timehi,W
246 	BTFSC	STATUS,C
247 	ADDLW	0x01			; Account for low bit roll over
248 	ADDLW	periodhi
249 	MOVWF	CCPR1H			; Set the hi bit of the O/C value
250 
251 
252 	RETURN
253 
254 
255 
256 ;---------
257 Receive:
258 
259 
260 	Banksel	RCREG
261 	MOVF	RCREG,W	
262 	MOVWF	cur_byte				; Store incoming byte	
263 
264 	XORLW	byte_start				; Check if it is the start byte
265 	BTFSC	STATUS, Z				; If start byte 
266 	GOTO 	StartByte				; Respond to start byte
267 
268 	MOVLW	0x06					; Check if CK byte (byte 6)
269 	XORWF	byte_counter,w		
270 	BTFSC	STATUS,	Z				; If CK Byte
271 	GOTO	CKByte
272 	MOVLW	byte_last				; Check if end byte value = (0x03)
273 	XORWF	cur_byte,w			
274 	BTFSC	STATUS,Z				; If End Byte, 
275 	GOTO 	LastByte					; Respond to end byte
276 	GOTO 	MiddleByte				; Else Respond to Middle bytes
277 
278 
279 
280 StartByte:	
281 					; Byte 0
282 	MOVLW	0x01
283 	CLRF	calc_CK
284 	CLRF	CK	
285 	MOVWF	byte_counter	
286 	Return
287 	
288 CKByte:	
289 	MOVF	cur_byte,w
290 	MOVWF	CK
291 	INCF	byte_counter, F 
292 	Return
293 		
294 
295 MiddleByte:
296 	
297 	INCF	byte_counter, F 
298 	MOVF	cur_byte,W
299 	XORWF	calc_CK,F
300 	Return
301 	
302 LastByte: 
303 	
304 	CLRF	byte_counter
305 
306 	MOVF	calc_CK,w
307 	XORWF	CK,W
308 	BTFSC	STATUS,	Z				; If two checksums match
309 	GOTO 	GoodMessage
310 
311 
312 	Call 	Test
313 	GOTO	BadMessage
314 
315 
316 
317 GoodMessage:
318 	MOVF	CK,w
319 	XORLW	popped_balloon
320 	BTFSC	STATUS,	Z				; If checksum says popped balloon
321 	GOTO	PoppedBalloon
322 		; REACT TO POPPED
323 
324 	MOVF	CK,w
325 	XORLW	happy_balloon
326 	BTFSC	STATUS,	Z				; If checksum says balloon not popped
327 	GOTO	HappyBalloon
328 	; React to HAPPY BALLOON
329 	GOTO	BadMessage
330 
331 HappyBalloon:
332 	; TOGGLE PORT C7
333 	MOVLW	0x20				
334 	banksel PORTC
335 	bsf		PORTC,7
336 
337 	GOTO	ClearCK
338 
339 PoppedBalloon:
340 	; TOGGLE PORT C7
341 	MOVLW	0x10				
342 	banksel PORTC
343 	bcf		PORTC,7
344 	GOTO	ClearCK
345 
346 
347 BadMessage:
348 	GOTO 	ClearCK 
349 
350 ClearCK:
351 	CLRF	calc_CK
352 	CLRF	CK
353 	Return
354 
355 	END
356 
357 
358 ;GET RID OF O/C