What are these pins for?

Discussion in 'Embedded Systems and Microcontrollers' started by ke5nnt, Sep 24, 2009.

  1. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    I'm working on trying to understand PIC architecture and assembly language, and having a heck of a time I might add.

    I've been working on this project for a while using a schematic I've heavily modified from the original schematic, though the pin connections are the same to the PIC.

    The end result is essentially an array of LEDs flashing alternately, much like police/fire type LED lights. Here is what I understand regarding the connections to the PIC pins:

    PIC is 16F628A

    Pin 4, MCLR is connected to +5V through a resistor (I kinda get this)
    Pin 5, Vss to ground (gotta have ground, ok, I get that)
    Pin 6, RB0 to MOSFET and Ground (output controlling flashes)
    Pin 7, RB1 to MOSFET and Ground (same as above)
    Pin 14, Vdd voltage in (I get it)
    Pins 15 and 16 to crystal for timing (I get it)

    What I'm confused over, are pins 17 and 18 RA0/AN0, RA1/AN1 respectively. Why make these connections? Are they being used as an I/O or analog comparators? Can someone explain why these are set up this way?
  2. jpanhalt


    Jan 18, 2008
    Another option is that the original designer simply tied them high by analogy with CMOS logic. That is not necessary for the PICs, particularly when you set them as outputs.

    Also, Microchip recommends a small capacitor from MCLR to ground so that resetting is prevented even if V+ dips. See the datasheet.

    What values for R3 and R4 are suggested?

  3. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    6.8k on both
  4. jpanhalt


    Jan 18, 2008
    That seems OK. Presumably, R5 and R6 are relatively low value, such as 100 to 200 ohm?

  5. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    The 16F628A has two comparators, it does not have an A/D.
  6. S_lannan

    Active Member

    Jun 20, 2007
    i was under the same impression.
    the pic16f628a does indeed have comparators.
    CMCON = 7 would turn them off.
    i can only assume where alberto said CMCOM = 7 was just a typographical error.
  7. S_lannan

    Active Member

    Jun 20, 2007
    What I'm confused over, are pins 17 and 18 RA0/AN0, RA1/AN1 respectively. Why make these connections? Are they being used as an I/O or analog comparators? Can someone explain why these are set up this way?

    Now when you said you have heavily modified the schematic, did you modify any parts of the circuit that connects to these pins?

    Post the source code too, we may be able to answer some questions regarding what is confusing you about the programming of the pic.
  8. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    Let's dive a little deeper into what I'm doing.

    I had an interest in building emergency style lighting, its something of a passion (where the majority of my electrical interests lie). I found a schematic that included a .hex file. The original schematic didn't work for me, so with help from the members here, the schematic was modified to the point where it works quite well to the best of my knowledge (as in it does what it's supposed to and nothing gets real hot). The only thing that gets remotely hot is the LM7809 which provides 9V to the LED arrays. I plan to include room in the future for a heat sink for that component.

    Through this entire process, the .hex code has remained the same. My goal now is to learn assembly so I can design my own code, preferably a program that can store/use multiple led flash patterns that you can change by pressing a momentary switch, must like most commercial emergency warning lights available.

    THIS page is where I found the ORIGINAL schematic and the downloadable .hex file. I don't know what to do with a .hex file in order to "look at the software". .hex files are gibberish to me and I'm not sure about using a disassembler. And at this point, assembly still confuses me quite a bit, although I admit I'm starting to grasp some things. I'm reading a lot. The .hex file is on this page though, if you know how to look at it.

    Yes, mostly to upgrade parts to handle more than 2 LEDs (my circuit has 60 of them). All part replacements were based on suggestions from members of AAC. My first thread showing suggestions from members and some project progress can be viewed on THIS THREAD.

    Both are 200Ω yes.

    My entire project including parts list can be found in the attached .pdf

    Thank you for all your help.
  9. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    What are the part values? Do you need the crystal?
  10. BMorse

    AAC Fanatic!

    Sep 26, 2009
    From what I can see in regards to the schematics (if indeed the hex code has not changed from what you downloaded from the site) it looks like the original designer used those pins possibly as digital inputs, he has them pulled low with 10k resistors when the switch is open and they go high when the switch is closed (the switch has nothing to do with powering the pic, the pic is powered regardless of switch position, I'm just mentioning this in case someone was thinking that was a power switch ;)~). Why he has them both tied together is another thing. Have you tried to wire those pins exactly as they are shown??
    Maybe it is to switch to a different pattern of flashing lights??
    I do not have a 628A available at the moment or I would try it out :( ....

    Just My .02
  11. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    Thanks for the reply on that, glad someone looked into it and I appreciate it. The only time I tried the schematic exactly as it was shown, particularly in reference to those two pins where you ask
    is when I built the device the first time and then asked for input here when I could not get the thing to operate.

    It was suggested to me a while back:

    I will throw one together on a breadboard again and see what happens.

    Any other input from anyone else would still be helpful if there's another opinion.
  12. jpanhalt


    Jan 18, 2008
    I don't understand the 10K resistor stuff with respect to the original schematic. That schematic shows pins 17 & 18 pulled hard high. There is a resistor to ground -- its purpose is unknown to me.

    In any event, if you pull 17 & 18 hard high you should not use them as outputs. Check the datasheet. It specifically cautions against driving an output pin high or low with an external circuit due to excessive current. Of course, if your program sets the pins high and keeps them there, there may be no harm, but why risk it. I doubt they are being used as inputs, as they will always be high.

    Can you post the disassembled program?

  13. BMorse

    AAC Fanatic!

    Sep 26, 2009

    If you look at the original schematic, those inputs are held low by the 10K resistors, they will only go high when you close the switch, In his new schematic, it does show them as being held high all the time.
  14. jpanhalt


    Jan 18, 2008
    Not quite sure about that other original schematic . I was referring to the original version that was attached to post #1 of this thread. They are attached directly to IC2 output there.

  15. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    I wish I could, I'd like to look at it myself. I can't find a disassembler that works for me. I'm still new at working with code/software.
  16. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    I rebuilt the device on the breadboard with pins 17 & 18 wired from 5V source through a switch. With switch closed, the device works normally. With the switch open, the device does nothing. I have no idea why the original designer did that.
  17. jpanhalt


    Jan 18, 2008
    Can you post the hex code?


    Edit: Never mind. I found it.
    Last edited: Sep 30, 2009
  18. jpanhalt


    Jan 18, 2008
    I have attached a disassembled version. My disassembler did not have 16F628A, so I used 16F628. Caution, I have very little experience doing disassembly and interpreting it. This part of the code looks relevant:
    Code ( (Unknown Language)):
    2. h_002a:
    3.         bcf     portb,rb0                       ;0x06,0 ;002a 1006
    4.         bcf     portb,rb1                       ;0x06,1 ;002b 1086
    5.         btfsc   porta,ra0                       ;0x05,0 ;002c 1805
    6.         goto    h_0031                                  ;002d 2831
    7.         btfsc   porta,ra1                       ;0x05,1 ;002e 1885
    8.         goto    h_0031                                  ;002f 2831
    9.         goto    h_002a                                  ;0030 282a  
    RA0 and RA1 are pins 17 and 18, respectively. The first BTFSC checks to see if pin 17 is clear (low), if it is low, then it checks RA1 (pin 18). If that is low, it keeps checking both pins. If either is set (high), the program goes to h_0031. The zipped file shows what it does after that.

    These pins seem to be used together as an on/off switch. But, they are hardwired together, so I don't understand why the program was designed to check both. For fun, I would disconnect pin 17 and tie it low through a 10K resistor, just to see what happens. If you need those pins for some other purpose, this disassembled program could be used to make appropriate changes.

    Hopefully, these comments will stimulate more discussion.

  19. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    .hex file disassembled as well... not sure if this is how it's supposed to look, it's my first attempt at a disassembler:

    Code ( (Unknown Language)):
    2. #==========Mode:MPASM Assembler
    3.         include        ""
    4. Reg_003        EQU        H'0003'
    5. Reg_005        EQU        H'0005'
    6. Reg_006        EQU        H'0006'
    7. Reg_01F        EQU        H'001F'
    8. Reg_020        EQU        H'0020'
    9. Reg_021        EQU        H'0021'
    10. Reg_022        EQU        H'0022'
    11. Reg_023        EQU        H'0023'
    12. Reg_026        EQU        H'0026'
    13. Reg_027        EQU        H'0027'
    14. Reg_028        EQU        H'0028'
    17.         ORG        H'00000'
    18.         GOTO        Label_00009
    20. Label_00001
    21.         CLRF        Reg_023
    22.         MOVWF        Reg_022
    24. Label_00002
    25.         MOVLW        H'00FF'
    26.         ADDWF        Reg_022,1
    27.         BTFSS        Reg_003,0
    28.         ADDWF        Reg_023,1
    29.         BTFSS        Reg_003,0
    30.         GOTO        Label_00008
    31.         MOVLW        H'0003'
    32.         MOVWF        Reg_021
    33.         MOVLW        H'00DF'
    34.         CALL        Label_00003
    35.         GOTO        Label_00002
    36.         CLRF        Reg_021
    38. Label_00003
    39.         ADDLW        H'00E8'
    40.         MOVWF        Reg_020
    41.         COMF        Reg_021,1
    42.         MOVLW        H'00FC'
    43.         BTFSS        Reg_003,0
    44.         GOTO        Label_00005
    46. Label_00004
    47.         ADDWF        Reg_020,1
    48.         BTFSC        Reg_003,0
    49.         GOTO        Label_00004
    51. Label_00005
    52.         ADDWF        Reg_020,1
    53.         NOP        
    54.         INCFSZ        Reg_021,1
    55.         GOTO        Label_00004
    56.         BTFSC        Reg_020,0
    57.         GOTO        Label_00006
    59. Label_00006
    60.         BTFSS        Reg_020,1
    61.         GOTO        Label_00007
    62.         NOP        
    63.         GOTO        Label_00007
    64. Label_00007
    65.         RETURN        
    68. Label_00008
    69.         BCF        Reg_003,7
    70.         BCF        Reg_003,5
    71.         BCF        Reg_003,6
    72.         NOP        
    73.         RETURN        
    75. Label_00009
    76.         MOVLW        H'0007'
    77.         MOVWF        Reg_01F
    79. Label_00010
    80.         BCF        Reg_006,0
    81.         BCF        Reg_006,1
    82.         BTFSC        Reg_005,0
    83.         GOTO        Label_00011
    84.         BTFSC        Reg_005,1
    85.         GOTO        Label_00011
    86.         GOTO        Label_00010
    88. Label_00011
    89.         BSF        Reg_005,0
    90.         CLRF        Reg_027
    92. Label_00012
    93.         MOVLW        H'0004'
    94.         SUBWF        Reg_027,0
    95.         BTFSC        Reg_003,0
    96.         GOTO        Label_00017
    97.         CLRF        Reg_026
    99. Label_00013
    100.         MOVLW        H'0007'
    101.         SUBWF        Reg_026,0
    102.         BTFSC        Reg_003,0
    103.         GOTO        Label_00014
    104.         MOVLW        H'0001'
    105.         XORWF        Reg_006,1
    106.         BSF        Reg_003,5
    107.         BCF        Reg_006,0
    108.         MOVLW        H'0032'
    109.         BCF        Reg_003,5
    110.         CALL        Label_00001
    111.         BTFSS        Reg_005,0
    112.         GOTO        Label_00010
    113.         INCFSZ        Reg_026,1
    114.         GOTO        Label_00013
    116. Label_00014
    117.         BCF        Reg_006,0
    118.         CLRF        Reg_026
    120. Label_00015
    121.         MOVLW        H'0007'
    122.         SUBWF        Reg_026,0
    123.         BTFSC        Reg_003,0
    124.         GOTO        Label_00016
    125.         MOVLW        H'0002'
    126.         XORWF        Reg_006,1
    127.         BSF        Reg_003,5
    128.         BCF        Reg_006,1
    129.         MOVLW        H'0032'
    130.         BCF        Reg_003,5
    131.         CALL        Label_00001
    132.         BTFSS        Reg_005,0
    133.         GOTO        Label_00010
    134.         INCFSZ        Reg_026,1
    135.         GOTO        Label_00015
    137. Label_00016
    138.         BCF        Reg_006,1
    139.         INCFSZ        Reg_027,1
    140.         GOTO        Label_00012
    142. Label_00017
    143.         CLRF        Reg_028
    145. Label_00018
    146.         MOVLW        H'0007'
    147.         SUBWF        Reg_028,0
    148.         BTFSC        Reg_003,0
    149.         GOTO        Label_00023
    150.         CLRF        Reg_026
    152. Label_00019
    153.         MOVLW        H'0002'
    154.         SUBWF        Reg_026,0
    155.         BTFSC        Reg_003,0
    156.         GOTO        Label_00020
    157.         MOVLW        H'0001'
    158.         XORWF        Reg_006,1
    159.         BSF        Reg_003,5
    160.         BCF        Reg_006,0
    161.         MOVLW        H'0032'
    162.         BCF        Reg_003,5
    163.         CALL        Label_00001
    164.         BTFSS        Reg_005,0
    165.         GOTO        Label_00010
    166.         INCFSZ        Reg_026,1
    167.         GOTO        Label_00019
    169. Label_00020
    170.         BCF        Reg_006,0
    171.         CLRF        Reg_026
    173. Label_00021
    174.         MOVLW        H'0002'
    175.         SUBWF        Reg_026,0
    176.         BTFSC        Reg_003,0
    177.         GOTO        Label_00022
    178.         MOVLW        H'0002'
    179.         XORWF        Reg_006,1
    180.         BSF        Reg_003,5
    181.         BCF        Reg_006,1
    182.         MOVLW        H'0032'
    183.         BCF        Reg_003,5
    184.         CALL        Label_00001
    185.         BTFSS        Reg_005,0
    186.         GOTO        Label_00010
    187.         INCFSZ        Reg_026,1
    188.         GOTO        Label_00021
    190. Label_00022
    191.         BCF        Reg_006,1
    192.         INCFSZ        Reg_028,1
    193.         GOTO        Label_00018
    195. Label_00023
    196.         GOTO        Label_00011
    197.         GOTO        Label_00010
    199. Label_00024
    200.         SLEEP        
    201.         GOTO        Label_00024
    203.         ORG        H'02007'
    204.         DE        11,10
    205.         END        
  20. jpanhalt


    Jan 18, 2008
    The corresponding section in your listing is Label_00010. Take a look at the disassembled program I attached. The labels are a little more understandable to me, at least. For example, instead of reg_005, 0, it uses porta, ra0. Porta and RA0 are easily found in the datasheet.


    The disassembler I used is PICDIS by William Couture ver. 2003. He comments to write "picemulator@yahoo.com" with any questions. Sorry, I did not save the actual link that I used to download it. The disassembled program opens easily complete with colors in MPLab.
    Last edited: Sep 30, 2009