C program to generate 5.2 kHz PWM signal on channels 0 and 1
C program pwm.c
/*
* Program to generate 5.2 kHz pulse width modulation
* on Port P 0 and 1
*/
#include "hc12.h"
main()
{
/* Choose 8-bit mode */
PWCLK = PWCLK & ~0xC0;
/* Choose left-aligned */
PWCTL = PWCTL & ~0x08;
/* Choose high polarity */
PWPOL = PWPOL | 0x0f;
/* Select clock mode 1 for Channels 1 and 0 */
PWPOL = PWPOL | 0x30;
/* Select N = 0 and M = 2 for Channels 1 and 0 */
PWCLK = PWCLK & ~0x38; /* N = 0 */
PWSCAL0 = 0x02; /* M = 2 */
/* Select period of 256 for Channels 1 and 0 */
PWPER1 = 255; /* Period 1 = PWPER1 + 1 */
PWPER0 = 255; /* Period 0 = PWPER0 + 1 */
/* Enable PWM on Channels 1 and 0 */
PWEN = PWEN | 0x03;
PWDTY1 = 127; /* 50% duty cycle on Channel 1 */
PWDTY0 = 63; /* 25% duty cycle on Channel 0 */
while (1)
{ /* Code to adjust duty cycle to meet requirements */ }
}