Select Page

XC8 Code for PIC10F320 – PIC10F322 – WPUA – XC8 Code – Weak Pull Ups

The PIC10F322 has internal Weak Pull Up Resistors on all the GPIO ports. Each one can be individually selected.

For those who might not know what an internal weak pull up is – it will tie a pin to positive voltage. Just like adding an external 10K resistor to positive rail. Grounding the line – will pull it to a 0 state. When the pin is released or left to float it will go back to the 1 state.

The internal weak pull up work the same – except it removes the external resistor part and keeps everything inside.

Steps to get the WPUA setup

WPUAbits.WPUA2 = 1; //Enable Weakpull up on A2

Second you must enable weakpulls ups in the option reg by setting the WPUEN bit to 0.

OPTION_REGbits.nWPUEN = 0; //Requires being enabled in option reg as well

About the Code


Below is the code for the weak pull up working on PORT.A2. To get the LED to light on PORT.A0 – a button is connected to PORT.A2 which is being held HIGH by the internal pull up. Grounding the pin will bring the PIN to a logic 0 level – causing the LED to light. When the button is released – the PIN goes back to logic level 1 and the LED goes off.

Something to note about the logic in the code – since we are using weak-pullups – this will bring a PIN to logic level 1 {HIGH} (+5) – this will be the default state. What the logic does is; if PORT.A2 is HIGH, turn the LED off – because the button is not pressed. When the button is pressed PORT.A2 will be LOW – when it is LOW – turn on the LED.

***Having issues getting the weakpull up to work on other pins. Your ICD could be causing the issue of the WPU on ports A0 and A1. The ICD will pull the ports low. For most testing I have been doing, I have been leaving the ICD connected and powering my simple setups. This was causing the WPU on A0 and A1 not to work – removing the ICD and powering the circuit from an independent power supply worked, both A0 and A1 worked as they should with the ICD removed.

The Circuit

swtich wpu

The Code

/*
 * File:  wpu_example.c
 * Author: Jamie Starling - GizoFoundry.com 
 *
 * Created on:  September 7, 2021, 7:45 PM
 * 
 * Code provided as-is.
 */


#include <xc.h>
#include <stdint.h>

//Device Configuration
#pragma config FOSC = INTOSC  // Oscillator Selection 
#pragma config BOREN = ON    // Brown-out Reset
#pragma config WDTE = OFF    // Watchdog Timer
#pragma config PWRTE = ON    // Power-up Timer
#pragma config MCLRE = OFF   // MCLR Pin Function Select bit->MCLR pin function is digital input, MCLR internally tied to VDD
#pragma config CP = OFF      // Code Protection 
#pragma config LVP = OFF     // Low-Voltage Programming 
#pragma config LPBOR = ON    // Brown-out Reset Selection bits
#pragma config BORV = LO    // Brown-out Reset Voltage Selection
#pragma config WRT = OFF    // Flash Memory Self-Write Protection

//Used to calculate the delay time - Change depending on processor Speed
#define _XTAL_FREQ 16000000  //16 MHz 

//Prototypes
void setup(void);
void check_button(void);


void main(void)
{
    setup();
    
    while(1)
    {
        check_button();
    }
}

void setup(void)
{
    OSCCONbits.IRCF = 0b111;  //Set System Clock to 16Mhz FOSC 4Mhz
    
    //Disable analog for A0 and A2
    ANSELAbits.ANSA0 = 0;
    ANSELAbits.ANSA2 = 0;
    
    TRISAbits.TRISA0 = 0; //A0 Output
    TRISAbits.TRISA2 = 1; //A2 Input
    
    WPUAbits.WPUA2 = 1; //Enable Weakpull up on A2   
    OPTION_REGbits.nWPUEN = 0; //Requires being enabled in option reg as well
    
    LATAbits.LATA0 = 0;
    
}

void check_button(void)
{
    //Keep in mind a pullup keeps the pin logic high. TO test for button press - the button when pressed with bring the pin to logic 0 or low.
    if (PORTAbits.RA2 == 0)
    {
        LATAbits.LATA0 = 1;  //Turn on Led when button is pressed
    }

    else
    {
        LATAbits.LATA0 = 0; //Turn off Led when button is released
    }

}

Have a Project or Idea!?

I am Available for Freelance Projects

My skills are always primed and ready for new opportunities to be put to work, and I am ever on the lookout to connect with individuals who share a similar mindset.

If you’re intrigued and wish to collaborate, connect, or simply indulge in a stimulating conversation, don’t hesitate! Drop me an email and let’s begin our journey. I eagerly anticipate our interaction!

jamie@jamiestarling.com


Pin It on Pinterest

Share This