ADC(8-bit) problem in PIC Microcontroller 16F72

Status
Not open for further replies.

ankit-max

Junior Member level 1
Joined
Dec 20, 2012
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,402
I have a PIC 16F72 Micro-controller. I have used VDD as PIC 5v and gnd (0 V) from the pic. The port pin A.0 is used as an analog pin to give analog voltage using a potentiometer. Circuit diagram is shown as below:


Using Proton as a compiler. Setting for ADC:

Declare Adin_Res 8
Declare Adin_Tad 32_FOSC
Declare Adin_Stime 50

ADCON1 = %00000010
TRISA = %00000111
OUTPUT PORTB.7 'LED connected
Low PORTB.7

I have connected a led to PORTB.7 which switches to ON state when voltage at PORTA.0 is greater than 3.5V otherwise stays OFF. The problem I am getting is when I start the PIC led blinks on and off now when I change the voltage through dimmer, it gets OFF and when I increase the voltage through dimmer, it still stays OFF even when the analog voltage to the pin A.0 is greater than 3.5v. For sometime I thought it may be the ground problem with ADC. But, I am kind of perplexed as to clear out what is going wrong. Please any help would be appreciated.

Code:
Device 16F72
Xtal 4
'Settings for ADC
Declare Adin_Res 8
Declare Adin_Tad 32_FOSC
Declare Adin_Stime 50

Output PORTB.7   'Led connected
Low PORTB.7
Dim x As Byte    'to store digital value
GoTo start
analog:
x=ADIn 0
DelayMS 10
Return

start:
x=0

lp:
GoSub analog
If x > 180 Then
High PORTB.7
ElseIf x < 180 Then
Low PORTB.7
EndIf
GoTo lp

Thanks.
 

Not sure about Basic, but if it was C you would need brackets around the expressions???

If (x > 180) Then
High PORTB.7
ElseIf (x < 180) Then
Low PORTB.7
EndIf
 

Well, no that is not the problem. In BASIC, there is no need to use brackets.
 

For TRSIB I used Output PORTB.7 and for TRISA I used TRISA = %00000111 though somehow it is deleted in the code. I will check it now if I have burned that code into the PIC.

And in the code setting for ADCON1 register is also missing. Let me check it then. Thank you!
 

Okay. I changed the code to this:

(For sensing 4V == digital value 204)
Code:
Device 16F72
Xtal 4

Declare Adin_Res 8
Declare Adin_Tad 32_FOSC
Declare Adin_Stime 50
ADCON1 = %00000010
TRISA = %00000111
TRISB = %00000000
'Output PORTB.7
Low PORTB.7
Dim x As Byte
GoTo start
analog:
x=ADIn 0
DelayMS 10
Return

start:
x=0
High PORTB.7
DelayMS 1000
Low PORTB.7
DelayMS 1000
High PORTB.7
DelayMS 1000
Low PORTB.7
lp:
GoSub analog
If x > 204 Then
Low PORTB.7
ElseIf x < 204 Then
High PORTB.7
EndIf
GoTo lp

But still the same ambiguity with the result. led keeps flashing even when the voltage at PORTA.0 is less than 4V or higher than that. It just keeps on flashing. Sigh!
 
Last edited:

What Basic Compiler are you using?

Ok. I got it. It's Proton Development Suite. I will try and let you know.

It is working fine. Yo adc is 8 bit. So, for 204 raw adc value input volts will be 4. See the simulation LED will be ON till vin is 4V (204) and it will be OFF if > 4 V (>204). You have to use Fosc/8.

Edit: Even if your pot position is in the center you get 20V. I think your adc is already damaged by giving high voltage.
 

Attachments

  • pds adc.rar
    37.3 KB · Views: 67
  • pd.jpg
    126.3 KB · Views: 84
Last edited:


Why should I use Fosc/8 instead of Fosc/32? Hmm, adc might be damaged. I will use another microcontroller and get back to you how it goes.

Thank you!
 

how did you came up with 180 is equal to 3.5V and 204 is equal to 4V? You assume you are using 8-bit, but it should be 10-bit since that is how you initialized the A/D module.

But i think that is the least of your problem.

First and foremost, your dimmer circuit is shaky. Make sure you limit input voltage to +5V.

That means you have a good divider circuit to limit the maximum 40V to +5V.

Doing that with potentiometer ONLY without another series resistor could potentially damage your chip if the pot is incorrectly adjusted.
 

The ADC is of 8-bit and so for 5 V = 255. I have some query regarding the setting of voltage reference for ADC. I have used a POT between voltage 40 V to make it equal to 5 V, which I have passed through a Voltage follower, and to protect I have used two diodes - one connected to ground with a series resistor. Then this input is given to PortA.0 pin. My question is Voltage reference of ADC is to use VDD, so it's using VDD of microcontroller, does it means the input voltage I am giving to the port pin A.0 should be in reference to that VDD, but whereas that input to the microcontroller is coming from the POT.

Ex: Say I power up PIC with a 9v Battery and I use 7805 regulator IC to give VDD(5V) to pic as a supply - ground being same. Now, I take a pot. I connect one send of pot to the +9V and the other end to the GND. And I give middle terminal of pot to the PortA.0 (Keeping 9v = 5v using divider), and vary the 9v voltage so I would get variation on PortA.0. So, would it give the correct result as ADC is using VDD as reference voltage which is supply of the PIC?

I will appreciate the answers. Thank you!
and other connected to +5V so voltage doesn't exceed 5v to the pin A.0
 

Ah yes you are right, it is 8-bit since your cleared the ADFM bit in your code.

You are also correct in your interpretation of the hardware.
 

Please answer me the query of the question I asked - so that I can nullify out this problem, if there is so.
 

You are already understanding it correctly. Its just a matter of experimenting on your part.

Also it would be nice if you upload a better, more accurate schematic of your hardware.
 


ADC input voltage is different from ADC reference voltage. Vref+ and Vref- pins are for reference. Connect vref- to gnd and vref+ to 5V and now you can give 0-5V adc input to adc input pins. If you adc is 8 bit you will get 255 as the max value for 5V. Why don't you post your circuit?
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…