Article sections

    What is the PIC10F322 Library?

    Jumping back and forth between various PIC Microcontrollers I was looking for a way to make moving between the MCUs a little easier. Thus this has been a little project of mine. Call it an Arduino like Library for the PIC Micros.


    Where can you find the latest version?

    Examples and the Includes can be found in my Github Site –




    • MPLAB
    • XC8
    • xc.h
    • stdint.h



    PIC10F322 Header File

    10F322_deviceconfig.h – needs to be included in all the projects. It contains the processor configuration bits, standard set of defines (true/false/on/off) and the _XTAL_FREQ  setting.

    It is import to change the _XTAL_FREQ  setting to the desired speed that you will run the processor at.  The default is set to 8Mhz.


    Port and Pin Numbering

    • 0 = PIN0 = PORTA.0
    • 1 = PIN1 = PORTA.1
    • 2 = PIN2 = PORTA.2
    • 3 = PIN3 = PORTA.3



    GPIO.H and  GPIO.C

    Provides standard GPIO Pin functions.

    pinMode(uint8_t pin, uint8_t direction)

    Sets the desired pin data direction and disables analog functions of that pin.


    • pinMode(0,INPUT) – set PortA.0 as input.
    • pinMode(1,OUTPUT) – set PortA.1 as output.


    digitalWrite (uint8_t pin, uint8_t value)

    Updates the desired pin to the desired value (1/0 – On/Off) – Should initialize the PIN as an output using pinMode in a setup function before using.


    • digitalWrite(0,ON) – set PortA.0 logic 1 or HIGH.
    • digitalWrite(2,OFF) – set PORTA.2 logic 0 or LOW.


    digitalRead (uint8_t pin)

    Reads the current input value on a given pin. Returns the value of the pin as either 1 or 0. Should initialize the PIN as an input using pinMode in a setup function before using.


    • pinValue = digitalRead (0) – reads the value on PORTA.0
    • pinValue = digitalRead (3) – reads the value on PORTA.3


    digitalToggle (uint8_t pin)

    Toggles a give pin, if the pin is off, digitalToggle will turn it on. If the pin was on, it will turn it off.  Should initialize the PIN as an output using pinMode in a setup function before using.


    • digitalToggle(1) – Toggles PortA.1 – State before 1 – State After 0
    • digitalToggle(2) – Toggles PortA.2 – State before 0 – State After 1



    OSC.H and OSC.C

    Sets the processor frequency and provides an option to enable and disable the reference clock output.


    cpuFreqSelect(uint8_t speed)

    Sets the processor to the desired frequency.  Remember the startup speed of the PIC10F322 is 8Mhz until you change it in software.

    Valid sections for speed:

    • MHZ16 = 16Mhz
    • MHZ8 = 8Mhz  **Default at powerup or reset
    • MHZ4 = 4Mhz
    • MHZ2 = 2Mhz
    • MHZ1 = 1Mhz
    • KHZ500 = 500Khz
    • KHZ250 = 250Khz
    • KHZ31 = 31Khz



    • cpuFreqSelect(MHZ16) – Set FOSC to 16Mhz
    • cpuFreqSelect(MHZ4) – Set FOSC Freq to 4Mhz

    enableClockReferenceOut ()

    Enable the clock reference output on pin PortA.2. The reference output is your FOSC/4.  So if you are running at 16Mhz, output would be 4Mhz.



    Disables the clock reference output



    WPA.H and WPA.C

    Provides interface to weak-pullup resistors. Disabled by default.

    Supposedly, unless I am reading it wrong the PIC10F322 has internal Weak Pull Up Resistors on all the GPIO ports. Each one should be individually selected.

    After my first attempt of getting the weak pull ups enabled on PORTA.1, then PORTA.0.. Ended with failure.

    Short story and a lot more than 5 minutes. It appears that the pull ups only work on PORTA.2 and PORTA.3. Maybe I have a bad CPU – replaced it with a new one.. Same results. Maybe a bad batch???

    enableWPUA (int8_t pin)

    Enables the weak-pullup on the selected pin.


    disableWPUA (int8_t pin)

    Disables the weak-pullup on the selected pin.



    • enableWPUA(2) – Enables the WPU on PortA.2
    • disableWPUA (3) – Disables the WPU on PortA.3



    PWM.H and PWM.C

    Provides interface to the PWM functions of the PIC10F322.  The 10F322 has two PWM output pins PORTA.0 and PORTA.1.  This interface the dutycycle is set by percent.  50 would be %50 cycle. etc.

    Please note to do the duty calculations – this function requires a lot of program space.

    Make sure that the _XTAL_FREQ  is set to the correct operation frequency.

    • 8MHz = PWM Frequency 19.61 kHz 8bits Duty
    • 16Mhz = PWM Frequency 15.7 kHz 10bits Duty


    pwmOut(uint8_t pin, uint16_t dutyCycle)

    Sets the pin as PWM out and sets the starting duty cycle.


    • pwmOut(0,75) – will set PORTA.0 as PWM out and to 75% Duty cycle
    • pwmOut(0,30) – will set PORTA.0 as PWM out and to 30% Duty cycle
    • pwmOut(1,90) – will set PORTA.1 as PWM out and to 90% Duty cycle


    setPWMDutyCycle (uint8_t pin, uint8_t duty)

    Updates the dutycycle of one of the PWM pins.  pwmOut will need to have been called first – after that you can use – setPWMDutyCycle to update the dutycycle.


    • setPWMDutyCycle(0,75) – will set PORTA.0 to 75% Duty cycle
    • setPWMDutyCycle(1,80) – will set PORTA.1 to 80% Duty cycle



    in PIC10F322

    Related Articles