|
|
View previous topic :: View next topic |
Author |
Message |
amdatlon
Joined: 07 Mar 2010 Posts: 10
|
PIC18F6722 , ADC configuration |
Posted: Sat Jul 09, 2011 3:14 pm |
|
|
Hello, please help
I want to measure the voltage of the ADC with PIC18f6722.
Included is the input A0.
But something wrong configuration of the PIC.
Simulation of Proteus, and gives a warning:
[PIC18 ADC] PC = 0x077E. Voltage references for ADC conversion yield a 0V range (V + = 0, V-= 0)
The result is:
V = 0
scheme:
http://img810.imageshack.us/img810/7855/71988142.jpg
Before that I tried to PIC18f458, and there was no problem. everything just worked.
But here something wrong, please help.
Thanks in advance!
Code: |
#include <18F6722.h>
#device PASS_STRINGS=IN_RAM
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOPUT
//#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
//#FUSES BBSIZ1K //1K words Boot Block size
#use delay(clock=20000000)
#use rs232(baud=15625,parity=N,xmit=PIN_C6, rcv=PIN_C7 ,bits=8,FORCE_SW /* INVERT*/,TIMEOUT=460/*360*/ )
#include "LCD.c"
//**************************************************
#byte pA =0xF80 //port a //
#byte pB =0xF81 //b //
#byte pC =0xF82 //c //
#byte pD =0xF83 //d //
#byte pE =0xF84 //port e //
#byte pF =0xF85 //port e //
#byte pG =0xF86 //port e //
// *********************************
#Define SetConfigPortA 1//100011 //33 100001
#Define SetConfigPortB 240//255 11110010
#Define SetConfigPortC 192//bx 00000011
#Define SetConfigPortD 0
#Define SetConfigPortE 0
#Define SetConfigPortF 0
#Define SetConfigPortG 0
Float
Volt;
float get_Voltage( )
{
Float Value;
Value= read_adc() ;
return Value;
}
void MAIN ( ){
set_tris_b(SetConfigPortB);
set_tris_a(SetConfigPortA);
set_tris_c(SetConfigPortC);
set_tris_d(SetConfigPortD);
set_tris_e(SetConfigPortE);
set_tris_f(SetConfigPortF);
set_tris_g(SetConfigPortG);
lcd_init();
setup_adc(ADC_CLOCK_INTERNAL); // Internal 2-6us
setup_adc_ports( AN0 );
set_adc_channel(0);
setup_comparator(NC_NC_NC_NC);
printf(lcd_putc,"Start..");
Delay_ms(300);
While(1)
{
Volt = get_Voltage();
printf(lcd_putc,"\fV=%3.2f " Volt );
Delay_ms(100);
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sun Jul 10, 2011 2:25 am |
|
|
Several things:
1) Is ADC_CLOCK_INTERNAL, recommended if your CPU clock is above 1MHz?.
2) Is the value returned by the ADC a float value?.
3) Remove the remark on the NOXINST fuse. This fuse is _required_.
4) Unless you need to use TRIS (are using fast_io mode), let the compiler handle this for you.
Now, look at the error message. Have you got Vdd being generated, and connected to your chip in your simulation?.
Best Wishes |
|
|
amdatlon
Joined: 07 Mar 2010 Posts: 10
|
|
Posted: Sun Jul 10, 2011 4:54 am |
|
|
Thank you for your guidance:)
Processor Clock is 5 mhz (20Mhz quartz crystal).
ADC returns an integer, but the function had other calculations, and this result is float.
I did the following:
in touch RA3 pin (VREF +) to +5 V
and changed the code to the following:
Code: |
setup_adc_ports( AN0 |VSS_VREF );
|
And the result was correct. Everything is perfect
If removed from the +5 V RA3, and placed in setup_adc_ports --- VSS_VDD,
Code: | setup_adc_ports( AN0 |VSS_VDD ); |
results, it is 0V (wrong).
But how to get chips to use an internal comparing voltage (+5 V), and not be connected externally to the RA3?
Since I am a beginner, make mistakes, sorry:)[/code] |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sun Jul 10, 2011 6:00 am |
|
|
Quote: | 1) Is ADC_CLOCK_INTERNAL, recommended if your CPU clock is above 1MHz?. Quote: | Processor Clock is 5 mhz (20Mhz quartz crystal). |
| So, what did you do??? Did you study Table 21-1 in the datasheet? And with that information, did you fix your code?
Extra info: when you study the datasheet you have to be careful which clock is referenced.
1) Fosc = external crystal == Processor frequency (20MHz in your project).
2) Fosc/4 == System cycle clock == internal clock = time for 1 instruction (5 MHz in your project)
amdatlon wrote: | But how to get chips to use an internal comparing voltage (+5 V), and not be connected externally to the RA3?
Since I am a beginner, make mistakes, sorry:) | It is not very difficult, but you have to read closely the given hints: Quote: | [PIC18 ADC] PC = 0x077E. Voltage references for ADC conversion yield a 0V range (V + = 0, V-= 0) | and: Quote: | Now, look at the error message. Have you got Vdd being generated, and connected to your chip in your simulation?. | I give you one additional hint: Always have the datasheet for your PIC processor ready for reading. In this situation you will have to take a look at Figure 21-1 showing the A/D block diagram. There are two pins not connected in your schematic... |
|
|
amdatlon
Joined: 07 Mar 2010 Posts: 10
|
|
Posted: Sun Jul 10, 2011 12:26 pm |
|
|
Sorry for bad English.
this table (20-1) I read it.
Well, to explain again.
I want to measure the analog signal port A0. I want to reference voltage is used internally to the CPU:
VREF-= 0V (GND)
and
VREF + = +5 V (Vdd)
Configured port:
Code: |
# Byte ADCON_0 = 0xFC2
# Byte ADCON_1 = 0xFC1
# Byte ADCON_2 = 0xFC0
# bit ADCON_1_VCFG1 = ADCON_1.5 // VREF -
# bit ADCON_1_VCFG0 = ADCON_1.4 // VREF+
......
.....
setup_adc(ADC_CLOCK_DIV_64);
setup_adc_ports (AN0);
ADCON_1_VCFG1 = 0; // VREF-= VSS
ADCON_1_VCFG0 = 0; // VREF + = VDD
set_adc_channel(0);
|
Correct?
because they did not report anything to the ADC ?!
If I use the following configuration
Code: |
ADCON_1_VCFG1 = 0; / / VREF-= VSS
ADCON_1_VCFG0 = 1; / / VREF + = 5V external power |
If you connect and RA3 (VREF +) at 5 V,
then ADC to operate normally.
But I do not want to reference an external voltage! |
|
|
amdatlon
Joined: 07 Mar 2010 Posts: 10
|
|
Posted: Sun Jul 10, 2011 2:24 pm |
|
|
Sorry is my mistake, I really have not seen out there VDD and VSS pins of the processor.
I thought it was the other PIC, and these pins are connected by default in Proteus.
Now everything works as I want to be:)
Thanks for the help:) |
|
|
|
|
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
|