1
\$\begingroup\$

I have a PIC16F18076 microcontroller that I have been trying to get working and it simply has been working very inconsistently or not a all. I'm programming it using MplabX IDE and a Pickit 4, the device always programs just fine and the Pickit always verifies the correct device is attatched.

All and all I just can't seem to get the thing to ever function as intended. Sometimes I program it to just turn an led on and keep it on but it can't seem to do it consistently, and sometimes it can take several seconds before the LED even turns on at all. I have a more complex program that I download to it when the tester seems to be working better, and that program begins with blinking an LED and the controller shows no signs of wanting to do anything at all.

I thought that maybe I had messed up something internally in the controller but buying new ones did not solve the problem either. I'm assuming my wiring is fine because the LED will light up occasionally, maybe its my code? I'll paste the beginning of the program here:

#include "mcc_generated_files/system/system.h"

/*
    Main application
*/
void setlow(void){
   II_SetLow(); 
   love_SetLow();
   want_SetLow();
   when_SetLow();
   we_SetLow();
   you_SetLow();
   your_SetLow();
   make_SetLow();
   to_SetLow();
   kiss_SetLow();
   hug_SetLow();
   me_SetLow();
   hot_SetLow();
   so_SetLow();
   dance_SetLow();
   smile_SetLow();
   happy_SetLow();
   cute_SetLow();
   body_SetLow();
   much_SetLow();
   laugh_SetLow();
}

int main(void)
{
    SYSTEM_Initialize();

    // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts 
    // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts 
    // Use the following macros to: 

    // Enable the Global Interrupts 
    INTERRUPT_GlobalInterruptEnable(); 

    // Disable the Global Interrupts 
    //INTERRUPT_GlobalInterruptDisable(); 

    // Enable the Peripheral Interrupts 
    //INTERRUPT_PeripheralInterruptEnable(); 

    // Disable the Peripheral Interrupts 
    //INTERRUPT_PeripheralInterruptDisable();
   
    int instate;
    int wasstate=0;
    int safe=0;
    heart_SetHigh();
    __delay_ms(1000);
    heart_SetLow();
    __delay_ms(500);
    heart_SetHigh();
    __delay_ms(1000);
    heart_SetLow();
    __delay_ms(500);
    heart_SetHigh();
    __delay_ms(1000);
    heart_SetLow();
    if(switch_GetValue()){
        safe=1;
    }
...

The pin under the heart alias is supposed to "beat" at the start of the program but never seems to. Overall I'm just confused on why I can't seem to make the thing work as expected. The lack of consistency is driving me nuts, I'd be happier if the thing consistently didn't work. Any help or ideas are greatly appreciated.

EDITED SCHEMATIC:

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$
18
  • 3
    \$\begingroup\$ Do you have decoupling capacitors? \$\endgroup\$
    – DKNguyen
    Commented Feb 24, 2023 at 18:14
  • \$\begingroup\$ No and I don't know that that is \$\endgroup\$ Commented Feb 24, 2023 at 19:26
  • \$\begingroup\$ It's pointless to debug without these because though they may not be required, if they are required anything can happen without them so you can't start debugging. All your traces have parasitic inductance and inductance produces a voltage drop or rise across itself whenever current through it rises or falls. That means changes in current demand will cause voltage spikes or dips to the component being powered. The faster these changers the worse the spikes and dips. and MCUs are very fast, variable activity loads. autodesk.com/products/fusion-360/blog/… \$\endgroup\$
    – DKNguyen
    Commented Feb 24, 2023 at 19:40
  • 1
    \$\begingroup\$ @KidWithComputer Bypass caps are drawn right in the data sheet you linked, page 17 has them pictured in the minimum required circuit needed for proper operation. Please provide your schematics so we can see what else might be a problem. \$\endgroup\$
    – Justme
    Commented Feb 24, 2023 at 19:51
  • 1
    \$\begingroup\$ Try writing a program that does the bare minimum: sets up the oscillator to a know value and then just turns an LED on and off in the realm of human time scales. Something like 5-10s where you can count. Use nothing but a bunch of do-nothing for loops. See how reliably it runs and if the time interval matches anything at all to the number of do nothing loops you have. If you make the loop real simple you can sort of guesstimate the amount of instructions each loop will take and along with the clock frequency estimate the real time delay.. No functions or anything if you can help it. \$\endgroup\$
    – DKNguyen
    Commented Feb 25, 2023 at 18:58

3 Answers 3

1
\$\begingroup\$

You mentioned input \$\overline{MCLR}\$ in the comments. It's not optional to connect this. On page 4 of the datasheet is figure 4.1, which is captioned "Minimum Recommended Connections", and you shouldn't ignore this.

The bar over "MCLR" tells you that this input is active low, meaning that this device will be "reset" when connected to 0V, ground. It will execute code only when connected to a high potential, the positive supply.

If not connected at all, the pin is "floating" and will act like an antenna, picking up all sorts of noise from the environment, and will tend to flap around between high and low, occasionally resetting, and preventing the IC from functioning as you expect.

The circuit in figure 4.1 holds \$\overline{MCLR}\$ low for about 100μs after power is applied, but then takes it high, causing it to reset cleanly, once at power on, until the power supply has had time to stabilise, and then remain high (normal operation) thereafter.

The IC may work with \$\overline{MCLR}\$ connected permanently to the positive supply, which you should do if you are unable to implement the circuit in figure 4.1, but that is not recommended. You should implement the suggested reset mechanism shown in figure 4.1.

As others have pointed out, the decoupling capacitor C2 in that figure is also essential, though your circuit will probably be fine without it. In engineering, "it will probably be fine" is not good enough, and you'd be foolish to omit that capacitor. It should be installed as close to the IC's power supply pins Vss and Vdd as possible.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ It certainly is optional to connect /MCLR, MCU will work fine with it unconnected. The MCU has an internal Power-On Reset (POR) and the /MCLR input can be disabled through programming. The /MCLR has an internal pull-up, perfectly strong enough as it would only be put in the IC so the pin can be unconnected. /MCLR has a noise filter for additional protection. (See datasheet section 10. Resets, inc: "The MCLR pin is connected to VDD through an internal weak pull-up. The device has a noise filter in the MCLR Reset path. The filter will detect and ignore small pulses.") Downvoting, I'm afraid. \$\endgroup\$
    – TonyM
    Commented Feb 25, 2023 at 14:09
  • 2
    \$\begingroup\$ @TonyM Fair enough \$\endgroup\$ Commented Feb 25, 2023 at 17:33
1
\$\begingroup\$

There are two programming modes for PICs high voltage "HVP" and low voltage "LVP".

With HVP, you enter programming by applying a voltage significantly above VDD to the MCLR pin.

With LVP, also known as InCircuitSerialProgramming, you trigger programming by controling the voltage on the ICSP access pin. This means the pin can't be used for normal IO but does mean you can reprogram without the need for an additional HV source for MCLR.

LVP is selected as an option by an initial use of HVP, and HVP can always be used for recovery

From experience I can say that if you use LVP but dont pull the ICSP pin to the non-programming state with a resistor, then you get intermittent operation due to the PIC floating in and out of programming mode.

(I had taken for granted that you would have a resistor pulling MCLR to VDD, but looking at your newly added schematic, perhaps you don't. In the absence of a pull up on MCLR you will randomly enter reset based on leakage to the MCLR pin.)

\$\endgroup\$
3
  • \$\begingroup\$ This more of a critic than an answer \$\endgroup\$ Commented Feb 25, 2023 at 2:19
  • \$\begingroup\$ additionally I don't know what LVP or HVP programming is but I think I'm writing in C on the mplab x ide. I'm probably using the less advanced programming method if that helps \$\endgroup\$ Commented Feb 25, 2023 at 2:48
  • 1
    \$\begingroup\$ Sorry, I have separate out the advice on providing more detail into a comment on the question, and clarified my answer. It may or may not be your issue, but it does give similar symptoms. \$\endgroup\$
    – colintd
    Commented Feb 25, 2023 at 9:10
0
\$\begingroup\$

To any future readers of this post, I was so frustrated that this wouldn't work and spent so many hours trying to figure out what the matter was. The simple answer was to open the configuration bits, and do everything you can to force high voltage programing and disabling all prompts mentioning an external oscillator in the MCC configuration bits tab. This instantly solved the problem and in fact the circuit works just fine without pulling MCLEAR high or using any capacitors.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.