PIC16F627A - LED does not lit

Discussion in 'Embedded Systems and Microcontrollers' started by tekinzen, Oct 28, 2010.

  1. tekinzen

    Thread Starter Member

    Nov 22, 2009
    30
    0
    A very frustrating problem I'm facing: I cannot make that LED to lit at all. Here is the simple assembly program, it just turns on an LED, nothing special:

    Code ( (Unknown Language)):
    1.  
    2. #include "p16f627a.inc"
    3.  
    4. STATUS equ 03h
    5. TRISA equ 85h
    6. PORTA equ 05h
    7.  
    8. bsf  STATUS,5
    9. movlw 00h  
    10. movwf TRISA  
    11. bcf  03h,5
    12. movlw 02h  
    13. movwf PORTA  
    14. end
    15.  
    My LED has the positive terminal connected to pin 18 on the PIC (bit 2 of PORTA) and negative terminal to the negative of the power supply (it actually connects through a resistor first).

    I know the LED works because I can connect its positive terminal to the Vdd terminal of the PIC and it turns on fine.

    I'm also using absolute and not relocable code.

    Could you please advise, what am I doing wrong?
     
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Make sure the comparators are disabled as well as just setting TRISA to output.

    Try using PortB for Digital I/O, it gets around that comparator problem.
     
  3. tekinzen

    Thread Starter Member

    Nov 22, 2009
    30
    0
    I have already set TRISA to all ports as output.
    Also tried turning the comparator off and using TRISB/PORTB (set bit 2, pin 7), no luck. The damn LED won't turn on :mad:
     
  4. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    When using PORTB/TRISB, do you get voltage on any pins when measuring with a multimeter?

    I'm looking for "known good" code for the 16F627 right now, figured I'd post this in the interim. It's on one of my thumb drives here somewhere...
     
  5. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Here is one in C (SourceBoost C, free download)

    The portion related to flashing an LED on PortA.6

    Code ( (Unknown Language)):
    1.  
    2. #pragma DATA _CONFIG, _CP_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
    3.  
    4. #define Heartbeat porta.6
    5.  
    6. void main( void ){
    7.     //Configure port A
    8.     trisa = 0x2F;
    9.     //Initialize port A
    10.     porta = 0x00;
    11.    
    12.     set_bit(pcon,3);  // 4Mhz Internal Oscillator
    13.  
    14.     cmcon = 7; //disable comparators
    15.     while( 1 )
    16.     {
    17.         Heartbeat=on;     // Heartbeat on PortA6, every top of loop a short blink.
    18.         delay_ms(100);
    19.         Heartbeat=off;    
    20. }
    21.  
    This works without a problem on a 16F627 compiled with SourceBoost C

    The only difference in the code (other than using PortA.6 instead of PortA.1 & 2) is the cmcon set to 7, and the internal oscillator defined.

    Code ( (Unknown Language)):
    1.  
    2. MOVLW 0x07
    3.     MOVWF gbl_cmcon
    4.  
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    The data sheet will give you many assembler examples. Just look at example 5.1. Also you must read the section named 14.0 SPECIAL FEATURES OF THE CPU. And set the configuration bit so they fir your application. Do you use a demo board or are you bread-boarding?
    Also some tip. I often recommend that beginners use internal osc if possible. And the banksel option. At the end of the header file you will find the correct naming for setting the configuration words

    Example Note this is for a 16F690 running internal osc 4 Mhz.
    Code ( (Unknown Language)):
    1. #include <p16F690.inc>
    2.     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_ON & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
    3.     cblock 0x20
    4. Delay1                   ; Define two file registers for the
    5. Delay2                   ; delay loop
    6.      endc
    7.  
    8.      org 0
    9. Start:
    10.      banksel TRISC
    11.      movlw 0
    12.      tris PORTC                ;test instr
    13. bcf       TRISC,0             ; make IO Pin B.0 an output
    14.      banksel PORTC
    15. MainLoop:
    16.      bsf       PORTC,0             ; turn on LED C0
    17. OndelayLoop:
    18.      decfsz    Delay1,f            ; Waste time.  
    19.      goto      OndelayLoop         ; The Inner loop takes 3 instructions per loop * 256 loopss = 768 instructions
    20.      decfsz    Delay2,f            ; The outer loop takes and additional 3 instructions per lap * 256 loops
    21.      goto      OndelayLoop         ; (768+3) * 256 = 197376 instructions / 1M instructions per sec sec.
    22.                                    ; call it a two-tenths of a second.
    23.  
    24.      bcf       PORTC,0             ; Turn off LED C0
    25. OffDelayLoop:
    26.      decfsz    Delay1,f            ; same delay as above
    27.      goto      OffDelayLoop
    28.      decfsz    Delay2,f
    29.      goto      OffDelayLoop
    30.      goto      MainLoop            ; Do it again...
    31.      end

     
  7. wannaBinventor

    Member

    Apr 8, 2010
    179
    4

    For the record, pin 18 on that PIC is PORTA,1 ----- not PORTA,2.

    The code looks correct for using pin 18, but I wanted to make sure that you were using PIN 18 (PORTA,1) and not PIN 1 (PORTA,2).
     
  8. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    Have you tested the LED with your meters DIODE test?

    Have you checked to see if there is 5v on pin 18?
     
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    On that MCU I think port C do not have any other functions than digital IO. Så you only have to set TRISC correct in order for it to work. But if the configuration word is not correct. Your chip may behave as it dead.
     
  10. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    It's also vital to put something like:
    Loop
    goto Loop
    before the end instruction.
    Otherwise the code just carries on through all the blank code on the PIC then starts again.
    But you would see some output so that isn't the only problem.
     
  11. tekinzen

    Thread Starter Member

    Nov 22, 2009
    30
    0
    Thank you all for your replies.

    I'm am testing this on a breadboard. My power supply is a 9v battery using a 5v regulator to provide power to the PIC.

    I'm not using a bypass capacitor to filter noise between the regulator and the PIC Vdd/Vss. Could that be a problem? I know that noise could screw up the logic of the PIC.

    I have not measured the current on the pins yet other than Vdd and Vss - was doing the experiment late at night...

    As for the PORTA pin bits, I'm getting confused now:

    i.e.:

    Code ( (Unknown Language)):
    1.  
    2. [FONT=Times New Roman][COLOR=red][COLOR=red][FONT=Times New Roman]Port A Pin       RA4     RA3     RA2     RA1     RA0[/FONT][/COLOR]
    3. [COLOR=red][FONT=Times New Roman]Bit Number      4          3          2          1          0[/FONT][/COLOR]
    4. [COLOR=red][FONT=Times New Roman]Binary            0          0          0          1          0 [/FONT][/COLOR]
    5.  
    6. 00010 = 02h
    7.  
    8. [/COLOR][/FONT]
    In my code the value 02h (00010) should set pin 18 high which corresponds to RA1 of PORTA but that is bit 1 not bit 2 (RA2) :confused:
    What I have found most interesting is that if I set all ports of TRISA as inputs (b'11111111') the LED lits no matter what pin I use to connect to the PIC. This is without setting any bits high. I am even more confused now...
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    What do use for configuration setting? That is the __config () setting. Please show us your current code. What kind of oscillator do use. Is it internal, or external. And please show us your current code.
     
  13. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Definately use the capacitor.
    As has already been mentioned you need to turn the comparitors off.
    From the datasheet:
    Code ( (Unknown Language)):
    1. EXAMPLE 5-1: Initializing PORTA
    2. CLRF PORTA ;Initialize PORTA by
    3. ;setting output data latches
    4. MOVLW 0x07 ;Turn comparators off and
    5. MOVWF CMCON ;enable pins for I/O
    6. ;functions
    7. BCF STATUS, RP1
    8. BSF STATUS, RP0;Select Bank1
    9. MOVLW 0x1F ;Value used to initialize
    10. ;data direction
    11. MOVWF TRISA ;Set RA<4:0> as inputs
    12. ;TRISA<5> always
    13. ;read as ‘1’.
    14. ;TRISA<7:6>
    15. ;depend on oscillator mode
    Then :
    BCF STATUS, RP0 ;for bank 0 - they omit in the datasheets.
     
  14. tekinzen

    Thread Starter Member

    Nov 22, 2009
    30
    0
    I am using the internal oscilator. I have already posted my code on the first page:

    Code ( (Unknown Language)):
    1.  
    2. #include "p16f627a.inc"
    3.  
    4. STATUS equ 03h
    5. TRISA equ 85h
    6. PORTA equ 05h
    7.  
    8. bsf  STATUS,5
    9. movlw 00h  
    10. movwf TRISA  
    11. bcf  03h,5
    12. movlw 02h  
    13. movwf PORTA  
    14. end
    15.  
    - move to bank 1
    - set all TRISA ports as output
    - move to bank 0
    - set bit 02h of PORTA high (pin 18)
     
  15. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    But how do tell the PIC to use internal oscillator? So PLEASE once more what do you use as configuration word or configuration bits. Mention in the data sheet in section
    14.1 Configuration Bits
     
  16. tekinzen

    Thread Starter Member

    Nov 22, 2009
    30
    0
    Maybe that IS my problem. I don't use any config bits, just the code that I have pasted above.

    I'm saying that I'm using the internal oscilator because I don't have an external in my circuit. Are you refering to the internal clock, right?

    Sorry, I'm new at this.
     
  17. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718

    Yes, you need to tell it which clock to use. What are you using to program the PIC? There are sometimes fuse options there, but setting them in the code is just as easy.
     
  18. tekinzen

    Thread Starter Member

    Nov 22, 2009
    30
    0
    I'm writing the code in MPLAB and programming with a "DIY-149bc" compatible programmer and "DIY-K149" (kitsrus.com)software.
     
  19. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    If you find the folder like this on your computer it has templates for most PICs:
    C:\Program Files\Microchip\MPASM Suite\Template\Code
    Change anything in the __CONFIG line that needs changing
    Code ( (Unknown Language)):
    1. ;**********************************************************************
    2. ;   This file is a basic code template for assembly code generation   *
    3. ;   on the PIC16F627A. This file contains the basic code              *
    4. ;   building blocks to build upon.                                    *
    5. ;                                                                     *
    6. ;   Refer to the MPASM User's Guide for additional information on     *
    7. ;   features of the assembler (Document DS33014).                     *
    8. ;                                                                     *
    9. ;   Refer to the respective PIC data sheet for additional             *
    10. ;   information on the instruction set.                               *
    11. ;                                                                     *
    12. ;**********************************************************************
    13. ;                                                                     *
    14. ;    Filename:        xxx.asm                                           *
    15. ;    Date:                                                            *
    16. ;    File Version:                                                    *
    17. ;                                                                     *
    18. ;    Author:                                                          *
    19. ;    Company:                                                         *
    20. ;                                                                     *
    21. ;                                                                     *
    22. ;**********************************************************************
    23. ;                                                                     *
    24. ;    Files Required: P16F627A.INC                                     *
    25. ;                                                                     *
    26. ;**********************************************************************
    27. ;                                                                     *
    28. ;    Notes:                                                           *
    29. ;                                                                     *
    30. ;**********************************************************************
    31.  
    32.     list      p=16f627A           ; list directive to define processor
    33.     #include <p16F627A.inc>       ; processor specific variable definitions
    34.  
    35.     errorlevel  -302              ; suppress message 302 from list file
    36.  
    37.     __CONFIG   _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT
    38.  
    39. ; '__CONFIG' directive is used to embed configuration word within .asm file.
    40. ; The lables following the directive are located in the respective .inc file.
    41. ; See data sheet for additional information on configuration word settings.
    42.  
    43.  
    44.  
    45.  
    46. ;***** VARIABLE DEFINITIONS
    47. w_temp        EQU     0x7E        ; variable used for context saving
    48. status_temp   EQU     0x7F        ; variable used for context saving
    49.  
    50.  
    51.  
    52.  
    53.  
    54. ;**********************************************************************
    55.     ORG     0x000             ; processor reset vector
    56.     goto    main              ; go to beginning of program
    57.    
    58.  
    59.     ORG     0x004             ; interrupt vector location
    60.     movwf   w_temp            ; save off current W register contents
    61.     movf    STATUS,w          ; move status register into W register
    62.     movwf    status_temp       ; save off contents of STATUS register
    63.  
    64. ; isr code can go here or be located as a call subroutine elsewhere
    65.  
    66.  
    67.     movf    status_temp,w     ; retrieve copy of STATUS register
    68.     movwf    STATUS            ; restore pre-isr STATUS register contents
    69.     swapf   w_temp,f
    70.     swapf   w_temp,w          ; restore pre-isr W register contents
    71.     retfie                    ; return from interrupt
    72.  
    73.  
    74. main
    75.  
    76. ; remaining code goes here
    77.  
    78.     goto    main          ;loop forever, remove this instruction, for test only
    79.  
    80.  
    81. ; initialize eeprom locations
    82.  
    83.     ORG    0x2100
    84.     DE    0x00, 0x01, 0x02, 0x03
    85.  
    86.  
    87.     END                       ; directive 'end of program'
    88.  
    89.  
     
  20. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Well Your problem is also that you do not listen then we try to help you:p
    Bu no need to be grumpy. See how Mark77 does it in posting 19 by using the __config statement. For beginners I recommend using _MCLRE_OFF. As it removes one posible source of error. Then working with PIC MCUs and assembler you will always find the configuration bits naming at the end of the include file. p16f627a.inc in your case.
     
Loading...