View previous topic :: View next topic |
Author |
Message |
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
Strange PIC Behaviour |
Posted: Tue Aug 16, 2011 6:24 am |
|
|
Hi,
I'm trying to run a simple program code on a bread board, it is behaving quite strange,
we i plug in the power supply it works ok!(blinking an LED) but when I reset it using a reset button, there is no response.
i'm using complier version 4.119, My program code is as follows:
Code: |
#include <18F2520.h>
#device ADC=10
#fuses HS,NOLVP,WDT256,INTRC_IO
#use delay (clock=8000000)
#include <flex_lcd.c>
#include <stdlib.h>
void main()
{
float volts;
float f;
int16 z; //should be signed int
set_tris_c(0xFF);
set_tris_b(0x07);
PORT_B_PULLUPS(true);
lcd_init();
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel( 0 );
delay_us(30);
while(1)
{
restart_wdt();
output_high(Pin_C4);
output_low(Pin_B7);
z=read_adc();
printf(lcd_putc,"\f%ld",z);
delay_ms(1000);
output_low(Pin_C4);
output_high(Pin_B7);
delay_ms(1000);
}
}
|
Is there something that I'm missing?
I updated the complier just couple of days back, prior to this I was using 4.093 which was working fine!
Please let me know if I ve to set some fuses or not!
Thanks
Sid |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Aug 16, 2011 7:47 am |
|
|
how does the 'reset button' connect to the PIC and what is the CIRCUITRY associated with that button? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Tue Aug 16, 2011 7:56 am |
|
|
Also (though probably not the cause of this), _just one oscillator fuse_. You can't have 'HS', and 'INTRC_IO'.
There seems to be a 'habit' appearing of people not looking at what the fuses do, and squirting extra ones at the code, with very unpredictable results....
Best Wishes |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Tue Aug 16, 2011 8:29 am |
|
|
The circuit for reset button is 10k pull up resistor along with 1k resistor in series to the button, while the other end is grounded! The circuit seems to be fine.
I guess i should check what Ttelmah suggest! |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
Not Solved |
Posted: Tue Aug 16, 2011 11:01 am |
|
|
Tried without HS but thats not helping either...don't know what to do!
Please help!
:( |
|
|
W4GNS
Joined: 28 Sep 2010 Posts: 14 Location: Virginia, USA
|
|
Posted: Tue Aug 16, 2011 12:32 pm |
|
|
I would try removing components from the board, and then shifting a place or two on then re breadboarding, Breadboards are far from perfect, though I use them myself _________________ PCWH 4.14 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 16, 2011 12:56 pm |
|
|
Get rid of 90% of your code.
1. Don't set the TRIS. Let the compiler do it.
2. Don't enable Watchdog timer in a test program. Also don't set it
for a 1.024 second timeout when you have delays of 1 second in the
program. You're asking for WDT reset when you do that. You can
fix it by adding restart_wdt to the #use delay() statement, but you
don't need WDT in your program.
3. Put in all the proper fuses that help to prevent start-up problems,
such as PUT and BROWNOUT.
4. Don't use the PGC and PGD pins (B6 and B7) for external circuits
such as LEDs because these pins are used by your ICD debugger or
programmer.
5. Your comment says 'z' should be signed, but 'int16' is unsigned.
Also, the ADC returns an unsigned result, so there is no need for 'z'
to be a signed integer.
6. Don't put in large #include files like stdlib.h when you don't need it.
It just makes it take longer to compile the program.
Try a very simple program:
Code: |
#include <18F2520.H>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4M)
//========================
void main()
{
while(1)
{
output_high(PIN_C4); // Blink LED
delay_ms(500);
output_low(PIN_C4);
delay_ms(500);
}
} |
|
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Tue Aug 16, 2011 7:02 pm |
|
|
Thanks PCM!
I tried what you suggested! but it is still not working :( I even compiled the program that you recommended but that is also not working.
when I assemble on Breadboard it working fine with the supply, but as soon as i press reset button it hangs.
what are the chances of MCLR pin getting damaged.
I don't understand this problem. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 16, 2011 8:03 pm |
|
|
Does your MCLR circuit look like this ? Or is it different ? Code: | +5v
|
<
> 10K
< ___ Switch
To 1K | _|_|_
MCLR ---/\/\/\/------------o o-----
pin |
--- GND
-
|
Have you looked at the MCLR pin with an oscilloscope to see that it's
normally at +5v, and when you push the button it goes down to a
solid 0 volts ? Then when you release it, it goes back to +5v ?
You said this is on a breadboard. Do you have a clean, stable +5v
for the Vdd on the PIC ? Do you have a 100 nF (0.1 uF) ceramic
capacitor between the Vdd pin on the PIC and ground ? It should be
placed very close to the PIC's Vdd pin. |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
No solutions yet |
Posted: Tue Aug 16, 2011 11:46 pm |
|
|
PCM the circuit is just the same as you have shown..but I have some finding here....
When I program using an ICD or a Pickit 3 it works..but as soon as I disconnect the ICD or pickit3 it stops working....
How should I proceed?
Sid |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Wed Aug 17, 2011 2:28 am |
|
|
Normal reason for that is ground, or power. Look three times more at your connections. Does the ground of the processor actually go to the ground of the board?. You _have_ a wiring problem somewhere.....
Best Wishes |
|
|
TMLtech
Joined: 20 Jul 2011 Posts: 21 Location: Finland
|
Re: No solutions yet |
Posted: Wed Aug 17, 2011 2:31 am |
|
|
Sid2286 wrote: | PCM the circuit is just the same as you have shown..but I have some finding here....
When I program using an ICD or a Pickit 3 it works..but as soon as I disconnect the ICD or pickit3 it stops working....
How should I proceed?
Sid |
What kind of voltage you have MCLR- pin when the power is on? If you have oscilloscope measure that pin, maybe there is some glitch.. |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
|
Posted: Wed Aug 17, 2011 2:33 am |
|
|
Everythings looks perfectly fine...capacitor at its place...everythings good!but as soon as i disconnect the board from the ICD or Pickit3 it stops working!
strange.. |
|
|
Sid2286
Joined: 12 Aug 2010 Posts: 119
|
Still no Success! |
Posted: Wed Aug 17, 2011 4:54 am |
|
|
I changed the IC and tried with another pic18f2520 and there I saw something as follows:
when I write a simple blink code such as
Code: |
#include <18F2520.H>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=8M)
#include <flex_lcd.c>
#include <stdlib.h>
//========================
void main()
{
while(1)
{
output_low(PIN_C0);
delay_ms(100);
output_high(PIN_C0);
delay_ms(100);
}
}
| it seems to work ok without ICD or Pickit3 but as soon as I write something for LCD as as:
Code: |
#include <18F2520.H>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=8M)
#include <flex_lcd.c>
#include <stdlib.h>
//========================
void main()
{
lcd_init();
while(1)
{
printf(lcd_putc,"\f Hello\n world");
output_low(PIN_C0);
delay_ms(100);
output_high(PIN_C0);
delay_ms(100);
}
}
| everything crashes..there is no display on LCD and no blinking of LED. Please suggested something...I'm struggling for complete 1 week now :( |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Wed Aug 17, 2011 7:08 am |
|
|
The suggestion has already been made - connection, connection, connection.....
Think for a moment what the ICD connection actually 'makes':
1) A ground connection.
2) A Vdd connection - also adds capacitance between grond and this pin.
3) An MCLR connection.
4) Pull _down_ connections to PGC, and PGD - these are required if the code is compiled for debug use, otherwise the chip won't run.
That your board doesn't work without the ICD, says that one of these connections is faulty in some way. Nothing else.
'Looking' good, is not a test. Get a meter, or oscilloscope, and actually _test_ the connections.
Best Wishes |
|
|
|