external voltage ref input on pic16f690 not working as expected.

Status
Not open for further replies.

Joop123

New Member
Ls,
I encountered a strange effect.
setting the pic to 4 ana in ana4-ana7 external vref.(2.5V)
Changing the voltage on the ref pin no effect on measured value ?? (ra1 pin18).

It is reading a inputs but instable.
a real war to get this to work I strapped dwn the program and left the ana parts.
I did something wrong but don’t know what. code attached..



Help will be appreciated.

regards,
Joop
 
Sorry,
code disapears however, here it is;


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <htc.h>
#include "usart.h"
#include <pic.h>
//==================
// pic 16f690
//==================
// mplab 8.760000
// pic c compiler(lite mode) V9.82
// problem
// external ref pin 18 RA1 pin18
// not reading reference value.
// test with 0, 1.25 , 2.5 and 4.90V pin 18 measured with fluk but printed result unchanged.(see below)
// What is wrong?


unsigned int Version =0;
unsigned int Rompointer;
unsigned int counter;

float Gemeten;
static void interrupt;
unsigned char ADCChannel=0;
unsigned int ADCRegisters[4]; // fill automaticly with AN4-AN7

static void interrupt
peripheral_interrupt(void)
{
int i;
if (ADIF)
{ // received int conversion is done
ADIF=0;
ADCRegisters[ADCChannel]=(((ADRESH <<8) + ADRESL));
}



if (T0IE && T0IF) //are TMR0 interrupts enabled
{
T0IF=0; //TMR0 interrupt flag
++counter;
if (counter == 1000){
counter=0;
while ( GO_DONE==1){} // wait on int conflict
for (i=0;i <=500;i++){}
ADCChannel++; // next channel
ADCChannel = (ADCChannel % 4); // wrap at 4
ADCON0&=0x1100011; // mask regsiter to clear use ref pin set (bit 6)
ADCON0|=(ADCChannel+4)<<2; // fill in new value counter from or after shift 0-3 channel 4-7

GO_DONE=1; // get it

}
}

} // ================= end of interrupt =============================



void main(void)
{

unsigned int i;
unsigned int p;
unsigned int q;


PCON=3;
ANS11 = 0;


TRISA= 0b00000010; // bit 1 input ref pin 18 = 2.5V
TRISB= 0b00000000;
TRISC= 0b00001111;
ANSEL =0b11110010; // make ana inputs (refpin) and ana4 to ana7 ana
ADCON1=0b00100000; // conv speed
VRCON=0;

ADON=1;
ADIE=1; // Enable ADC Ready interrupt
ADIF=0; // clr int flag (adc)

PEIE=1; // Enable peripheral int.
GIE=1; // Global int. enable

// RCIE=1; //com int
// SSPIE=0;
// CCP1IE=0;


TMR0 = 0;
OPTION_REG = 0B10001000;
T0IE = 1; //enable TMR0 overflow interrupts
GIE = 1; //enable Global interrupts

init_comms();

printf("\r start \n"); // test output




while (0==0) // loop
{
for (i=0;i <= 10000;i++){} // slow dow loop
for (i=0;i <=3;i++){
while ( GO_DONE==1){} // avoid conflict


// printf ("%f",xxx) not working!! workaround
//Gemeten=((float)ADCRegisters);
//Gemeten= (Gemeten*.0314807-100)/.391;
// printf(";%d,%d",i,Gemeten);
// q = (int) Gemeten;
// p = (int) ((Gemeten-q)*100);
// printf("%d.%d ", q, p);

//printed result
printf(";%d,%d ==> ",i,ADCRegisters); // produce adc value
}
printf("\r\n");
}
}
 

Attachments

  • main.c
    2.9 KB · Views: 130
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…