Jump to content

Problem With 16f616 Atod Reading Data


Recommended Posts

I'm having trouble getting the 16F616 to do a conversion on any ADC pin but RA0. Using the SourceBoost C compiler under MPLAB 8.10. With ANSEL = 1, TRISA = 1, adcon0.CHS0 = 0, and channel 0 selected, ADC works fine and writes ADC data to port c. With any other ADC input selected, and appropriate registers modified, the 'adc_measure( n )' function does not write the appropriate ADC value to port c.

 

See code below. Int Osc set to 4 MHz, all other features disabled except MCLR. Programmed using ICD2.

 

/*

16F616_analog_in.c */

 

//CODE to test 16F616 analog input functionality

// Read an analog in channel and power LEDs as a function of value

 

//#pragma DATA _CONFIG, _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF

// Set configuration fuses.

// Interrupt vector 0x004

 

#pragma CLOCK_FREQ 4000000

 

#include <pic16f616.h>

#include <system.h>

//#include <icd2.h> // not needed with adapter board.

#include <adc.h>

 

 

 

void main()

{

ansel = 0x02; // Config all ADC pins to digital except RA1

trisa = 0x02; //configure all port A pins as output except RA1

trisc = 0; //configure all port C pins as output

 

//Initialize ADC

set_bit( adcon0, ADFM ); // AD result needs to be right justified

//set_bit( adcon0, ADON ); // ADC on & consuming power

clear_bit( adcon0, VCFG ); // Vref+ = Vdd

//set_bit( adcon1, PCFG2 ); // Vref- = Vss

set_bit( adcon1, ADCS1 ); // Select Tadc = Tosc / 32 (this depends on the Xtal here 10 MHz, should work up to 20 MHz)

set_bit( adcon0, CHS0 ); // if cleared, Channel 0 is ADC in (RA0, pin 13). If set, Channel 1 is ADC in (RA1, pin 12).

clear_bit( adcon0, CHS1 ); //

clear_bit( adcon0, CHS2 ); //

clear_bit( adcon0, CHS3 ); //

volatile bit adc_on @ ADCON0.ADON; //AC activate flag

adc_on = 1; // Activate AD module

 

//Every 250ms sample ADC, discard 4 low bits and output into port B

while( 1 )

{

portc = adc_measure( 1 ) >> 4; // Read RA1, shift right 4 places, send to Port C

delay_ms( 250 ); // wait

}

}

 

This code does NOT function.

 

What am I doing wrong?

 

JT

Link to post
Share on other sites

I think the code produced by adc_measure() may not be correct for this chip. adcon0 is AND'ed with 0xc7 and the channel number passed is shifted left 3 bits. This means that CHS0 is being ignored. The 16F616 has 4 channel selection bit so the mask should have been 0xc3 and the channel selection shifted 2 bits. CHS3 should be kept = 0.

 

The adc_measure does not work on PIC's that only have 8 bit adc, such as PIC16F716.

 

Cheers

 

Reynard

Link to post
Share on other sites
I think the code produced by adc_measure() may not be correct for this chip. adcon0 is AND'ed with 0xc7 and the channel number passed is shifted left 3 bits. This means that CHS0 is being ignored. The 16F616 has 4 channel selection bit so the mask should have been 0xc3 and the channel selection shifted 2 bits. CHS3 should be kept = 0.

 

The adc_measure does not work on PIC's that only have 8 bit adc, such as PIC16F716.

 

Cheers

 

Reynard

Reynard,

 

That was the problem! I redefined adc_measure() to AND ADCON0 with 0xC7 and shift "channel" left twice as you said, and the function now works on RA1.

 

Thanks for the help.

 

JT

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...