CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

I can't get my A/D conversion to work? Can anyone help?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
[terminate]



Joined: 08 Jan 2006
Posts: 26

View user's profile Send private message

I can't get my A/D conversion to work? Can anyone help?
PostPosted: Sun Jan 08, 2006 3:56 pm     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 08, 2006 4:04 pm     Reply with quote

At a minimum, you need to add the NOLVP fuse. Without that fuse,
when Pin B5 goes high, it will lock up the PIC.
[terminate]



Joined: 08 Jan 2006
Posts: 26

View user's profile Send private message

PostPosted: Sun Jan 08, 2006 5:21 pm     Reply with quote

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
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 08, 2006 5:38 pm     Reply with quote

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.
[terminate]



Joined: 08 Jan 2006
Posts: 26

View user's profile Send private message

PostPosted: Sun Jan 08, 2006 6:02 pm     Reply with quote

I have tried it from its most simple basis and those added steps are for debug ONLY.

I am using 3.203
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 08, 2006 6:34 pm     Reply with quote

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.
[terminate]



Joined: 08 Jan 2006
Posts: 26

View user's profile Send private message

PostPosted: Sun Jan 08, 2006 6:54 pm     Reply with quote

it was to test if the channel was broken.

other than that the code is fine.
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Jan 08, 2006 8:07 pm     Reply with quote

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);
   }
}


[terminate]



Joined: 08 Jan 2006
Posts: 26

View user's profile Send private message

PostPosted: Sun Jan 08, 2006 9:03 pm     Reply with quote

thanks i'll try that out.
[terminate]



Joined: 08 Jan 2006
Posts: 26

View user's profile Send private message

PostPosted: Sun Jan 08, 2006 9:21 pm     Reply with quote

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?
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sun Jan 08, 2006 9:31 pm     Reply with quote

You used
Code:

#define ALL_ANALOG   

After
Code:

   SET_TRIS_B(0x00); 

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... Shocked
are they some special type ?


Keep well,


Humberto
Ttelmah
Guest







PostPosted: Mon Jan 09, 2006 3:46 am     Reply with quote

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
[terminate]



Joined: 08 Jan 2006
Posts: 26

View user's profile Send private message

PostPosted: Mon Jan 09, 2006 4:07 am     Reply with quote

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!
Ttelmah
Guest







PostPosted: Mon Jan 09, 2006 7:35 am     Reply with quote

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
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Mon Jan 09, 2006 8:08 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group