XC8 Code for PIC10F322 – PWM Functions

Struggling with the PWM functions of the PIC10F322.  Here is something I put together to get the PWM going quickly and easily. 

The pwm.c and pwm.h headers can be found in my GitHub here – https://github.com/JamieStarling/MCU_FUN/tree/master/Microchip/PIC10F322/ 

along with pwm_example.c

Super simple to use – however it only works with the device running at 8Mhz or 16Mhz and you want to make sure you set the _XTAL_FREQ correctly in your device_config.h file.  At 8Mhz it will give you an 8bit resolution, 16Mhz, 10bit resolution. 

Set it up by setting the desired PWM pin to output and then.

pwmOut(0,50);  –  Where 0 is the pwm pin – in this case PWM0 is on pin 0 – 50 is the duty cycle.  

To setup PWM1 with a 75% duty.. it would be  pwmOut(1,75);

To change the PWM duty cycle once PWM is going – is done by – setPWMDutyCycle(pin,duty);  so, setPWMDutyCycle(0,25) with set pin 0 to 25% duty. 

A quick note about the pins – on the PIC10f322 there are two hardware PWM devices, connected to pin 0 and pin 1. you also need to include the gpio library.

 

example code

/*
 * File:   main.c
 * Author: Jamie
 *
 * Created on August 4, 2020, 5:26 PM
 */


#include "includes/10F322_deviceconfig.h"
#include "includes/gpio.h"
#include "includes/pwm.h"

void main(void) {
    
    pinMode(0,OUTPUT);
    pwmOut(0,50);
    
    while(1);
    
    
}