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.

PIC16f819 ADC problem

Status
Not open for further replies.

fuzedmass

Junior Member level 2
Joined
Apr 11, 2011
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,527
Hello people, hope that every body is fine.

I m using PIC16f819 ADC module for a pretty simple task, that is if the analog voltage is greater that 0.6volts on RA1 it should start toggling the RB7 pin with a delay of 1second.
My hardware seems to be fine, on default all the outputs are HIGH. LED is connected to RB7 and it glows when RB7 equals to 0v. The analog voltage coming on pin RA1 ground is common with micro-controller's ground.
I m using internal oscillator of 8mhz, and I think I have done the right settings in CONFIGURATION BITS settings.

The problem is I think I have configured everything right in both the hardware and software, but it never works. If I simply do toggling of led using a delays it works. That means controller and oscillator etc settings are right.

Please HELP me :)

CODE:


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <htc.h>
 
/************Pin Assignment**********/
#define photo_probe_in RA2
#define photo_probe_neg_in RA3
#define mid_red_led RA4 //out
#define relay_out3 RB0 //out
#define relay_out1 RB1 //out
#define relay_out2 RB2 //out
#define low_green_led RB3 //out
#define bottom_red_led RB4 //out
#define mid_green_led RB5 //out
#define top_green_led RB6 //out
#define TOP_RED_LED RB7 //out
#define push_button RA0
#define probe_pos_in RA1
/************Pin Assignment**********/
 
void my_configrations(void);
void my_1sec(void); //1sec delay ftn
 
unsigned int my_digi=0;
 
void main(void)
{
 
my_configrations();
    while(1)
    {
    my_highoutput();
    }
}
 
void my_configrations(void)
{
 
 
//setting Internal OSC to run at 8mhz
OSCCON = 0b01110100; 
//setting Internal OSC to run at 8mhz
 
//configuring I/O
TRISA0=1; //configure as INPUT
TRISA1=1; //configure as INPUT
TRISA2=1; //configure as INPUT
TRISA3=1; //configure as INPUT
 
TRISA4=0; //configure as OUTPUT
TRISB0=0; //configure as OUTPUT
TRISB1=0; //configure as OUTPUT
TRISB2=0; //configure as OUTPUT
TRISB3=0; //configure as OUTPUT
TRISB4=0; //configure as OUTPUT
TRISB5=0; //configure as OUTPUT
TRISB6=0; //configure as OUTPUT
TRISB7=0; //configure as OUTPUT
 
RA4=1; //on default its HIGH
RB0=1; //on default its HIGH
RB1=1; //on default its HIGH
RB2=1; //on default its HIGH
RB3=1; //on default its HIGH
RB4=1; //on default its HIGH
RB5=1; //on default its HIGH
RB6=1; //on default its HIGH
RB7=1; //on default its HIGH
 
//configuring I/O
 
 
//ADC
ADCS2=0; //AD clk div by 2 disabled
ADFM=1; //right justified
PCFG3=0;PCFG2=0;PCFG1=0;PCFG0=0; // AN0 to AN4 is analog and AVdd and AVss and refrence
ADCS1=0;ADCS0=1; //FOSC/8
//ADIE=1; //enable ADC interrupt
//GIE=1;
//ADC
 
 
}
 
void my_highoutput(void)
{
unsigned int high_temp=0,x=0;
//have to choose AN1 for this purpose
CHS2=0;CHS1=0;CHS0=1; //select AN1
 
for(x=0;x<10;x++)
{ //average
 
my_1msec();
GODONE=1;//start ADC module
while(GODONE);//wait till it completes the conversion
my_digi=0;
//my_digi=ADRESL;//only using the lower register as the analog voltages are small
 
my_digi=ADRESH; 
my_digi=my_digi<<8;   //shift to left for 8 bit 
my_digi=my_digi|ADRESL; //10 bit result from ADC 
high_temp=high_temp+my_digi;
} //average
 
high_temp=high_temp/10;
 
    if(high_temp > 122)
    {
    TOP_RED_LED=!TOP_RED_LED;
    my_1sec();
    }
 
}
 
 
void my_1msec(void)
{
    unsigned int x=0,y=0;
    for(x=0;x<30;x++)
    {
        for(y=0;y<5;y++);
    }
}
 
 
void my_1sec(void)
{
    unsigned int x=0,y=0;
    for(x=0;x<1773;x++)
    {
        for(y=0;y<102;y++);
    }
}





HERE IS WHOLE CODE ... PLEASE HELP ME ...

THANKS

---------- Post added at 16:20 ---------- Previous post was at 16:19 ----------

I give 2.5Volts usually on RA1 but nothing happens, LED stays OFF (means getting 5volts all the time)
 
Last edited by a moderator:

my_digi=ADRESH;
my_digi=my_digi<<8; //shift to left for 8 bit
my_digi=my_digi|ADRESL; //10 bit result from ADC
replace the above with this
my_digi=ADRESH;
my_digi=my_digi<<2; //shift to left for 2 bit
my_digi=my_digi|(ADRESL >> 6); //right shift ADRESL for 6 places, because of the MSB 2bits only contain data
i think it may help you
 

hello Rafeeq, thanks for replying . But i DOnt think that there is a problem in doing in my way ???

---------- Post added at 08:50 ---------- Previous post was at 08:41 ----------

Rafeeq, I simulated your way and actually its wrong. I mean the logic is all wrong.

I have simulated my code giving ADRESL and ADRESH values, whether my logic was right or not, and it was right.

More Help Guys !
 

Help me, please !

---------- Post added at 17:44 ---------- Previous post was at 16:09 ----------

I have done simulation in proteus too and there is also problem, means something wrong with ADC ...

I have set different ADC clocks to like Fosc/2, FRC , FOSC/4 etc ... no result :-(
 

if you are using internal osc. then i think change the ADCS1=0;ADCS0=1; bits to internal RC osc. setting
 

if you are using internal osc. then i think change the ADCS1=0;ADCS0=1; bits to internal RC osc. setting

hello Zohaib, you are wrong, it doesnt work either, actually by doing your setting, the ADC TAD settings get less than the recommended one, so your way is not right ...

Doing the simulations in Proteus, I have found that the program get stuck at WHILE(GODONE==1) ....

any thing ?
 

I think the problem is
GODONE=1;//start ADC module
while(GODONE==1);//wait till it completes the conversion

I m giving 5v in proteus on analog pin, but LED is not toggling, I have toggled LED in proteus with simple delay and worked ...

i have used polling technique on ADIF too but didnt work either ...

whats the thing I m missing ?
 

place some delay after reading the values say 50 microseconds
 

I have one thing to share here-

I really don't know htc.h takes care of config registor or not but I think config is missing here -

typedef unsigned int config;
config __at 0x2007 __CONFIG = _INTRC_OSC_NOCLKOUT & _PWRTE_ON & _BODEN_OFF & _WDT_OFF &_CP_OFF;

I think you need to add this to your code after
#include <htc.h>
Also you may need to add header file of PIC16f819
#include<PIC16F819.h>


Also pease refer the link below for more C code on PIC -

https://www.microchipc.com/sourcecode/#interface

Good Luck
 

place some delay after reading the values say 50 microseconds

I'll do it, but I dont think thats the problem ...

I told you already, that is never comes to execute any other statement that is written after WHILE(GODONE==1);

---------- Post added at 17:42 ---------- Previous post was at 17:41 ----------

I have one thing to share here-

I really don't know htc.h takes care of config registor or not but I think config is missing here -

typedef unsigned int config;
config __at 0x2007 __CONFIG = _INTRC_OSC_NOCLKOUT & _PWRTE_ON & _BODEN_OFF & _WDT_OFF &_CP_OFF;

I think you need to add this to your code after
#include <htc.h>
Also you may need to add header file of PIC16f819
#include<PIC16F819.h>


Also pease refer the link below for more C code on PIC -

https://www.microchipc.com/sourcecode/#interface

Good Luck

thanks Milind for replying, i m configuring the CONFIG BITS in MPLAB myself, so I dont think thats the problem ...

including HTC.C includes PIC16f819 ...

:-( HELP ME

---------- Post added at 17:43 ---------- Previous post was at 17:42 ----------

here is the one line in PIC16f81x.h header file:

#warning Header file pic16f81x.h included directly. Use #include <htc.h> instead.

I think it means that including HTC.H includes 16f819 files too

---------- Post added at 17:49 ---------- Previous post was at 17:43 ----------

like now I m writing this way:

GODONE=1;//start ADC module
while(GODONE);//wait till it completes the conversion
TOP_RED_LED=!TOP_RED_LED;// toggle anyway

now according to this, it should toggle all the time ... as now I m just using ADC module for no purpose, but on the PROTEUS simulation, it never toggles, however if I toggle it directly through DELAYS the LED does toggle ...

so HELP ?
 

my settings 9.jpg
 

updated my HITEC c compiler from version 9.60 to 9.80, but problem still persist
 

may i give you the working hex code with all your requirement ?
 

ok, i'll send you tomorrow, today i am in a remote location.
 

I switched to CSS C compiler and used their built in function of ADC and it worked, so what am I missing in HITEC C compiler ? :-(
 

I am still stucked with using HITEC C compiler, HELP ME PLEASE
 

In myview ..... I think you are missing ADON=1....try this ......

ADON =0;
DelayMs(10);
ADON =1;
DelayMs(10);
ADIE =1;

In case of Hightech compiler you need to declear this mostly ......and I think CCS takes cares of it internally ..... However I never used any of these compliers I uses SDCC compiler with gputil linker for generating the code

Good Luck
 

no it doesnt work like that, 2ndly I want to use GODONE for polling not ADON bits ...
 

What I am trying to tell is in your my_configrations function try to add the part -

ADON =0;
my_1msec(10);
ADON =1;
my_1msec(10);

Bcz to work with ADC in PIC you need to make A to D convertor section ON .....

I had developed ADC in PIC16F877a with GODONE method only ......

but what I was doing there is Programming ADCON0 and ADCON1 registors like this -
ADCON1 = 0b11111100;
ADCON0 = 0b10001001;

then use this -

ADON =0;
DelayMs(10);
ADON =1;
DelayMs(10);
ADIE =1;

DelayMs(10);

GO_DONE = 1;
//DelayMs(10);
while(GO_DONE)
{
continue;
}
DelayMs(10);
value =ADRESL;
value +=(ADRESH<<8);
dec_value =value;
DelayMs(5);

value =0;
DelayMs(10);

GO_DONE = 1;
//DelayMs(10);
while(GO_DONE)
{
continue;
}
DelayMs(10);
value =ADRESL;
value +=(ADRESH<<8);
dec_value +=value;
DelayMs(5);

This was working for me .......

I hope this will solve your problem in Hitech C complier....

Good Luck
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top