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 CCS Technical Support

18F6527 Internal A/D converters

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
bill conley
Guest







18F6527 Internal A/D converters
PostPosted: Wed Nov 15, 2006 8:27 pm     Reply with quote

For the 1st time ever, I'm having trouble reading the internal A/D converter of a PIC??
- I'm using the 18F6527 @ 10Mhz
- #device ADC=10
- setup_adc_ports(ALL_ANALOG);
- setup_adc (ADC_CLOCK_INTERNAL);
- All chanels return 1024?????

Code looks something like this;
set_adc_channel(0);
delay_us(100);
temp_ad = Read_ADC(); //
delay_us(200);
fprintf((COMM_PORT1),"+3Vdc: %lu\r",temp_ad);

set_adc_channel(1);
delay_us(100);
_dvdc_ = Read_ADC(); //
delay_us(200);


Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 16, 2006 2:02 pm     Reply with quote

Post your version of the compiler.
bill conley
Guest







18F6527 Internal A/D converters
PostPosted: Sun Nov 19, 2006 4:43 pm     Reply with quote

Sorry...it's 3.249
Guest








PostPosted: Sun Nov 19, 2006 6:48 pm     Reply with quote

Did you check the NOLVP fuse? Laughing
bill conley
Guest







18F6527 A/D Problem
PostPosted: Sun Nov 19, 2006 7:20 pm     Reply with quote

Oh Gee, I wonder who this is???

- #fuses NOLVP,HS,PROTECT,NOWDT,PUT,BROWNOUT//,BORV45
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 19, 2006 10:39 pm     Reply with quote

Did you declare 'temp_ad' as an int16 ? You need to do that.

If that's not the problem, then you should post a test program.
Copy and paste it in, from MPLAB. In other words, post a program
similar to what you posted above, but not code fragments. It should
be complete.
bill conley
Guest







PostPosted: Mon Nov 20, 2006 10:33 am     Reply with quote

Not int16 but long int, I think that's the same?



//read internal A/D on channel 0, (voltage input into controller)
void check_vin()
{
long int temp_ad = 0;
byte i;

/*
set_adc_channel(_3VDC_REF);
delay_us(100);
_3vdc_ = read_ADC(); //
delay_us(200);

set_adc_channel(_5VDC_);
delay_us(100);
_dvdc_ = read_ADC(); //
delay_us(200);

set_adc_channel(_5VDC_REF);
delay_us(100);
_5vref_ = read_ADC(); //
delay_us(200);

set_adc_channel(_48VDC_PWS_MAIN);
delay_us(100);
main_pws_input = read_ADC(); //
delay_us(200);

set_adc_channel(PPD_DETECT);
delay_us(100);
_vpos_ = read_ADC(); //
delay_us(200);


set_adc_channel(_12VDC_PWS_OPAMPS);
delay_us(100);
_12vdc_ = read_ADC(); //
delay_us(200);
*/

set_adc_channel(0);
delay_us(100);
temp_ad = Read_ADC(); //
delay_us(200);
//fprintf((COMM_PORT1),"+3Vdc: %lu\r",temp_ad);

set_adc_channel(1);
delay_us(100);
_dvdc_ = Read_ADC(); //
delay_us(200);

set_adc_channel(2);
delay_us(100);
_5vref_ = read_ADC(); //
delay_us(200);

set_adc_channel(3);
delay_us(100);
main_pws_input = read_ADC(); //
delay_us(200);

set_adc_channel(4);
delay_us(100);
_vpos_ = read_ADC(); //
delay_us(200);


set_adc_channel(5);
delay_us(100);
_12vdc_ = read_ADC(); //
delay_us(200);


//for(i=0;i < 10 ;i++)
//{
// temp_ad +=Read_ADC();
//}
//_3vdc_ = ( (long int)(temp_ad/10)); //
}



main()
{
byte l_hr,l_min,l_sec,l_day,l_mo,l_yr;
byte remote_page_num = 0;
byte i;
long int long_i;

long int ad_1 = 0;
long int ad_2 = 0;
long int ad_3 = 0;
long int ad_4 = 0;
float num_1 = 0;
float num_2 = 0;
float num_3 = 0;
float num_4 = 0;


char Received; //Added for 2nd UART
char Received2;
int rxflag=0;

//EEPROM_ADDRESS address;
//init_ext_eeprom(); //init e2rom
enable_interrupts(int_rda);
enable_interrupts(INT_RDA2);

int_count=INTS_PER_SECOND;
set_rtcc(0);
setup_counters (RTCC_INTERNAL, (RTCC_DIV_256 | RTCC_8_BIT));
setup_wdt(wdt_off);
enable_interrupts (INT_RTCC);
enable_interrupts(GLOBAL);

disable_interrupts (int_ext);

enable_interrupts (int_ccp3);
setup_ccp3(CCP_CAPTURE_RE); // Configure CCP2 to capture fall

enable_interrupts (int_ccp5);
setup_ccp5(CCP_CAPTURE_RE); // Configure CCP2 to capture fall

setup_psp(psp_disabled);
setup_adc_ports(ALL_ANALOG);
setup_adc (ADC_CLOCK_INTERNAL);


//setup_adc_ports( ALL_ANALOG );
port_b_pullups(TRUE);


set_tris_c(0xBF); // Set pin C6 as an output
set_tris_g(0xFD); // Set pin G1 as an output


do
{
check_vin();
} while(TRUE);
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Nov 20, 2006 12:30 pm     Reply with quote

Your test program is too complicated. Try this simple program
and see if it works.
Code:

#include <18F6527.h>
#device adc=10
#fuses HS, NOWDT, PUT ,BROWNOUT, NOLVP
#use delay(clock=10000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

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

setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(20);

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

}
Ttelmah
Guest







PostPosted: Mon Nov 20, 2006 3:30 pm     Reply with quote

You have a huge amount of stuff in your code, that has been remarked out. Remove this, and repost, _showing the declarations of the variables used to hold the ADC valuea_. Also use the 'code' buttons to post.
Also, consider using an array, or struture to allow the whole thing to be done with one loop. Something like:
Code:

structure values {
    int16 threeDC;
    int16 fiveVDC;
    int16 fiveVRef;
    int16 fortyeightVDC;
    int16 PPDDetect;
    int16 OpAmpPwr;
}

union {
    int16 readings[6];
    stucture values ADC;
} VIN;

void read_volts(void) {
    int8 ctr;
    for (ctr=0;ctr<7;ctr++) {
       set_adc_channel(ctr);
       delay_us(20);
       VIN.readings[ctr]=read_adc();
    }
}

//Then in your main:
while (true) {
    read_volts();
    printf("Three V line is at %d\n\r",VIN.ADC.threeDC);
    delay_ms(500);
}

Just showing how this would work for one value (but reading six).
A lot smaller, simpler to read etc. etc..

Best Wishes
bill conley
Guest







PostPosted: Mon Nov 20, 2006 9:17 pm     Reply with quote

Thanks Guys...I think I have it working. It wasn't the firmware, it was the hardware.

-
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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