'18F4431 32MHz XTL REMOTE_SLAVE 164 389 030223 1200
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 SIMULATION_WAITMS_VALUE = 1 'Comment in for SIM out for PIC
Dim servoCount as byte
Dim i as Word
Dim wordTemp As Word
Dim servoPos(8) as word
Dim frame as word
'need to ensure the 164 is reset at power on.
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 'snd clock
For i = 0 to 7
LATC.2=1 'send positive clock edge
servoPos(i)=i*250+2000 '1ms(2000) to 1.875(1 7/8ths - 3750)ms in 1/8th mS steps
LATC.2=0 'send negative edge
Next i
TRISC=%11111010 'CCP0 (RC2) & RC0 output
servoCount=8 'cause it to reset
T1CON=%00100000 'prescaler = 4
T1CON.0=1 'start timer
CCP1CON=%1000 'will go high on interrupt - will start a 0.5mS pulse
frame=1000 'start everything in 4000 cycles
While 1
If PIR1.CCP1IF Then 'has CCP1 triggered?
wordtemp.HB = CCPR1H 'get value of CCPR1 into wordTemp
wordtemp.LB = CCPR1L
If CCP1CON = 0x08 Then 'have we started the 1000 cycle pulse
CCP1CON = 0x09 'yes so end the pulse after 0.5mS
wordtemp = wordtemp + 1000 'adding 1000 will make pulse 0.5mS long
Else
LATC.0=0 'clear the data pin
CCP1CON = 0x08 'No so output the timed gap
if servoCount<8 then
'still doing the servos so add remainder of time
wordtemp = wordtemp+servoPos(servoCount)
wordTemp=wordTemp-1000 'knock of the 1000 already elapsed
frame=frame-servoPos(servoCount)
servoCount = servoCount + 1
Else
'done all the servos so just the frame gap to do
wordtemp = wordtemp +frame
frame=31000 '40,000(20mS) minus time of positive pulse(0.5mS)
servoCount=0 'start all over again
LATC.0=1 'will become first pulse
Endif
Endif
CCPR1H = wordtemp.HB 'put value back into CCPR1
CCPR1L = wordtemp.LB
PIR1.CCP1IF = 0 'clear interrupt flag
Endif
'change any servo positions here
Wend
End