Simple PIC12F509 code does not work - Page 1
Forum section: electronics / general

author: ocset
author_rank: Super Contributor
timestamp: September 08, 2017, 11:08:31 am
content: 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:/** 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 variablesuint8_t count;void main(void) {GP0 = 0;GP1 = 0;GP2 = 0;GP4 = 0;GP5 = 0;OPTION = 0xD7;TRISGPIO = 0x20;GP4 = 0;//5 second delayfor (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 OFFwhile(1){while(GP5 == 0) ; //See out the rest of low STATUSwhile(GP5 == 1) ; //See out the high STATUSwhile(GP5 == 0) ; //See out the low STATUSwhile(GP5 == 1) ; //See out the high STATUS//STATUS LOW SECOND__delay_us(500);GP4 = 1; //FET ONwhile(GP5 == 0) ; //See out the rest of low STATUSwhile(GP5 == 1) ; //See out the high STATUSwhile(GP5 == 0) ; //See out the low STATUSwhile(GP5 == 1) ; //See out the high STATUS__delay_us(500);GP4 = 0;}while(1){;}return;}

author: Bruce Abbott
author_rank: Frequent Contributor
timestamp: September 08, 2017, 02:34:22 pm
content: 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?

author: woody
author_rank: Frequent Contributor
timestamp: September 08, 2017, 04:02:25 pm
content: 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

author: Bruce Abbott
author_rank: Frequent Contributor
timestamp: September 08, 2017, 08:13:59 pm
content: I may have missed the obvious...Quote from: treez on September 08, 2017, 11:08:31 amPin 5 is GP2, not GP5. Either input the square wave on pin 2 (GP5) or change the code to use GP2 as the input.

author: ocset
author_rank: Super Contributor
timestamp: September 08, 2017, 10:02:17 pm
content: 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.

author: rstofer
author_rank: Super Contributor
timestamp: September 09, 2017, 01:07:29 am
content: Quote from: treez on September 08, 2017, 10:02:17 pmThat NEVER happens, I don't care how simple the code may be!