Hi,
This is the control section of the project: https://forum.allaboutcircuits.com/...location-pic-in-oshonsoft.148795/post-1270205
I think that having a CONTROL SECTION to the project, will hopefully, save some confusion, if calculations and ideas are posted here instead of the main PBCs and programs for the peripherals etc.
This SERVO CONTROL CODE was kindly written by members of ETO, but I'm a bit stuck with a change to clock speed. It uses OSCCON which sets the OSC to 8mHz, where the main programs that this will be added into uses 8mHz Crystals X4 (PLL) so 32mHz.
I've been modifying it for testing, and I hope it is still ok. It moves the SERVOs incrementally, and once I;ve got this figured out, I can send it numbers to move the SERVOs instead.
If anyone kows how to change this I would appreciate it.
C.
This is the control section of the project: https://forum.allaboutcircuits.com/...location-pic-in-oshonsoft.148795/post-1270205
I think that having a CONTROL SECTION to the project, will hopefully, save some confusion, if calculations and ideas are posted here instead of the main PBCs and programs for the peripherals etc.
This SERVO CONTROL CODE was kindly written by members of ETO, but I'm a bit stuck with a change to clock speed. It uses OSCCON which sets the OSC to 8mHz, where the main programs that this will be added into uses 8mHz Crystals X4 (PLL) so 32mHz.
I've been modifying it for testing, and I hope it is still ok. It moves the SERVOs incrementally, and once I;ve got this figured out, I can send it numbers to move the SERVOs instead.
Code:
'18F4431 8MHz XTL PCB9 REMOTE_SLAVE SERVO 8 020324 0903
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 servoDir(8) As Byte
Dim frame As Word
Dim rec As Byte
Dim pos As Word
'Dim dir As Byte
Symbol rled = PORTD.7
'OSCCON = %01110000 '& h70 8mHz XTL PLL
TRISA = %11000000 '7=OSC, 6=OSC,
TRISB = %00000000 '0=SEROUT
TRISC = %11110010 '6=1-slave4431_cs, 3=74HC164 MR, 2=74HC164 CLK, 0=74HC164 DATA'<<<<<<<<<<<<
'TRISC = %11111010 'CCP0 (RC2) & RC0 output'<<<<<<<<<<<<<<<<<<<<@
TRISD = %00000000
LATC.0 = 0 'ensure data is low
LATC.2 = 0 'and clock
LATC.3 = 1 'Master reset HIGH for run.'<<<<<<<<<<<<<<
'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
'servoDir(i) = i And 1 '<<<<<<<added
'Next i
'Start up led
rled = 1
WaitMs 1000
rled = 0
WaitMs 1000
rled = 1
WaitMs 1000
rled = 0
WaitMs 1000
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'Captrue compare for timer1
INTCON.GIE = 1'!!!!!!!!!!!
ServoPos(0) = 1000
ServoPos(1) = 1100
ServoPos(2) = 1200
ServoPos(3) = 1300
ServoPos(4) = 1400
ServoPos(5) = 1500
ServoPos(6) = 1600
ServoPos(7) = 1700
'ServoPos(0) = 1500
'ServoPos(1) = 1500
'ServoPos(2) = 1500
'ServoPos(3) = 1500
'ServoPos(4) = 1500
'ServoPos(5) = 1500
'ServoPos(6) = 1500
'ServoPos(7) = 1500
While 1 '\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
While ServoCount = 0 'wait for 20mS to pass (=1/50th of a second)
Wend
While ServoCount <> 0
Wend
'Break'<<<<<<<<<<<<<<<<<<<<<
For i = 0 To 7
ServoPos(i) = ServoPos(i) + 1
'dir = servoDir(i) 'doesn't matter if ISR happens as servoPos isn't changed by ISR
pos = ServoPos(i)
If pos < 2000 Then 'have we gone past the end?
pos = 2000 'yes so make it the end stop
Endif
If pos > 4000 Then 'same for other end
pos = 4000
Endif
INTCON.GIE = 0
ServoPos(i) = pos
INTCON.GIE = 1
Next i
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 (is data pin of shift register)
Endif
Endif
CCPR1H = wordTemp.HB 'put value back into CCPR1
CCPR1L = wordTemp.LB
PIR1.CCP1IF = 0 'clear interrupt flag
Endif
Resume
C.
Last edited: