View previous topic :: View next topic |
Author |
Message |
[terminate]
Joined: 08 Jan 2006 Posts: 26
|
I can't get my A/D conversion to work? Can anyone help? |
Posted: Sun Jan 08, 2006 3:56 pm |
|
|
Heres my code:
Code: |
#include <18F4520.h>
#fuses NOWDT, XT, NOLVP
#use delay(clock=4000000)
#use fast_IO(B)
void main(void)
{
int value;
SET_TRIS_B(0x00); // set port B as output
setup_port_a(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
output_high(PIN_D1);
while (1==1)
{
delay_ms(100);
value = Read_ADC();
delay_ms(500);
output_b(value);
}
}
|
Right now, I have the AN0 (RA0) pulled high to 5V and i'm not getting anything on the 8 leds attached to port B.
Do you see anything wrong?
thanks for the NOLVP
I did that and still no luck.
Could it be the 18F452 uses 10bit adc and im only outputting 8 bits to port B (8 bits).
Also, I tried to debug with an LED which turns off if value == 0, and it is always off.
update
I kinda have it working...
Code: |
#include <18F4520.h>
#fuses NOWDT, XT, NOLVP
#use delay(clock=4000000)
#use fast_IO(B)
void main(void)
{
int8 value;
SET_TRIS_B(0x00); // set port B as output
setup_port_a(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(1);
output_high(PIN_D1);
output_b(0x00);
while (1==1)
{
delay_ms(300);
value = Read_ADC();
output_b(value);
}
}
|
The problem here, RB7 is pulled high (bit 8) at startup (having AN1 pulled high as well), then when i put AN1 to low, RB7 goes to low, which is expected, but then i pull AN1 to high and RB7 doesn't go to high.
another interesting thing, when i power cycle it, and leave it high, the LED turns off after a certain time which is very weird.
(i removed the VREF- and VREF+ to ground and high and this fixed the power cycling problem)
Any idea?
Thanks!
much appreciated!!
Andrew
[/code][/b]
Last edited by [terminate] on Sun Jan 08, 2006 4:43 pm; edited 6 times in total |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 08, 2006 4:04 pm |
|
|
At a minimum, you need to add the NOLVP fuse. Without that fuse,
when Pin B5 goes high, it will lock up the PIC. |
|
![](templates/subSilver/images/spacer.gif) |
[terminate]
Joined: 08 Jan 2006 Posts: 26
|
|
Posted: Sun Jan 08, 2006 5:21 pm |
|
|
now it doesn't want to update...
Code: |
#include <18F4520.h>
#fuses NOWDT, XT, NOLVP
#use delay(clock=4000000)
#use fast_IO(B)
void main(void)
{
long value;
int counter=0;
SET_TRIS_B(0x00); // set port B as output
setup_port_a(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(1);
output_high(PIN_D1);
output_b(0x00);
while (true)
{
if (counter!=0)
{
output_high(PIN_D0);
delay_ms(200);
output_low(PIN_D0);
delay_ms(200);
}
delay_ms(300);
value = Read_ADC();
output_low(PIN_D0);
delay_ms(100);
output_b(value);
counter++;
}
}
|
AN1 is pulled high then low, but output on B never changes
however if AN1 is pulled low at start, output at B is different than it would be if it was high |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 08, 2006 5:38 pm |
|
|
You're adding complexity to your test program. Keep it simple.
Go back to a program that's similar to the first one.
What is your compiler version ? This will be a number such as 3.191
or 3.241, etc. It can be found at the start of the .LST file, which will
be in the project folder. |
|
![](templates/subSilver/images/spacer.gif) |
[terminate]
Joined: 08 Jan 2006 Posts: 26
|
|
Posted: Sun Jan 08, 2006 6:02 pm |
|
|
I have tried it from its most simple basis and those added steps are for debug ONLY.
I am using 3.203 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 08, 2006 6:34 pm |
|
|
I don't know what you're doing. In your first program, you were
using A/D channel 0. Then you switched to channel 1.
I can't help if the hardware or software is constantly changing. |
|
![](templates/subSilver/images/spacer.gif) |
[terminate]
Joined: 08 Jan 2006 Posts: 26
|
|
Posted: Sun Jan 08, 2006 6:54 pm |
|
|
it was to test if the channel was broken.
other than that the code is fine. |
|
![](templates/subSilver/images/spacer.gif) |
dyeatman
Joined: 06 Sep 2003 Posts: 1924 Location: Norman, OK
|
|
Posted: Sun Jan 08, 2006 8:07 pm |
|
|
This variation of the code works fine on an 18F8722 with my output set to Port D. (Don't have the 18F4520 and my LEDS are already connected on port D).
With a couple of minor tweaks for your hardware it should run on yours if the hardware is OK.
Code: |
#include <18F8722.h>
#device ADC=8
#fuses NOWDT, XT, NOLVP
#use delay(clock=4000000)
void main(void)
{
int8 value;
setup_port_a(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
output_d(0x00);
while (1)
{
delay_ms(300);
value = Read_ADC();
output_d(value);
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
[terminate]
Joined: 08 Jan 2006 Posts: 26
|
|
Posted: Sun Jan 08, 2006 9:03 pm |
|
|
thanks i'll try that out. |
|
![](templates/subSilver/images/spacer.gif) |
[terminate]
Joined: 08 Jan 2006 Posts: 26
|
|
Posted: Sun Jan 08, 2006 9:21 pm |
|
|
okay i've tried this (a little modification of your code)
Code: |
void main(void)
{
int8 value;
setup_port_a(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);
output_b(0x00);
while (1)
{
delay_ms(300);
value = Read_ADC();
output_b(value);
}
}
|
and it still doesn't work... here's a picture of my circuit:
The LEDs at the top are not all there, but none of them turn on when i have AN0 pulled high. The verticals are connected on the right side (which are cut off in the picture)
I'm using a 5V power supply.
Any clue? |
|
![](templates/subSilver/images/spacer.gif) |
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Sun Jan 08, 2006 9:31 pm |
|
|
You used
After
So the following PINs had been defined as analog inputs.
A0 A1 A2 A3 A5 E0 E1 E2 B2 B3 B1 B4 B0
To exclude PORTB pins to being analog inputs, replace
#define ALL_ANALOG
for
#define AN0_TO_AN7
Then set PORTB as outputs.
SET_TRIS_B(0x00);
Regarding your picture:
I can see some LED's in the upper left without any series resistor...
are they some special type ?
Keep well,
Humberto |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Mon Jan 09, 2006 3:46 am |
|
|
As an add on comment, you _must_ put current limiting resistors in the LED connections. Without these, if the chip tries to turn an LED 'on', it'll risk damaging it's output pins...
Further comments. you _must_ put both supply and ground connections to the chip. You only have supply connections to one side, and the chip will _not_ function correctly with this done.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
[terminate]
Joined: 08 Jan 2006 Posts: 26
|
|
Posted: Mon Jan 09, 2006 4:07 am |
|
|
Thanks,
I was wondering why theres two sets of VDD and VSS on this chip. What's the real reason?
I'll try doing what you suggested and hopefully it'll work! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Mon Jan 09, 2006 7:35 am |
|
|
The 'reason', is that as dies get larger, it is impossible to distribute the current round the chip, without significant droop. Hence multiple connections become necessary, and if not used, there can be significant voltages internally between parts of the die, leading to erratic operation. On _large_ chips, you can easily be talking in excess of 100 supply/ground connections...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Jan 09, 2006 8:08 am |
|
|
Are rows 29-31 your crystal or ceramic resonator? The connection to the PIC should be as short as possible. Can you put the resonator directly into rows 13 & 14, and put the small caps from 13 & 14 directly to the side buss? Actually better yet would be to run the small caps directly to a GND pin of the PIC.
If the PIC has multiple VCC & GND pins you should connect all of them. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
![](templates/subSilver/images/spacer.gif) |
|