Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

BCD Thumbwheel

Options
  • 28-07-2009 11:57am
    #1
    Registered Users Posts: 38


    Hey guys,

    Just wondering if anyone has used a BCD Thumbwheel switch as part of a project? Having a little trouble getting it to work with my 16F627 PIC. Using C to program it.


Comments

  • Registered Users Posts: 1,228 ✭✭✭carveone


    monkles wrote: »
    Hey guys,

    Just wondering if anyone has used a BCD Thumbwheel switch as part of a project? Having a little trouble getting it to work with my 16F627 PIC. Using C to program it.

    Hummm. Shouldn't be hard but you may be forgetting something.

    A BCD thumbwheel just has a common followed by four pins for each bit. Let's say you used Port B (0 to 3) wired to each of the four bit pins, and then wire the Common pin to 0v ground.

    Remember that the switch will then ground the pins for the setting you require, so we need pull-up Resistors (aha!).

    So enable the internal weak pullups for Portb (check the datasheet for that), make sure the port is an input (trisb=0xff, which will set all eight bits but you get the idea).

    Then:

    foo=PORTB

    will read all 8 bits into foo, so you should do something like:

    foo=foo & 0x0F

    Tada! Oh yeah, don't forget that that answer is inverted (you are pulling down each pin). I'll leave the rest up to you.


  • Registered Users Posts: 38 monkles


    Thank you for your help. I have all of that working fine for PORTB. Only using the top four bits for it so I have them isolated and shifted to lower nibble. I need to do the same with the lower four bits in PORTA. I understand there are no internal pull-ups for this port so I added external ones. I turned off the comparators to allow PORTA pins to be used as normal I/O pins. I isolated the bits I need and inverted the output from the thumbwheel (I presume I dont need to shift them anywhere as they are already in the lower nibble). I said

    myVar = myVar ^ 0x0F;

    But it's still not reading it properly and I get weird results. Im using 2 of the top bits as outputs but they shouldn't be affecting the result when I invert should they?


  • Registered Users Posts: 1,228 ✭✭✭carveone


    You've got me. I'll have to think about it.
    monkles wrote: »
    Im using 2 of the top bits as outputs but they shouldn't be affecting the result when I invert should they?

    It's been a while but I'm pretty sure the values set on the outputs read back when you read from the port (they're looped back). So if the top 2 bits are set to 1 and you do that Xor operation to invert then you'll get myVar with the top 2 bits still set to 1. You could try anding them out before (or after) with

    myVar = myVar & 0x0F

    just to make sure.

    Edit: Checked the datasheet. Yup, they're looped back. The 627 datasheet says you need to configure porta as digital inputs by setting CMCON but I guess you've already done that!


  • Registered Users Posts: 38 monkles


    Thanks again but it still refuses to work!! When I invert it, it seems to be setting all the outputs from the thumbwheel high. So instead of for example getting 3(0011) or 7(0111) or whatever I am getting 15(1111)! And its only supposed to go to 9!!!


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    You've double-checked all the wiring, yes? No small whoopsies like wiring the common pin on the thumbwheel to +5 instead of ground? No fried chips or parts? You've checked the voltages on the output pins of the thumbwheel with a voltmeter to make sure the lines are going to the right level?


  • Advertisement
  • Registered Users Posts: 38 monkles


    Yes all the wiring is correct. I have 4 10K resistors connected to each pin as pull ups and common wired to ground. Is this correct?


  • Registered Users Posts: 1,228 ✭✭✭carveone


    monkles wrote: »
    Yes all the wiring is correct. I have 4 10K resistors connected to each pin as pull ups and common wired to ground. Is this correct?

    Certainly sounds ok. You seem to be getting 0000 on each pin input though which is bizarre. When the thumbwheel is set to 0, each pin should be +5V. Assuming it is actually changing properly then I'm stumped :confused:


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    You setup CMCON correctly, but did you setup TRISA correctly?


  • Registered Users Posts: 184 ✭✭stylers


    check with your meter that the pins are indeed changing and at the expected voltages. if ok, double check the TRISA setup, and CMCON should be 0x07. what compiler are you using, and how are you debugging ?. If all seems correct, have a look at the assembly output to see exactly what its compiling to..
    wishing many happy hours of embedded headscratching :D


  • Registered Users Posts: 38 monkles


    Ya i have CMCON set up like that and TRISA as 0b11101111; (Like to use binary!) I need RA4 set as an output for now. Im using MPLAB. Checked the ouput from the pins and it seems to be grounding the them no bother but the ones that are supposed to be 5V are neither! Anyone know what might be the problem there?


  • Advertisement
  • Registered Users Posts: 184 ✭✭stylers


    well that port setup looks ok, but you say the pins aren't going to 5V as expected ?, or somewhere in between ?. must be the thumbwheel setup.. maybe there are internal pull up/down resistors in it or its not as simple as it seems. double check with the meter on resistance range.. or try maybe 1K pull ups.

    your code is prob ok, but you could use the stimulus in MPLAB and toggle the pins while simulating to check this also..


  • Registered Users Posts: 38 monkles


    I just tried it using PORTB instead and it worked perfectly first go so it's definitely not the code or the thumbwheel or the wiring cos I checked that a million times. It HAS to have something to do with the resistors or the way I have PORTA set up. I bet its something stupid I left out! Thanks everyone for ye're help. I'm off to pull the last few wisps outta my head! I'll let ye know how I get on!


  • Registered Users Posts: 1,228 ✭✭✭carveone


    monkles wrote: »
    Ya i have CMCON set up like that and TRISA as 0b11101111; (Like to use binary!) I need RA4 set as an output for now. Im using MPLAB. Checked the ouput from the pins and it seems to be grounding the them no bother but the ones that are supposed to be 5V are neither! Anyone know what might be the problem there?

    What, like 2.5 volts? Well, if there was nothing at all connected to the pin and it was in high impedence state (input), it would float at 2.5v because the two clamping diodes would hold it there.

    But 10k to +5v from an input is more than enough to bring an input high. There must be a voltage divider in there somewhere. Remove the thumbwheel from the circuit, leave the resistors there and ground each input manually checking the response from reading porta.

    I assume you've breadboarded this and so haven't lost your mind rewiring yet!

    As stylers says, maybe the thumbwheel is a funny one and has internal resistors. That would certainly mess with you... I also hope you have a pic16f627 (because that's a funny one - the 16f628 is what most people use) and have told the compiler that that's what you have!


Advertisement