Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
The fastest PWM on a 16F @ 20MHz is 5MHz. What would you need a 20MHz PWM for anyway?
void pwm(char pins, unsigned int duty, unsigned int freq, unsigned int timer) // PWM Function
{
unsigned int times, on, off;
TRISA = ~(pins & 0xFF);
on = (65536 - duty);
off = (65536 - freq) - on;
INTCONbits.TMR0IF = 0; // Clear OVF Flag
LATA = pins; // Initialize LATA
T0CON|=0x80;
for(times=timer;times>0;times--) // Duration of playing
{
if(freq<=duty)
{
LATA = pins;
}
else
{
TMR0H = (on >> 8) & 0xFF; //Load TMR0H byte first
TMR0L = on & 0xFF; // Load TMR0L byte next
while(!INTCONbits.TMR0IF); // Wait for timer
INTCONbits.TMR0IF = 0; // Clear OVF Flag
LATA = ~LATA; // Invert output
TMR0H = (off >> 8) & 0xFF;
TMR0L = off & 0xFF;
while(!INTCONbits.TMR0IF);
LATA = ~LATA;
}
}
}