View previous topic :: View next topic |
Author |
Message |
vasiliok
Joined: 17 May 2011 Posts: 19 Location: Kaunas, Lithuania
|
PIC18F45K22 ADC config problem |
Posted: Sat Jun 04, 2011 2:23 am |
|
|
Hi all!
I HAD a problem working with ADC.
Using version 4.120 of PCWHD.
Here is the code that is NOT working.
Compilator gives no error, ADC just not working:
Code: | setup_adc_ports(sAN18);
setup_adc(ADC_CLOCK_DIV_16|ADC_TAD_MUL_2);
...
...
while(TRUE){
value_adc = read_adc();
delay_us(40);
...
...
|
Then I RTFM :-) and wrote such a code for manual ADC configuration:
Code: | #byte ANSELA = 0xf38
#byte ANSELB = 0xf39
#byte ANSELC = 0xf3a
#byte ANSELD = 0xf3b
#byte ANSELE = 0xf3c
#byte ADCON0 = 0xfc2
#byte ADCON1 = 0xfc1
#byte ADCON2 = 0xfc0
ANSELC = 0b11110000; // c4 c5 c6 c7
ANSELD = 0b00001000; // d3
ADCON0 = 0b01001011; // sAN18, ADC=ON, Start conversion!
ADCON2 = 0b10001101 ; // Right Justify, 2 TAD, Fosc/16
...
...
while(TRUE){
value_adc = read_adc();
delay_us(40);
...
...
|
And this one is working perfectly!!!
What am I doing wrong? Or is it compilator config problem? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jun 05, 2011 3:30 pm |
|
|
I didn't have to do all the things you did to make it work. I only had to fix
the routine for set_adc_channel(). I replaced it with the macro shown
below. Now it works.
I connected the center tap of 5K trimpot (with each end connected to +5v
and GND) to pin C6 on the 18F45K22. I turned the trimpot from one from
end to other slowly, and I got this output, which is correct:
Quote: |
00 00 12 27 3f 53 60 72 80 ac dc ff ff ff
|
Code: |
#include <18F45K22.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=16M)
#use rs232(baud=9600, xmit=PIN_B1, INVERT)
#byte ADCON0 = 0xFC2
int8 temp;
#define set_adc_channel(x) \
temp = ADCON0 & 0x03; \
temp |= (x << 2); \
ADCON0 = temp;
//======================================
void main(void)
{
int8 value_adc;
setup_adc_ports(sAN18);
setup_adc(ADC_CLOCK_DIV_16);
set_adc_channel(18);
while(TRUE){
value_adc = read_adc();
printf("%x ", value_adc);
delay_ms(500);
}
} |
This bug with set_adc_channel() exists in versions 4.120 and 4.121.
I have emailed CCS just now with the details on this bug, so they
should fix it in the next version.
Last edited by PCM programmer on Sun Jun 05, 2011 3:59 pm; edited 1 time in total |
|
|
vasiliok
Joined: 17 May 2011 Posts: 19 Location: Kaunas, Lithuania
|
|
Posted: Sun Jun 05, 2011 3:58 pm |
|
|
Thanx for info, PCM programmer!
Interesting!
I hope the bug will be fixed. |
|
|
Michu
Joined: 05 Dec 2011 Posts: 3
|
|
Posted: Fri Mar 16, 2012 10:17 am |
|
|
Thanks... solved my problem with reading AN24 as well... |
|
|
|