CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Help with reading analog value
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Help with reading analog value
PostPosted: Thu Oct 13, 2005 5:26 pm     Reply with quote

Hi guys, I'm very new to microchips and am still trying to figure out what all they can do and how to make them do it. I've tried several scripts to try and get the following configuration on the chip with no luck. I'm using a f628A chip and I'm wanting A0 and A1 to be able to read a voltage value (i'm not wanting to compare the two or to determine if the voltage is higher or lower than a defined voltage, I just want it to be able to read that X.XX volts are being applied to pin A0 or A1). Thus i've used the set_tris_A(0X03); command to set pins A0 and A1 to inputs and all others to outputs. I've done a set_tris_B(0x00) so that all of port B are outputs and i've got a wire comming out of pin B1 going thru a series of buttons with resistors and then returning to A0. I'm expecting that when a button is pressed the resulting voltage that will be applied to A0 should be able to be read and then displayed on my LCD. I have the LCD working just fine but can not seem to get pin A0 to read a voltage like i'm wanting. Any help would be much appreciated. Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 13, 2005 5:42 pm     Reply with quote

To read an analog voltage level, it's easier if you use an A/D converter.
The 16F628A doesn't have one.

It does have a comparator, so you could step through the Vref voltages
and test the input each time, and eventually discover the input voltage,
within a certain range.

If you just want to experiment, then get a PIC that has an A/D.
The 16F88 has got one, and it's the same package size. It has
roughly the same pinout as the 16F628A.
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Thu Oct 13, 2005 5:43 pm     Reply with quote

There are plenty of experts that will be able to help you. If you give them more info they will ofer all sorts of help.

Give them:
What development board you are using (if it's a commercial board)
What compiler version
What code you've tried so far

Also, there is a post with forum policy that suggest a minimum amount of info to provide:

http://www.ccsinfo.com/forum/viewtopic.php?t=17013 (item 4)

That should get you going,

Good luck,

John


EDIT:

PCM beat me to it....
Guest








PostPosted: Thu Oct 13, 2005 8:56 pm     Reply with quote

Ok well i'm using PCM C Compiler, Version 3.190 as for hardware i'm just using a wirewrap board that I have set up here. Nothing special just power supplied to chip along with ground. A 32.768 Khz crystal and 50pf caps connected to OSC1 and 2. I am trying to set up a 6 button keypad that will use 3 wires total. I'm doing this using the schematic shown in figure 12-1 http://ww1.microchip.com/downloads/en/DeviceDoc/41261a.pdf on this page. I"m only using RB1 and AN0 and AN1 since I only need 6 keys. I'm trying to figure out tho how to get the f628A chip to correctly determine which key was pressed. I guess i'm not sure how to configure the comparator to work correctly and if I'm going to have to rewire slightly in order for it to work. Again i'm still very new to microchips and electronics so any example or pointers in the right direction would be greatly appreciated. Thanks again!
Guest








PostPosted: Thu Oct 13, 2005 8:57 pm     Reply with quote

Ok well i'm using PCM C Compiler, Version 3.190 as for hardware i'm just using a wirewrap board that I have set up here. Nothing special just power supplied to chip along with ground. A 32.768 Khz crystal and 50pf caps connected to OSC1 and 2. I am trying to set up a 6 button keypad that will use 3 wires total. I'm doing this using the schematic shown in figure 12-1 http://ww1.microchip.com/downloads/en/DeviceDoc/41261a.pdf on this page. I"m only using RB1 and AN0 and AN1 since I only need 6 keys. I'm trying to figure out tho how to get the f628A chip to correctly determine which key was pressed. I guess i'm not sure how to configure the comparator to work correctly and if I'm going to have to rewire slightly in order for it to work. Again i'm still very new to microchips and electronics so any example or pointers in the right direction would be greatly appreciated. Thanks again!
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Fri Oct 14, 2005 8:31 am     Reply with quote

This is interesting.....

Did you catch the part in PCM's post where he mentions that the part you are using doesn't have an ADC...only a comparator? I went and briefly looked at the datasheet and saw AN0 and AN1 on Port A and immediately assumed ADC also. (even began to ask PCM about it.... but after many misguided posts I decided to dig deeper.) I learned that if you look deeper in the datasheet you'll see that those are indeed only comparators. And they will not work for what you are attempting.... at least not in the simple way described in the "Tips and Tricks".

A big part of the experts being able to help after you get an appropriate part will be a simple program. Write only what you need to demonstrate the application and the problem. Post it, ensuring that you include everything... #fuses, etc. And use the "Code" feature when you do.

The info that you are wire-wrapping will be usefull when the whole picture comes together.

Good luck,

John

EDIT:

ps - Sign up and get an account so you can log in. You'll be able to edit and remove double posts, etc. It also shows that you're serious about the CCS compiler and you're not just "drive by" posting..... Smile
Guest








PostPosted: Fri Oct 14, 2005 9:23 am     Reply with quote

so if I understand you correctly your saying that with the 16f628A chip there is no way to read a voltage on pin A0 or A1.
I'm trying to figure out what PCM ment when he said
Quote:
It does have a comparator, so you could step through the Vref voltages
and test the input each time, and eventually discover the input voltage,
within a certain range.


I'm just not sure how to use the comparator in order to do this. Also correct me if i'm wrong but after looking at the datasheet for my chip it appears that I would have to use RA3 and RA4 (since these are the cmp1 and cmp2 pins).[/code]
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Fri Oct 14, 2005 9:45 am     Reply with quote

(Experts, correct me if I'm wrong)

Correct, the 628 part only has comparators and not analog to digital converters (ADCs). The schema in the "Tips and Tricks" guide relies on ADCs.

Comparator: compares two voltages

ADC: converts a voltage to a digital value (which allows for the simple and quick resolution of which button has been pressed in your application)

PCM suggested a work around for using comparators, but it would probably more complicated than what you want right now. I would suggest a part that has ADC capability. Which begs the question... are you stuck on the 16F628A? Like PCM suggested there are pin compatible parts, just look on the Microchip website. Their parametric search would make it fairly simple to find.

So.... My suggestions are: Get the right part, try the application in the "Tips and Tricks" the way they intended, read up on ADCs and Comparators, and keep on plugging.

John
Ttelmah
Guest







PostPosted: Fri Oct 14, 2005 9:51 am     Reply with quote

It is _possible_ to read the voltage, but only by using 'tricks'. The circuitry to do this easily (an ADC), is not present in the chip you are using. You could (as an example), have an output connected to a capacitor, with a current source that charges it up at a constant rate. Have this voltage connected to one input of the comparator module, and the other input of the same comparator section connected to the signal. then discharge the capacitor by switching on the output and pulling it low, waiting for a moment, then releasing this drive so the capacitor starts to charge, and time how long it takes till the comparator switches. You could also for low accuracy, use the internal programmable voltage reference to do the same job. This was the idea suggested by PCM programmer. However each of these solutions is going to be complex, and involve a fairly complete knowledge of what is going to happen. Alternatively an external ADC could be attached, but then you need to know how to program this.
Conversely On a chip with an ADC, it becomes a matter of just setting this up, selecting the channel, and taking a reading. Code examples for this, have been posted many times here.
So there is a 'way' of doing it to some extent or another, but the methods are complex.

Best Wishes
Guest








PostPosted: Fri Oct 14, 2005 6:50 pm     Reply with quote

As suggested above i've signed up for a username. I've looked into getting the 16F88 chip that was suggested above. My question is can i use both the RA2 and RA3 pins to detect a voltage on? The reason I ask is that I see that one is designated as VRef- and the other as VRef+ I need a chip that can measure 2 sets of voltages (one reading at a time). I'm trying to set up a 6 key keyboard and use as few as possible pins. On the 628A chip that I already have I only have the following pins availble to work with RA0 thru RA4, RB0 and RB1 I need a total of 3 pins to be OUTPUT pins for my LCD that I am currently using, the remained of the pins can be used for the keys. That only leaves me with 4 pins left. I know I could get a chip that has more pins but I'm working with an existing project that is already built and running using a F84 chip and i'm wanting to add an LCD and keypad to it. Using a chip with a larger number of pins would require a new socket to be fit onto a board that does not have enough space for it. My only other option that I can think of is to add a second 628A chip and to have the second chip run the keys and the LCD and then connect the 2 chips together and have the keypad/lcd chip pass info to the eeprom of the first chip. I'm just not sure if I'll be able to figure out the "how to" in order to make the 2 chips talk. Does anyone have any suggestions on this?
timtalk



Joined: 14 Oct 2005
Posts: 10

View user's profile Send private message

PostPosted: Fri Oct 14, 2005 6:52 pm     Reply with quote

dunno why it did not show me as logged in on that last post. Sorry.
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Fri Oct 14, 2005 7:39 pm     Reply with quote

Sounds like you're getting there....

(Experts, again, correct me if I'm wrong....)

The Vref designations on the pins are one of the options for the pin. You are interested in the ANx designation that denotes that that pin can be used as an analog input. (Pins have a variety of purposes they can serve.)

This is just me... I would use AN0 and AN1 for my keyboard. The setup of the analog ports will be simplest that way. However, if hardware requires, you can use any AN capable pin.

So this would lead us to say:

Code:

RA0/AN0: Keypad Analog
RA1/AN1: Keypad Analog
RA2:     LCD
RA3:     LCD
RA4:     LCD
RB0:     Keypad
RB1:     Keypad


Let us know if that's going to work. And you may want to see if any of the pros weigh in with any remarks on my post. It should be flexible if you're wire wrapping.

Good Luck,

John
timtalk



Joined: 14 Oct 2005
Posts: 10

View user's profile Send private message

PostPosted: Fri Oct 14, 2005 8:27 pm     Reply with quote

I want to make sure I'm fully understanding this. The F628A chip that I already have has ANX pins on it... specificaly AN0, AN1, AN2, AN3 which by what you have posted would indicate that the chip I already have would work. Or is it that the F88 has an A/D converter onboard thus it can convert any pin that can take an ANALOG signal???
dyeatman



Joined: 06 Sep 2003
Posts: 1933
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Oct 14, 2005 8:42 pm     Reply with quote

Before you have a prayer of getting this project off the ground any time soon you have to understand the hardware before you can write any code to support it.

If you read the F628A datasheet page one there is no mention of the F628 having any Analog to Digital (ADC) converters so the answer to your question is still NO, there is NO ADC in the F628 period. The F628A ANx pins are Analog Comparators which will not be what you want for this project.

On the 16F88 datasheet page one is says specifically "10-bit, 7-channel Analog-to-Digital Converter" This means it has SEVEN 10-bit resolution ADCs that can be used in various combinations detailed in the DATA SHEET starting on Page 113, pins RA0, RA1, RA2, RA3, RA4, RA5, RB6 and RB7.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sat Oct 15, 2005 7:27 am     Reply with quote

Rather than trying to use the comparators of the F628A as an A/D I would suggest you read Microchip App Notes AN512 & AN513 that describe making an A/D with any digital PIC pins. You will learn how an A/D works and the nature of accuracy verses resolution. You will understand your A/D, not just know how to use it.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group