'Readadc' and converting to BCD

Ga-Retired

New Member
Hello everyone,just another simple question(for the experts) from a newbie.Been researching the forums for the info I need but in the interest of time,thought I'd get some great direction and advice on my project.Got an 18M2+ on the breadboard with an LM34DZ on pin C.2 using the 'readadc' into b0.I'm using 2, MC14513BCP's BCD to 7 segment decoders to drive a 2 digit,7 segment C.C. display through 330 ohm resistor arrays for the segments.All is well on that end and I got the display working as a counter with a CD4026.My question is and where I'm alittle (alot) confused, is how to write the code from C.2 onto the BCD inputs.I've got the a,b,c,d (1248) of Digit 1(MSD)on B.0,B.1,B.2,B.3 and digit 2 (LSD) on B.4,B.5,B.6,B.7 of the 18m2+.Just want to read the output from the LM34 (in Fahrenheit) from~0 deg. to 99 deg.
Thank you all for your help,hope this sort of made sense to you and look forward to your replys.Its totally amazing what this Picaxe chip is capiable of doing based on what I've seen other members do with it. My best,Ga-retired
 

westaust55

Moderator
Welcome to the PICAXE forum.

To convert the binary value which you see in decimal when you use SEROUT and put a has (#) in front of the variable name you need to convert the binary to BCD.

If following the READADC command your temperature is in byte variable b0 and you have done the value in degF in the range is 0 to 99 then the following code will convert to BCD format:

b1 = b0/10 * 16 ; put tens digit (0 to 9) into upper nybble of variable b1
b1 = b0 //10 + b1 ; add in the units digit (0 to 9)
pinsB = b1
 

Ga-Retired

New Member
Westaust55,thank you so very much for your rapid and informative reply to my concern.I'll set this up and give it a try!My best,Ga-Retired
 

Ga-Retired

New Member
Hi Everyone once again.I still haven't gotten this LM34 to BCD thing worked out correctly yet.Tried a different sensor on the Debug screen and it appears to display the Var B.0 as expected.But using the above code as Westaust 55 suggested, it comes up with decimal numbers not even in the ball park!When I place raw BCD code on the inputs it displays the correct decimal values.My BCD inputs are as depicted in my above post and I due beleive I'm correct in my wiring setup as I used this same display setup in another project with hardwired BCD inputs and it works fine.Any code corrections I need to make/enclude would be very much welcomed.Thanks to all of you and please excuse my lack of experience in this matter,I'm learning abit SLOWLY!
 

g6ejd

Senior Member
BCD Count sequence:

00000000 00
00000001 01
00000010 02
00000011 03
00000100 04
00000101 05
00000110 06
00000111 07
00001000 08
00001001 09
00010000 10
00010001 11
00010010 12
00010011 13
00010100 14
00010101 15
and so on

Is that what your getting from the ADC reading when converted to BCD?

Thanks to KeithRB and russbow for pointing out my brain fade :)
 
Last edited:

Ga-Retired

New Member
Thanks russbow & KeithRB for you replys and no I'm not getting that debug sequence but one,#4.So in the interest of making a recheck of the LM34 temp sensors with a VM,1 appears to be decreasing in value (along with being way out of tolerance)when held between my fingers.The other one is right on the button and works as expected so of course this is in the circuit now.But I still don't get the proper temp reading on the "tens digit,the "units"digit appears right.When held in the fingers the "Units" digit goes up and down as would be normal.Do you think I may have the BCD inputs reverse on the "tens" digit On the 18M2 I have all "B" pins,B.0-B.7 as outputs.B.0-B.3 are a,b,c,d 'Units digit', B.4-B.7 a,b,c,d for the "tens digit".I think I'm getting this thing sort of doing what I need but if someone can jump in here and let me know if I'm on the right path or some more suggestions it would be greately appreicated,thanks everyone for your help,I want to get this project moving along!my best,Ga-retired
 

hippy

Ex-Staff (retired)
What you have sounds right but you can double check with a program that puts out a specific bit patterns to the display, eg ...

Code:
Main:
  dirsB = %11111111 
  pinsB = %01010000
  Pause 1000
  Goto Main
If it's wired correctly that should show "50". If not try "pinsB=%10100000". You can also try %00010000, %00100000, %01000000 and %10000000; each of those should display "10", "20", "40" and "80", and in that order if wired correctly.
 

Ga-Retired

New Member
Thankyou Hippy for that 'test' code and with very good results I'm happy to report!All numbers displayed as expected. So now that its wired correctly,Im really puzzled as to why its not displaying the correct temperature.I'm using the code Westaust 55 in the post reply above sent me.I'll keep working on it but as always guys,your suggestions are most welcome!Ga-retired
 

westaust55

Moderator
Hi Everyone once again.I still haven't gotten this LM34 to BCD thing worked out correctly yet.Tried a different sensor on the Debug screen and it appears to display the Var B.0 as expected.But using the above code as Westaust 55 suggested, it comes up with decimal numbers not even in the ball park!When I place raw BCD code on the inputs it displays the correct decimal values.My BCD inputs are as depicted in my above post and I due beleive I'm correct in my wiring setup as I used this same display setup in another project with hardwired BCD inputs and it works fine.Any code corrections I need to make/enclude would be very much welcomed.Thanks to all of you and please excuse my lack of experience in this matter,I'm learning abit SLOWLY!
If I am understanding correctly, you have a value in a variable and then convert it to BCD format using the calculation I provided. But then do not see the expected value in the Programming Editor DEBUG display.

This would be correct because the BCD value is not a binary or decimal value as such but an encoded value (BCD is an acronym for Binary Coded Decimal).

For example take a value received in byte variable b0 = 45 (binary value %00101101 )
If you now apply my calculation, first we extract the tens digit as a decimal using
b1 = b0 / 10 ==> a value of 4 in decimal as expected.
Then we multiply by 16 to put the value (in the range 0 to 9) into the upper 4-bits (a nybble) of the new variable. 4 * 16 = 64 decimal or binary %01000000
If you take the upper 4 bits %0100 that is the binary representation for 4.

Next we extract the units digit using:
B1 = b0 //10 ==> 5 decimal with a binary value of %00000101
This units part is already in the lower 4-bits of the final variable, so we now just in the already calculated upper nybble:
%01000000 + %00000101 = %01000101 = 69 decimal

For the desired BCD encoded number we must look at the two nybbles separately.
From the upper nyble we have %0100 representing our “4” and in the lower nybble we have %0101 representing our “5”.

Thus taking the decimal value 45 (= %00101101) and converting to BCD gives %01000101 which when sent to a BCD display will appear as “45” but in the DEBUG screen will show up as “69”.

trust this makes some sense.
 

Ga-Retired

New Member
Westaust55,thankyou for your insite and with your input and easy to understand explaination of this subject and others,I feel encouraged that I'll get this working in due time.I only wish manual 2 was this detailed and easy to understand!Ha Ha

Now ,with that being said and all,with the correct BCD code on the inputs to the decoders via your code, I should read the correct decimal numbers.So I can't see why that isn't the case when I checked with the DVM and the output from the LM-34 was .743 mv,should read (rounded)74 deg F. but reads 41-43 up to 46-48 with finger pressure.

Just a quick question,I do have a DS18b20 and I'm very tempted to put it to work but I'll need to convert the results to F degs. plus your BCD code to get it to display correctly right?Do you have a simple whole number C deg. to F deg conversion code?I saw one of the members posting code for all the conversions but looked in more detail than what I'm trying to accomplish here.

Thanks for listening to my dribble,but I sure would like to 'Get-ur -done' so I can get on with the next hurtle.Much thanks everyone,Ga-retired
 

russbow

Senior Member
I would recommend the DS18B20 route and the readtemp command - so easy to get a result.

Do a forum search for the conversion - about 3 lines of code that will sit easy in your program and then break the resultant value into BCD as previously discussed.
 

MPep

Senior Member
In that case, please post your existing code as it is, and a circuit diagram for us to take a peek at. :)
 

westaust55

Moderator
Now ,with that being said and all,with the correct BCD code on the inputs to the decoders via your code, I should read the correct decimal numbers.So I can't see why that isn't the case when I checked with the DVM and the output from the LM-34 was .743 mv,should read (rounded)74 deg F. but reads 41-43 up to 46-48 with finger pressure.
I believe you are trying to use the value from the READADC input without scaling which is the source of the problem.
If you have a voltage of 0.743 volts into the ADC input on a PICAXE operating at 5 volts then the value placed in the byte variable will be:
0.743 * 255 / 5 = 37 which is close to the 41 you indicate.
This calculation come from the fact that at 5 volts input, the READADC input will return a value of 255.

If you are in fact operating from 3 x AA cells with 4.5 volts then
0.743 * 255 / 4.5 = 42 !

Looks like you in fact need to apply a *2 scaling factor to get the desired temp value if using 5 volts.
37 * 2 = 74
Or a factor of 1.76 if using 4.5 volts
Therefore your code needs to be something like
READADC <pin>, b0 ; where <pin> in your case is C.2
B0 = b0 * 2 ; or b0 = b0 * 176 / 100
B1 = b0 / 10 * 16
B1 = b0 // 10 + b1
PinsB = b1 ; don't forget the dirsB command to set portB as outputs

Suggest let's get what you have hooked up working properly rather than jump ship to the DS18B20 leaving the earlier work abandoned. You can experiment later with the DS18B29 and compare the differences for yourself with both working.
 
Last edited:

ValueAdd

Senior Member
The dirsB command mentioned would need to be: dirsB = %11111111
which would be placed near the top of the program in the initialisation prior to any many program loop reading the ADC input and outputting data to the portb pins
 

Ga-Retired

New Member
Thanks to all who have given their input on this project. Westaust55: I got to once again give you my gratitude for your determined interest in getting a solution to my problem,and guess what,you have hit the nail on the head!That scaling add in to my code did the trick.I feel so good about this now I thought I'd try and learn some more on the DS18B20 with a convert to F degs.

I found several conversions C to F but need just a bare bones basic code,any suggestions would be welcome.Thanks to all,its a pleasure having a good group of folks that are willing to help those of us that are ....... well, lost!GN,Ga-retired
 

marks

Senior Member
Hi Ga-Retired,
welcome to the forum
only just read the last few posts
but heres some code to try just view using the serial terminal
Code:
SYMBOL Ds18b20       = C.1
SYMBOL Temperature   = W0  

Main:
Readtemp12 Ds18b20,Temperature                               
                          
  Convert:                                      
                                
     Temperature = Temperature +880 **7373 -67             'Fahrenheit (0F to 257F)                 
                         
   SERTXD (CR, LF, "Temperature  ",#Temperature,"  Degrees F", CR, LF)
   
PAUSE 1000
GOTO Main
 

Ga-Retired

New Member
Hi Marks,and thank you for that welcome. In Fact,your posts on the conversions are what I've been looking at,wow,you did alot of work on those for sure.I'm going to give this C to F code a try but first a few questions on the math.I see it will convert to 0 deg. F to 257 geg. F. Seeing that the temp in this neck of the woods will only range between 15 to ~ 99 degs F. is why I choose to keep it to 2 digits,that and basic for learning purposes.With the max temp readout of 99 deg F being my top limit what will I have to modify in your code to adjust for this if anything?Do I just stay with "readtemp" command and not the "readtemp12" command?
Can't wait to try this out,these temp sensors are really neat gadets!Thanks for your help Marks,without the help of you nice gentlemen I'd still be stumped on this project!I'd like to post my schematic,which I drew up in "Electronics Workbench",to the forum,do you see any problems in doing this you may know of?All the help as always is very much needed and appreicated,Ga-retired
 

marks

Senior Member
Hi Ga-Retired,
If your temperature between those limits then its never a problem
if temperature = 102 degF then it will just display 02 on your two digit display

everyone likes a schematic I usually include one in threads that I start
these may interest you.

perhaps time to go 3 digit lol!
http://www.picaxeforum.co.uk/showthread.php?20756-CountDown-Timer-3-Digit-7-Segment-reusing-an-axe133-(18m2)

circuit of basic supply and ds18b20
http://www.picaxeforum.co.uk/showthread.php?23925-HIH-5030-5031-Low-voltage-Humidity-Sensor

i must get a lm34 or similiar to tryout I wish i have more time on hand lol.
 

Ga-Retired

New Member
Thank you for that info and as you mentioned Marks,a schematic is in order and I would like to display one ...............if I could nail down the best display driver/decoder I have in my junk box.Right now its on for a MC14513 which I have a few of left from a former project.Anyone know if I can display a "Electronics Workbench" file on this site?

I got the DS18B20 and the 18m2+ to display in F degs using one of your 'Convert' programs,I see you have several, is one program more accurate for my setup over any of the others,I'm using only 2 digits for a 0 - 99 degs. F setup?

I'm attempting a "wireless" project using the "RFIN & RFOUT" commands,its going to be some kind of FUN!Another question now is how to get the 8 bytes of data from the DS18B20 into a code to send to the "RFIN on a 14m2 for transmit to a 18m2+.Just doing alot of research but not finding much having to do with BCD info. Thanks all,I really enjoy this forum,goobs of great info here.My best,Ga-Retired
 
Top