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.

adc problem in pic16f877a

Status
Not open for further replies.

anjalimidhuna

Member level 3
Member level 3
Joined
Jan 4, 2014
Messages
62
Helped
2
Reputation
4
Reaction score
2
Trophy points
8
Location
india
Visit site
Activity points
358
HI ALL,

I am doing my first project in pic. i have successfully completed the display portion. but can't do with adc. it always shows the same value. i am using pic16f877a and HI TECH C compiler. i want to display the ac voltage.i have selected AN2 for this.
 

hello,

i want to display the ac voltage

what is the peak to peak value and frequency ?
Is the AC centered around zero volt or has a DC average value.
remenber ADC input is 0 to 5V .. so AC must be measured around +2,5V DC average
with AC amplitude of +-2,5V peak to peak .
 

HI paulfjujo,
thax for the rply.
i want to display the value of average ac from the mains ultimately(230V, 50 Hz). but now i just tried to display the instantaneous value which directly taken from the adc port pin. it always shows 9.
 

What is the ADC input signal voltage you have given (For getting the value 9 )?

it always shows 9 even the signal is off
this is the code i used


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/********************program to test adc functioning*********************/
#include<pic.h>
#include<htc.h>
#include<stdlib.h>
#include"lcdfn.h"
#include"ADC.h"
 
unsigned long value;
char *int_to_string(unsigned long int i, unsigned char buf[]);
void convert(unsigned long adc_value);
//MAIN STARTS HERE
void main()
{
    Lcd_Init();
    adc_init();
    LCD(Lcd_clear,ModCmd);
    Delay_ms(100);
    LCD(FstLin_FstColumn,ModCmd);
    while(1)
    {
    value=AdcRead(ch0);
    convert(value);
    }   
}
void convert(unsigned long adc_value)
{
unsigned int int_part,deci_part;
unsigned char strngIntPart[],strngDeciPart[];
unsigned char strngInt[4],strngDeci[4];
 
int_part=adc_value/100;
deci_part=adc_value%100;
 
int_to_string(int_part,strngIntPart);
int_to_string(deci_part,strngDeciPart);
 
Disp_STRING(strngIntPart);
LCD('.',ModData);
Disp_STRING(strngDeciPart);
}
 
char *int_to_string(unsigned long int i, unsigned char buf[])
{
    unsigned char temp;
    unsigned char s = 0,t = 0;
    while(i) 
    {
        buf[s++] = i % 10 + 0x30;
        i /= 10;
    }
    buf[s] = 0;
    s-=1;
    for(;t<s;t++,s--) 
    {
        temp = buf[s];
        buf[s]=buf[t];
        buf[t]=temp;
    }
    return buf;
}

 
Last edited by a moderator:

hello,

did you allready checked your "convert(value);" function..
with direct value assigned to the variable "value"..
to separate problems
ADC problem
or display problem
 

Post code of ADCRead() function.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*INITIALISE ADC*/
void adc_init()
{
    TRISA=0XFF;
    TRISE0=1;
    ADCON1=0X80;
}
 
/* READ ADC VALUES*/
unsigned char AdcRead()     
{
    ADCON0=0x55;
    while(GODONE==1);
    temp=((ADRESH<<8)+ADRESL);
    return temp;    
}

 
Last edited by a moderator:

hello


Code:
unsigned char AdcRead()	
{
ADCON0=0x55;
while(GODONE==1);
temp=((ADRESH<<8)+ADRESL);
return temp;	
}

return value must be integer 16 bits not unsigned char
Where is defined temp ?
in the main you use a long int to receive result of AdcRead..
so
Code:
[B]unsigned int [/B]AdcRead()	
{
[B]unsigned int temp;[/B]
ADCON0=0x55;
while(GODONE==1);
temp=((ADRESH<<8)+ADRESL);
return temp;	
}
 

hello


Code:
unsigned char AdcRead()	
{
ADCON0=0x55;
while(GODONE==1);
temp=((ADRESH<<8)+ADRESL);
return temp;	
}

return value must be integer 16 bits not unsigned char
Where is defined temp ?
in the main you use a long int to receive result of AdcRead..
so
Code:
[B]unsigned int [/B]AdcRead()	
{
[B]unsigned int temp;[/B]
ADCON0=0x55;
while(GODONE==1);
temp=((ADRESH<<8)+ADRESL);
return temp;	
}

i have made the correction.but now it only shows '.' i think the problem is with my convert(value) function..
 

"9" means 9V or the hex code "1001" ? How many bit is the ADC ? If you have not properly terminate the ADC channel, you may get some noise. Try reading by grounding the ADC input.
 

Actually problem is with my code.. i got the correct result by using micro C using i built function. now i am trying to make it my own in mplab. but it doesn't work. :cry:
 

hello,


in your conv to string routine, you use an undefined table instead of a pointer of this table

first declare the table wich receive the chars

unsigned char buf[16];

and modify the function

Code:
char *int_to_string(unsigned long int i, unsigned char* buf)
{
    unsigned char temp;
    unsigned char s = 0,t = 0;
    while(i) 
    {
        buf[s++] = i % 10 + 0x30;
        i /= 10;
    }
 buf[s] = 0;
    s-=1;
    for(;t<s;t++,s--) 
    {
        temp = buf[s];
        buf[s]=buf[t];
        buf[t]=temp;
    }
    return buf;
}

it's works !!! tested with C18 MPLAB


Code:
Value=513;int_to_string(Value, buf);
 k=fprintf(_H_USART,(const rom unsigned char *)"ADC= %ld    %s\r\n",Value,buf); 
 
Value=1023;
int_to_string(Value, buf);
 k=fprintf(_H_USART,(const rom unsigned char *)"ADC= %ld    %s\r\n",Value,buf); 


Value=Mesure_ADC(0);
int_to_string(Value, buf);
 k=fprintf(_H_USART,(const rom unsigned char *)"ADC= %ld    %s\r\n",Value,buf); 
 CRLF()

gives this result on terminal

ADC= 513 513
ADC= 1023 1023
ADC= 224 224

int_to_string(Value, buf); is enough to get the result ...
Why using long for value , result of ADC measure... maximum is 1023
integer size is enough.


or rename function as

long_to_string(Value,buf);
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top