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

AN1 and AN2 no output

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



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

AN1 and AN2 no output
PostPosted: Mon Oct 19, 2015 2:55 am     Reply with quote

Hi, i would like to know why AN1 and AN2 result show 0. How to solve this problem?

Code:

#include <18F4550.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

main()
{
  int value1,value2,value3;
 
  setup_adc_port( AN0_TO_AN2);
  setup_adc( ADC_CLOCK_INTERNAL );
                         
  while(true)
  {                           
    set_adc_channel( 0 );
    value1 = Read_ADC();
    delay_ms(100);

    set_adc_channel( 1 );
    value1 = Read_ADC();
    delay_ms(100);

    set_adc_channel( 2 );
    value1 = Read_ADC();
    delay_ms(100);

    printf("\rAN0: %4i AN1: %4i  AN2: %4i",value1,value2,value3);
         
  }

}
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Mon Oct 19, 2015 4:19 am     Reply with quote

Look at your code again. You declare value1 to value3 but only use value1 when reading the ADC.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 19, 2015 4:21 am     Reply with quote

In addition to that:

The 18F4550 data sheet says:
Quote:
For the A/D converter to meet its specified accuracy,
the charge holding capacitor (CHOLD) must be allowed
to fully charge to the input channel voltage level

This means after you change the A/D channel, you must
wait for the specified acquisition time before doing an A/D
conversion. The data sheet specifies this as 2.45 usec.

Currently you have no delay after changing the A/D channels.
You need to add a delay after each channel change.
You could use delay_us(5), to be safe.

The data sheet also says:
Quote:
The maximum recommended impedance for analog
sources is 2.5 kΩ.

Make sure that the external circuits connected to the A/D pins
has a resistance 2.5K maximum.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Oct 19, 2015 4:48 am     Reply with quote

Hi,
Sorry for typing mistake. But value2 and value3 result still 0.


Code:

#include <18F4550.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

main()
{
  int value1,value2,value3;
 
  setup_adc_port( AN0_TO_AN2);
  setup_adc( ADC_CLOCK_INTERNAL );
                         
  while(true)
  {                           
    set_adc_channel( 0 );
    value1 = Read_ADC();
    delay_ms(100);

    set_adc_channel( 1 );
    value2 = Read_ADC();
    delay_ms(100);

    set_adc_channel( 2 );
    value3 = Read_ADC();
    delay_ms(100);

    printf("\rAN0: %4i AN1: %4i  AN2: %4i",value1,value2,value3);
         
  }

}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 19, 2015 4:58 am     Reply with quote

Did you read my post ? What about the needed delays after you change
each ADC channel ? They are still missing from your code.
temtronic



Joined: 01 Jul 2010
Posts: 9225
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Oct 19, 2015 5:19 am     Reply with quote

this is wrong...
setup_adc( ADC_CLOCK_INTERNAL );

according to the datasheet it should only be used for FOsc <1MHz. You are running the PIC at 20 MHz
'INTERNAL' for all PICs is meant for 'sleep' mode operation.
And you still need the delay_us(5) after each setting of the adc channel.

Jay
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Mon Oct 19, 2015 6:15 am     Reply with quote

Hi,

Why are you using '%i' in your printf statement? That is for pointers. You should use '%u' instead.

You know, CCS provides a lot of example code to demonstrate many PIC features and compiler functions. Have you looked at any of them? They are provided for a reason!
_________________
John

If it's worth doing, it's worth doing in real hardware!
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Oct 19, 2015 6:26 am     Reply with quote

Ok, i've add delay_us(5) after change each adc channel. What should i replace setup_adc( ADC_CLOCK_INTERNAL ) ?


Code:

#include <18F4550.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

main()
{
  int value1,value2,value3;
 
  setup_adc_port( AN0_TO_AN2);
  setup_adc( ADC_CLOCK_INTERNAL );
                         
  while(true)
  {                           
    set_adc_channel( 0 );
    delay_us(5)
    value1 = Read_ADC();
    delay_ms(100);

    set_adc_channel( 1 );
    delay_us(5)
    value2 = Read_ADC();
    delay_ms(100);

    set_adc_channel( 2 );
    delay_us(5)
    value3 = Read_ADC();
    delay_ms(100);

    printf("\rAN0: %4i AN1: %4i  AN2: %4i",value1,value2,value3);
         
  }

}
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Mon Oct 19, 2015 6:37 am     Reply with quote

Hi,

How about consulting the CCS manual, and studying the 'setup_adc' function?
_________________
John

If it's worth doing, it's worth doing in real hardware!
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Oct 19, 2015 6:23 pm     Reply with quote

Hi guys,

It works, thank you very much Very Happy
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Mon Oct 19, 2015 6:37 pm     Reply with quote

Hi,

You should post your corrected code, so the next guy can benefit from this thread! Pay it forward!
_________________
John

If it's worth doing, it's worth doing in real hardware!
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Oct 19, 2015 7:06 pm     Reply with quote

Code:

#include <18F4550.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

main()
{
  int value1,value2,value3;
 
  setup_adc_port( AN0_TO_AN2);
  setup_adc( ADC_CLOCK_INTERNAL );
                         
  while(true)
  {                           
    set_adc_channel( 0 );
    delay_us(5)
    value1 = Read_ADC();
    delay_ms(100);

    set_adc_channel( 1 );
    delay_us(5)
    value2 = Read_ADC();
    delay_ms(100);

    set_adc_channel( 2 );
    delay_us(5)
    value3 = Read_ADC();
    delay_ms(100);

    printf("\rAN0: %4u AN1: %4u  AN2: %4u",value1,value2,value3);
         
  }

}
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Oct 19, 2015 7:38 pm     Reply with quote

Sorry, I have another one question, if i change setup_adc_port( AN0_TO_AN2); to setup_adc_port( AN0_TO_AN5); , but at the same time i just used only AN0 and AN1. Do I have to connect AN2, AN3, AN4 and AN5 pin to ground?
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Tue Oct 20, 2015 12:48 am     Reply with quote

_Every_ unused input, should always be connected to something.

On digital inputs, ground, or supply. Doesn't matter which. Floating digital inputs, increase the risk of spurious operation, and will increase power consumption a little. If the pin floats into the transition region of the gate for a prolonged period, it can actually reduce the life of the chip....
On analog inputs, floating ones will increase noise seen on the other inputs (they are clamped by the Vref, not the supply rails, and if a spike is clamped into this, it is seen on the other inputs).....

Remember gates contain diode structures. Electrons flowing through a pin will create current _somewhere_. Currents in unwanted parts of a chip, are a recipe for unexpected behaviour....
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Tue Oct 20, 2015 1:11 am     Reply with quote

Dear Ttelmah,

Thank you very much for your clear explanation.
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