|
|
View previous topic :: View next topic |
Author |
Message |
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
Analog Conversion 18F4620 |
Posted: Fri May 02, 2008 8:29 am |
|
|
I have never had problems with analog conversion before. This is a fairly new board, good ground plane, 4.96vdc regulation. I am using 3 analog inputs. One is a temp sensor (0-5v out), one channel is a 10k pot and the 3rd channel is grounded for now. I keep getting full scale output on a channel that only has .75 volts on it. At the same time, the other channels increase in value, even the grounded input gives me a reading of 7-9. I have tried new chips but they all do the same. Here is the program. Thanks for looking. Compiler is 3.249.
Code: |
#include <18f4620.h>
#device ICD=TRUE
#device adc=10
#FUSES WDT //Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV21 //Brownout reset at 2.1V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOPBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#use delay(clock=20000000,RESTART_WDT)
//#include <bootloader.h>
#include <math.h>
#include <stdlib.h>
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#use fast_io(e)
#zero_ram
#use rs232(baud=9600,parity=N,xmit=pin_c6,rcv=pin_c7,bits=8,ERRORS)
int16 tempf,windt,windcnt,windir,raincnt,stotal,auxcnt,auxanalog;
#int_TIMER1
TIMER1_isr(){
set_timer1(15536);//.01 sec interrupts
windt++;
}
#int_EXT
EXT_isr(){
windcnt++;
}
#int_EXT1
EXT1_isr(){
raincnt++;
}
#int_EXT2
EXT2_isr(){
auxcnt++;
}
void main(){
//********** INITIALIZE CHIP **********
set_tris_a(0b00010111);
set_tris_b(0b00110111);
set_tris_c(0b10001100);
set_tris_d(0b11000000);
set_tris_e(0b00000000);
setup_adc_ports(AN0_TO_AN2|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_16|ADC_TAD_MUL_8);
//I have tried many settings above and am now guessing what the settings should be...
setup_wdt(WDT_ON);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_INTERNAL|T3_DIV_BY_1);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT1);
enable_interrupts(INT_EXT2);
enable_interrupts(GLOBAL);
while(1){
restart_wdt();
if (windt >= 300){
set_adc_channel(0);
delay_us(10);
windir = read_adc();
set_adc_channel(1);
delay_us(10);
tempf = read_adc();
set_adc_channel(2);
delay_us(10);
auxanalog = read_adc();
disable_interrupts(GLOBAL);
stotal = tempf + windcnt + windt + windir + raincnt + auxanalog + auxcnt;
//printf("$WTH,%04lu,%04lu,%04lu,%04lu,%04lu,%04lu,%04lu,%04lu\r\n",tempf,windcnt,windt,windir,raincnt,auxcnt,auxanalog,stotal);
printf("$WTH,%04lu,%04lu,%04lu\r\n",tempf,windir,auxanalog);//analog only
enable_interrupts(GLOBAL);
windt = 0;
windcnt = 0;
raincnt = 0;
auxcnt = 0;
}
}
}
|
Output of serial port:
Code: |
$WTH,0158,0013,0471
$WTH,0157,0002,0472
$WTH,0157,0000,0472
$WTH,0157,0000,0471
$WTH,1023,0016,0539
$WTH,1023,0008,0541
$WTH,1023,0009,0539
$WTH,0157,0000,0471
$WTH,0157,0000,0472
$WTH,0157,0000,0472
$WTH,1023,0011,0540
$WTH,1023,0010,0539
$WTH,1023,0009,0541
$WTH,0157,0000,0470
$WTH,0158,0000,0473
$WTH,0157,0004,0471
$WTH,0157,0000,0471
$WTH,1023,0010,0540
$WTH,1023,0008,0540
$WTH,1023,0010,0540
$WTH,0158,0000,0471
$WTH,0158,0000,0472
$WTH,0157,0000,0471
$WTH,1023,0009,0540
$WTH,1023,0009,0539
$WTH,1023,0010,0539
$WTH,0157,0000,0472 |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 02, 2008 11:05 am |
|
|
I would make a test program that simply reads and displays the 3 A/D
channels in a while() loop, with a short delay (1/2 sec) for the loop.
I would get rid of all the fast i/o and tris statements, also get rid of all
interrupts and timer statements. Disable the Watchdog timer. Make
the printf statement much more simple. Just use "%lu" for the format
string. Don't do any math on the results. Get rid of the ICD=TRUE
and run it as a standalone program. |
|
|
ljbeng
Joined: 10 Feb 2004 Posts: 205
|
|
Posted: Fri May 02, 2008 1:33 pm |
|
|
I did all you said, made it a very basic program with 1s delay between serial xmit. It does this now, at least it's a pattern....
Code: |
$WTH,157,278,603
$WTH,156,274,606
$WTH,157,276,525
$WTH,159,279,486
$WTH,157,274,565
$WTH,155,279,599
$WTH,156,276,564
$WTH,157,278,492
$WTH,1023,343,1023
$WTH,1023,346,1023
$WTH,1023,344,1023
$WTH,1023,347,1023
$WTH,1023,343,1023
$WTH,1023,325,1023
$WTH,157,275,620
$WTH,159,277,514
$WTH,156,282,504
$WTH,157,278,578
$WTH,157,280,606
$WTH,158,277,540
$WTH,159,274,492
$WTH,1023,343,1023
$WTH,1023,343,1023
$WTH,1023,343,1023
$WTH,1023,344,1023
$WTH,1023,347,1023
$WTH,1023,343,1023
$WTH,1023,343,1023
$WTH,157,275,563
$WTH,159,272,501
$WTH,157,278,513
$WTH,157,277,587
$WTH,157,274,614
$WTH,157,274,536
$WTH,162,274,493
$WTH,1023,347,1023
$WTH,1023,343,1023
$WTH,1023,342,1023
$WTH,1023,348,1023
$WTH,1023,349,1023
$WTH,1023,342,1023
$WTH,1023,344,1023
$WTH,158,274,536
$WTH,159,269,489
$WTH,157,278,536
$WTH,157,273,599
$WTH,157,273,591
$WTH,157,276,516
$WTH,158,280,485
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 02, 2008 1:53 pm |
|
|
Post the program. |
|
|
|
|
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
|