Digital inputs (Binary) Into 28x

biketrialsdave

New Member
Hey there, I have connected 7 digital inputs to a 28x (Input pins 1 to 7 - NOT including input pin 0 because that is already being used) and each of the 7 bits represents a binary value. Is there a way in picaxe to set a variable to just these pins to get a decimal value? I have seen something like b0 = pins but I think this assigns all the pins? Is there a way to set b0 so it is the decimal equivalent of the 7 bit binary on those input pins mentioned above?

Thanks for any help,
Dave
 

Fowkc

Senior Member
If you take b0 = pins and then divide by two, each bit shifts down one place in the byte. Thus you will have the value on the upper 7 pins.

As an example, let's say you have the number 73 on the top 7 inputs, and input0 is low. Then b0 = pins gives b0 = 10010010 = 146. So clearly b0 = b0/2 will give b0=73 as required.
 

biketrialsdave

New Member
This is the code ive got to read the inputs, do a calculation on them and display them on the lcd..Not tryed this yet though, anyone spotted any obvious mistakes? (I think the a0 line is wrong)


'127 steps with 7 bits
'NEED TO MAKE IT SO THIS IS TRUE assumes 0 = 0lux and 127 = top lux
'1000 lux range = steps of 8
'500 range = steps of 4

if input a0 = 1 then read 'end of conversion signal from comparator to input a0 to start reading the result into the pic!


read:


let b1 = pins
b2 = b1/2
b2 = b2*1000
b3=b2/127 'returns quotient
b4=b2//127 'returns remainder


init: pause 500 ‘ wait for display to initialise
main: serout 7,N2400,(254,128) ‘ move to start of first line
SEROUT 7,N2400, (b3),("."),(b4),"Lux")


'Example
'inputted 127 into switches,
'127*1000=127000
'127000/127 = 1000 - correct

'inputted 63 into switches - middle,
'63*1000=63000
'63000/127 = 496. - correct)
'63000//127 = remainder
 

BeanieBots

Moderator
Several errors I'm afraid.
First off, a byte variable (eg b0) can only hold a number in the range 0 to 255. So, your line "b2 = b2*1000" will not give the expected result. You need to use word variable. Read up on variables because each word variable uses two byte variables (eg w0 uses b0 and b1). You will have reasigning to do.

"read" is a reserved word so you cannot use it as a label. Simply change it read1 or something similar.

"a0" is not valid. Think you mean pin0.
The serout has too many paranthesis.
Why not try putting your code in the simulator and/or using the quick syntax checker?

Edited by - beaniebots on 15/03/2007 21:26:59
 

Fowkc

Senior Member
Actually, by a0 I think he meant "physical leg 2" or ADC0, used as a digital input. It's referred to as "ADC0 / In a0" in the manual diagram for the 28X.

In this case the correct expression is:
<code><pre><font size=2 face='Courier'>
IF porta pin0 = 1 THEN
</font></pre></code>
 

biketrialsdave

New Member
Ive revised my code and now got this:

let w0 = pins
w4 = w0/2
w4 = w4*1000
w5=w4/127 'returns quotient
w6=w4//127 'returns remainder

pause 500

init: pause 500 &#8216; wait for display to initialise
serout 7,N2400,(254,1)
main: serout 7,N2400,(254,128) &#8216; move to start of first line
SEROUT 7,N2400, (&quot;12345&quot;, w5)
main1: serout 7,N2400,(254,192) &#8216; move to start of first line
serout 7,N2400,(&quot;lux&quot;) &#8216; output text


end



The problem I am having is that the lcd displays greek symbols for the variable w5. I have seen this problem loads on the forum and cant seem to solve it! Ive got the latest lcd board (with i2c) and ive even connected the rst pads as stated in the axe033 manual. If I put a number in &quot; &quot; then it displays fine, just wont display the variable!
Any ideas?

Thanks,
Dave
 

BeanieBots

Moderator
You need to put # infront of the variable when using serout.
SEROUT 7,N2400, (&quot;12345&quot;, w5) will send the ASCII character of whatever is the value of w5. eg, if w5=$30 then &quot;0&quot; will be displayed.
SEROUT 7,N2400, (&quot;12345&quot;, #w5) will convert the contents of w5 into ASCII and send each character in turn.

If you are using the AXE033 in I2C mode, then there is no equivalent of the # operator and you must do the conversion yourself. ie, break down the number into each digit and then add $30 to convert to ASCII.
 

biketrialsdave

New Member
Hey, sorry I was supposed to remove the 12345 bit (Was just doing some testing!) So say the value of W5 is 800 then to display that I would need to put #w5? Sorry If im not following!

Thanks,
Dave
 

biketrialsdave

New Member
Is there any way of combing two word variables? I ask this because I am getting a 6 digit figure when I times the value by 1000. Changing the 1000 to a 100 then timsing by 10 at the end works but this casues an accuracy error.

Thanks,
Dave
 

biketrialsdave

New Member
let w0 = 255
w1 = w0/2
w2 = w1*100 '*1000 is too big for a word variable!
w3 = w2/255
w4 = w3*10
w5 = w2//255

init: pause 500 &#8216; wait for display to initialise
serout 7,N2400,(254,1)
main: serout 7,N2400,(254,128) &#8216; move to start of first line
SEROUT 7,N2400, (#w4,&quot;.&quot;,#w5)
main1: serout 7,N2400,(254,192) &#8216; move to start of first line
serout 7,N2400,(&quot;Input signal &quot;,#w0) &#8216; output text


end


Thats what Ive got at the moment, that should return a value of 500 but doesnt.
 

BeanieBots

Moderator
It is sort of possible but a bit beyond me.
Using the method you are doing can be made to give better accuracy.
What is the actual sum you want to do?
 

biketrialsdave

New Member
Well basically I have a 7 bit binary number going into my picaxe chip from a ramp ADC. I want this binary number to represent a value of lux. My range is 0 to 500 lux, so when I have a binary value of all 1's (127) then the display should show 500 lux and when all 0's, 0 lux. I know that lux isnt a linear scale but thats not important for now. Because there are 8 input pins and I am using the command let w0 = pins then If I divide the value by 2 at the start this will solve the problem of the spare input pin. (Cant use 8 bits!) By timsing the value by a 1000 then dividing by 255 I should get the correct value.

Thanks
Dave
 

biketrialsdave

New Member
This topic may be relavant:
http://www.rev-ed.co.uk/picaxe/forum/topic.asp?topic_id=6118&amp;forum_id=29&amp;Topic_Title=7+bit+binary+into+28x+PIC&amp;forum_title=No+new+posts+please%21+21&amp;M=False&amp;S=True

I am intersted in the idea of assigning a decimal value to each bit...Not sure how to go about this though.

Dave
 

BeanieBots

Moderator
This is about as good as it will get:-

w0=255
w0=w0*79/40

gives 503 for 255 input. That is 0.6% error.
A 7-bit input will only give a resolution of 0.8% anyway so the error in the maths is of little importance in comparison.
 

biketrialsdave

New Member
Qoute from the link posted a few posts back:

&quot;The simplest method would be to assign a decimal value to each of the bits and then add them up.&quot;
 

BeanieBots

Moderator
OK, stop!
You need to make your mind up.
What does your sensor tell you about the light level?

What is lux when the sensor outputs 1?
What is the lux when the sensor outputs 2?
etc. etc.
ie, what is the relationship between lux and your sensor output?
Now that we've got your requested linear relationship working, let's scrap it and start again!
 

biketrialsdave

New Member
Sorry, Ill try to make this easier to understand :)

I have a 7 bit Ramp ADC connected to an LDR.
I have calibrated the ADC so that at 500 lux the ADC shows the highest possible binary value (127 in decimal)
My range is 0 to 500 lux
The binary value is going into a 28x with a serial LCD connected to it.
I want to display the Lux level on the lcd. When a button is pressed the counter starts producing the ramp output, when the ramp has hit the corrcet voltage the end of conversion signal stops the count and the binary values are stored in the d-type latches (Nothing special there) The signal will also go to my picaxe and tell it to read that binary value into the PIC.

Hope thats made sense...
Dave
 

BeanieBots

Moderator
Yes, I got that.
127=500 lux
0=0 lux
What would the lux be if it read 63?
If the answer is 250 lux then all is well, job done.
If it's not, then what is the relationship between the reading and the lux?

 

biketrialsdave

New Member
Ahh I understand what your saying. I will have to wait until tomorrow when I can get to all my things. Ill take as many values as I can for different lux levels.

Thanks,
Dave
 
Top