1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Multiplex switches

Discussion in 'Microcontrollers' started by AtomSoft, Mar 3, 2008.

  1. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    ok. So hows the schematic so far? You think if i program the junebug blink code into the pic using this schematic .. will it work ?
     
  2. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    While im waiting for a reply on the above take a look at this. Does it make sense? I was trying to integrate the ICSP with a breadboard. What cha think?

    [​IMG]

    Basically you jump from the board to the breadboard then jump the pins of the chip to the locations above. I might just make it like a a plug and play thing So you put chip on board and all is already connected to breadboard.
     
    Last edited: Mar 5, 2008
  3. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Have a look at this thread for some pics of how it's done.

    "Plug and play" will only work if you only use certain PICs. Different models have MCLR, PGD and PGC in different places.

    VPP goes to the MCLR pin. This connection also gets a 33K or so pullup resistor to VDD.

    5V and Ground go to the power rails.

    PGD and PGC go to the PGD and PGC pins on the MCU.

    Very simple.

    To make the breadboard socket I took a 2x5 ICD socket and soldered it directly to a 2x5 piece of two row header.
     
    Last edited: Mar 5, 2008
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US

    Thanks for the info. I actually was just bored and needed something to do lol I am not going to make that breadboard stuff was just looking into it while i wait for anyone who can confirm that this is ok:

    [​IMG]
     
  6. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    So witch 1 2 and 3 on the Junebug has to be ON in order to debug it am i correct?
     
    Last edited: Mar 5, 2008
  7. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Also is a macro like a Function ? Like in VB6 you would:
    Code (text):

    public function SayHi (TheName as string)
          msgbox "Hi " & TheName & "!"
    end function
     
    So for junebug it would be like

    Code (text):

    LED macro x,y ; MACRO LED <PORTA>, <TRISA>

    Public function LED(x, y) as long
          LATA = x
          TRISA = y
         
          Delay();
    End function
     
    and to call it would be like

    Code (text):

    LED1 = LED ("00000001", "10111110")
     
    Would that be a good comparison to ?

    Code (text):

    LED macro x,y ; MACRO LED <PORTA>, <TRISA>
          movlw x
          movwf LATA       ; LATA = x
          movlw y
          movwf TRISA      ; TRISA = y
          call Delay           ; call the Delay subroutine
          endm                 ; end macro
     
    and
    Code (text):

    LED1 LED b'00000001', b'10111110'
     
     
    Last edited: Mar 5, 2008
  8. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    I stated posting this this AM but did not submit it.

    The most common way to hook up the ICSP is to put a male pinheader on the target board. Use a ribbon cable with female IDC connectors on both ends. Like in the picture of the Junebug with a Mongoose.
     
  9. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    I did change it back to Male Header on the schematic. I was just asking if the above schematic seems ok. Like if i wire that way would i be able to program it and also use it.
     
  10. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    What is the purpose of R4 the 22K resistor between RA0 and VDD? RA0 in one of the three charlieplex lines.

    When drawing a schematic do not run the lines inside the PIC and through the text. It makes it hard to look at. It is OK, even common, to have lines cross in a schematic. They only connect where there is a dot at the intersection.

    Do not think of a macro as a function. A function is a single bit of code that you call with parameter substitution (goto and return from).

    A macro is a pattern that can be used with substitution. The pattern is expanded inline. So if you use the macro six times you will have six copies of the macro code. A macro definition does not generate any code. Each time you use the macro code is generated.

    NOTE: Compilers have the option of using a new copy of a function for each call. This is called an inline function. But for reasons of sanity I choose to ignore it here.
     
  11. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Ok i think i have a somewhat clear outlook on marcos now so it actually copies the code everytime its called?

    so for every "LED1 LED b'00000001', b'10111110' ; LED <PORTA>, <TRISA>" there is a :
    Code (text):

    LED macro x,y ; MACRO LED <PORTA>, <TRISA>
    movlw x
    movwf LATA ; LATA = x
    movlw y
    movwf TRISA ; TRISA = y
    call Delay ; call the Delay subroutine
    endm ; end macro
    Count equ 0 ; delay loop counter
    org 0 ; reset vector
    bsf ADCON1, 0 ; make RA0 digital
     
    Hows this:
    [​IMG]
     
  12. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    Regarding assembler macros. It is time to fire up MPLAB write some code with macros and assemble it. Look at the listing to see what is done each time you use a macro. You do not need me check it for you... you have the tools.

    Schematic looks better, could work as is.

    You might want to add a connector for power unless you always intend to power it via the ICSP connector.

    Swap C1 end for end so that the curved (earth/gnd) part of the device connects to VSS. It is not a polarized cap so it would work either way but it is better to correct it just the same.
     
  13. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Ok ill try to code a macro or use bills and edit it to try to make my own and ill flip the cap.
     
  14. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Code (text):

      0002    0E01     MOVLW 0x1                      17:    LED1   LED b'00000001', b'10111110'    ; LED <PORTA>, <TRISA>
      0004    6E89     MOVWF 0xf89, ACCESS
      0006    0EBE     MOVLW 0xbe
      0008    6E92     MOVWF 0xf92, ACCESS
      000A    EC27     CALL 0x4e, 0
      000E    0E40     MOVLW 0x40                     18:    LED2   LED b'01000000', b'10111110'    ; LED <PORTA>, <TRISA>
      0010    6E89     MOVWF 0xf89, ACCESS
      0012    0EBE     MOVLW 0xbe
      0014    6E92     MOVWF 0xf92, ACCESS
      0016    EC27     CALL 0x4e, 0
     
    So it actually recreated the code for each time LED was called. Does this waste more memory based on how many times you use it?

    So its sort of like a function but instead of having that one block of code to run anything it copys the block to the address where the call to it was made and adds the information from the call into the copy of itself.

    Sounds about right?

    I also notice it called 0x4e which is 004E in the code which in turn is the DELAY address. Pretty cool stuff. Thanks for the info .

    Just noticed something im going to try and make a macro brb 2 min
     
  15. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    How would i go about setting 1 pin ? Like

    TRISA BIT 2 to ouput?

    I know how to set them all like 00111101 thats sets every pin tho.

    Like can i specify which pin i want to set like a TRISA.7 = 0 or something

    how would i do that in ASM or do i always have to set the others as well?

    is it BCF and BSF?
    like
    BCF TRISA,7,0
     
    Last edited: Mar 5, 2008
  16. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    So would this be how the HIGH(LED#) would work?

    Code (text):

    HIGHA  macro x          ; MACRO HIGHA <PORTA BIT>
        BCF TRISA,x,0       ; Clear the TRISA.X to make it output
            BSF LATA,x,0        ; Set LATA.X to High
        endm               ; end macro

     
    or

    Code (text):

    HIGHA  macro x          ; MACRO HIGHA <PORTA BIT>
        BCF TRISA,x,0       ; Clear the TRISA.X to make it output
            ;BTG PORTA x        ; Toggle PortA.X or should i use
            BSF LATA,x,0         ; BIT SET LATA.X
        endm               ; end macro

     
    Used like:
    HIGHA 3 ; This sets bit 3 on trisa to output and sets it high.

    These seem like some nice commands i barely see in use:

    CPFSEQ
    CPFSGT
    CPFSLT
    MOVF
    MOVFF
    MOVWF
     
    Last edited: Mar 5, 2008
  17. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    Look at TRISA and PORTA in the SFR (Special Function Registers) window and you can see the bits changing as you step through the code.

    Macros do not replace functions. Functions do not replace macros.

    A macro is mostly just a shorthand (shortcut) way of generating code. Each time you use a macro it generates code based on the macro definition. But it does not generate any more code then if you had written the same code without the macro. It saves you time and keystrokes. Makes the code more readable if done correctly.

    A function is code you call. In general there is only one copy of it. It saves program memory but there is overhead in setting up the parameter passing.

    For shorter "things" a macro is more efficent.

    You can learn more by running code in the simulator then I could teach you on the forum. I am not fluent in C18 assembler and do not care to be. Paid my dues a long time ago.
     
  18. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Got this to work pretty cool. I seen it in SwordFish i think. Thought id make it in ASM
    Code (text):

            list p=18F1320
            include <p18F1320.inc>
            CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

    HIGHA  macro x          ; MACRO HIGHA <PORTA BIT>
           BCF TRISA,x       ; Clear the TRISA.X to make it output
           BSF LATA,x         ; BIT SET LATA.X
           call Delay
           endm                ; end macro
    LOWA  macro x           ; OPPOSITE OF : MACRO HIGHA <PORTA BIT>
           BCF TRISA,x        ; Clear the TRISA bit
           BCF LATA,x      'Clear the LATA bit
           call Delay
           endm                ; end macro
           Count equ 0
           org 0
    Start
           movlw     b'01111111'  ;This is just to see what changed in SFR (7 has to be 0  in LATA )
           movwf LATA              ;set it the same
           movwf TRISA             ;set it the same
           HIGHA 2
           LOWA 2
           goto Start
    Delay
        decfsz Count, f                 ; decrement Count and skip when zero
            goto $-2                        ; not zero? repeat
            return                          ; return
            END
     
     
    Last edited: Mar 5, 2008
  19. Hero999

    Hero999 Banned

    Joined:
    Apr 6, 2006
    Messages:
    14,902
    Likes:
    79
    Location:
    England
    Here's a way to multiplex 10 switches. I got the idea from blueroomelectronics' website, except he uses it for LEDs, so I thought, why not do it with siwtches?

    If you worried about the diodes, you can get SMT packages that contain two in a package.

    This will only use three I/O pins for 10 switches or just two if you don't use the reset pin.
     

    Attached Files:

  20. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    That's the method I use on the Dragonfly. Of course a PIC with more pins is often better / cheaper than adding glue logic.
    I used the 4017 to make it impossible to run more than one display / switch at once. Another popular demux is the 74LS138
     
  21. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    wow nice im looking at Data Sheet for 4017 right now. The 74LS138 looks a bit confusing to me lol. Maybe because i just went over quickly will be sure to check out both.

    Tell me what you think of this idea:

    I dont own a laser printer so i hand draw my schematics onto a PCB But i seem to miss the chip sizes. Like i drill wrong. But i bought a Pre Made Radio Shack board and just marked it so i can drive perfect(close to it) holes and fit a chip nicely here are some pics:

    [​IMG]

    and

    [​IMG]

    The other holes not in the corner with the black lines where hand done and none are good. So i would suggest PCB Pref board if hand drawn or even if printed just to get perfect or close to perfect holes.(without as much movement)
     

Share This Page