PDA

View Full Version : Read a BCD value (Thumbwheel Switch)



charudatt
- 5th September 2003, 20:58
Hello Friends,

I am want to try and read a BCD value available on a particular port and store it in a variable. Can anyone help me with the code.

Basically I am interested in reading a thumbwheel connected at a given port.

Thank you.

Melanie
- 6th September 2003, 10:52
A BCD Switch is very simple... in it's popular form it has a common followed by four pins... A, B, C, D, or 1, 2, 4, 8.

Let's use PORTB...

Wire A (or 1) pin to PORTB.0
Wire B (or 2) pin to PORTB.1
Wire C (or 4) pin to PORTB.2
Wire D (or 8) pin to PORTB.3
Wire the Common pin to Vss (0v Ground).

The switch will pull-DOWN the relevant pins for the setting you require, so we need pull-up Resistors. The easiest way is to enable the internal weak pull-up's for PORTB... so at the beginning of your code put (see the Datasheet for your chosen PIC)...

OPTION_REG.7=0

Also you need to enable the port for input, so again at the beginning of your code you need to put...

TRISB=$FF

actually only the last four bits (bits 3 to 0) need to be configured for input, but my example set the whole port (all eight bits) that way.

Finally, to read the whole port into a previously defined byte variable, within your program execute...

Myvar=PORTB

This will read the WHOLE port into variable Myvar, so to isolate just the lower four bits (PBP manual section 4.17) should the upper four bits be getting in the way...

Myvar=Myvar & $0F

Finally, remember that your answer will be INVERTED because in our example we are PULLING-DOWN, so $0F will be all pins OFF, whilst $00 will be all pins ON. To invert the other way (again PBP manual 4.17)...

Myvar=Myvar ^ $0F

Novice, newbie or not, you can build your own truth table once you have wired and played to discover what pins pull up or down depending on your switch position... it will be a better learning excercise for homework than having me tell you here.

Melanie

charudatt
- 6th September 2003, 19:30
Hello Melanie,

Thanks a lot for taking time out and helping me with all that so nicely. I shall always remember it and will surely experiment with all the code and get back to you if I need some more help.

Actually I am enjoying programing with PIC BASIC and my next step would be to read the MT8870, the DTMF decoding chip.

I can surely do a lot (experiment) with what you have just taught

Thanks once again.

charudatt
- 8th September 2003, 16:36
Hello Melanie,

The code worked excellently without any error , first shot. did not have to do much, just a little bit of basic code (declaring variables) and thats it.

Now I am feeling a bit adventerous and I have connected two thumbwheel switches on PortB (Occupying all eight bits). The code still works good on the Last four bits. Now can you help me read the other thumbwheel into a seperate variable. Actually just want to know how you seperate the other four bits.

Thank you once again.

Ingvar
- 8th September 2003, 17:56
Hi,

Just pretend it's Mel..........

Othervar=PORTB
Othervar=Othervar & $F0
Othervar=Othervar ^ $F0

Changing $0F to $F0 will check bits 4-7 instead of 0-3.

Since theese bits are on the high nibble, you must get them to the lower nibble. You can do this many ways.

Shift it right four steps........
Othervar=Othervar >> 4

or divide by 16.....
Othervar=Othervar / 16

or use the assemblerinstruction swapf.......

@ swapf _Othervar,f

If you use one of the first two you can skip the"&" stage since those bits will be lost anyway.

Othervar=PORTB
Othervar=Othervar ^ $F0
Othervar=Othervar >> 4

Personally i'd go for......

Othervar= PORTB & $F0 'Read PortB and isolate bits 4-7
Othervar= Othervar ^ $F0 'Invert bits 4-7
@ swapf _Othervar,f 'put bits 4-7 into 0-3(and vice versa)

just because it would be the fastest way to do it.

/Ingvar

charudatt
- 8th September 2003, 22:26
Thanks Ingvar/


I shall try and get back to you . Regards

bearpawz
- 19th April 2005, 19:00
I’m posting here because I think it’s relevant.

If your looking for just the upper 4 bits, can you just use My_Variable = My_Variable >> 4? The help file (which is more or less the PBP manual copied) in microcode studio says when you shift values the new values shifted in are 0.

Example: (and again, I am asking if I am reading this correctly)

My_Variable = %11110101 ‘I want the upper four bits
My_Variable = My_Variabel >> 4 ‘Shift left 4 places, shifting in zeros… should now be %00001111

Is that correct?

mister_e
- 19th April 2005, 22:03
My_Variable = %11110101 ‘I want the upper four bits
My_Variable = My_Variabel >> 4 ‘Shift left 4 places, shifting in zeros… should now be %00001111

Is that correct?

Yup! That's right!

KD6OJI
- 5th December 2005, 11:53
I too am a NU B. I understand the coding from Melanies example. But how would I config PORTA 0-3 to read my switch. Seems the option register only controls port B options. I know it is probably uber simple, just eluding me at the time!

Thanks in advance...

Shawn

KD6OJI
- 5th December 2005, 12:04
I found my answer!!!

CMCON and VRCON control the configuration and behavior of PORTA..

wow.. love these things, learing a lot just flashing LED's

CocaColaKid
- 5th December 2005, 19:03
You also have to use external pull-ups for porta. I believe portb is the only one that has them.

Dwayne
- 5th December 2005, 19:30
Hello KD6OJI,

What class are you? Extra Class here for decades. have a few Hams around here... some from England, Some from Cal <g>, and some from Kansas. And another in 8 land.

Dwayne

KD6OJI
- 5th December 2005, 21:52
External pullups aren't a problem, thats easy.
just had to figure out HOW to tell port A not to be fancy, just be a lowly input pin.


As for me, Tech no code since 92.. Living in Dallas TX area now, originally licensed in CA.