Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC16F1786 ADC issue

Status
Not open for further replies.

bandi12

Newbie level 2
Joined
Oct 31, 2015
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
19
Hello. I am trying to measure voltage with this PIC, it has a 12 bit ADC because i got wrong voltages displayed i tried to display the ADC reading values and i got a surprise.

At 0V ADC is 64660 and goes up to 65510 meaning 2.1V from then suddenly drops to 0 and starts counting up to 1230 meaning 5V.

I used MikroC for PIC build in functions for ADC:

Code:
void main() {

C1ON_bit = 0;                       // Disable comparators
C2ON_bit = 0;

TRISA = 0x7;
ANSELA = 0x7;
InitLCD();
ADC_Init();

while(1) {

char txt[10];
unsigned int adcRead = ADC_Get_Sample(2);
WordToStrWithZeros(adcRead, txt);
LCD_Out(1,2,txt);
Delay_ms(500);
}

}
 

Hi,


Reference of your PIC ?
FOSC value ?
Where are LCD variables (config) init ?

better to put variable outside of the main !
Code:
char txt[10];
unsigned int adcRead;

void main() 
{
C1ON_bit = 0;                       // Disable comparators
C2ON_bit = 0;

TRISA = 0x7;
ANSELA = 0x7;
InitLCD();
ADC_Init();

while(1)
 {
    adcRead = ADC_Get_Sample(2); 
    WordToStrWithZeros(adcRead, txt);
    LCD_Out(1,2,txt);
   Delay_ms(500);
 }
}


try to use MCU register instead of MikroC ADC lib ...
 

I don't posted the LCDInit() it's the standard init and a hello message ( they are in a different .h file)

I don't touched the ADC registers, until now ( with 10bit ADC) mikroC's ADC_Init() did all the configs, but this time i think it has some problems.

I will try to read the datasheet and found out how to use the ADC from registers ( i don't really worked with registers until now, i used the already made functions of mikroC).
 

hello,

i don't have this PIC to test this program , but must be like this..
i suppose reading Chanel RA0 in single end mode.



//see table page 169 of datasheet DS40001637C
Code:
unsigned int Adc_measure;


void Init_ADC()
{
ADCON0.ADRMD=0;  // 12 bits mode
ADCON2.ADFM=1; // Result format 1= Right justified    
ADCON2.ACQT=1; // Acquisition time 7 = 20TAD 2 = 4TAD 1=2TAD    
ADCON2.ADCS=6; // Clock conversion bits 6= FOSC/64 2=FOSC/32    
 // Set external reference source    
ADCON1.ADPREF =0; //+ Vref = +Vdd    
 ADCON1.ADNREF = 0; // -Vref- =Vss    
 ADCON2.TRIGSEL = 0;  
//   negative is VSS  (single end measure) or select another chanel if differential
  ADCON2.CHS =0x0F; 
  ADCON0.ADON = 1; // Start ADC  
}  
 
unsigned int Get_ADC0()     // <- you can add channel as parameter !
{
unsigned int MSB,LSB;
unsigned int Result;
 ADCON0.GO=1; // And begin A/D conv.    
  while(ADCON0.DONE);    
  ADCON0.ADON = 0; // Stop ADC    
  MSB= ADRESH;
 LSB= ADRESL;   
 Result= MSB <<8 + LSB;
return Result;
}


void main()
{
.. init hardware ..
Init_ADC();
while(1)
{
Adc_measure=Get_ADC0();
.... display 
..
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top