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

Some days you want to pull your hair out.  Datasheet read one thing – what happens in the real world is different.

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.

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.   Except it removes the external resistor part.

However – I thought this would be a quick little post I would write up – one of those things you think.. It will just take 5 minutes..  Not..

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???

Anyway – below is the code for the weak pull up working on PORTA.2.  To get the LED to light on PORTA.0 – a button is connected to PORTA.2 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 PORTA.2 is HIGH, turn the LED off – because the button is not pressed.  When the button is pressed PORTA.2 will be LOW – when it is LOW – turn on the LED.

 

#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  //8 MHz (default after Reset)

//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 A.0 and A.2
    ANSELAbits.ANSA0 = 0;
    ANSELAbits.ANSA2 = 0;
    
    TRISAbits.TRISA0 = 0; //A.0 Output
    TRISAbits.TRISA2 = 1; //A.2 Input
    
    WPUAbits.WPUA2 = 1; //Enable Weakpull up on A.2   
    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
    }

}

Something You Might Be Intrested In


 

10F32X Rapid Prototype Board

Make The PIC10F322 and other PIC10F Processors Breadboard Friendly for Quick and Easy Prototyping.

{Click to learn more}

PIC10F322- XC8 Code - WPUA Weak Pull Ups