Pic18F14k50 - ADC causing pic to hang? Possible?

Status
Not open for further replies.

krich

New Member
I'm having an odd issue with my PIC18F14K50 chip and have basically ignoring for a while. I'm to a point in my project where I can't ignore it anymore and my best (admittedly feeble) troubleshooting skills have not revealed the cause.

I have a small bit of code (displayed below) that initializes the USART and the ADC, and then goes into the main loop. The main loop simply samples the ADC a number of times, finds the maximum value over that period, performs some calculations on the max number and then uses printf to dump the results to the USART. Rinse, Repeat.

The input to the is a current transformer fed through a full wave rectifier and gain=1 peak detector circuit. The voltage input is roughly proportional to the current passing through the transformer coil. I've spent quite a bit of time making sure that it is correct and I've double and triple checked it with my scope and dummy loads and it's showing the proper voltage levels (max 1V @ ~16.5A).

Here's my issue:

When I power on the PIC, it runs through the main loop a few times and then hangs. Reset it and it runs a few more times and then quits. Reset it again and it runs through the main loop once and dies. The number of times through the loop is inconsistent, but the hangs are almost certain. A couple times I've been able to run it for hours without issue. To troubleshoot, I installed the blinking LED, thinking it could be the USART that is simply not giving me output. Well, no. When the PIC hangs, so does the LED. This leads me to think that it could be an ADC issue (with my code), but at this point I'm not terribly sure.

I thought it might be high voltage spikes, or analog noise that could be confusing the ADC, so I've checked the output voltages and I don't see anything like that. I thought it might be something wrong with my MCLR reset pin. Nope. Different ADC channel. No go. To try to make the problem go away, I've moved off my (self made) development board onto a very bare bones breadboard setup and I still get the issue. Cry.

Here's to hoping one of you guys has seen this behavior before and can explain to me the error of my ways. Here's the full code:

 
Last edited:
Do you have a Microchip programmer which also has in-circuit debugging like the PICkit 3? And also using MPLAB?

If yes, then that helps me a lot to find problems. Perhaps you could stop it when it hangs and see where in the program it is at when you stop it. Also you can use break points.
 
Remember your PIC GPIO comes up randomised. Isolate that u haven't assumed a startup zero value by clearing all your GPIO variables with a loop as the first item to do when u pwr up. I had a simailar situation 2 weeks ago and that was partially responsible. The other issue was the PGM pin. A new pic comes with PGM /LVP enabled. Thus u have to ensure that your config forces LVP_OFF otherwise the PGM pin can float hi and send the pic in prg mode. If u not using the PGM PIN gnd it with a 10K .
 
Thanks guys. Good thoughts, but at the end of the day, we should all remember to read the silicon errata. In my case,

When the ADC is configured to operate with the internal FRC oscillator (ADCON2<2:0> = X11) and the device is not in Sleep, then the ADC may fail to complete the conversion which is indicated by the GO/DONE bit of the ADCON0 register remaining in the GO state indefinitely.​

and

Under certain device operating conditions, the ADC conversion may not complete properly. When this occurs, the ADC Interrupt Flag (ADIF) does not get set, the ADGO/ DONE bit does not get cleared, and the conversion result does not get loaded into the ADRESH and ADRESL result registers.​

Problem solved. Lesson learned. Time to go find another MCU...
 
I read where some people were have same problem they change oscillator setting. I got some for the usb haven't tried adc
 
...The other issue was the PGM pin. A new pic comes with PGM /LVP enabled. Thus u have to ensure that your config forces LVP_OFF otherwise the PGM pin can float hi and send the pic in prg mode. If u not using the PGM PIN gnd it with a 10K .
Just make sure the LVP is off and forget the resistor. Or did you say that?
 
Read the datasheet Sheet Errata There a problem with that chip ADC It can some how not finish a conversion and locks the chip microchip fixed it if you have newer chips some with v8 on there label
 
Reactions: 3v0
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…