Gerardo Ramos

How to program a Servo motor in KL25Z

Discussion created by Gerardo Ramos on Aug 10, 2017
Latest reply on Aug 17, 2017 by Erich Styger

I must program a Servomotor in freescale, I have this program but the servo can´t power on.
I need to make the servo go from 0° to 180°

Please help me!
Could you please explain me each part of the correct form to program a servo motor.


#include "MKL25Z4.h"

int main (void){
SIM->SCGC5 |= 0x1000; /* enable clock to Port D */

PORTD->PCR[2] = 0x0400; /* PTD1 used by TPM0 PIN DEL PRIMER SERVOMOTOR */
SIM->SCGC6 |= 0x01000000; /* enable clock to TPM0 */
SIM->SOPT2 |= 0x01000000; /* use MCGFLLCLK as timer counter clock */


while(1){
TPM0->SC = 0; /* disable timer */
TPM0->CONTROLS[3].CnSC = 0x20 | 0x08; /* edge-aligned, pulse high */
TPM0->MOD = 43702; /* Set up modulo register for 60 kHz */
TPM0->CONTROLS[3].CnV = 437; /* Set up channel value for 1% dutycycle */
TPM0->SC = 0x0C; /* enable TPM0 with prescaler /16 */
delayms(20);

}}
void delayms(int n)
{int i;
int j;
for(i=0; i < n; i++)
for (j=0; j < 7000; j++){} }

Outcomes