Author Topic: Simple PIC12F509 code does not work  (Read 1761 times)

0 Members and 1 Guest are viewing this topic.

Offline ocsetTopic starter

  • Super Contributor
  • ***
  • Posts: 1516
  • Country: 00
Simple PIC12F509 code does not work
« on: September 08, 2017, 11:08:31 am »
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 GP5, 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 sucessfully but doesnt work.

Code: [Select]
/*
 * 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 Edit: September 08, 2017, 10:01:14 pm by treez »
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Simple PIC12F509 code does not work
« Reply #1 on: September 08, 2017, 02:34:22 pm »
You mean GP4 is staying low?

Seems to work in the simulator, so I suspect a hardware problem. Can you post the schematic and/or a photo of the setup?

 
The following users thanked this post: ocset

Offline woody

  • Frequent Contributor
  • **
  • Posts: 291
  • Country: nl
Re: Simple PIC12F509 code does not work
« Reply #2 on: September 08, 2017, 04:02:25 pm »
GP5 is the input, right? So if that stays low, what is it connected to?

Assuming you have real hardware tie an LED (with a series resistor) to GP0 and use that LED as a signal to find out where your program actually comes (I am not familiar with the XC8 compiler so might have the syntax wrong):

gp0=1;
while(1);

This always helps me to find out where my program takes an unexpected left turn.

And I presume you have executed the mandatory 'blink-a-led' exercise with your hardware to make sure the controller actually runs (has power on the right pins, has its oscillator and fuses configured correctly).

Paul
 
The following users thanked this post: ocset

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Simple PIC12F509 code does not work
« Reply #3 on: September 08, 2017, 08:13:59 pm »
I may have missed the obvious...

All its supposed to do is read a square wave voltage input on pin 5,

Pin 5 is GP2, not GP5. Either input the square wave on pin 2 (GP5) or change the code to use GP2 as the input.
 
 
The following users thanked this post: ocset

Offline ocsetTopic starter

  • Super Contributor
  • ***
  • Posts: 1516
  • Country: 00
Re: Simple PIC12F509 code does not work
« Reply #4 on: September 08, 2017, 10:02:17 pm »
Sorry i do apologise, GP5 is the input and GP4 is the output. My Bad. I am kicking myself now.
Thanks Ill try the LED thing etc...the code is so simple that i was hoping to just write it once and done.
« Last Edit: September 08, 2017, 10:04:24 pm by treez »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Simple PIC12F509 code does not work
« Reply #5 on: September 09, 2017, 01:07:29 am »
the code is so simple that i was hoping to just write it once and done.

That NEVER happens, I don't care how simple the code may be!
 
The following users thanked this post: JPortici, BrianHG, ocset


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf