Controlling a brushless with dsPic

Discussion in 'Embedded Systems and Microcontrollers' started by alexix, Mar 27, 2014.

  1. alexix

    Thread Starter New Member

    Mar 27, 2014
    5
    0
    I have a dsPIC30F4012, a brushless motor and this ESC

    After reading the web, I understood that ESC require servo pulse (1-2ms up then 18-19 ms down) so I made this code:

    Code ( (Unknown Language)):
    1. int16_t main(void){
    2. TRISB = 0x00;
    3. PORTB = 0x00;
    4.  
    5. int i=0;
    6. for(i=0;i<300;i++){
    7. PORTB=0x01;
    8. __delay32(30000);   // 1 ms?
    9. PORTB=0x02;
    10. __delay32(600000-30000);    // 19 ms?
    11. }
    12. while(1){
    13.         PORTB = 0x01;
    14.     __delay32(60000);   // 2 ms?
    15.     PORTB = 0x00;
    16.     __delay32(600000-60000); // 18 ms?
    17.     }
    18. }
    If my understandings are correct, it should start the motor at 0 speed (1ms pulse) for a few seconds, then go to full speed ( 2ms pulse)

    I'm using a 16MHz crystal, powering my ESC at 12V, 2A plug and my pic at 5V, 1A.
    The 3 wire cable from the ESC I connected like this:
    brown - vss
    red - 5v
    yellow - RB0 port

    The esc details specify that it 480Hz+ high refresh rates but I did not understood how to use this

    The motor doesn't start, it sometimes starts beeping in pairs of two

    Can anyone tell me where did I go wrong?

    sorry if I'm a hard-headed newb in advance
     
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    You need to check your delay times.

    Your C compiler should have an easier delay function.

    Does it have Delay_mS() or Delay_uS() functions?

    Otherwise, if you really want to do the job properly it is best to use one of the hardware timer modules to generate the periods. :)
     
  3. alexix

    Thread Starter New Member

    Mar 27, 2014
    5
    0
    So do you think other then delay my stuff is ok?

    I'm using XC16 compiler from MPLABX and this delay function is the only one that I found, it should take clock cycles as paramether, I took an example from the internet where 15000000 was 0.5 seconds, so I don't know if it applies correctly for me.
     
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    The task you are attempting is to generate HI pulse period of exact duration between 1000uS and 2000uS (which controls motor ESC speed), and a LO period of approx 20mS (LO period deson't matter that much).

    It would help a great deal if you have some way to view the output pulses, like a 'scope.

    Without a 'scope you will be working blind and guessing if your code is making the right periods.
    :)
     
  5. alexix

    Thread Starter New Member

    Mar 27, 2014
    5
    0
    I found out that I can use __delay_ms but I had to configure my OSC first.

    I got the bitch spinning :D thank you
     
  6. fernan82

    New Member

    Apr 19, 2014
    26
    9
    Unless that is all you're doing (in which case the dsPIC is way overkill for your project), you might want to use the PWM module on your chip.
     
Loading...