Published © GPL3+

FRDMK82F Servo and Brushless Motor Control

Servos and brushless motors are the heart of any RC project, and now you can use them on the FRDMK82F.

IntermediateProtip1 hour921
FRDMK82F Servo and Brushless Motor Control

Things used in this project

Hardware components

Kinetis Freedom Board with FlexIO
NXP Kinetis Freedom Board with FlexIO
×1
Servos (Tower Pro MG996R)
×1

Software apps and online services

NXP Kinetis Design Studio
Or other supported IDE

Story

Read more

Schematics

Servo pinout

Connect to the board like so
File missing, please reupload.

Code

Servo.c

C/C++
Sample code to run a servo (or brushless motor). This is the basis for my work-in-progress servo library. As soon as the library has full functionality and I have checked it doesn't interfere with other functions I will add the improved code as another option. You have to put in your own code for the value writing. I was going to put in my own, but because there are so many GPIO blocks and ways to write to one I decide to leave it pseudo code.
Note: If you continue to write a value of 0 degrees to the servo it will not move! This is just to demonstrate the method.
#include "fsl_debug_console.h" //generally you should always have these two
#include "board.h"

#define CLK_SPEED XYZ//Put your board's clock speed here in Hz, FRDM-K82f = 150000000

//Prototypes:
void delay(int);

//Variables:
int highTime; //in us
int lowTime; //in us

//Code:
void delay(int time)
{
  uint32_t delayNum = CLK_SPEED / 100000 * time //turn into 1 us then multiply
  volatile uint32_t cnt = 0U;
  for(cnt = 0U; cnt < delayNum; ++cnt){
    _asm("NOP");
  }
}

int main(void)
{
  int testPulseWidth = 1000 //in us, = 1 ms
  highTime = testPulseWidth;
  lowTime = 20000 - highTime; //make sure it will occur every 20ms
  while(1)
  {
    setpin(HIGH) //not real code! there is no ; so the compiler will find this immediately
    delay(highTime);
    setpin(LOW)
    delay(lowTime); //wait until a full 20 ms has elapsed
  }
}

Credits

Comments