5 timer example, Ktam3874/pitx software guide – Kontron KTAM3874-pITX User Manual
Page 59

KTD-S0057-I
Page 55 Linux® Programming Examples (DebianTM)
KTAM3874/pITX Software Guide
close
(fd);
printf ("UART transmission successfully finished.\n\n");
return
0;
}
10.5 Timer Example
The timer module offers a variety of possibilities, for example realization of PWM outputs. The demon-
stration program only shows an easy (simple) implementation to start and stop the PWM interface and
cannot be called twice without a reboot process. The frequency range is well suited for motor control
applications (5 to 30 kHz).
The picture demonstrates the use of several different duty cycle values.
There is a problem with the maximum value of 255. The equation for
val
in routine
start_pwm
gives the
right result of 0xFFFFFFFF but the PWM output level approaches zero. You can avoid this issue with fol-
lowing additional lines
val = (FREQ_VAL * duty_cycle) / DUTY_MAX;
if (duty_cycle == 255)
write_timer_reg
(TMAR,
0xFFFFFFFE);
else
write_timer_reg (TMAR, RELOAD_VAL + val);
or alternative with
val = ((FREQ_VAL * duty_cycle) / DUTY_MAX) - 1;
write_timer_reg (TMAR, RELOAD_VAL + val);
but one fact remains: both limits (0 and 255) are generating short spikes. For a zero value it is possible to
stop the timer. The program execution needs root rights (compiled on Ubuntu
TM
distribution).
/* Timer test program
* Copyright (c) 2013 Kontron Technology A/S
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*/