View previous topic :: View next topic |
Author |
Message |
Geps
Joined: 05 Jul 2010 Posts: 129
|
Blinking LED On A PIC18F4520 |
Posted: Tue Jul 13, 2010 7:17 am |
|
|
Hi,
I'm trying to blink an LED on a PIC18F4520.
Code is as follows:
Code: | #include <18F4520.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP,
#use delay(crystal=4MHz)
void main()
{
while(TRUE)
{
output_high(PIN_A1);
delay_ms(500);
output_low(PIN_A1);
delay_ms(500);
}
} |
I have an external 4MHz resonator connected to the device.
Compiler is PCWH 4.087
I've tried various fuses HS, XT, INTRC_IO, INTRC and crystal/clock but no combination seems to work. The program compiles and programs with no errors.
Any ideas?
Cheers, |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Jul 13, 2010 9:48 am |
|
|
Did you try this:
Code: |
#use delay(clock=4000000)
|
How is you LED connected to RA1? What value of resistor are you using?
Is your MCLR pin pulled up to Vcc with a resistor?
Did you include a 0.1uF ceramic capacitor between Vcc and Vss pins of the PIC? |
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Tue Jul 13, 2010 10:08 am |
|
|
Yup, I've reverted back to that line.
The LED is connected via a 200R resistor to VDD.
MCLR is pulled high with a 10K resistor.
100nF is accross the supply on pins 19 & 20.
And when I scope the resonator I get a perfectly timed 4MHz waveform. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 13, 2010 10:35 am |
|
|
Does this schematic show your circuit ? The cathode of the LED should
go to ground.
Code: |
pin 200 ohms LED
A1 ---/\/\/\/------->|----
|
|
----- Ground
---
-
|
To test the LED circuit, disconnect it from pin A1 on the PIC, and
temporarily connect the left side of the resistor to +5v. It should work.
If not, the connections could be wrong, or the polarity of the LED could
be wrong, or the resistor might be a larger value than you think it is. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Tue Jul 13, 2010 10:38 am |
|
|
What about PIN_A1 being defaulted to analog?
setup_adc_(NO_ANALOGS); _________________ Google and Forum Search are some of your best tools!!!! |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Jul 13, 2010 11:49 am |
|
|
dyeatman wrote: | What about PIN_A1 being defaulted to analog?
setup_adc_(NO_ANALOGS); |
You don't need it.
The output_high() and output_low() do that automatically.
If the cathode of the LED is connected to GND as per PCM Programmer's diagram above, take a voltmeter and measure what voltage you get at the anode. Should be 0.7 to 1.2V or so. If you are getting nothing there is something wrong with your connection.
Or your LED could be connected backwards. |
|
|
gordonac14
Joined: 13 Jul 2010 Posts: 5
|
|
Posted: Tue Jul 13, 2010 11:59 am |
|
|
i = 0; //reset counter
while(1)
{
LED = i & 1;
if(i & 1)
{
for(j = 0; j < 40000; ++j);
}
else
{
for(j = 0; j < 65000; ++j);
}
i++;
if(i == 15)
i = 0;
} |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Jul 13, 2010 12:01 pm |
|
|
Nice code. What does it do? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jul 13, 2010 12:14 pm |
|
|
Actually output_high() and output_low() do not configure Port A for digital.
They automatically set the TRIS if you use standard i/o mode (which is
the default mode). CCS has a convention, in their start-up code, to
configure Port A to be all digital. So normally you don't have to do it. |
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Tue Jul 13, 2010 2:50 pm |
|
|
I typically sink the current rather than source it so it's like this:
Code: |
pin 200 ohms LED
A1 ---/\/\/\/------->|----
|
|
----- 5V
|
The LED lights up fine when (the resistor 'end' is) connected to 0V.
I've also tried 2 other PICs with the same output (all brand new) so I'm fairly happy that it's not faulty chip. |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Jul 13, 2010 3:17 pm |
|
|
I am stumped.
How about if you just put:
output_low(PIN_A1);
and not toggle it. Does your LED come on? |
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Wed Jul 14, 2010 2:49 am |
|
|
I tried that approach before and it didn't it work unfortunately.
However it gets more confusing.......
This morning I've played around testing pins and voltages thinking it might be blinking on another pin/port. I've swapped chips and reprogrammed every time.
It then started blinking.....however, the resonator now has no output instead of the sine wave it had before and indeed it works without a resonator attached so seems to be using it's own internal clock. And the timing is off. Which is interesting because I had it in this thread here: http://www.ccsinfo.com/forum/viewtopic.php?p=134592&highlight=#134592 And never manged to solve it. Further Googling suggested the chip itself was awkward to use as other people had very similar problems.
With this program:
Code: | #include <18F4520.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP,
#use delay(clock=200000)
void main()
{
while(TRUE)
{
output_high(PIN_A1);
delay_ms(500);
output_low(PIN_A1);
delay_ms(500);
}
} |
I've now got it flashing at 624ms 50% duty cycle - this with no external clock or oscillator. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 14, 2010 11:06 am |
|
|
What product are you using for a programmer ? |
|
|
Geps
Joined: 05 Jul 2010 Posts: 129
|
|
Posted: Thu Jul 15, 2010 1:59 am |
|
|
Pickit 2
I've ordered the development board from CCS so hopefully I can use that to find out just where the problem lies by reverting back to my old design one bit at a time. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Jul 15, 2010 11:56 am |
|
|
mkuang wrote: | dyeatman wrote: | What about PIN_A1 being defaulted to analog?
setup_adc_(NO_ANALOGS); |
You don't need it.
The output_high() and output_low() do that automatically. | the output functions control the TRIS register but do not disable the analog port in my v4.077. And in all the older compiler versions you had to add this line too, so unless something changed at CCS I guess you better still add this line.
You also will have to disable the comparators.
I tried testing your example code. As I don't have v4.087 I tried it in my v4.077. When I added Code: | setup_adc(NO_ANALOGS); | wrong code was generated. Can you post your list file?
Also post the firmware version for your PicKit2.
Last edited by ckielstra on Thu Jul 15, 2010 12:12 pm; edited 1 time in total |
|
|
|