CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

18F26K22 A/D example Please
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 13, 2011 3:07 pm     Reply with quote

This program should work with vs. 4.110:
Code:

#include <18F26K22.h>
#device adc=10
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

//======================================
void main(void)
{
int16 result;

setup_adc_ports(sAN0);
setup_adc(ADC_CLOCK_DIV_4);
set_adc_channel(0);
delay_us(10);

while(1)
  {
   result = read_adc();
   printf("%lx \n\r", result);
   delay_ms(500);
  }

}


I tested it with an 18F45K22 in hardware, with vs. 4.110, and it worked.
It displayed these values in the terminal window as I turned a trimpot
from one end to the other:
Quote:

0001
0001
0034
014a
01a7
023d
032d
03ae
03ff
03ff
bdrmachine



Joined: 24 Jan 2010
Posts: 14

View user's profile Send private message

PostPosted: Sun Feb 13, 2011 3:14 pm     Reply with quote

Thanks for the reply I'll test it out. In the mean time I have the program working using manual setting of registers and testing / toggling the GO/DONE bit.

#byte ANSELA = 0xf38
#byte ANSELB = 0xf39
#byte ANSELC = 0xf3a
#byte ANSELD = 0xf3b
#byte ANSELE = 0xf3c
#byte ADCON0 = 0xfc2
#byte ADCON1 = 0xfc1
#byte ADCON2 = 0xfc0

#byte CM1CON0 = 0xf78
#byte CM2CON0 = 0xf77



//setup_adc(ADC_CLOCK_INTERNAL);
//setup_adc_ports(sAN0|VSS_VDD);
ANSELA = 1;
ANSELB = 0;
ANSELC = 0;
ANSELD = 0;
ANSELE = 0;

ADCON0 = 1; //AN0 (only) selected and a/d enabled
ADCON1 = 0; //Vss & Vdd are reference
ADCON2 = 0x81; //Rigth justify,

CM1CON0 = 0;
CM2CON0 = 0;


Thanks Again
Brian
bdrmachine



Joined: 24 Jan 2010
Posts: 14

View user's profile Send private message

PostPosted: Sun Feb 13, 2011 7:47 pm     Reply with quote

PCM programmer

I tested the code with my hardware and I get the same results as before (ANSELA remains at POR value).

The manual register select code work as expected.

Thanks
Brian
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 13, 2011 8:53 pm     Reply with quote

The POR (power-on-reset) value of ANSEL is "all 1's" for the analog bits.
This means they are all configured as analog pins on power-up. I looked
at the .LST file for vs. 4.110 and it doesn't write to ANSELA (0xF38).
It leaves it in the power-on-reset state.
bdrmachine



Joined: 24 Jan 2010
Posts: 14

View user's profile Send private message

PostPosted: Mon Feb 14, 2011 8:01 am     Reply with quote

I thought [setup_adc_ports(sAN0|VSS_VDD);] would have set ANSEL to 0x01 but it doesn't. Armed with this knowledge all I should have to do is make sure I manually set ANSEL.

Thanks
Brian
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Mon Feb 14, 2011 10:01 am     Reply with quote

Glad you got it working. Some of those "features" can drive you nuts !

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
bdrmachine



Joined: 24 Jan 2010
Posts: 14

View user's profile Send private message

PostPosted: Mon Feb 14, 2011 10:31 am     Reply with quote

Only with all of your help!

Thanks
Brian
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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