Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Hardware ESC 8xSERVO CONTROL on PIC (Oshonsoft BASIC)

Status
Not open for further replies.
They're working because you wrote the code using RC0 as the clock whereas the hardware needs RC2 to be the clock hence why the other code isn't working.

Mike.
Hi M,
I read your CODE over and over, then I spotted this line (In plain sight)
[ LATC.0 = 1 'connected to data in of shift register will clock in a high when CCP1 goes high ]

So mine was working because 2x wrongs made a right.
I'll swap it all over.
C
 
Hi M,
I read your CODE over and over, then I spotted this line (In plain sight)
[ LATC.0 = 1 'connected to data in of shift register will clock in a high when CCP1 goes high ]

So mine was working because 2x wrongs made a right.
I'll swap it all over.
C
Hi M,
I swapped the 2x wires from my CODE over, and the wires themselves, and it shows the same as #435.

I tried your translated CODE from #389, and it produces this:
No CLK.

You were going to send your C CODE HEX, but I haven't received it.
C
 

Attachments

  • No CLK.jpg
    No CLK.jpg
    189.2 KB · Views: 134
I've now got the logic analyser to work but don't understand it,

What does this mean,
View attachment 140322

The scale at the top suggests that the 0.235mS is correct but what is the 20mS and the 49.99Hz????
And how can channel 0 be the 8MHz clock, it looks more like 1.5kHz.

Mike.
Hi M,
Is the 20mS and 49.99mHz the FRAME?

I looked again at an early anaysis #408, and there's both CLK and DATA, so the error appears to be my end.
Leave it with me.
C
 
Last edited:
Can we try something completely different?

This code should give a 1kHz square wave on CCP1 pin (RC2)
Code:
'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 wordTemp As Word

OSCCON = %01110000  '& h70
TRISA = %11000000   '7=OSC, 6=OSC,
TRISC = %11111010   '6=1-slave4431_cs, 2=74HC164 CLK, 0=74HC164 DATA
wordTemp = 0
CCPR1H=tempWord.HB
CCPR1L=tempWord.LB
CCP1CON=0b1000
T1CON = 1  'timer 1 started
While 1  'loop forever
  If PIR1.CCP1IF Then
    CCP1CON=CCP1CON XOR 1
    tempWord=tempWord+4000
    CCPR1H=tempWord.HB
    CCPR1L=tempWord.LB
    PIR1.CCP1IF=0
  Endif
Wend
End
This will confirm IF the CCP module is able to be used.

The instruction I'm not sure of is,
CCP1CON=CCP1CON XOR 1
This should toggle bit zero of CCP1CON

Mike.
Edit, you may be able to use Toggle CCP1CON.0
 
Last edited:
Can we try something completely different?

Can we try something completely different?

This code should give a 1kHz square wave on CCP1 pin (RC2)
Hi M,
I had a few goes, and this produced this:
I changed 2x'wordtemp' to 'tempword' is this correct?
C


Code:
'18F4431 32MHz XTL REMOTE_SLAVE 164 120223 0900
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 tempword As Word

OSCCON = %01110000  '& h70
TRISA = %11000000  '7=OSC, 6=OSC,
TRISC = %11111010  '6=1-slave4431_cs, 2=74HC164 CLK, 0=74HC164 DATA
tempword = 0
CCPR1H = tempword.HB
CCPR1L = tempword.LB
CCP1CON = 0b1000
While 1  'loop forever
Toggle CCP1CON.0  '<<<<<<<<<<<<<<<<<<
If PIR1.CCP1IF Then
    'CCP1CON = CCP1CON Xor 1
    'Toggle CCP1CON.0  '<<<<<<<<<<<<<<<<<<
    'CCP1CON = 0x09'<<<<<<<<
    tempword = tempword + 4000
    CCPR1H = tempword.HB
    CCPR1L = tempword.LB
    PIR1.CCP1IF = 0
    'CCP1CON = 0x08'<<<<<<<<
Endif
Wend
End
 

Attachments

  • CCP.jpg
    CCP.jpg
    156.2 KB · Views: 142
Last edited:
Hi M,
While working through your CODE and my understanding, I had an idea!
I use FRAME as the 20mS total time, and subtract all of the SERVO times from FRAME.

After each SERVO has been switched on for 'it's' time, then wait the remainder 'FRAME'

I only used CCP1 for this, which eliminates the need for 2xdifferent INTERRUPT times, in other words, TMR2 is not neded.
What do you think?
C
 
Assuming that channel 1 is the output, it appears to be something like 900kHz.

What happens if you keep the original code I.E.
Code:
'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 wordTemp As Word

OSCCON = %01110000  '& h70
TRISA = %11000000   '7=OSC, 6=OSC,
TRISC = %11111010   '6=1-slave4431_cs, 2=74HC164 CLK, 0=74HC164 DATA
wordTemp = 0
CCPR1H=wordTemp.HB
CCPR1L=wordTemp.LB
CCP1CON=0b1000
T1CON = 1           'timer 1 started
While 1             'loop forever
  If PIR1.CCP1IF Then
    Toggle CCP1CON.0
    wordTemp=wordTemp+4000
    CCPR1H=wordTemp.HB
    CCPR1L=wordTemp.LB
    PIR1.CCP1IF=0
  Endif
Wend
End
The above is different to #444 as I edited that but didn't think it mattered as it (the BBS) said you'd not visited since the previous day. I added T1CON=1 which your code doesn't have so I guess you visited but weren't logged in.

Does the above make a 1kHz output on RC2? Note, the order of instructions is important. In this case it's important that CCPR1H is written before CCPR1L.

Generating a frame time will not work as it can be as long as 12mS (20-8*1mS) or 96,000 clock cycles and the maximum time that can be added in one go is 65,535 cycles. It could be done by dividing by two and adding it twice but the timer 2 interrupt shouldn't cause any problems.

Mike.
Edit, I've corrected the typos. I.E. wordTemp.
 
Alternatively, timer 1 can have a prescaler of 4 which would give a frame time of 40,000 and servo times from 2000 to 4000. This would enable calculating a frame time.

We can try this if you like.

However, I would still like to know if the above code gives a 1kHz signal.

Mike.
 
Assuming that channel 1 is the output, it appears to be something like 900kHz.

What happens if you keep the original code I.E.
Code:
'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 wordTemp As Word

OSCCON = %01110000  '& h70
TRISA = %11000000   '7=OSC, 6=OSC,
TRISC = %11111010   '6=1-slave4431_cs, 2=74HC164 CLK, 0=74HC164 DATA
wordTemp = 0
CCPR1H=wordTemp.HB
CCPR1L=wordTemp.LB
CCP1CON=0b1000
T1CON = 1           'timer 1 started
While 1             'loop forever
  If PIR1.CCP1IF Then
    Toggle CCP1CON.0
    wordTemp=wordTemp+4000
    CCPR1H=wordTemp.HB
    CCPR1L=wordTemp.LB
    PIR1.CCP1IF=0
  Endif
Wend
End
The above is different to #444 as I edited that but didn't think it mattered as it (the BBS) said you'd not visited since the previous day. I added T1CON=1 which your code doesn't have so I guess you visited but weren't logged in.

Does the above make a 1kHz output on RC2? Note, the order of instructions is important. In this case it's important that CCPR1H is written before CCPR1L.

Generating a frame time will not work as it can be as long as 12mS (20-8*1mS) or 96,000 clock cycles and the maximum time that can be added in one go is 65,535 cycles. It could be done by dividing by two and adding it twice but the timer 2 interrupt shouldn't cause any problems.

Mike.
Edit, I've corrected the typos. I.E. wordTemp.
Hi M,
Here's #447
I posted #444 in #445 plus my edits
C
 

Attachments

  • #447.jpg
    #447.jpg
    174.3 KB · Views: 132
Alternatively, timer 1 can have a prescaler of 4 which would give a frame time of 40,000 and servo times from 2000 to 4000. This would enable calculating a frame time.

We can try this if you like.

However, I would still like to know if the above code gives a 1kHz signal.

Mike.
Hi M,
Yes, try which ever looks best.
EDIT: Ignore the gaps in 'CH0' for now.
C
 
Last edited:
Is channel one 1kHz, it looks to have an on time of 0.5mS which would be 1kHz but it's too zoomed in to tell.

I'll work on some code to just use CCP1.

Mike.
 
Is channel one 1kHz, it looks to have an on time of 0.5mS which would be 1kHz but it's too zoomed in to tell.

I'll work on some code to just use CCP1.

Mike.
Hi M,
Here it is zoomed out.
C
 

Attachments

  • #451.jpg
    #451.jpg
    182.4 KB · Views: 129
Here's my first attempt at an Oshonsoft version without priority interrupts.
Code:
'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 wordTemp 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
for i=0 to 7
  servoPos(i)=i*250+2000; '1ms(2000) to 1.875(7/8ths - 3750)ms in 1/8th mS steps
next
TRISC=0b11111010          'CCP0 (RC2) & RC0 output
servoCount=8
T1CON=0b00100000
T1CON.0=1
CCP1CON=0b1000            'will go low on interrupt
PIE1.CCP1IE=1
frame=1000                'start everything in 4000 cycles
INTCON.PEIE=1
INTCON.GIE=1
while(1)
                          'adjust servo positions here
wend

End

On Interrupt
Save System
If PIE1.CCP1IE And PIR1.CCP1IF Then  'Is it a CCP1 interrupt
  If CCP1CON = 0x08 Then  '& h8 Then  'have we started the 4000 cycle pulse
    CCP1CON = 0x09  '& h9  'yes so end the pulse after 0.5mS
    wordtemp.HB = CCPR1H
    wordtemp.LB = CCPR1L
    wordtemp = wordtemp + 1000
    CCPR1H = wordtemp,HB
    CCPR1L = wordtemp.LB
  Else
    LATC.0=0;
    CCP1CON = 0x08  '& h8  'No so output the timed gap
    if servoCount=8 then
      wordtemp.HB = CCPR1H
      wordtemp.LB = CCPR1L
      wordtemp = wordtemp - 1000 + servoPos(servocount)
      CCPR1H = wordtemp,HB
      CCPR1L = wordtemp.LB
      frame=frame-servoPos(servoCount)
      servocount = servocount + 1
    else
      wordtemp.HB = CCPR1H
      wordtemp.LB = CCPR1L
      wordtemp = wordtemp +frame
      CCPR1H = wordtemp,HB
      CCPR1L = wordtemp.LB
      frame=31000           '40,000-9,000
      servoCount=0
      LATC.0=1
    endif
  Endif
  PIR1.CCP1IF = 0
Endif
Resume
Note that in order to move a servo disable interrupts around it, I.E.
INTCON.GIE=0
servoPos(4)=nnnn
INTCON.GIE=1

The frame time is automatically calculated in the interrupt so don't worry about that.

Mike.
 
Can you try that code and let me know what happens?

Thanks,

Mike.
Hi M,
A few minor edits, shown 'EDIT<<<<<<<<<<<<
Here's FRAME and SERVO 1.

As a 1st attempt at OSH, you've overtaken me, after 4million ish attempts:)
C
 

Attachments

  • Frame 16.jpg
    Frame 16.jpg
    165 KB · Views: 138
  • SERVO 1.jpg
    SERVO 1.jpg
    170.3 KB · Views: 133
  • 18F4431 32MHz XTL REMOTE_SLAVE 164 130223 0900 EDITED.doc
    2.5 KB · Views: 167
That looks pretty good except for the traces. The corrections to the code appear to mainly typos except for the line,
On High Interrupt
Why does it need to be on high interrupt, the manual suggests that normal interrupts can be used.
ISR.png


As there's only one interrupt it seems silly to use priority when there's no need.

Can you explain what the traces are in the diagrams? They look to be completely wrong.

Mike.
 
CCP1CON=CCP1CON XOR 1
 
That looks pretty good except for the traces. The corrections to the code appear to mainly typos except for the line,
On High Interrupt
Why does it need to be on high interrupt, the manual suggests that normal interrupts can be used.
View attachment 140435

As there's only one interrupt it seems silly to use priority when there's no need.

Can you explain what the traces are in the diagrams? They look to be completely wrong.

Mike.
Hi M,
Yes, the errors were mostly [ . ; ( ) ]
I don't understand the ON INTERRUPT error, but this CODE will be added to larger CODE, where a GPS INTERRUPT will be, which could be LOW INTERRUPT, so this is why I chose it.

The analyser traces:
CH0 is connected to the 8mHz XTL on the PCB.
CH1 is connected to PORTC.2 on the PIC
CH2 is connected to PORTC.0 on the PIC.
CH3 Shift register 1
CH4 Shift register 2
CH5 Shift register 3


C
 

Attachments

  • ON INTERRUPT.jpg
    ON INTERRUPT.jpg
    81.9 KB · Views: 135
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top