You are on page 1of 5

;

;
;
;
;

Initialization based on Greg Campbell's Lab 10, 4-22-15


edits by Josh starting 5/9/2015
edits by Josh starting 5/12/2015
edits by Josh starting 5/13/2015
more edits by Josh 5/14/2015
list P=PIC16F690
#include "p16f690.inc"
__config (_CP_OFF & _WDT_OFF & _PWRTE_ON & _HS_OSC & _BOR_ON & _MCLRE_OFF)

;
;
;

Variable Definitions

FirstByte
LastByte
ByteReceived
CheckSum
TransmitByte
IsQuerying
BalloonIsPopped
IsIntact
IsPopped
IsCorrupt
TransmitCounterLO
TransmitCounterHI
PoppedCheckSum
IntactCheckSum
WREG_TEMP
STATUS_TEMP
PCLATH_TEMP
;
;
;

;
;
;

equ
equ
equ
equ
equ
equ
equ
equ
equ
equ
equ
equ
equ
equ
equ
equ
equ

0x02
0x03
0x20
0x21
0x22
0x23
0x24
0x25
0x26
0x27
0x28
0x29
0x53
0x5F
0x70
0x71
0x72

org
goto
org
goto

0
Main
4
Receive

Initialization Code

Main Code Starts Here

Main:
call
call
clrf

Initialize
InitTimer1
TransmitByte

clrf
clrf

TransmitCounterHI
TransmitCounterLO

btfss
goto
bcf
btfss

PIR1,CCP1IF
Loop
PIR1,CCP1IF
IsQuerying,0

; go initialize GPIO/EUSART
; go initialize Timer 1
; set the contents of TransmitByte
to 0x00

Loop:

call AddBytesFor2ms
btfsc IsQuerying,0
call

AddBytesFor98ms

; check if the flag is set


; go back to keep checking for flag
; clear the flag if it is set
; if the 0th bit is set (we've just
finished the interval between a
query)
; change the output compare value
; do the next instruction if the
0th bit is set
; change the output compare so
we're not querying

comf

IsQuerying,f

goto

Loop

; compmlement the bits in


IsQuerying
; keep doing this loop

Initialize:
; Initialize Ports A, B, and C (Inputs on A, outputs on B, output on C3)
bcf
STATUS,RP0
; prepare to clear/initialize
ports(data bank 0)
bcf
STATUS,RP1
clrf PORTA
clrf PORTB
clrf PORTC
bsf
STATUS,RP1
; prepare to clear ANSEL/ANSELH,
disable pull-ups (data bank 2)
clrf ANSEL
clrf ANSELH
bcf
WPUB,WPUB5
; disable weak pull-up resistor on
B5 (Rx line)
bcf
WPUB,WPUB7
; disable weak pull-up resistor on
B7 (Tx line)
bsf
STATUS,RP0
; prepare to write to
TRIS/OPTION_REG/WPUA (data bank1)
bcf
STATUS,RP1
movlw 0xff
; set pins on Port A to be inputs
movwf TRISA
bsf
TRISB,TRISB5
; set B5 as an input
bcf
TRISB,TRISB7
; set B7 as an output
bcf
TRISB,TRISB4
; set B4 as an output
bcf
TRISB,TRISB6
; set B6 as an output
bcf
OPTION_REG,NOT_RABPU
; globally enable pull-up
resistors
bsf
WPUA,WPUA0
; enable weak pull-up resistor on
A0
;Asynchronous Transmission/Reception Initialization
movlw
0x20
; set baud rate to 9600; note this
gives us a 1.3% error, SPBRG=32
movwf SPBRG
;
bcf
STATUS,RP0
; prepare to set GIE and PEIE in
INTCON (data bank 0)
bsf
INTCON,GIE
; enable global interrupts
bsf
INTCON,PEIE
; enable peripheral interrupts
bcf
PIR1,RCIF
; clear the receive interrupt flag
bcf
PIR1,TXIF
; clear the transmit interrupt
flag
bsf
STATUS,RP0
; prepare to set PIE1 (data bank 1)
bsf
PIE1,RCIE
; enable receive interrupts
bcf
STATUS,RP0
; prepare to set RCSTA (data bank
0)
bsf
RCSTA,SPEN
; enable Rx and Tx lines as serial
ports
bsf
RCSTA,CREN
; enables continuous receive
(enables reception)
bcf
RCSTA,RX9
; 8-bit reception
bcf
RCSTA,ADDEN
; clear the address detect enable bit
bsf
STATUS,RP0
; prepare to write to BAUDCTL (data
bank 1)
bcf
BAUDCTL,SCKP
; noninverting Tx polarity
bcf
BAUDCTL,WUE
; receiver is operating
normally (not wake-up)
bcf
BAUDCTL,ABDEN
; auto-baud rate disabled
bcf
TXSTA,SYNC
; asynchronous mode
bcf
TXSTA,SENDB
; don't send break
transmission
bsf
TXSTA,TXEN
; enable transmission

bcf

STATUS,RP0

; get back to bank 0

InitTimer1:
bcf
STATUS,RP0
movlw 0x31
movwf T1CON
movlw 0x08

; get back to bank 0


; 1:8 prescaler, enable clock
; compare mode, generate flag on

match
movwf CCP1CON
bcf
PIR1,CCP1IF

; clear the flag in case it's

set
clrf IsQuerying
; clear IsQuerying
call AddBytesFor98ms
; since we're not querying we want
an interval of 98ms
return
AddBytesFor98ms:
; add a value of 0xEF42
bsf
PORTB,4
movlw 0x42
addwf CCPR1L,f
movlw 0xEF
btfsc STATUS,C
addlw 1
addwf CCPR1H,f
return
AddBytesFor2ms:
; add a value of 0x04E2
bcf
PORTB,4
movlw 0xE2
addwf CCPR1L,f
movlw 0x04
btfsc STATUS,C
addlw 1
addwf CCPR1H,f
call TransmitToTiva
return
TransmitToTiva:
btfsc BalloonIsPopped,0 ; check if the balloon has been popped
call TransmitPopped
btfss BalloonIsPopped,0
call TransmitIntact
return
TransmitIntact:
movlw 0xAA
; start byte
movwf TXREG
; data to be transmitted out to the
Tiva
return
TransmitPopped:
movlw 0x55
; start byte
movwf TXREG
; data to be transmitted out to the
Tiva
return
;
;Interrupt Response Routine
;
Receive:
movwf
WREG_TEMP
movf STATUS,W
clrf
STATUS
movwf
STATUS_TEMP
movf
PCLATH,W
movwf
PCLATH_TEMP
clrf
PCLATH

;Store W Register
;Grab Status Register
;Clear Status Register
;Store Old Status
;Grab PCLATH
;Store Old PCLATH
;Clear PCLATH

DisableInterrupts:
bcf
bcf
bcf
ReceiveData:

STATUS,RP0
STATUS,RP1
INTCON,GIE
movf

RCREG,W

;Bank0
;
;Disable Interrupts
; move the received data

into the W
movwf ByteReceived
; store away the received data
xorlw FirstByte
; check whether the byte is 0x02
btfss STATUS,Z
; if it isn't set then we want to
check if it's the end bit
goto CheckForLastByte
clrf IsPopped
clrf IsIntact
clrf PoppedCheckSum
clrf IntactCheckSum
goto Leave
CheckForLastByte:
movf ByteReceived,W
; put ByteReceived into the W
xorlw LastByte
btfsc STATUS,Z
; if the status bit is set, we've
received 0x03, and we want to check everything
goto CheckIfPopped
;
IntactCheckSumByte:
movf ByteReceived,W
; we're checking if the byte
corresponds to an intact checksum
xorlw IntactCheckSum
btfss STATUS,Z
; if it's not the Intact Check sum,
we'll check for popped
goto PoppedCheckSumByte
bsf
IsIntact,0
goto Leave
PoppedCheckSumByte:
movf ByteReceived,W
; we're checking if the byte
corresponds to an popped checksum
xorlw PoppedCheckSum
btfss STATUS,Z
; if it's not the poppedCheckSum we
go to AddToCheckSum
goto AddToCheckSum
bsf
IsPopped,0
goto Leave
AddToCheckSum:
movf ByteReceived,W
xorwf CheckSum,f
goto Leave
CheckIfPopped
btfss IsPopped,0
; if we've succesfully gotten
0x03, and IsPopped is set, then we set BalloonIsPopped
bcf
BalloonIsPopped,0
btfss IsPopped,0
bcf
PORTB,6
btfss IsPopped,0
goto Leave
bsf
BalloonIsPopped,0
bsf
PORTB,6
Leave:
clrf
STATUS
; clear Status Register
movf
PCLATH_TEMP,W
; grab Old PCLATH
movwf
PCLATH
; restore PCLATH
movf
STATUS_TEMP,W
; grab Old Status
movwf
STATUS
; restore Status
swapf
WREG_TEMP,f
; flip Old WREG
swapf
WREG_TEMP,W
; restore Old W without affecting Status
retfie
; re-Enables Interrupts

end

You might also like