View previous topic :: View next topic |
Author |
Message |
hundil Guest
|
18F24J10 ADC does not read |
Posted: Tue Mar 13, 2007 6:24 am |
|
|
Hiii everybody !!
I have a code below to read an analog value on AN0 pin
But PIC18F24J10 does read the same value even when AN0 input is tied to GND.
The value read by PIC is about 770.
What is wrong in the code ?
Any help will be appreciated
hundil
CODE :
#include <18F24J10.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES H4_SW //High speed osc with SW enabled 4x PLL
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES PROTECT //Code protected from reads
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES PRIMARY //Primary clock is system clock when scs=00
#use delay(clock=10000000,RESTART_WDT,oscillator=10M)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
#bit GO_DOWN=0xfc2.1
long volt=0;
int1 on=0;
#int_AD
AD_isr()
{
set_adc_channel(0);
delay_us(20);
volt=read_adc;
if(on==1){
output_high(PIN_B1);
on=0;
}
else
{
output_low(PIN_B1);
on=1;
}
clear_interrupt(INT_AD);
disable_interrupts(INT_AD);
}
void main()
{
set_tris_a(0x05);
setup_adc_ports(AN0|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_32|ADC_TAD_MUL_12);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_AD);
enable_interrupts(GLOBAL);
setup_oscillator(False);
output_high(PIN_B7);
delay_ms(1000);
output_low(PIN_B7);
printf("\r\nSYSTEM");
do{
GO_DOWN=1;
delay_ms(100);
printf("\r\nV:%ld", volt);
enable_interrupts(INT_AD);
delay_ms(100);
}while(1);
} |
|
|
KaraMuraT
Joined: 16 May 2006 Posts: 65 Location: Ankara/Turkey
|
|
Posted: Tue Mar 13, 2007 6:56 am |
|
|
Hi,
I think you have misunderstood the INT_AD. INT_AD is triggered when the coversion finishes. you don't need this interrupt.
Also you're not getting the ADC value frequently. In fact you're never getting it. You're while(1) loop also must be revised.
add this line to your setup section (after main() started)
Code: | set_adc_channel(0); |
remove this interrupt section:
Code: | #int_AD
AD_isr()
{
set_adc_channel(0);
delay_us(20);
volt=read_adc;
if(on==1){
output_high(PIN_B1);
on=0;
}
else
{
output_low(PIN_B1);
on=1;
}
|
And then if your analog source is always powered and does not need a output from PIC make this revision to your code...
Code: | do{
GO_DOWN=1;
delay_ms(100);
printf("\r\nV:%ld", volt);
enable_interrupts(INT_AD);
delay_ms(100);
}while(1);
} |
remove the lines above and revise with these:
Code: |
while(1)
{
printf("\r\nV:0x%3LX", read_adc());
delay_ms(1000); /// most of the systems require just about 10-50 us,
/// 1 sec is OK for your eyes.
}
|
This code will generate values between 0x000 and 0x3FF I specifically stated "%3LX " for comparison. 0x000 means ground and 0x3FF means VDD. Rest is linear to this interval. Don't mix it with voltage values. You just have to measure your VDD first, and than multiply it with this result as :
Read Voltage = (Read value / 0x3FF) * Your VDD (Most probably 5 Volt)
If your range is out of VDD just use 2 resistors for Voltage dividing. Choose bigger values to keep the power consumption low.
We have to take care our world. isn't it? _________________ /// KMT
/// www.muratursavas.com |
|
|
hundil Guest
|
|
Posted: Thu Mar 15, 2007 4:35 am |
|
|
I have done what you recommend me in your forum message.
But the code is working on 18F452 & 16F877 but not on 18F24J10.
Have you ever tried such a ADC reading code on one of PIC18FXXJXX
best wishes
hundil |
|
|
KaraMuraT
Joined: 16 May 2006 Posts: 65 Location: Ankara/Turkey
|
|
Posted: Thu Mar 15, 2007 4:50 am |
|
|
I do not have any experience on 18F24J10 but I've looked at the datasheet. Unlike 18F452 and 16F877 , 18F24J10 is not pin compatible with them. In fact you have a 28 pin & 44 pin difference. AN0 input is Pin 2 on 28 pin 18F24J10. You should check your connections.
Rest should be the same. it's using same nanowatt ADC silicon engine with other nanowatt series. _________________ /// KMT
/// www.muratursavas.com |
|
|
w_heller Guest
|
18F24J10 ADC |
Posted: Thu Oct 18, 2007 12:08 pm |
|
|
I was having the same trouble and when I looked at the generated assembly code I found that the ADC calibrate bit (bit7 of 0xfc2 ADC0n1)was being set on at all times. I patched the problem by after the SET_ADC_CLOCK command I added a lin to clear that bit and got the ADC to work. This appears to be a bug in the compiler support of the 18F24J10 the earlier parts do not have a calibrate bit. |
|
|
|