Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Why does simple PIC12F509 code not work?

Status
Not open for further replies.

Flyback

Well-Known Member
Hello,

Please do you know why my C code (XC8) for PIC12F509 doesnt work?

All its supposed to do is read a square wave voltage input on pin 5, and then change GP4 from high to low every second low-going of GP5.

However, GP4 is always just staying low. Do you kow why?

It builds successfully but doesn’t work

Code:
/*
 * File:   jitter.c
 * Author:
 *
 * Created on 8 sept 2017, 23:55
 */

//this code jitters

//This uses PIC12F509
//MPLAB X IDE
//XC8 C compiler (free)

#define  _XTAL_FREQ 4000000

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


//
#pragma config OSC = IntRC      // Oscillator Selection bits (external RC oscillator)
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled)
#pragma config CP = OFF         // Code Protection bit (Code protection off)
#pragma config MCLRE = OFF       // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is MCLR)

//I have not set the MCLR pin as an input because it will be noise susceptible.
//Therefore i set MCLR up as reset, but i will never use it as reset..but will
//simply tie the pin top Vdd on the PCB.

  
//Declare functions which set up the microcontroller
//void    disable_interrupts(void);   //How do this?
//void    disable_pullups(void);      //How do this?

//    TRIS = 0x18;
  
//Declare variables
    uint8_t   count;

void main(void) {
    GP0 = 0;
    GP1 = 0;
    GP2 = 0;
    GP4 = 0;
    GP5 = 0;
    OPTION = 0xD7;
    TRISGPIO = 0x20;
  
    GP4 = 0;
  
    //5 second delay
    for (count=1;count<=50;count++)   {
    __delay_ms(100);
    }


    while(GP5 == 0) ;
    while(GP5 == 1) ;
    //STATUS LOW FIRST
    //When it gets to this point, the STATUS input has just gone low

    __delay_us(500);
    GP4 = 0;              //FET OFF
  
    while(1){
    while(GP5 == 0) ;   //See out the rest of low STATUS
    while(GP5 == 1) ;   //See out the high STATUS
    while(GP5 == 0) ;   //See out the low STATUS
    while(GP5 == 1) ;   //See out the high STATUS 
    //STATUS LOW SECOND
    __delay_us(500);
    GP4 = 1;              //FET ON
    while(GP5 == 0) ;   //See out the rest of low STATUS
    while(GP5 == 1) ;   //See out the high STATUS
    while(GP5 == 0) ;   //See out the low STATUS
    while(GP5 == 1) ;   //See out the high STATUS
    __delay_us(500);
    GP4 = 0;
    }
  
  

    while(1){;}

    return;
}
 
Last edited:
What have you got connected to GP4? If it is being loaded high with something like an LED to 5V then that would cause this to happen. Also, your delay 500uS could miss transitions. Would it be better to have delay 125uS after each while(GP5) to eliminate bounce - although bounce can last longer than 125uS.

Edit, just noticed your not toggling GP4 so it's not a RMW problem. Are you sure GP5 is toggling correctly? Can you try a simple flash LED on GP4 program?

Mike.
 
Your missing a lot
Code:
/*
 * File:   main.c
 * Author: burt
 *
 * Created on August 20, 2017, 7:17 PM
 */
// CONFIG
#pragma config OSC = IntRC_RB4EN// Oscillator Selection bits (Internal RC oscillator/RB4 function on RB4/OSC2/CLKOUT pin)
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled)
#pragma config CP = OFF         // Code Protection bit (Code protection off)
#pragma config MCLRE = ON       // RB3/MCLR Pin Function Select bit (RB3/MCLR pin function is MCLR)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#define _XTAL_FREQ 4000000
#include <xc.h>
#include <stdint.h>
  
       uint8_t   count;


void main(void) {
    OSCCAL= 0b01111110;
    GP0 = 0;
    GP1 = 0;
    GP2 = 0;
    GP4 = 0;
    GP5 = 0;
    OPTION = 0xD7;
    TRISGPIO = 0x20;
  
    GP4 = 0;
  
    //5 second delay
    for (count=1;count<=50;count++)   {
    __delay_ms(100);
    }


    while(GP5 == 0) ;
    while(GP5 == 1) ;
    //STATUS LOW FIRST
    //When it gets to this point, the STATUS input has just gone low

    __delay_us(500);
    GP4 = 0;              //FET OFF
  
    while(1){
    while(GP5 == 0) ;   //See out the rest of low STATUS
    while(GP5 == 1) ;   //See out the high STATUS
    while(GP5 == 0) ;   //See out the low STATUS
    while(GP5 == 1) ;   //See out the high STATUS 
    //STATUS LOW SECOND
    __delay_us(500);
    GP4 = 1;              //FET ON
    while(GP5 == 0) ;   //See out the rest of low STATUS
    while(GP5 == 1) ;   //See out the high STATUS
    while(GP5 == 0) ;   //See out the low STATUS
    while(GP5 == 1) ;   //See out the high STATUS
    __delay_us(500);
    GP4 = 0;
    }
  
  

    while(1){;}

    return;
}
 
Last edited:
These go after configure words
Code:
#define  _XTAL_FREQ 4000000
#pragma config OSC = IntRC // don't let you use gp4
#pragma config OSC = IntRC_RB4EN// Oscillator Selection bits (Internal RC oscillator/R4 function on RB4/OSC2/CLKOUT pin)
#include <xc.h>
#include <stdint.h>
 
Last edited:
You have to set it to this
#pragma config OSC = IntRC_RB4EN// Oscillator Selection bits (Internal RC oscillator/R4 function on RB4/OSC2/CLKOUT pin)
cant use gp4 if not
 
Your right I been using xc8 and mplab x it shows up as option but the data sheet only shows that for the 16f505 not the 12f509
 
Also, what i meant in the top post is that GP4 is supposed to go low/high/low.....etc, ie change every second low going of the GP5 input signal......
 
You are missing one thing....

However, GP4 is always just staying low. Do you kow why?

If GPIO4 is an input, then that statement makes me think your clock input is at fault!!
 
Thanks, i must admit i cant find mention of "IntRC_RB4EN" in the PIC12F509 datasheet...
PIC12F509 datasheet
https://ww1.microchip.com/downloads/en/DeviceDoc/41236E.pdf

..Please could you possibly advise where you found that config statement?

I think i sucessfully used PIC12F509's GP4 as an input a few months back and i definetely didnt use "IntRC_RB4EN"
It is a bit confusing. As I understand it, your only input is GP5 (pin2), and GP4 (pin3) is the output that is connected to the 2N7000. How does that gel with the quote above?

How is your mosfet connected? Maybe it is time for a schematic.

John

OOPS, cross posted. Still valid is how the mosfet is connected.
 
That I understood after post #12. Thanks for confirming it.

Are you monitoring the output with a scope or just the action of the mosfet? Is the mosfet source connected to ground?
 
Thanks yes source is to ground...yes i looked at GP4 with a scope whilst i took GP5 high and low continuously....GP4 just stayed low....unfortunately.
 
I just simmed it on proteus... Your code is fine...

I also had a problem with a fet... I used a pic12f1840 and RA4 to drive a speaker via a 2n7000... Also didn't work in real life... It was fine on the simulator...

Do fet's lock up the pin?
 
I used a 2N7000 years ago on a 12F509 to run a relay. No problem. But, I had a pull down resistor on its gate. Since his output is staying low, maybe the input needs a pull down, if his source is high impedance. In other words, the input pin may not be toggling as planned.

Have you scoped the input at the pin? Or, substitute just a resistor for the mosfet?
 
hi,
I believe John could be correct, the Vgs and Vlow are very close.
E
 

Attachments

  • A02.gif
    A02.gif
    19.1 KB · Views: 222
  • A03.gif
    A03.gif
    8.4 KB · Views: 197
Just had a second look at your code, it falls straight through and so your output will only go low once for a period of half a millisecond. Is that what you intended?

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top