• AVR Freaks

Hot!PIC16f676 Problem when power is reset!

Author
buildlab
Starting Member
  • Total Posts : 32
  • Reward points : 0
  • Joined: 2019/06/26 11:50:03
  • Location: 0
  • Status: offline
2019/07/18 21:41:39 (permalink)
0

PIC16f676 Problem when power is reset!

Hi, i am a beginner in PIC coding.I have a circuitry with PIC16f676,the code is below as mentioned. 

// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (INTOSC oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RA3/MCLR pin function select (RA3/MCLR pin function is MCLR)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)

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

#include <xc.h>
#include<pic.h>
#define _XTAL_FREQ 8000000
int lt_low,lt_high,ht_low,ht_high,dryrun;
int status = 0,controls = 0,motorstatus = 0;    // conditions

int readAdc(int);
void main(void) {
    ANSEL = 0X00;
    TRISA = 0X00;
    TRISC = 0X00;
    ANS6 = 1;    // ADC input pins connected to vcc through a switch and a 390k resistor to pull it down.
    ANS5 = 1;
    ANS4 = 1;
    ANS2 = 1;
    ANS7 = 1;
    TRISC0 = 1;
    TRISA2 = 1;
    TRISC2 = 1;
    TRISC1 = 1;
    TRISC3 = 1;
    
    PORTA = 0X00;
    PORTC = 0X00;
    ADCON1 = 0b100;
    
    while(1)
       {
        lt_low = readAdc(2);
        lt_high = readAdc(4);
        ht_low = readAdc(5);
        ht_high = readAdc(6);
        dryrun = readAdc(7);
        if((lt_low > 500)&&(lt_high > 500)&&(status == 0))
    {
            status = 1;
        }
        if((lt_low < 500)&&(status == 1))
    
    {
            status = 0;
            controls = 0;
        }
        if((ht_low < 500)&&(ht_high < 500)&&(status == 1)&&(controls == 0))
    {
            __delay_ms(15000);
            RC4 = 1;
        controls = 1;
        }
 if ((ht_low < 500)&&(ht_high < 500)&&(status == 0))
        {
            RC4 = 0;
        }
 if ((ht_high > 500) &&(status == 1))
 {
    RC4 = 0;
    controls = 0;
 }
        if ((dryrun > 500)&&(motorstatus == 0))           // DRYRUN independent from all other conditions.
        {
            RC5 = 1;
            motorstatus = 1;
        }
        if ((dryrun < 500)&&(motorstatus == 1))
        {
            RC5 = 0;
            motorstatus = 0;
        }
       
    }
}
int readAdc(int channel){
    int vol;
        ADCON0 = 0x81 | (channel << 2);
       
        __delay_us(20);
        GO = 1;
        while(GO == 1);
        vol = (ADRESH << 8) + ADRESL;
        __delay_us(5);
        return vol;
}



When the pic is in a condition where the LED connected to RC4 is GLOWING, And the power is reset. The led will not be GLOWING anymore(ie the RC4 pin is not on) even though the switches connected to the analog inputs are in the same condition before the power was gone. To my wonder, if i toggle the switches connected to the analog inputs it will work. 
i cant understand whether it is a coding problem or a hardware problem. i revised the code, the logic seems alright.
                                 Hardware side i have pulled up MCLR pin using a 10k resistor only. Crystal oscillator circuitry then the analog inputs and the led.does that seems to be the problem for this ?.
Please help.
                        
post edited by buildlab - 2019/07/22 23:27:33
#1

14 Replies Related Threads

    buildlab
    Starting Member
    • Total Posts : 32
    • Reward points : 0
    • Joined: 2019/06/26 11:50:03
    • Location: 0
    • Status: offline
    Re: PIC16f676 Problem when power is reset! 2019/07/19 00:17:32 (permalink)
    0
    Even when there is a condition where to check status of a RC3/ANS7 analog pin if it is grater than 500 a pin RC5 should go high.
    so here i am using a ON/OFF switch and the condition is ON so the led on RC5 is ON. when the power is reset on the ON condition, the LED is off it needs a toggle ON/OFF to turn ON again.
    #2
    pcbbc
    Super Member
    • Total Posts : 1381
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: PIC16f676 Problem when power is reset! 2019/07/19 01:22:17 (permalink)
    +1 (1)
    What state are the switches in?
    Which, and what states, do you need to toggle to get the LED to light again?
    Why are you using switches when they are analogue inputs? Why not just use digital IO?
    Where is your circuit diagram for external switches?
    You are aware that RC4 will only turn on after 15 seconds due to your delay. This apples at power on as the chip does not automatically “remember” its state at power off.

    Why not single step through in the debugger and find where your problem is?
    Sorry, I think PIC16F676 is too old to support debugger without a header. And assume you don’t have one (read weren’t provided with one).
    Why not switch to a newer, cheaper part that does?
    Or use the simulator.

    Trying to get code to work, especially when you are learning, without interactive debug is like trying to learn to ride a bike with your hands tied behind your back. You will crash often and find it very painful and unrewarding experience, unless of course you are a masochist.
    #3
    buildlab
    Starting Member
    • Total Posts : 32
    • Reward points : 0
    • Joined: 2019/06/26 11:50:03
    • Location: 0
    • Status: offline
    Re: PIC16f676 Problem when power is reset! 2019/07/19 09:42:02 (permalink)
    0
    The switches were in ON condition. i was using switches because i found this problem and wanted to make sure it not because of faulty inputs. Actual inputs are supposed to be the inputs from a sensor.
    The problem is 
     
      if ((dryrun > 500)&&(motorstatus == 0))
            {
                RC5 = 1;
                motorstatus = 1;
            }
            if ((dryrun < 500)&&(motorstatus == 1))
            {
                RC5 = 0;
                motorstatus = 0;
            }
    The above code is for just toggling the led on and off according to the switch conditions.
    so the problem i am facing is that when the switch is in ON condition and the light is on.
    when power is reset, the LED is off. The switch needs to be toggled once ON/OFF to make the led glow.i was wondering why i was getting such a problem,i dont have a PICkit 3 i am using a k150 programmer. 

     

    #4
    mlp
    boots too small
    • Total Posts : 825
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: PIC16f676 Problem when power is reset! 2019/07/19 09:51:01 (permalink)
    0
    (deleted)
    post edited by mlp - 2019/07/19 09:52:22
    #5
    ric
    Super Member
    • Total Posts : 24582
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16f676 Problem when power is reset! 2019/07/19 22:25:45 (permalink)
    +1 (1)
    buildlab
    ...
    When the pic is in a condition where the LED connected to RC4 is GLOWING, And the power is reset. The led will not be GLOWING anymore(ie the RC4 pin is not on) even though the switches connected to the analog inputs are in the same condition before the power was gone. To my wonder, if i toggle the switches connected to the analog inputs it will work. 
    i cant understand whether it is a coding problem or a hardware problem.

    It's your code problem.
    The condition to turn RC4 on depends upon:
            if((ht_low < 500)&&(ht_high < 500)&&(status == 1)&&(controls == 0))



    So apart from the state of the switches, it requires "controls" to be 0, and "status" to be 1.
    However, straight after a power on, "controls" and "status" are both zero.
    You don't set "status" to 1 until you see both switches in the opposite condition.
     
    So, I would say your device is behaving exactly how you programmed it to.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    pcbbc
    Super Member
    • Total Posts : 1381
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: PIC16f676 Problem when power is reset! 2019/07/20 00:40:59 (permalink)
    +2 (2)
    The condition for setting status = 1 depends on different variables...
    if((lt_low > 500)&&(lt_high > 500)&&(status == 0))


    The fact that they are just one character different (l vs h) is not helpful. Also the naming isn’t particularly helpful and there’s no explanation from the OP as to what their signals mean or indeed which switches they are toggling. And the post starts out with LED on RC4 in post #1 but then implies RC5 in post #4.

    It is no wonder we are confused.
    #7
    ric
    Super Member
    • Total Posts : 24582
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16f676 Problem when power is reset! 2019/07/20 01:06:44 (permalink)
    +2 (2)
    pcbbc
    The condition for setting status = 1 depends on different variables...

    Well spotted.
    The same argument applies to the code in post#4
        if ((dryrun > 500)&&(motorstatus == 0))
        {
            RC5 = 1;
            motorstatus = 1;
        }
        if ((dryrun < 500)&&(motorstatus == 1))
        {
            RC5 = 0;
            motorstatus = 0;
        }

    I can't explain which branch it is in exactly, because the OP has left out two vital pieces of information.
    [1] What turns the LED on. RC5 high, or RC5 low?
    It matters if the other end is connected to Vdd or Vss
    [2] Does turning the switch on make the dryrun voltage go low, or go high?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    buildlab
    Starting Member
    • Total Posts : 32
    • Reward points : 0
    • Joined: 2019/06/26 11:50:03
    • Location: 0
    • Status: offline
    Re: PIC16f676 Problem when power is reset! 2019/07/22 23:14:16 (permalink)
    0
    Hi Ric, sorry for the late reply.
        Even i thought it was a code problem. 
        *RC5 HIGH turns the led ON.(The other end was connected to vss)
        *Turning the switch on makes the ADC values go beyond 500.(HIGH)
     
               For the DRYRUN suppose when the Switch is on and the LED is ON.
    And if the power is reset, i have made MOTORSTATUS "0" in the setup.
    According to the ADC channel the switch in ON position so the value above 500 so the LED RC5 should go HIGH rite ?.

    IS it because of hardware problem ?. because in datasheet minimal circuit it needs a rc circuit with a 1k or above resistor and a .1uf capacitor(says it is not critical) to pullup MCLR.
    Instead i put only a 10k to pull up MCLR pin.
    #9
    ric
    Super Member
    • Total Posts : 24582
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16f676 Problem when power is reset! 2019/07/22 23:28:40 (permalink)
    +1 (1)
    Do you still have the problem if you remove all the code which writes to RC4 ?
    If you do, what about if you remove the test for motorstatus ?
    i.e. change
            if ((dryrun > 500)&&(motorstatus == 0))


    to
            if (dryrun > 500)



    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    buildlab
    Starting Member
    • Total Posts : 32
    • Reward points : 0
    • Joined: 2019/06/26 11:50:03
    • Location: 0
    • Status: offline
    Re: PIC16f676 Problem when power is reset! 2019/07/22 23:53:39 (permalink)
    +2 (2)
    Hi Ric, 
               It worked!!. i ddn had to edit the code. it was a hardware problem. as i mentioned above i was using a 10k resistor to pull up MCLR pin instead of a 1k resistor as in data sheet. never thought it was because of the resistor because the 10k was working fine and the datasheet also mentioned 1k or above.i think it was something related to POWER ON reset.
    Thank you for your support Ric.  
    post edited by buildlab - 2019/07/22 23:57:03
    #11
    mcovington
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2019/07/12 09:19:40
    • Location: 0
    • Status: offline
    Re: PIC16f676 Problem when power is reset! 2019/07/23 17:53:15 (permalink)
    0
    Did you include the capacitor?  I'm thinking of p. 60 of this:
    http://ww1.microchip.com/downloads/en/DeviceDoc/40039F.pdf

    Also, there is a bit you can set (MCLRE) that makes it unnecessary to connect anything to MCLR.
    #12
    ric
    Super Member
    • Total Posts : 24582
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16f676 Problem when power is reset! 2019/07/23 18:15:18 (permalink)
    +1 (1)
    I would have expected the 10k pullup to work, so long as that was the ONLY thing connected to MCLR.
    You don't need external capacitors etc.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #13
    SummerQ
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2019/07/29 19:38:16
    • Location: 0
    • Status: offline
    Re: PIC16f676 Problem when power is reset! 2019/08/04 23:21:15 (permalink)
    -1 (1)
    About Microchip's PIC18F25K20T-I / SS
     
    Problem
    The Data in the flash ROM which is built in the PIC18F25K20 may disappear or be damaged at the timing when the power off the microcontroller.
     
    The program does not operate to write flash memory during power off.
     
    For example, if the voltage drops gently by turning off the power supply, The flash data did not disappear or damaged.
     
    But When the voltage drops suddenly by the power switch mounted on the board is shut off,  The flash data will be lost or damaged.
     
    We had used PIC18F25K20T-I/SS over 5000pcs, ever.
    Never happen this disappear or be damaged with above operation.
     
     
    Is that operation is normal for PIC18F25K20T-I/SS?
     
    Is there a workaround?
     
    Thanks and Br,
    #14
    pcbbc
    Super Member
    • Total Posts : 1381
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: PIC16f676 Problem when power is reset! 2019/08/05 09:14:41 (permalink)
    +2 (2)
    Hijacked thread with duplicate post.
    See here for standalone post.
    #15
    Jump to:
    © 2019 APG vNext Commercial Version 4.5