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

Inconsistent Analog output result

 
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

Inconsistent Analog output result
PostPosted: Thu Oct 29, 2015 7:02 pm     Reply with quote

Hi,
I have problem with reading AN0 and AN1. I'm using TeraTerm the see the output result. The problem is output data shown on TeraTerm is not consistent as below.
Code:

2.517
2.517
2.517
2.51 7
2.517
2.517


And sometimes result come out:
Code:

2.517
2.517
2. 517
2.517


My code as below:
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)
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>

main()
   {
      char ch;
      unsigned char key;                   
      float  value, min;
   

         while(true)
            {
              ch=getc();

              if(ch=='Z')
                 {
                     while (key!=32)
                           {

                        setup_port_a( ALL_ANALOG );
                        setup_adc( ADC_CLOCK_INTERNAL );
                        set_adc_channel( 0 );
                        delay_us(50);

                             if(kbhit())
                              {key=getch();}

                                value = Read_ADC();
                                min=value*5/1023 ;
                                printf("\r%2.3f ",min);
                               
                           } key=0;
                 }


              if(ch=='z')
                 {
                     while (key!=32)
                           {

                        setup_port_a( ALL_ANALOG );
                        setup_adc( ADC_CLOCK_INTERNAL );
                        set_adc_channel( 1 );
                        delay_us(50);

                             if(kbhit())
                              {key=getch();}

                                value = Read_ADC();
                                min=value*5/1023 ;
                                printf("\r%2.3f ",min);
                               
                           } key=0;
                 }
           }
   }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 29, 2015 8:21 pm     Reply with quote

Trouble-shoot it. Cut out all the excess code and see if you still get
the problem. Example:
Code:

#include <18F4550.h>
#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()
{
float min;
   
min = 2.517;

while(TRUE)
  {
   printf("\r%2.3f ",min);
   delay_ms(500);
  }

}

And post your compiler version.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Thu Oct 29, 2015 8:50 pm     Reply with quote

Hi,
I think the problem is

Code:

while (key!=32)


Because (key!=32) is push spacebar. I have change it to (key!=13) which is 'ENTER' key. But the problem will become worst. What should i change it to?
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Oct 29, 2015 9:42 pm     Reply with quote

Code:

setup_port_a( ALL_ANALOG );
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );


move this block to the line just before your While{} loop in MAIN.
it only needs to execute ONCE - and then remove the later instance of same

you might also initialize 'key' to a value when you declare it.

it is poor practice to code a variable controlled loop but not set
a safe control value before it's first use.
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Fri Oct 30, 2015 1:19 am     Reply with quote

I'd suspect the problem has nothing to do with the PIC.....

In Teraterm, turn off 'local echo'. Currently what is happening, is the character you are typing, is getting echoed locally, so corrupts the data being displayed.

Separately, ADC_CLOCK_INTERNAL, is _not_ recommended for your PIC above 1MHz. This will degrade accuracy (especially as the code starts to do more), so should be corrected (read the data sheet).

Then repeat the mantra "when using the hardware UART, I must always have 'ERRORS' selected, unless I am handling errors on the UART myself".

A search on the forum will find lots about these, but the local echo is your current problem.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Fri Oct 30, 2015 4:11 pm     Reply with quote

Dear ttelmah,

You are correct... When i turn off local echo, this problem will not appear. However can you tell me what should i change ADC_CLOCK_INTERNAL to?
temtronic



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

View user's profile Send private message

PostPosted: Fri Oct 30, 2015 5:01 pm     Reply with quote

While the ADC section is common to a lot of PICs, you're best to look in the datasheet for your PIC, find the ADC section. In there will be a table or chart with the appropriate settings based on PIC speed (20MHz in your case). You may have 2 or 3 options but internal is NEVER one (unless you put the PIC to sleep and need an ADC reading), very, very uncommon....


Jay
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Fri Oct 30, 2015 7:34 pm     Reply with quote

I already check the pic18f4550 data sheet and 18F4550.h file. When used crystal 20MHz, need to used

Code:

 setup_adc( ADC_CLOCK_DIV_16 );

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