PIC 12f683 Servo/ ESC Project

Discussion in 'Embedded Systems and Microcontrollers' started by Maketronic, Sep 27, 2013.

  1. Maketronic

    Thread Starter Active Member

    Mar 21, 2009
    49
    0
    Hi Folks

    I am currently working on an Electronic Speed Controller (ESC) project for a friend for a DC outboard motor for his kayak.
    I have sucessfully built one in the past using a 12f683 driving mosfets and once I got over some glitches it worked well.
    He has come back to me now wanting reverse also.
    So I had a bright idea.
    Why not use a powerful off the shelf brushed radio control ESC for all the power electronics and just drive it with my 12f683.

    The circuit uses An0 as an input from a pot as the source for the speed, GP2 as an output for the PWM signal to the ESC and GP4 and GP5 to show direction on leds.

    The code was based on some which I found on a forum somewhere and modified a little to suit my application.

    Original author of the code may even be on here and recognise it
    Code ( (Unknown Language)):
    1.  
    2. program ServoControl
    3.   Symbol SPIN    = gpio.2            ' Servo Output Pin
    4.   Dim   CCPR     as Word Absolute $13 ' CCPR1L:CCPR1H pair
    5.         ServoPos as Word
    6.         calcfloat as float
    7.         Temp     as integer
    8.         anval as float
    9.   sub procedure Interrupt()
    10.  
    11.         If SPIN = 1 Then
    12.            SPIN = 0                   ' turn servo off
    13.            CCPR = 20000 - CCPR        ' off time to finish 20ms
    14.         Else
    15.            SPIN = 1                   ' turn servo on
    16.            CCPR = ServoPos            ' on time in microseconds
    17.         End If
    18.         PIR1.CCP1IF = 0               ' clear int flag
    19.   End sub
    20.   sub procedure INIT()
    21.         CMCON0    = 7                  ' turn off comparators
    22.        ansel = 1
    23.         trisio = 1
    24.         ServoPos = 2000
    25.  
    26.   end sub
    27.   Sub procedure CCP_SETUP()
    28.         intcon=0
    29.         pie1 = 0
    30.         INTCON.peie = 1
    31.         intcon.GIE = 1                 ' GIE, PEIE enabled
    32.         T1CON   = 0                   ' no prescale; off;  1us ticks
    33.         TMR1H   = 0                   ' clear timer1
    34.         TMR1L   = 0
    35.         CCP1CON = 11                  ' reset timer1 on compare-match
    36.         CCPR    = 2000             ' match int value; 0 deg position
    37.         PIR1.CCP1IF  = 0              ' clear int flag
    38.         PIE1.CCP1IE  = 1              ' enable int
    39.         T1CON.TMR1ON = 1              ' timer1 run
    40.    end sub
    41.    main:
    42.          osccon = %01100001
    43.  
    44.          INIT()
    45.          CCP_SETUP()
    46.          adc_init()
    47.  
    48.          for Temp = 0 to 1000
    49.          ServoPos = ServoPos -1
    50.          delay_ms(2)
    51.          next Temp
    52.          While 1=1
    53.             anval = adc_get_sample(0)
    54.             calcfloat=((anval/1023)*1000)+1000
    55.             ServoPos = calcfloat
    56.             delay_ms(1)
    57.             if ServoPos > 1550 then
    58.             gpio.4= 1
    59.             gpio.5 = 0
    60.             end if
    61.             if ServoPos < 1450 then
    62.             gpio.4= 0
    63.             gpio.5= 1
    64.             end if
    65.             if ServoPos < 1450 then
    66.             if ServoPos < 1550 then
    67.             gpio.4 = 1
    68.             gpio.5 = 1
    69.             end if
    70.             end if
    71.  
    72.          Wend
    73.    end.
    74.  
    A few queries.
    Firstly ANSEL, until I set it to 1 the output on my PWM pin was just oscilating with what appeared to be random 8mhz noise, can anybody explain why this was.
    Secondly GP4 regardless of whether I set it on or off in code, appears to be on all the time, is there a property of GP4 on a 12f683 that I have missed?
    I have tried to work through data sheets and I arent the best at this but I am very curious for my own learning as to what might have been going on here.
    It has taken some hours to get the code to here due to one reason or another
    (couldnt get the analog to play ball, was either all on or all off then I realised it was because I had anval set as an integer and is was rounding it during my scaling maths)

    Before anybody asks the for loop early in the code is a calibration routine to let the esc know the pwm values.

    The code is in MikroBasic by the way.

    Hopefully somebody might be able to enlighten me.

    Kind Regards

    Bruce
     
  2. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    It might seem defeatist, but you would save on electronic hardware and avoid the possibility of catastrophic failure if you perform the reversing function with a plain old double-pole double-throw switch. If you were building a servo system, then you'd need electronic reversing, but if it's just a motor under manual control, maybe it's not worth the hassle.
     
  3. Maketronic

    Thread Starter Active Member

    Mar 21, 2009
    49
    0
    Hi

    I certainly did consider that, however decided against it for a couple of reasons.
    1stly, my friend has a control box for this located remote from the power electronics so a simple dpdt isnt an option. Of course a dpdt relay might be.
    2ndly, the esc gives a bit more idiot proofing. Have a motor going full speed foward and accidentally knock the dpdt to reverse and all hell could break loose, electronically and mechanically.

    As a project I could almost use it as is, just trying to find out why gp4 is behaving oddly and ansel is affecting interrupts for my own learning.

    Regards

    Bruce
     
Loading...