I am trying to create a pwm that does the following : At first the arm od the servo motor must be in neutral position than, every 0.5 seconds the position of the arm of the servo will move by 30 degrees. Code is written in C and for the board freedom kl25z.
Here is the code that i tried. the arm of the servo does not responds at all.
#include <MKL25Z4.H>
#include <stdio.h>
#include <math.h>
#include “utils.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 = 299999; /* Set up modulo register for 50 Hz */
TPM0->CONTROLS[3].CnV = 15000; /* Set up channel value for 5% dutycycle for 0 degree */
TPM0->SC = 0x0C; /* enable TPM0 with prescaler /16 */
Delay(5000000);
TPM0->CONTROLS[3].CnV = 17499; /* Set up channel value for for duty cycle 5.833 30 degree */
Delay(5000000);
TPM0->CONTROLS[3].CnV = 19999; /* Set up channel value for duty cycle 6.66333 value 19999 60 degree servo*/
TPM0->SC = 0x0C;
Delay(5000000);
TPM0->CONTROLS[3].CnV = 22500;
TPM0->SC = 0x0C;
Delay(5000000);
}}
Shash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.