Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! 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.
T

treez

Guest
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, GP5 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;
}
 

What is the initial state at GP5 ? 1 or 0 ?
 

However, GP5 is always just staying low.

Apart from the unusual method how you implemented a button reading, haven't you defined GPIO5 as input?
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
However, GP5 is always just staying low. Do you kow why?
Because it's the input not the output! I assume you mean GP4.
You are feeding your square wave to pin 5 which is GP2 not GP5.

Brian.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
My apologies, My bad big time here, i should have said i am feeding the square wave to GP5, and the GP4 is the one that is an output.

- - - Updated - - -

Apart from the unusual method how you implemented a button reading, haven't you defined GPIO5 as input?
Yes i made GP5 an input
 

Thanks, i also meant that GP4 is supposed to go high/low/high/low ..ie change every second low-going of GP5

- - - Updated - - -

i believe that from a logic viewpoint, the code that i have written is fine, but there must be some syntax issue.
 

Try this: Slightly reformatted for clarity and some changes to bit names, it compiles and the .asm looks reasonable but I haven't tested it.
Code:
/* 
 * File:   jitter.c
 * Author: 
 *
 * Created on 8 sept 2017, 23:55
 * Modified by 'Betwixt' 9 Sept 2017, 09:46
 */

//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       // GPIObits.GP3/MCLR Pin Function Select bit (GPIObits.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 variables
    uint8_t   count;

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


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

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

Brian.
 

..ie change every second low-going of GP5

- - - Updated - - -

i believe that from a logic viewpoint, the code that i have written is fine

From a logic viewpoint it could make false detections in the case of any other inputs diverging from the specified timing of 1s; the code as is actually accepts any flickering from input.
 

Isn't language wonderful - so many ways to interpret the same thing!

What Treez code does is check for 'alternating' input on GP5. Low-high-low-high
In that context 'second' is after first and before third rather than being a time period :-D

Brian.
 

Thanks yes, it is a mains jitter circuit, to make alternate 20ms current input vary in amplitude, -the cct gives us a zero crossing signal every 10ms which stays low for 1ms.......we have to change the circuit gain after every two zero crossings, to make alternate 20ms sines different in amplitude, in order to provide "jitter."
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top