Menu

Read Binary

Help
CDRIVE
2017-05-11
2017-05-22
1 2 > >> (Page 1 of 2)
  • CDRIVE

    CDRIVE - 2017-05-11

    The Chip is a 16F84A and I want to read the binary value (1 or 0) of a Dip-8 Switch. Each switch will be connected to RB0 - RB7 that will be set as Inputs. Then I need to convert the Binary values to Decimal. I plan to use the decimal value as a variable for use with the Wait command.

    My first thought was to create an array to hold the binary (Byte) values. Loop through RB0 - RB7 to read the logic states and stuff them in the array. Then ......?

    I'm not sure I even need an array. Would a Byte variable work?

    Any guidance would be graciously accepted.

     
  • mmotte

    mmotte - 2017-05-11
    wttm = PortB
    wait wttm ms
    
     
  • jackjames

    jackjames - 2017-05-12

    To read the door, you need to enable PORTB pull-up resistors and connect the other side of the dip switch to ground.
    To enable the pull-up, set the bit 7 of the OPTION register to 0.

     
  • CDRIVE

    CDRIVE - 2017-05-12

    mmotte,

    I didn't reply immediately because I'm not quite seasoned in GCB yet. The code snippet you posted stopped me cold. I had trouble believing it was that simple. To my pleasant surprise it was!!

    Thank you!
    CJ

     
  • Chris Roper

    Chris Roper - 2017-05-12

    Or to save a Byte or two:

         wait PortB ms
    
     
  • CDRIVE

    CDRIVE - 2017-05-12

    Jack,
    In my application the inputs will require the opposite; ....pull-down's. Actually, if I use digital switches in place of the DIP8 I won't need pull-down resistors at all. You see this project is strictly for virtual use (Spice) and will never see a hardware development stage.

    Thanks,
    CJ

     
  • CDRIVE

    CDRIVE - 2017-05-12

    As usual Chris. Always good stuff from you!

    Cheers,
    CJ

     
  • William Roth

    William Roth - 2017-05-12

    As you may have realized by now there is no need to convert binary to decimal. Just read the port and use the variable for the delay

    You may consider something like this example.
    .

    #define My_Delay PORTC  '//substitution
    
    Dir PORTC IN         '// DipSwitch read Port
    Dir PortB.7 out      '// Test LED
    
    '// Test Loop
    Do
        Pulseout PortB.7, MY_DELAY ms
        Wait (MY_DELAY * 2) ms 
    Loop
    
     
  • jackjames

    jackjames - 2017-05-13

    For CDRIVE:
    The value you read is the reverse of the value you need.
    For that you have to make the XOR of the value read or (255-read value).

     
  • CDRIVE

    CDRIVE - 2017-05-13

    William, thanks for the clever code to test my circuit. It beats the hell out of the method I've been usng.

    Jack, Thanks for the tip but I'm sorry to say that I don't understand the issue.

    CJ

     
  • Chris Roper

    Chris Roper - 2017-05-13

    Chris,

    You mentioned that you were working with SPICE, which package are you using?

    Cheers
    The other Chris

     

    Last edit: Chris Roper 2017-05-13
  • CDRIVE

    CDRIVE - 2017-05-13

    Chris R,

    I'm using Tina Classic Design Suit (V11). S

    Since you asked and since I said I'd be using an 8 pos Dip switch (DIP8), I should also mention that I abandoned that approach in favor of using Tina's Hex Keypad model. Why they call it a Hex Keypad escapes me because it outputs 4 bit binary. This will be a much better solution than the dip switch aproach. The keypad provides digits 0 - 9 and characters A - F. Because it only provides 0 - 9 I decided to make the Wait variable also 0 - 9, as I really don't need the variable to span 0 - 255.

    Cheers!

    Chris S.

     
  • Chris Roper

    Chris Roper - 2017-05-13

    4 Bit Binary is hex.

    Hex is an abbreviation of Hexadecimal, which is the base 16 number system.
    So the output range is 0x0 to 0xF
    in binary that would be 0000 to 1111
    or in decimal 0 to 15

    In the case of a hex keypad A-F are not characters they are numbers.

    I have Tina 9 - TI but I don't think it has any microcontroller support.
    I got it years ago for a project and have hardly ever used it since.
    I must fire it up and see :)

    Cheers
    Chris

     
  • CDRIVE

    CDRIVE - 2017-05-14

    Wow, my digital ignorance must be shining like a Super Nova! The knowledge gained is more than worth My embarrassment though. Now I know and understand that I can get 0 -15 out of that Hex keypad.

    As far as Tina TI is concerned, it's been (probably) 10 to 12 years since I used that free version.

    Thanks Bunches!
    Chris.S

     
  • CDRIVE

    CDRIVE - 2017-05-20

    I'm getting some weird results with the code below. When the Keypad is set to (1) I expected a PW = 1uS on PortB.7 but I'm getting a pulse width of approx 130uS. I would also expect the Wait period to also = 1uS but it's closer to 4uS.

    #Option Explicit
    #Chip 16F628A, 20
    #config osc xt
    
    
    Dir PortA.2 In
    Dir PortB In
    Dir PortB.7 Out
    
    
    #Define PulseWidth PortB
    
    CheckSw:
    
      Do While PortA.2 = 0
    '  nothing to do here but loop
      Loop
    
      Do While PortA.2 = 1
        PulseOut Portb.7, PulseWidth  uS   'HexKeypad = 1
        Wait PulseWidth  uS
      Loop
    GoTo CheckSw
    

    I'm obviously doing something wrong but I don't know what.

    Thanks,
    Chris S.

     
  • CDRIVE

    CDRIVE - 2017-05-20

    I'm getting some weird results with the code below. When the Keypad is set to (1) I expected a PW = 1uS on PortB.7 but I'm getting a pulse width of approx 130uS. I would also expect the Wait period to also = 1uS but it's closer to 4uS.

    #Option Explicit
    #Chip 16F628A, 20
    #config osc xt
    
    
    Dir PortA.2 In
    Dir PortB In
    Dir PortB.7 Out
    
    
    #Define PulseWidth PortB
    
    CheckSw:
    
      Do While PortA.2 = 0
    '  nothing to do here but loop
      Loop
    
      Do While PortA.2 = 1
        PulseOut Portb.7, PulseWidth  uS   'HexKeypad = 1
        Wait PulseWidth  uS
      Loop
    GoTo CheckSw
    

    I'm obviously doing something wrong but I don't know what.

    Thanks,
    Chris S.

     
  • William Roth

    William Roth - 2017-05-20

    The program is doing exacty what it is told to do.

    You are reading PORTB for Pulsewidth, yet you are using PORTB.7 as an output. So when the pulseout pin goes high , PORTB will have a value of 1 + 128. (Bit 1 ON , BIt 7 ON)

    Put the pulseout pin on a port other than PortB an it should be ok.

    Alternately you can mask out PORTB bit 7 like this

    PulseOut Portb.7, (PulseWidth AND 127)  uS   'HexKeypad = 1
    

    .
    Also you should note that PulseOut will never produce a 1.us pulse. As low as it can go is is about 2.5 us with a 32MHz Clock speed. This is due to instruction overhead. The pulses become accurate to within +- 1 us at about 5us and above.

     

    Last edit: William Roth 2017-05-20
  • CDRIVE

    CDRIVE - 2017-05-20

    That makes sense and it chalks me up to yet another duh moment. Are there any 16F chips that can run a clock speed of 32MHz or higher? If so please suggest one.

    BTW, is this a typo? Is that supposed to be 5uS and below?

    The pulses become accurate to within +- 1 us at about 5us and above.

    Thanks,
    Chris S.

     

    Last edit: CDRIVE 2017-05-20
  • William Roth

    William Roth - 2017-05-20

    Not a typo. Pulseout (Pin, variable us) will not be accurate where variable <5 They become more accurate when var >= 5.

     

    Last edit: William Roth 2017-05-20
  • William Roth

    William Roth - 2017-05-20

    To select a PIC micricontroller I generally do a parametric search at Microchip,com, Here's the link for PIC 16.
    .
    http://www.microchip.com/ParamChartSearch/chart.aspx?branchID=1002

    Go to the MAX CPU column and check 32. Refine your search further by selecting Pin Count, ram, etc

     

    Last edit: William Roth 2017-05-20
  • CDRIVE

    CDRIVE - 2017-05-20

    My apologies William. I mis-read that sentence. My brain read:
    The pulses become "inaccurate" to within +- 1 us at about 5us and above.

    I'm suffering a yet undiagnosed ailment. One of my multiple simptoms includes my brain not firing on all eight cylinders. I've had so damn many blood tests that I think there's nothing left to feed my brain!

    Chris, thanks for the chip list.

    Cheers,
    Chris S.

     
  • CDRIVE

    CDRIVE - 2017-05-20

    I'm having difficulty matching the 16F chips returned using the filters with the list of chip models provided in the Tina library. Will any of these listed clock >20MHz?

    Thanks,
    Chris S

    PIC16F87,PIC16F88,PIC16F873A,PIC16F874A,PIC16F876A,PIC16F877A,PIC16F83,PIC16CR83,PIC16CR84,PIC16F785,PIC16HV785,PIC16F913,
    PIC16F916,PIC16F917,PIC16F946,PIC16F610,PIC16HV610,PIC16F616,PIC16HV616,PIC16F882,PIC16F883,PIC16F884,PIC16F886,PIC16F887,
    PIC16F872,PIC16F873,PIC16F874,PIC16F876,PIC16F877,PIC16F737,PIC16F747,PIC16F767,PIC16F777,PIC16F870,PIC16F871,PIC16F84,PIC16F84A,
    PIC16F819,PIC16F73,PIC16F74,PIC16F76,PIC16F77,PIC16F73A,PIC16F74A,PIC16F76A,PIC16F77A,PIC16F72,PIC16F684,PIC16F688,PIC16F716,
    PIC16LF722,PIC16F723,PIC16LF723,PIC16F724,PIC16LF724,PIC16F726,PIC16LF726,PIC16F727,PIC16LF727,PIC16F631,PIC16F677,PIC16F685,
    PIC16F689,PIC16F690

     
    • Chris Roper

      Chris Roper - 2017-05-20

      Those are all an older generation of devices.
      You would have to look each one up individually to be sure but I think the
      fastest of the bunch is the PIC16F727 and that only goes to 16Mhz.
      When was Tina last updated? those devices are all several years old, the
      list I posted are all within the last year or two.

      On 20 May 2017 at 17:14, CDRIVE cdrive@users.sf.net wrote:

      I'm having difficulty matching the 16F chips returned using the filters
      with the list of chip models provided in the Tina library. Will any of
      these listed clock >20MHz?

      Thanks,
      Chris S

      PIC16F87,PIC16F88,PIC16F873A,PIC16F874A,PIC16F876A,
      PIC16F877A,PIC16F83,PIC16CR83,PIC16CR84,PIC16F785,PIC16HV785,PIC16F913,
      PIC16F916,PIC16F917,PIC16F946,PIC16F610,PIC16HV610,PIC16F616,PIC16HV616,
      PIC16F882,PIC16F883,PIC16F884,PIC16F886,PIC16F887,
      PIC16F872,PIC16F873,PIC16F874,PIC16F876,PIC16F877,PIC16F737,
      PIC16F747,PIC16F767,PIC16F777,PIC16F870,PIC16F871,PIC16F84,PIC16F84A,
      PIC16F819,PIC16F73,PIC16F74,PIC16F76,PIC16F77,PIC16F73A,
      PIC16F74A,PIC16F76A,PIC16F77A,PIC16F72,PIC16F684,PIC16F688,PIC16F716,
      PIC16LF722,PIC16F723,PIC16LF723,PIC16F724,PIC16LF724,PIC16F726,
      PIC16LF726,PIC16F727,PIC16LF727,PIC16F631,PIC16F677,PIC16F685,
      PIC16F689,PIC16F690


      Read Binary
      https://sourceforge.net/p/gcbasic/discussion/579126/thread/ee8a7f26/?limit=25#a77d


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/gcbasic/discussion/579126/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

       
  • CDRIVE

    CDRIVE - 2017-05-20

    That list was from Tina10. I don't have a list for Tina11 version. It's quite vast but accessed through a scrolled properties window. Getting that list into a text file won't be easy.

    As you can see I'm using a 16F628A that I don't see in the Tina10 list but it's in V11. The datasheet specs it to 20MHz.

    Thanks,
    Chris S.

     
1 2 > >> (Page 1 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.