Overthinking what must be simple


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2006
    Posts
    36

    Default Overthinking what must be simple

    Hi experts,
    I would like to create a physically changable but otherwise static byte variable by using an 8 pin DIP switch for a 16f877a to "read." (Apply 5V from a common rail to the pins for "ON")

    So, I'd like to set the DIP before power-on to, say, 01111111 to represent the number 127, which will be a critical element but not change for the remainder of the program unless I reset or do some other interrupt. (A hidden away "field changeable" property without the need for displays, etc.)

    Such that the pins on/off will create a variable in the range from 0-255.

    Assuming I have all the pins set to digital, et al, I can't think of how to place the bits from the pins into place.

    This has to be so simple I'll be embarrased, but can anyone clue me in?

    Thanks

    Chris

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ChrisHelvey View Post
    Hi experts,
    I would like to create a physically changable but otherwise static byte variable by using an 8 pin DIP switch for a 16f877a to "read." (Apply 5V from a common rail to the pins for "ON")

    So, I'd like to set the DIP before power-on to, say, 01111111 to represent the number 127, which will be a critical element but not change for the remainder of the program unless I reset or do some other interrupt. (A hidden away "field changeable" property without the need for displays, etc.)

    Such that the pins on/off will create a variable in the range from 0-255.

    Assuming I have all the pins set to digital, et al, I can't think of how to place the bits from the pins into place.

    This has to be so simple I'll be embarrased, but can anyone clue me in?

    Thanks

    Chris
    Hi Chris,
    This is a fairly well traveled rod from noobieville , what you are diong is BCD and you are correct re: 127. http://melabs.com/resources/articles/bcdnumbers.htm
    http://www.picbasic.co.uk/forum/arch....php/t-66.html
    Here is a program that varies an output time according to BCD input.
    Code:
    ASM
        ifndef __12F675
            error "12F675 not selected"
        endif
    ENDASM
    @ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF 
    
        PCON = %00000011    ' BOD off, No POR flag set
    DEFINE  NO_CLRWDT 1
    DEFINE OSC 4
    
        OSCCAL = %1111100   ' SET OSC TO MAXIMUM SET OSC TO 4 MHZ INTERNAL
        CMCON   = 7	        ' Disable Analog Comparators
        ANSEL   = 0         ' Disable A/D module, all digital
        WPU.4 = 0           ' Disable weak pull up on GPIO.4
        
        B0    VAR word         ' Byte holds 0 - 255 Word 0 - 65535
        X     VAR WORD
        
        Index VAR BYTE         ' Byte variable only has to hold 0 - 15
        
        LED   VAR GPIO.5       ' Alias GPIO.5 as LED will wire to
                               ' transister base to control device
        
        TRISIO = %00011111     ' RA0 to RA3 inputs for DIP switch
        
        OPTION_REG = %11111111 ' Dis/Enable internal pull-ups Bit 7
                               ' Bits 0-2 prescaler 
                               ' Bit 3 prescaler assignment 1 = WDT 0 = TMR0
                               ' Bit 4 TMR0 source edge select bit
                               ' 1 trigger on High to Low 
                               ' 0 trigger on Low to High
                               ' Bit 5 TOCS TMR0 clock source select
                               ' 1 = transition on GP2
                               ' 0 = internal clock (CLKOUT)
                               ' Bit 6 INTEDG Interrupt Edge Select
                               ' 1 = Interrupt on rising edge GP2
                               ' 0 = Interrupt on falling edge GP2
                               ' Bit 7 Pullup resistor enable bit
                               ' 1 = disabled 0 = enabled by individual port 
                               ' latch values.
    
    
        
    
        
    Main:  
         IF (GPIO.4 = 1) THEN
         B0 = 2500 ' Must be a word variable to hold this value
         else      ' HE HE else it,"ain't gonna work!"
         B0 = 250  ' and it will always default to this value!
      endif    
      
        Index = GPIO & $0F   ' Read DIP switch AND mask result
        LOOKUP2 Index,[200,300,400,500,600,700,800,900,1000,2000,_
        3000,4000,5000,6000,7000,8000],X ' notice the X variable
        
        
        HIGH LED
        PAUSE b0
        LOW LED
        PAUSE X    ' X is a WORD variable so it can store 300 to 8000
        
    GOTO Main
    
        END
        POKECODE @$3FF, $50  ' reintroduces osscal correction data
                             ' $3FF tells it where to code, $50 is the
                             ' Cal value unique to each individual chip.
    JS
    Last edited by Archangel; - 8th July 2008 at 03:49.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Sep 2004
    Location
    Mentor, Ohio
    Posts
    352


    Did you find this post helpful? Yes | No

    Smile

    Hi Chris,

    So you have 8 dip switches with one switch on each pin of a port. You want the switch being set to ON to be a high or 5vdc. You would have 8- 4.7K or 10K resistors, again one resistor on each pin with the other side of each resistor going to Gnd. These are called "pull down" resistors.

    Now to read the value set by the switches simply read the "PortValue".
    An example would be:
    PortValue VAR BYTE
    PortC = PortValue
    If PortValue = 127 Then

    You can also use "Select Case" instead of the multiple IF .. Then 's
    You control when the switches get read with your program. I used this setup once a couple of years ago reading the value of a PC card in a card cage from a dip switch setting.

    HTH,

    BobK

  4. #4
    Join Date
    Jul 2006
    Posts
    36


    Did you find this post helpful? Yes | No

    Default

    Ah yes, I have my residence in NoobieVille and am traversing the road to dangerous and wild lands more frequently than I am comfortable with. He He..

    I knew it had to be simple. So, basically, the whole port is just assigned to a variable.

    ....of course....

    Overthinking the obvious I was!

    If I wanted 16 bits, and thus a word variable, would I simply add them together?
    PortValue VAR WORD
    PortValue = PortB + PortC ???

    Thank you for your reply. Now I can try things I REALLY don't understand. "Heading deep into the mountain canyon."

    Chris

  5. #5
    Join Date
    Jul 2006
    Posts
    36


    Did you find this post helpful? Yes | No

    Default

    No, wait a minute. Adding won't do it, will it? 01111111 + 01111111 = 11111110

    Hmm....

  6. #6
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Check out the lookup table in the program I listed, maybe you dont need so many settings . . .
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  7. #7
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ChrisHelvey View Post
    No, wait a minute. Adding won't do it, will it? 01111111 + 01111111 = 11111110

    Hmm....
    Not sure how you would do it but 2 ports probably make a word something like
    MyVar VAR WORD
    PortB myvar.lowbyte:
    PortC myvar.HiByte . . .
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  8. #8
    Join Date
    Jul 2006
    Posts
    36


    Did you find this post helpful? Yes | No

    Default

    Yeah, that makes sense. I forgot about the lowbyte, HiByte reference.
    No, I don't need that many settings. I was just curious.

    Thanks a lot. I think I can get it done now.


    Chris

  9. #9
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ChrisHelvey View Post
    No, wait a minute. Adding won't do it, will it? 01111111 + 01111111 = 11111110

    Hmm....
    Yep,
    127 + 127 = 254
    As long as you only use seven bits on each port and add those you will stay below the 255 limit.

    How many combos can you come up with using two 7 bit numbers like that. One heck of a comination lock.
    Dave
    Always wear safety glasses while programming.

  10. #10
    Join Date
    Oct 2005
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    And, if you wanted to do this with 3 pins instead of 8 add a 165 PISO shift register.

Similar Threads

  1. Simple RF remote control code
    By Bruce in forum Code Examples
    Replies: 13
    Last Post: - 22nd January 2014, 10:45
  2. A Serial GLCD 128x64 Simple Project
    By Oldspring in forum Off Topic
    Replies: 0
    Last Post: - 8th March 2010, 20:58
  3. Simple Blinking LED - WTF!!
    By johnnylynx in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 1st February 2010, 06:19
  4. Replies: 4
    Last Post: - 7th September 2005, 14:11
  5. Simple Serial communications
    By kitcat in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 18th July 2005, 19:42

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

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