AnsweredHot!PIC 12F629 malfunctioning on real device

Author
MaarioS
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2016/01/20 04:46:23
  • Location: 0
  • Status: offline
2018/03/14 04:34:04 (permalink)
0

PIC 12F629 malfunctioning on real device

Hi there, first of all I want to state that I graduated IT and know programming overall, although this is my first and the only time now where I try to program a software for PIC 12F629 by myself. This chip plugs into a device (gaming cartridge in old console) that reads a signal and changes outputs whenever the input signal stays low for around 2 seconds. Unfortunately I find issues when I test it on real hardware and it looks like the chip randomly switches outputs from time to time even if the button wasn't pressed and probably some other glitches caused by this chip... More details below, first is the program:
 
#include <htc.h>
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif
__CONFIG(FOSC_INTRCCLK & WDTE_OFF & MCLRE_OFF);

#define th 50000 //experimental value defines how many time needed to activate pattern change
char eepromvar;
unsigned short counter;
#define PATTERN_MAX 3
const char effect[PATTERN_MAX+1] = {0x00,0x04,0x02,0x06};
void main(void){
    GPIO=0;
    CMCON = 0x07; // turn off comparators
    TRISIO=0b00001000;
    //char effect[] = {0x00,0x04,0x02,0x06};
    counter = 0;
    eepromvar=EEPROM_READ(0x00);
    if(eepromvar > PATTERN_MAX){
        eepromvar = 0;
        EEPROM_WRITE(0x00,eepromvar);
    }
    GPIO = effect[eepromvar];
    while(1){
        if(GP3 == 0){
            if(counter <= th){
                if(counter == th){
                    
                    eepromvar++;
                    if(eepromvar > PATTERN_MAX){
                        eepromvar = 0;
                    }
     EEPROM_WRITE(0x00,eepromvar);
     GPIO = effect[eepromvar];
                }
                counter++;
            }
        }
        else{
            counter = 0;
        }
    }
}

 
I will try to illustrate it without any pictures: this chip is configured like this: GP3 pin is configured as input and all other pins are configured as output. GP3 is connected to /RESET signal and output pins are connected to the memory pins on the cartridge. So if the RESET button is pressed for at least 2 seconds then it switches the pattern on the output pins and switches the memory. This is all it should do. In addition it also saves the last pattern used in EEPROM and restores it once the device is turned on again. Unfortunately as I mentioned this chip doesn't typically work well as the chip randomly crashes, stops working or switches patterns when it is not expected to do... This chip is also connected to 3.3V VDD voltage, although /RESET input pin goes to 5V when it is high, although I don't think it should be the issue here as the datasheet clearly states that 12F629 operating voltage is 2.0V-5.5V and GP3 pin can go as high as 13V. IN THEORY I think that this program should work correctly. I already tried to connect VDD pin and GP3 through a series resistor 1K and it only made it worse, trying to connect GP3 pin to /RESET pin through series resistor 75ohm doesn't work either. Does anyone have any ideas or suggestions for it?? I will be so grateful to hear responses. Many thanks in advance!!
 
I also noticed another strange thing- when I try to output signal "1" through GP4 pin, it doesn't work well because in reality it outputs 1.65V as I measured it when the 12F629 is connected to 3.3V VDD, while all other output pins give 3.3V when outputting "1"... This really turned me away as I have no idea what I'm doing wrong. Again any suggestions for this problem will be appreciated, thanks!!
#1
qɥb
Monolothic Member
  • Total Posts : 2014
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: online
Re: PIC 12F629 malfunctioning on real device 2018/03/14 05:45:19 (permalink)
+1 (1)
MaarioS
...
I also noticed another strange thing- when I try to output signal "1" through GP4 pin, it doesn't work well because in reality it outputs 1.65V as I measured it when the 12F629 is connected to 3.3V VDD, while all other output pins give 3.3V when outputting "1"... This really turned me away as I have no idea what I'm doing wrong. Again any suggestions for this problem will be appreciated, thanks!!

This config setting:
FOSC_INTRCCLK
tells the PIC to output Fosc/4 on the GP4 pin.
What you are reading as 1.65 volt is really a 1MHz square wave.
Change the setting to:
FOSC_INTRCIO
 
 

PicForum "it just works"
#2
MaarioS
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2016/01/20 04:46:23
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 05:55:59 (permalink)
0
OK I have this one figured out already so many thanks about the suggestion!! So how about the main issue in this topic?? Is something wrong in the routine or maybe I should use different instructions for reading and outputting?? Just guessing, I have no idea what goes wrong here...
#3
jack@kksound
code tags!
  • Total Posts : 2431
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 08:00:06 (permalink) ☼ Best Answerby MaarioS 2018/03/14 17:38:29
0
This chip is also connected to 3.3V VDD voltage, although /RESET input pin goes to 5V when it is high, although I don't think it should be the issue here as the datasheet clearly states that 12F629 operating voltage is 2.0V-5.5V and GP3 pin can go as high as 13V.

Input pins are specified as Vdd+0.3v MAX (except MCLR - GP3). Applying a voltage greater then this maximum can result in very unexpected behvior. The GP3 (MCLR) input is also the Vpp (programming) input. Raising this above Vdd sufficiently will cause the pic to enter program mode, again with unexpected results (looks like random resets).
#4
MaarioS
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2016/01/20 04:46:23
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 08:14:33 (permalink)
0
jack@kksound
This chip is also connected to 3.3V VDD voltage, although /RESET input pin goes to 5V when it is high, although I don't think it should be the issue here as the datasheet clearly states that 12F629 operating voltage is 2.0V-5.5V and GP3 pin can go as high as 13V.

Input pins are specified as Vdd+0.3v MAX (except MCLR - GP3). Applying a voltage greater then this maximum can result in very unexpected behvior. The GP3 (MCLR) input is also the Vpp (programming) input. Raising this above Vdd sufficiently will cause the pic to enter program mode, again with unexpected results (looks like random resets).




So the GP3 pin configured as input still cannot exceed VDD range while working?? Is this possible to connect the chip to 5V but make the outputs output 3.3V voltage while "1" in any way??
#5
jack@kksound
code tags!
  • Total Posts : 2431
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 08:33:21 (permalink)
0
The GP3 pin (any pin for that matter) should not exceed Vdd during operation. The output voltage levels will follow the Vdd so normally 3.3 volt ouputs ar enot possible with a 5 volt Vdd. You can "simulate" an open drain output and use an external pull-up resistor to the 3.3 volts: Clear the output pin to logic 0 (i.e. GPIO3 = 0;), then use the TRISIO3 setting to determine the output level: TRISIO3 = 1; (pin is set to input mode which turns off the output) results in high output level (determined by the external resistor)and TRISIO3 = 0;(turns on the output) results in a low output level. This will work on any of the GPIO pins (change the 3 to the correct I/O pin number).
#6
MaarioS
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2016/01/20 04:46:23
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 08:53:37 (permalink)
0
jack@kksound
The GP3 pin (any pin for that matter) should not exceed Vdd during operation. The output voltage levels will follow the Vdd so normally 3.3 volt ouputs ar enot possible with a 5 volt Vdd. You can "simulate" an open drain output and use an external pull-up resistor to the 3.3 volts: Clear the output pin to logic 0 (i.e. GPIO3 = 0;), then use the TRISIO3 setting to determine the output level: TRISIO3 = 1; (pin is set to input mode which turns off the output) results in high output level (determined by the external resistor)and TRISIO3 = 0;(turns on the output) results in a low output level. This will work on any of the GPIO pins (change the 3 to the correct I/O pin number).




Sorry I don't quite understand this. Would you explain it in greater detail?? Right now I'm testing my circuit and connected 12F629 straight to 5V and I THINK it functions correctly right now, as long as it doesn't glitch out again. So you all might be right that my issue here might be 3V-5V incompatibility I wasn't aware of... At this point I will connect PIC 12F629 straight to 5V but the outputs must be 3.3V when HIGH, so I think I should use another voltage transceiver for this. Although is this possible to make outputs output 3.3V straight from the chip in any way?? The chip has 6x I/O pins, while I always use 1 input pin and 2-4 output pins, so there is always at least 1 pin that is left unused. Many thanks for your suggestions in advance!!
#7
jack@kksound
code tags!
  • Total Posts : 2431
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 09:07:20 (permalink)
0
OK I will try.
To make a GPIO pin act like an open drain output:
(an open drain output only pulls the output LOW (logic 0), an external resistor is used to pull the poutput HIGH (logic 1) to whatever voltage you wish (3.3 in your case - an external 3.3 volt supply point is where the pull-up resistorr is connected).
 
Select a GPIO pin, we will use GPIO3 as an example.
Clear the GPIO pin state to a LOGIC 0:
GPIO3 = 0;
To set the GPIO3 pin to an output LOGIC 0 state:
TRISIO3 = 0;
This puts the GPIO3 pin in the OUTPUT mode and drives the output LOW (we set that level with the GPIO3 = 0 statement)
TO set the GPIO3 pin to a LOGIC 1 state:
TRISIO3 = 1;
This puts the GPIO3 pin in the INPUT MODE which turns off teh GPIO3 output drive and puts the pin into the high impedance mode, the pin is allowed to be pulled high by the external resistor.
Leave the GPIO3 pin at logic 0 (never do: GPIO3 = 1). Alwyas control the output state of the GPIO3 pin by changing the TRISIO3 bit.
This method can be used on any GPIO pin that you want to use as an output and have a logic 1 level different from the Vdd level. This LOGIC 1 level cannot exceed the Vdd level but can be anything LESS THAN Vdd. Make it higher than Vdd and the pin will again have too high a voltage on it (exceed Vdd). SO the external pullup resistors can be connected to 3.3 volts but NOT anything above 5 volts  (your Vdd).
#8
MaarioS
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2016/01/20 04:46:23
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 09:19:05 (permalink)
0
OK then I kinda understand it but not quite fully, are you sure the example GPIO3 is right?? This pin can actually be configured as input only, so I'm kinda confused here. Can we use GPIO5 as an example since it is not used in my circuit at all and is left unconnected?? The entire chip will work on 5V only from now on, so downgrading outputs to 3.3V level is what I want to achieve here and seems compatible in this situation
#9
jack@kksound
code tags!
  • Total Posts : 2431
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 10:29:18 (permalink)
0
OK, yes GPIO3 was a bad choice as it is input only, sorry.
However the method will work on other GPIO pins that are capable of digital output mode.
To use a different pin simply replace the 3 in the examples with the other pin number (5 for GPIO5, etc.).
I appologize but understanding this concept does require at least a little understanding of an open drain output configuration and the standard output configuration of the pic.
 
#10
MaarioS
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2016/01/20 04:46:23
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 10:52:39 (permalink)
0
OK, so are you trying to tell me that in order to force output pins to output 0V or 3.3V (or anything lower than VDD), the output pins need to be hardwired to 3.3V through a pull up resistor each and in order to trigger 0V or 3.3V, the pin polarity needs to be switched between input and output?? This is actually a very clever way to do it and seems opt for my design. Is 10Kohm pull up good for this purpose??
#11
jack@kksound
code tags!
  • Total Posts : 2431
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 11:21:18 (permalink)
0
Yes that sounds like you have the idea. This method has been around a while and discussed in these forums (and others) many times before, commonly used for I2C outputs when not using default I2C I/O pins. The value of the pullup resistor will need to be sufficient to provide the current required by whatever circuit you attach to the output pin but not so low as to exceed the sink current specification of the pic pins (see datasheet).
#12
MaarioS
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2016/01/20 04:46:23
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 12:44:37 (permalink)
0
What exactly do you mean by the resistor value?? Well at this point I'm not sure what the typical value for a pull up resistor is for designs like this but I think the most popular value for a pull up is 10Kohm, so will this be OK for this PIC??
#13
PStechPaul
Super Member
  • Total Posts : 1710
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 13:41:40 (permalink)
0
You can also make an open drain by using a Schottky diode. It has only about 200 mV drop at low current levels so the low output will still be below typical logic zero level of 0.5 V. Another method is a low gate voltage NMOS device, but that will reverse the logic. I like to use 4.7k for pull-ups, but 10k should be OK. It depends on how much current the external device requires, as well as the slew rate, which is a function of the pull-up and capacitance.
 
Also, you could use a series resistor and a Schottky diode to VDD so as to keep the reset signal within spec, while using a 3.3 VDC power supply for everything else.
post edited by PStechPaul - 2018/03/14 13:52:26

 
#14
jack@kksound
code tags!
  • Total Posts : 2431
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 14:39:07 (permalink)
+1 (1)
Determination of the proper value of resistor is not a popularity contest, it depends on the requirements and demands of the load circuit attached to the I/O pin. Basic Ohm's Law calculations. How much current does this laod require/draw? What minimum input voltage is required to recognize the output state as a valid LOGIC 1 level?
Once you know the current and voltage requirements the value of the pullup resistor can be calculated to meet those requirements (all this at DC or low frewuencies - gets more complicated as the frequency gets higher).
My guess is that these concepts are a little beyond your current understanding so maybe tell us what you are going to attach to the I/O pins and someone might be able to determine a good value for the pullup resistors for you.
#15
MaarioS
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2016/01/20 04:46:23
  • Location: 0
  • Status: offline
Re: PIC 12F629 malfunctioning on real device 2018/03/14 17:37:31 (permalink)
0
This is the kind of response I kinda expected stating these is no universal solution etc. although there is almost nothing to connect the pin to, it is just PIC pin + memory pin and this is all. What I did is I connected the PIC pin through 3.9K resistor to VDD 3.3V and I can see the entire circuit works great and as expected and the actual voltage on the pin when high measures at 3.27V while the entire circuit measures at around 3.29V so this is just accurate. Many thanks for all for your help and I can't believe I have finally figured this out!!
post edited by MaarioS - 2018/03/14 17:40:35
#16
Jump to:
© 2018 APG vNext Commercial Version 4.5