View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
Inconsistent Analog output result |
Posted: Thu Oct 29, 2015 7:02 pm |
|
|
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
|
|
Posted: Thu Oct 29, 2015 8:21 pm |
|
|
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
|
|
Posted: Thu Oct 29, 2015 8:50 pm |
|
|
Hi,
I think the problem is
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
|
|
Posted: Thu Oct 29, 2015 9:42 pm |
|
|
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: 19557
|
|
Posted: Fri Oct 30, 2015 1:19 am |
|
|
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
|
|
Posted: Fri Oct 30, 2015 4:11 pm |
|
|
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: 9249 Location: Greensville,Ontario
|
|
Posted: Fri Oct 30, 2015 5:01 pm |
|
|
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
|
|
Posted: Fri Oct 30, 2015 7:34 pm |
|
|
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 );
|
|
|
|
|