'18F4431 32MHz XTL REMOTE_SLAVE 164 160223 2330
Define CONFIG1L = 0x00
Define CONFIG1H = 0x06 '8mHz XTL x4 =32mHz
Define CONFIG2L = 0x0c
Define CONFIG2H = 0x20
Define CONFIG3L = 0x04
Define CONFIG3H = 0x80
Define CONFIG4L = 0x80 'Set for HVP
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40
Define CLOCK_FREQUENCY = 32
Define SINGLE_DECIMAL_PLACES = 2
Define STRING_MAX_LENGTH = 20
Dim wordTemp As Word
Dim i As Word
Dim cnt As Byte
dim fifo(32) as byte
dim fifoStart as byte
dim fifoEnd as byte
Dim ccpDone As Bit
Dim previous as Bit
Dim TXbyte as byte
Dim bitCount as byte
Dim str as string
dim serialCount as word
OSCCON = %01110000 '& h70
TRISA = %11000000 '7=OSC, 6=OSC,
TRISC = %11111010 '6=1-slave4431_cs, 2=74HC164 CLK, 0=74HC164 DATA
LATC.0 = 0 'ensure data is low
LATC.2 = 0 'and clock
For i = 0 To 7
LATC.2 = 1 'send positive clock edge
LATC.2 = 0 'send negative edge
Next i
TRISC = %11111010 'CCP2 (RC1) output
T1CON = %00100000 'prescaler = 4
T1CON.0 = 1 'start timer
PIE2.CCP2IE = 1 'CCP2 interrupts enable
INTCON.PEIE = 1
INTCON.GIE = 1
While 1
if serialCount>=9600 then 'every second do this
serialCount=0
str="Mike Was Ere!!" + CrLf
for i=0 to len(str))
putFifo(str(i))
next i
Endif
Wend
End
proc putFifo(dat as byte)
dim temp as byte
temp=fifoStart
if ((fifoEnd-temp) AND 0x1f)=0x1f then 'is fifo full
while fifoStart=temp 'yes so wait for interrupt
wend 'to make room
Endif
fifo(fifoEnd)=dat 'add data to queue
fifoEnd=(fifoEnd+1) AND 0x1f 'and increment pointer
End Proc
function getFifo() as byte 'note, doesn't test if fifo is empty
getFifo=fifo(fifoStart) 'so always call getFifoLen first
fifoStart=(fifoStart+1) AND 0x1f
End Function
function getFifoLen())
getFifoLen=(fifoEnd-fifoStar) AND 0x1f
End Function
On High Interrupt 'go via location 0x0008
Save System
if PIR2.CCP2IF Then
serialCount=serialCount+1
wordTemp.LB=CCPR2L 'ensure next interrupt is
wordTemp.HB=CCPR2H 'in 208*2=832 instruction cycles
wordTemp=wordTemp+208 '208 is 2,000,000/9600
CCPR2L=wordTemp.LB 'write it back
CCPR2H=wordTemp.HB
LATC.1=PORTC.1 'ensure latch is same as current CCP output
if ccpDone then
bitCount=0
if getFifoLen() then
TXbyte=getFifo()
CCP@CON=%1001
ccpDone=false
previous=0
Endif
else
'we're sending a byte
if bitCount<9 then
if TXbyte.1=previous then
'just need the interrupt
CCP2CON=%1010
else
'we need to flip the bit
if previous Then
'we need a zero next
previous=0
CCP2CON=%1001
else
'we need a one next
previous=1
CCP2CON=%1000
endif
endif
TXbyte=shiftRight(TXbyte,1)
else
'doing the stop bits
if bitCount<10 then
if previous then
CCP2CON=%1010 'just need interrupt
else
previous=1
CCP2CON=%1000 'go high next interrupt
endif
else
ccpDone=true
Endif
Endif
Endif
bitCount=bitCount+1
PIR2.CCP2IF=0
Endif
Resume