1. 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.
    Dismiss Notice

Why does simple PIC12F509 code not work?

Discussion in 'Microcontrollers' started by Flyback, Sep 8, 2017.

  1. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    2,010
    Likes:
    34
    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 (text):

    /*
     * 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: Sep 8, 2017
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    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.
     
    • Like Like x 1
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    I hope you have a very clean square wave on GPIO5... As Mike said that pic running at 4Mhz will bounce straight through that code..
     
    • Like Like x 1
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn

    Your missing a lot
    Code (text):

    /*
     * 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: Sep 8, 2017
    • Like Like x 1
  6. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    These go after configure words
    Code (text):
    #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: Sep 8, 2017
    • Like Like x 1
  7. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    2,010
    Likes:
    34
    Thanks, its a little 2n7000 fet
     
  8. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    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
     
    • Like Like x 1
  9. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    2,010
    Likes:
    34
    Thanks, i must admit i cant find mention of "IntRC_RB4EN" in the PIC12F509 datasheet...
    PIC12F509 datasheet
    http://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"
     
  10. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    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
     
    • Like Like x 1
  11. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    2,010
    Likes:
    34
    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......
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    You are missing one thing....

    If GPIO4 is an input, then that statement makes me think your clock input is at fault!!
     
    • Like Like x 1
  13. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    2,010
    Likes:
    34
    Thanks, GP4 is the output, GP5 is the input
     
  14. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,071
    Likes:
    524
    Location:
    Cleveland, OH, USA
    ONLINE
    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.
     
    • Like Like x 1
  15. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    2,010
    Likes:
    34
    Thanks, GP5 is input, GP4 is output....output GP4 is into gate of fet.
     
  16. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,071
    Likes:
    524
    Location:
    Cleveland, OH, USA
    ONLINE
    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?
     
    • Like Like x 1
  17. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    2,010
    Likes:
    34
    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.
     
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    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?
     
    • Like Like x 1
  19. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,071
    Likes:
    524
    Location:
    Cleveland, OH, USA
    ONLINE
    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?
     
    • Like Like x 1
  20. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,240
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    I believe John could be correct, the Vgs and Vlow are very close.
    E
     

    Attached Files:

    • A02.gif
      A02.gif
      File size:
      19.1 KB
      Views:
      17
    • A03.gif
      A03.gif
      File size:
      8.4 KB
      Views:
      17
    • Like Like x 1
  21. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    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.
     
    • Like Like x 1

Share This Page