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.
Can you try to compile this?
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

Const NUM_SERVOS = 10
Const buf_len = 80

Dim Buff(buf_len) As Byte
Dim StrCount As Byte
Dim isDone As Bit
Dim WordTemp As word
Dim Ms As Long
Dim Count As Byte
Dim ServoCount As Byte
Dim I As Byte
Dim ServoPos(NUM_SERVOS) As word
Dim Char As Byte
I suspect DONE (only needs to be a bit as can only be 0 or 1) and maybe BUFFER to be reserved words.

Mike.
 
Does it give a reason for the errors?
I see,
Const BUFFER=80
is fine But
Dim Buff(BUFFER) as byte
isn't, what is the error here?
does,
Dim Buff(80) as byte
compile?
I also notice that num_servos isn't defined hence why
Dim servoPos(num_servos) As integer
is an error.
Does the code in #358 compile?

Mike.
At least we're allowed constants now.
Edit, is oshonsoft not case critical? I.E. Byte is the same as byte or buffer is the same as BUFFER.
Edit2, is integer the same as word I.E. 16 bit variables?
Edit3, if you do x=6-2*3 is the answer zero or 12?
Hi M,Ssometimes I get a reason, and sometimes, I have to check D/S

Let's try a couple of things,
Does this compile?
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

Const NUM_SERVOS = 10
Const BUFFER = 80

Dim Buff(BUFFER) As Byte
Dim StrCount As Byte
Dim Done As Byte
Dim WordTemp As Integer
Dim Ms As Long
Dim Count As Byte
Dim ServoCount As Byte
Dim I As Byte
Dim ServoPos(NUM_SERVOS) As Integer
end
If not, what error does it give?
If it's the Const lines then try changing Const to Symbol.

Mike.
Hi M,
This on it's own shows:
Dim Buff(BUFFER) As Byte Incorrect ARRAY
Dim StrCount As Byte 'Reserved
Dim Done As Byte 'Reserved
Dim WordTemp As Integer Syntax
Dim Ms As Long
Dim Count As Byte
Dim ServoCount As Byte
Dim I As Byte
Dim ServoPos(NUM_SERVOS) As Integer Syntax
 
Hi m,
Should I be working on your first CODE, where the RC0 andRC2 flashed
Or #355?

Back later
C
 
Does this fix the errors?
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

Const NUM_SERVOS = 10

Dim buff(80) As Byte
Dim sCount As Byte
Dim isDone As Bit
Dim WordTemp As word
Dim Ms As Long
Dim Count As Byte
Dim ServoCount As Byte
Dim i As Byte
Dim ServoPos(NUM_SERVOS) As word
Dim Char As Byte
end


Mike.
Edit, changed strCount to sCount.
 
Does this fix the errors?
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

Const NUM_SERVOS = 10

Dim buff(80) As Byte
Dim sCount As Byte
Dim isDone As Bit
Dim WordTemp As word
Dim Ms As Long
Dim Count As Byte
Dim ServoCount As Byte
Dim i As Byte
Dim ServoPos(NUM_SERVOS) As word
Dim Char As Byte
end


Mike.
Edit, changed strCount to sCount.
Hi m,
COUNT Reserved
Dim ServoPos(NUM_SERVOS) As Word'Incorrect ARRAY
---------------------------------------------------------
If it is necessary to count the number of pulses that come to one of the micrcontroller's pins during a certain period of time, there is COUNT statement available for that purpose. It has three arguments. The first one is the pin that is connected to the source of pulses. COUNT statement will setup the pin as an input pin. The second argument defines the duration of the observation expressed in milliseconds and it must be a numeric constant in the range 1-10000. The last argument of this statement is a Byte or Word variable where the counted number of pulses will be stored after its execution. COUNT statement uses internal Timer0 peripheral module. There is COUNT_MODE parameter available that can be setup with #define directive. If it is set to value 1 (default value) COUNT statement will count the number of rising pulse edges. If COUNT_MODE = 2, the number of falling edges will be counted.
#define COUNT_MODE = 1
Dim num_of_pulses As Word
Count PORTB.0, 1000, num_of_pulses
------------------------------------------------------------------------
C
 
Hi M,
If you prefer to work in these steps, that's fine. (I think it will take a long time)
Or I can carry on with your choice of CODE to do the same.
C
 
Hi M,
It may be good for you to download the evealuation free compy of the Oshonsoft simulator here:

I think you get 30 free goes, so you'll get the errors I get.
C
 
Hi M,
I don't always see EDITs, as I've moved down the thread!

This:
------------------------------------------------
Dim x As Byte
main:
x = 6
WaitMs 1
x = 6 - 2 * 3
WaitMs 1
Goto main
End
---------------------------------------------------
Gives 6 0 6 0 etc
 
Hi M,
Here is todays CODE:
It compiles and runs in the sim.
The [ servocount ] counts to 2 then zeros.
RC0 and RC2 are flashing
C
 

Attachments

  • 18F4431 32MHz XTL REMOTE_SLAVE 164 3 020223 1600.txt
    4.9 KB · Views: 243
  • 020223 1600.jpg
    020223 1600.jpg
    198.2 KB · Views: 225
There's some silly mistakes in there, the most obvious one being,
If PIE1.TMR2IE And PIR1.TMR2IF Then 'Is it a CCP1 interrupt
Which should be,
If PIE1.CCP1IE And PIR1.CCP1IF Then 'Is it a CCP1 interrupt

There's also zeroing timer 1 isn't a 16 bit write.

I've made some changes to fix these things and removed the UART code,
Code:
'18F4431 32MHz XTL REMOTE_SLAVE 164 3 020223 1600
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

TRISA = %11000000  '7=OSC, 6=OSC,
TRISB = %00000000
TRISC = %11110000  '6=1-slave4431_cs, 2=74HC164 CLK, 0=74HC164 DATA
TRISD = %00000000  '6=led, 7=led, 5=sync
TRISE = %00000000

Dim wordtemp As Word
Dim ms As Long
Dim cnt As Byte
Dim servocount As Byte
Dim i As Byte

Dim servoPos(9) As Word  'Byte  'integer  'BYTE is an INTEGER?

Enable High  'This is set for SERVOS
Enable Low  'This is set for GPS later

main:  '\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
OSCCON = %01110000  '& h70

'setup 1mS interrupt = 8,000,000/16 = 500,000/10 = 50,000 set PR2=49 = 50,000/50 = 1000 = 1mS
T2CON = %01001110  'pre=16 post=10
PR2 = 49  'This is timer 2 period register. I.E. reset after 50 clocks (0 to 49 = 50 counts)
PIE1.TMR2IE = 1  'timer 2 interrupts enable
T1CON = 0  'timer 1 stopped

For i = 0 To 8
    servoPos(i) = i * 1000 + 8000  '1ms(8000) to 1.875(7/8ths - 15000)ms in 1/8th mS steps
Next i

INTCON.PEIE = 1
INTCON.GIE = 1
While 1  'loop forever because everything is done on interrupts
'adjust servo positions here
Wend

On High Interrupt
Save System
Break  '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
If PIE1.TMR2IE And PIR1.TMR2IF Then  'Is it a timer 2 interrupt
    ms = ms + 1  'Yes, so increment ms variable
    cnt = cnt + 1  'If cnt >= 20 Then  'start every 20mS
    If cnt >= 20 Then  'start every 20mS
        T1CON = 0 'stop timer 1
        TMR1L = 0 'zero timer 1
        TMR1H = 0 'zero timer 1
        cnt = 0  'Reset count
        CCP1CON = %00001000  ' & h8  'CCP1 pin low and high on match - will be first pulse
        CCPR1L = 0x13  '& hd 'Start pulse in 0.25mS (2000 clock cycles)?
        CCPR2H = 0x07  '& h7
        PIE1.CCP1IE = 1 'enable CCP1 interrupts
        PIR1.CCP1IF = 0 'ensure interrupt flag is clear
        servocount = 0  'reset servoCount
        LATC.0 = 1      'connected to data in of shift register will clock in a high when CCP1 goes high
        T1CON = 1       'start timer 1
    Endif
    PIR1.TMR2IF = 0  '0 = no TMR2 To PR2 match occurred
Endif
If PIE1.CCP1IE And PIR1.CCP1IF Then  'Is it a CCP1 interrupt
    LATC.0 = 0  'Yes, so clear the data in pin as the high has already been clocked in
    If servocount = 9 Then  'have we done all servos?
        PIE1.CCP1IE = 0  'yes so no more CCP1 interrupts
    Endif
        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 = CCPR1H * 256 + CCPR1L
            wordtemp = wordtemp + 4000
            CCPR1L = wordtemp And 255
            CCPR1H = wordtemp / 256
        Else
            CCP1CON = 0x08  '& h8  'No so output the timed gap
            wordtemp = CCPR1H * 256 + CCPR1L
            wordtemp = wordtemp - 4000 + servoPos(servocount)
            CCPR1L = wordtemp And 255
            CCPR1H = wordtemp / 256
            servocount = servocount + 1
        Endif
    PIR1.CCP1IF = 0
Endif
Resume
I've also removed the two lines that write to CCPR1 as this is not a 16 bit write. Nothing should ever write to CCPR1, don't know why they bothered to convert the Pic file and change a word variable into a byte - stupid.

Mike.
 
There's some silly mistakes in there, the most obvious one being,
If PIE1.TMR2IE And PIR1.TMR2IF Then 'Is it a CCP1 interrupt
Which should be,
If PIE1.CCP1IE And PIR1.CCP1IF Then 'Is it a CCP1 interrupt

There's also zeroing timer 1 isn't a 16 bit write.

I've made some changes to fix these things and removed the UART code,
Code:
'18F4431 32MHz XTL REMOTE_SLAVE 164 3 020223 1600
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

TRISA = %11000000  '7=OSC, 6=OSC,
TRISB = %00000000
TRISC = %11110000  '6=1-slave4431_cs, 2=74HC164 CLK, 0=74HC164 DATA
TRISD = %00000000  '6=led, 7=led, 5=sync
TRISE = %00000000

Dim wordtemp As Word
Dim ms As Long
Dim cnt As Byte
Dim servocount As Byte
Dim i As Byte

Dim servoPos(9) As Word  'Byte  'integer  'BYTE is an INTEGER?

Enable High  'This is set for SERVOS
Enable Low  'This is set for GPS later

main:  '\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
OSCCON = %01110000  '& h70

'setup 1mS interrupt = 8,000,000/16 = 500,000/10 = 50,000 set PR2=49 = 50,000/50 = 1000 = 1mS
T2CON = %01001110  'pre=16 post=10
PR2 = 49  'This is timer 2 period register. I.E. reset after 50 clocks (0 to 49 = 50 counts)
PIE1.TMR2IE = 1  'timer 2 interrupts enable
T1CON = 0  'timer 1 stopped

For i = 0 To 8
    servoPos(i) = i * 1000 + 8000  '1ms(8000) to 1.875(7/8ths - 15000)ms in 1/8th mS steps
Next i

INTCON.PEIE = 1
INTCON.GIE = 1
While 1  'loop forever because everything is done on interrupts
'adjust servo positions here
Wend

On High Interrupt
Save System
Break  '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
If PIE1.TMR2IE And PIR1.TMR2IF Then  'Is it a timer 2 interrupt
    ms = ms + 1  'Yes, so increment ms variable
    cnt = cnt + 1  'If cnt >= 20 Then  'start every 20mS
    If cnt >= 20 Then  'start every 20mS
        T1CON = 0 'stop timer 1
        TMR1L = 0 'zero timer 1
        TMR1H = 0 'zero timer 1
        cnt = 0  'Reset count
        CCP1CON = %00001000  ' & h8  'CCP1 pin low and high on match - will be first pulse
        CCPR1L = 0x13  '& hd 'Start pulse in 0.25mS (2000 clock cycles)?
        CCPR2H = 0x07  '& h7
        PIE1.CCP1IE = 1 'enable CCP1 interrupts
        PIR1.CCP1IF = 0 'ensure interrupt flag is clear
        servocount = 0  'reset servoCount
        LATC.0 = 1      'connected to data in of shift register will clock in a high when CCP1 goes high
        T1CON = 1       'start timer 1
    Endif
    PIR1.TMR2IF = 0  '0 = no TMR2 To PR2 match occurred
Endif
If PIE1.CCP1IE And PIR1.CCP1IF Then  'Is it a CCP1 interrupt
    LATC.0 = 0  'Yes, so clear the data in pin as the high has already been clocked in
    If servocount = 9 Then  'have we done all servos?
        PIE1.CCP1IE = 0  'yes so no more CCP1 interrupts
    Endif
        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 = CCPR1H * 256 + CCPR1L
            wordtemp = wordtemp + 4000
            CCPR1L = wordtemp And 255
            CCPR1H = wordtemp / 256
        Else
            CCP1CON = 0x08  '& h8  'No so output the timed gap
            wordtemp = CCPR1H * 256 + CCPR1L
            wordtemp = wordtemp - 4000 + servoPos(servocount)
            CCPR1L = wordtemp And 255
            CCPR1H = wordtemp / 256
            servocount = servocount + 1
        Endif
    PIR1.CCP1IF = 0
Endif
Resume
I've also removed the two lines that write to CCPR1 as this is not a 16 bit write. Nothing should ever write to CCPR1, don't know why they bothered to convert the Pic file and change a word variable into a byte - stupid.

Mike.
Hi M,
I'm not sure which 'stupid BYTE' you mean, but that was likely to be me that did it.

The above program only flashes RC0 and RC2 1xtime.
SERVOCOUNT stays at 0
WORDTEMP stays at 38019
CNT counts in a loop
I counts in a loop
C
 
The stupid byte comment was not your doing.
Each Pic chip has a file which describes all the SFRs and each 16bit SFR has 3 definitions, E.G.
TMR1L the low byte of timer one
TMR1H the high byte of timer one
TMR1 the word definition of timer one
Oshonsoft left the word definition as a byte.

Can you add these two line before the servo initialisation loop.
ms=0
tempWord=0

Before
For i = 0 To 8
servoPos(i) = i * 1000 + 8000 '1ms(8000) to 1.875(7/8ths - 15000)ms in 1/8th mS steps
Next i
Note, this is setting up 9 servos as the start of servo 9 is the end of servo 8's pulse and so is needed.

And see what's in them at the break.

Is the interrupt definitely executing the lower CCP1 code? CCP1IE can only get set if cnt reaches 20.

cnt should be counting to 19 and resetting to zero. Is this happening?

Are we executing the code in #371?

Mike.
 
I think we can do (well, according to the manual we can)
Dim CCPR1word As word @ CCPR1L
Dim TMR1word As word @ TMR1L
Does this compile?

Mike.
 
I think we can do (well, according to the manual we can)
Dim CCPR1word As word @ CCPR1L
Dim TMR1word As word @ TMR1L
Does this compile?

Mike.
Hi M,

Dim CCPR1word As Word '@ CCPR1 Only compiles with '@ CCPR1 out.
Dim TMR1word As Word '@ TMR1L " " " "
C
 
I did, of course, mean wordTemp.

Does
Dim CCPR1word As word @ 0x0FBE
compile?

I realise it's your bedtime now so no more questions for now.


Mike.
 
I'm guessing it'll only let you declare variables in user RAM (SFRs) which Oshonsoft has restricted to the 0 to 511 area.

Mike.
 
The stupid byte comment was not your doing.
Each Pic chip has a file which describes all the SFRs and each 16bit SFR has 3 definitions, E.G.
TMR1L the low byte of timer one
TMR1H the high byte of timer one
TMR1 the word definition of timer one
Oshonsoft left the word definition as a byte.

Can you add these two line before the servo initialisation loop.
ms=0
tempWord=0

Before
For i = 0 To 8
servoPos(i) = i * 1000 + 8000 '1ms(8000) to 1.875(7/8ths - 15000)ms in 1/8th mS steps
Next i
Note, this is setting up 9 servos as the start of servo 9 is the end of servo 8's pulse and so is needed.

And see what's in them at the break.

Is the interrupt definitely executing the lower CCP1 code? CCP1IE can only get set if cnt reaches 20.

cnt should be counting to 19 and resetting to zero. Is this happening?

Are we executing the code in #371?

Mike.
Hi M,
1st BREAK
0.12us
all '0'

2nd BREAK
1005.12 us
i = 9
all rest '0'

3rd BREAK
2005.12 us
ms =1
CNT = 1
SERVOPOS() 0-8 filled
i=9
Rest '0'

?x BREAK
CNT= flows over 20, resets

RC0 and RC2 ON.

Possible errors of me counting!
C
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top