View Full Version : 16F88 Analog In
Mugelpower
- 23rd March 2008, 18:12
Hi Gurus of Picland!
yeeees I know thats quite simple thing for you all and I could find it in the data sheet.....but thats my first time on 16F88 . LCD works of course, its just this ansel adcon thingy....
and i searched in the forum but everyone uses RA inputs for analog. They are used for the lcd so I like to use RB7 which hasnt anything else to do.
I would appreciate Your help.
code:
'************************************************* ***************
'* Name : Ananlog1.BAS *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2008 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 23.03.2008 *
'* Version : 1.0 *
'* Notes : 16F88 LCD 2x8 connected as ususal *
'* : Portb.7 analog IN 0-2V *
'************************************************* ***************
Include "modedefs.bas"
CMCON = 7 ' PortA = digital I/O
ANSEL = %00000110 ' Will set RB7 as analog I hope
ADCON0 = %11100001
ADCON1 = %10000010
adval var word 'Create adval to store result
Define ADC_BITS 8 'Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uSbut show on LCD Value:3
Define osc 4
loop: ADCIN 7, adval ' Read channel 0 to adval
Lcdout $fe, 1 ' Clear LCD
Lcdout "Value: ", DEC adval ' Display the decimal value
Pause 100 ' Wait .1 second
Goto loop ' Do it forever
End
mister_e
- 23rd March 2008, 21:13
Your ANSEL setting is wrong... RB7 is AN6 and must be set to 1 in ANSEL bit 6.
Seems you revert bit order or something like that.
ANSEL=%76543210
Mugelpower
- 24th March 2008, 11:03
Mister_E,
thaks for your suggestion.
I tried several ansel settings
from
ANSEL=%01000000
to
ANSEL=%10000000
to
ANSEL=%01000110
and a dozen similar versions.
when I switch on the LCD shows for half a second "Value:03" and then
"Value:00"
Yes..its because of internal bit during startup.
Switched an Lcdout $FE,1,"Hello"
Pause 500
into just to show the PIC works and goes through the loop.
I could switch input to any RB exept RB03 which is used for the LCD.
Input voltage is now 0-2,5V .just for checking purposes.
Do I need a reference voltage?
Acetronics2
- 24th March 2008, 12:58
Hi Mügel
the obvious thing here ...
- is you select Channel 7 ( ADCIN 7 ),
- Choose channel 1 and 2 for the ADC ,
- choose channel 4 as default Analog channel,
- right justify the result ( Ok, here , why not ? ) ,
- use a 16 bits variable for the result,
- and , at last use an 8 bits conversion ...
Might I add your comment tells "read channel 0" for an " ADCIN 7 " ...
is that clear enough for you ???
Alain
Mugelpower
- 24th March 2008, 14:04
Hello Ace,
I changed (tried to) the channel but not the comment .
Puzzling together the analog thing is difficult for me as its the first time.
Did that with a 12F675 worked great.
You were second in the following "code saving contest": http://www.picbasic.co.uk/forum/showthread.php?t=8219&highlight=12F675
Now I try to save one simple 12F675 and Iīm in trouble again.
No, What you told below isnīt clear enough for me although it shows the typical "Newbie defines rubbish"
in this thread:http://www.picbasic.co.uk/forum/showthread.php?t=709&highlight=16f88+adcon
Melanie tells us that the adcon thingy affects only Porta.
But the data sheet page 114 tells us (you, not me) how to set the adcon0 register bit to select channel 6 = RB7.
So, thats REALLY irritating to me.
so I hope thats clear enough for you.
I know Iīm juggling bits like shooting in the dark but its always the same:
search a code that works for me and then modifiing.
All codes used RA ports as analog input. Canīt use that.
Acetronics2
- 24th March 2008, 14:39
Hello Ace,
But the data sheet page 114 tells us (you, not me) how to set the adcon0 register bit to select channel 6 = RB7.
So, thats REALLY irritating to me.
so I hope thats clear enough for you.
.
Mhhhh,
I do not understand you : you ask for code ready to paste !!!
No, no, no ...
Forget the ' 675
Just take your '88 datasheet ADC section ...
READ it carefully,
beginning by the ANSEL config
WHAT do you have to write to select Channel 6 ( RB 7 ) ???
Now ADCON 0 Config
WHAT do you have to write to select RC clock ?, Channel 6 ?, AD OFF ( ADCIN will turn it ON in time )
Now, ADCON 1 Config
WHAT do you have to write to select Right justif. ?, Voltage refs ???
THAT's all you have to do ... but that must suit your harware config !!!
You don't see here ANY ref to other existing devices ... so, forget that " cut ( ? ) and paste" horrible habit.
Alain
Mugelpower
- 24th March 2008, 14:57
Hi Ace,
I donīt need a complete code.
I soldered RB4-7 parallel so it doesnīt matter which of those is my analog input.
0-2,5V to analog input , showing value on LCD...ready. I know how to set the lcd, how to set some osc defines (and manipulate the xxxx.inc files) the only thing I donīt know is how to tell my 16F88 which input to use as analog in. And I donīt know why I need to set 3 registers for this, or even more.
It doesnīt matter if the variable dimension is this way or that, will get that clear after some try and error and maybe a little thinking.
But this ansel adcon thingy makes my head fuzzy.
when experimenting with serial transmission I often had only one baud rate that worked and didnīt know why.
Iīm now below even this state because I canīt get my analog in to work without my 12F675.
skimask
- 24th March 2008, 15:35
I soldered RB4-7 parallel so it doesnīt matter which of those is my analog input.
Ya...that'll help...
the only thing I donīt know is how to tell my 16F88 which input to use as analog in. And I donīt know why I need to set 3 registers for this, or even more.
Probably because the datasheet for that particular PIC tells you what you need to set and where to set it and how to set it to do what you need to do. PBP is good, but sometimes it's not the 'Do-All, End-All', Holy Grail, for everything.
Iīm now below even this state because I canīt get my analog in to work without my 12F675.
Seems to be an ongoing problem.
Different problems in different areas, most seemingly related to problems with reading/understanding (mostly initially reading) the datasheets for the particular PIC you are using.
PIC#1 (be it whichever type) is very similar to PIC#2 (whichever type)...
But by no means are they always equal...sometimes they're not even close.
The 'F628A that you were using is close to the 'F88, but the 'F88 is much newer, with more options in some areas, and, for most practical purposes, the 'F88 isn't even close to the 12F675. They're different families. Basic bare-bones code written for one, will most likely execute on the other without a problem. But once you get into the internal modules of the PICs, register locations change, register names change, register functions change, even bit positions can change.
Go to Microchip.com, download the latest datasheets for the PICs you are using, and skim thru them.
Bookmark the pages with registers and functions you are interested in and READ them, comprehend them, play with them.
mister_e
- 24th March 2008, 16:45
In theory, you shouldn't take care of ADCONx too much when using ADCIN as the DEFINEs should take care of it for you. The only one thing you might need to set is the Right-Justified results when you use 10Bits resolution. This said, i'm not an ADCIN user.
try those settings...
Define OSC 4
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uSec
CMCON = 7 ' Disable analog comparator
ANSEL = %01000000 ' set AN6 (RB7) as analog, others to digital
ADCON1.7 = 1 ' Right justified results... duh... really needed in 8 bits???
adval VAR BYTE
PAUSE 500 ' lcd settle time
loop:
ADCIN 6, adval ' Read channel 6 (RB7)
Lcdout $fe, 1,"Value: ", DEC adval ' Clear the LCD and show result
Pause 250 ' wait a litlle bit
Goto loop ' Do it forever
Notice that your analog source impedance must be lower than 10K
Mugelpower
- 24th March 2008, 17:28
Thanks a bundle Mister_E,
now the code works with your help. had only very small changes to make
and I guess there will be more PIC-fanatics who can use this:
'************************************************* ***************
'* Name : Ananlog1.BAS *
'* Author : [Mister_E] *
'* Notice : Copyright (c) 2008 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 23.03.2008 *
'* Version : 1.0 *
'* Notes : 16F88 LCD 2x8 connected as ususal *
'* : Portb.7 analog IN 0-2V *
'************************************************* ***************
Include "modedefs.bas"
Define OSC 4
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uSec
CMCON = 7 ' Disable analog comparator
ANSEL = %01000000 ' set AN6 (RB7) as analog, others to digital
ADCON1.7 = 1 ' Right justified results... duh... really needed in 8 bits???
adval VAR word
PAUSE 500 ' lcd settle time
loop:
ADCIN 6, adval ' Read channel 6 (RB7)
Lcdout $fe, 1,"Val:",dec3 adval ' Clear the LCD and show result
Pause 250 ' wait a litlle bit
Goto loop
End
mister_e
- 24th March 2008, 19:19
Mugel,
The most important thing is not what i'd done... the most important thing for you now is to understand what i'd done. Period.
You're welcome anyways ;)
Mugelpower
- 25th March 2008, 08:30
Mister_E,
theres quite a difference between understanding something and doing something by yourself. After everything works all falls into place and looks logical.
For me it was important to get the starting point without inventing the wheel again.
You helped me a lot and my work goes like this:
looking at a code, trying to understand, modifiing for own needs and getting back to the beginning if everything fails.
I guess this method is acceptable. fact is: if you got enough of the time*intelligence product you can act like Conrad Zuse or Albert Einstein and invent everything from scratch.
The datasheets are very useful for programmers who are used to this kind of work. That may take years for me to get that far. If you learn something new without teacher or advisor you experience way more work and lots of wrong ways that could be avoided if only somebody tells you in the right moment NOT to do something specific. Two years ago programming Microcontrollers was mentally out of reach for me. Now Iīm doing that myself and got some useful gadgets that partly astonished my friends because they by themself donīt grasp more than basic electronics if even that bit . They thought you need an engineering degree in informatics to get that straight. for my part I got an engineering degree, but in mechanical engineering and thats really far away from electronics. Most of my student friends thought of electronics as a "never touch" thing. The guy that knew about transistors was suspicious.
But PBP and this forum make things go!
Point is: couldnīt get it done without "a little help from my friends". Thats it: Help.
Thanks a bundle.
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.