Read a BCD value (Thumbwheel Switch)



THE BOOK of DT's INTERRUPTS is an organization of interrupt service routines and other works written by Darrel Taylor, RIP.

Interrupt routines are arranged per Darrel's original list. Darrel Taylor's Instant Interrupts are an extension of the work of Tim Box whom in October of 2002 wrote INT_CTRL.pbp

Many of the forum members felt this is a long overdue project.

THE BOOK of DT's INTERRUPTS
will be a valuable resource and a tribute to Darrel Taylor the "Fanatical Contributor".

THE BOOK of DT's INTERRUPTS is located at http://dt.picbasic.co.uk/

+ Reply to Thread
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2003
    Location
    INDIA
    Posts
    161

    Default Read a BCD value (Thumbwheel Switch)

    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.

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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

  3. #3
    Join Date
    Sep 2003
    Location
    INDIA
    Posts
    161


    Did you find this post helpful? Yes | No

    Default Thanks a million for teaching the way out

    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.

  4. #4
    Join Date
    Sep 2003
    Location
    INDIA
    Posts
    161


    Did you find this post helpful? Yes | No

    Default Two Thumbwheel Switches on PortB.

    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.

  5. #5
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Thumbs up

    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

  6. #6
    Join Date
    Sep 2003
    Location
    INDIA
    Posts
    161


    Did you find this post helpful? Yes | No

    Default Thanks

    Thanks Ingvar/


    I shall try and get back to you . Regards

  7. #7
    Join Date
    May 2004
    Posts
    81


    Did you find this post helpful? Yes | No

    Default

    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?

  8. #8
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by bearpawz

    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!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  9. #9
    Join Date
    Dec 2005
    Posts
    8


    Did you find this post helpful? Yes | No

    Question So how would one do this with PORTA on a 16F628?

    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

  10. #10
    Join Date
    Dec 2005
    Posts
    8


    Did you find this post helpful? Yes | No

    Wink Arghhhhhh... documentation is da bomb..

    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

  11. #11


    Did you find this post helpful? Yes | No

    Default

    You also have to use external pull-ups for porta. I believe portb is the only one that has them.

  12. #12
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    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
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  13. #13
    Join Date
    Dec 2005
    Posts
    8


    Did you find this post helpful? Yes | No

    Talking Thanks!

    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.

Similar Threads

  1. Cleaning up code
    By Tobias in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd December 2009, 07:14
  2. SEROUT WORD variable problem
    By Tobias in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 19th April 2009, 11:20
  3. Q: using MCLR for Input on 12F683
    By picster in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 31st January 2009, 15:25
  4. How to read pulse from reed switch
    By passion1 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 9th June 2007, 14:29
  5. Changing declared variables names on the fly
    By jessey in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 16th December 2006, 06:34

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts