Here's a version that (hopefully) should work on interrupts,
Code:
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
Define SIMULATION_WAITMS_VALUE = 1 'Comment in for SIM out for PIC
Dim wordTemp As word
Dim ServoCount As Byte
Dim i As Word
Dim ServoPos(8) As word
Dim frame 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
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
PIE1.CCP1IE=1
INTCON.PEIE=1
INTCON.GIE=1
While 1
'adjust servo positions here
wend
end
on high interrupt 'go via location 0x0008
save system
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 (0.5mS) 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=39000 '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
resume
resume
There's no priority set as the default is to vector to the high priority interrupt (0x0008).
Can we just try this as is and see if it works?
Here's a picture of the ISR in Notepad++,
View attachment 140498
I hope you agree that having the indenting aids in following what the code is doing.
The dotted lines match up the If, Else & EndIf that go together.
The first If isn't actually needed as CCP1 is the only thing that can trigger interrupts but that's about to change.
The second If decides if we're doing the 0.5mS positive pulse or the remainder of the servo time.
The third If decides if we've done all servos and if so outputs the remaining time (in frame) and resets everything.
Also attached is the text file.
Mike.