PIC16F627A - LED does not lit

Thread Starter

tekinzen

Joined Nov 22, 2009
30
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:

Rich (BB code):
#include "p16f627a.inc"
 
STATUS equ 03h
TRISA equ 85h
PORTA equ 05h

bsf  STATUS,5 
movlw 00h   
movwf TRISA  
bcf  03h,5 
movlw 02h   
movwf PORTA  
end
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?
 

thatoneguy

Joined Feb 19, 2009
6,359
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.
 

Thread Starter

tekinzen

Joined Nov 22, 2009
30
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:
 

thatoneguy

Joined Feb 19, 2009
6,359
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...
 

thatoneguy

Joined Feb 19, 2009
6,359
Here is one in C (SourceBoost C, free download)

The portion related to flashing an LED on PortA.6

Rich (BB code):
#pragma DATA _CONFIG, _CP_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT

#define Heartbeat porta.6

void main( void ){
    //Configure port A
    trisa = 0x2F;
    //Initialize port A
    porta = 0x00;
    
    set_bit(pcon,3);  // 4Mhz Internal Oscillator

    cmcon = 7; //disable comparators
    while( 1 )
    {
        Heartbeat=on;     // Heartbeat on PortA6, every top of loop a short blink.
        delay_ms(100);
        Heartbeat=off;     
}
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.

Rich (BB code):
MOVLW 0x07
    MOVWF gbl_cmcon
 

t06afre

Joined May 11, 2009
5,934
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
The banksel directive is an instruction to the assembler or linker to generate the appropriate bank selecting code for an indirect access of the register address specified by label. Only one label should be specified. The linker will generate the appropriate bank selecting code. For 14-bit instruction width (most PIC12/PIC16) devices, the appropriate bit set/clear instruction on the IRP bit in the STATUS register will be generated.

Example Note this is for a 16F690 running internal osc 4 Mhz.
Rich (BB code):
#include <p16F690.inc>
    __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_ON & _BOR_OFF & _IESO_OFF & _FCMEN_OFF)
    cblock 0x20
Delay1                   ; Define two file registers for the
Delay2                   ; delay loop
     endc
 
     org 0
Start:
     banksel TRISC
     movlw 0
     tris PORTC                ;test instr
bcf       TRISC,0             ; make IO Pin B.0 an output
     banksel PORTC
MainLoop:
     bsf       PORTC,0             ; turn on LED C0
OndelayLoop:
     decfsz    Delay1,f            ; Waste time.  
     goto      OndelayLoop         ; The Inner loop takes 3 instructions per loop * 256 loopss = 768 instructions
     decfsz    Delay2,f            ; The outer loop takes and additional 3 instructions per lap * 256 loops
     goto      OndelayLoop         ; (768+3) * 256 = 197376 instructions / 1M instructions per sec sec.
                                   ; call it a two-tenths of a second.
 
     bcf       PORTC,0             ; Turn off LED C0
OffDelayLoop:
     decfsz    Delay1,f            ; same delay as above
     goto      OffDelayLoop
     decfsz    Delay2,f
     goto      OffDelayLoop
     goto      MainLoop            ; Do it again...
     end

 
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).

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).
 

t06afre

Joined May 11, 2009
5,934
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.
 

Markd77

Joined Sep 7, 2009
2,806
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.
 

Thread Starter

tekinzen

Joined Nov 22, 2009
30
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.:

Rich (BB code):
Port A Pin       RA4     RA3     RA2     RA1     RA0
Bit Number      4          3          2          1          0
Binary            0          0          0          1          0 
 
00010 = 02h
 
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...
 

t06afre

Joined May 11, 2009
5,934
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.
 

Markd77

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

Thread Starter

tekinzen

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

Rich (BB code):
#include "p16f627a.inc"
 
STATUS equ 03h
TRISA equ 85h
PORTA equ 05h
 
bsf  STATUS,5 
movlw 00h   
movwf TRISA  
bcf  03h,5 
movlw 02h   
movwf PORTA  
end
- move to bank 1
- set all TRISA ports as output
- move to bank 0
- set bit 02h of PORTA high (pin 18)
 

t06afre

Joined May 11, 2009
5,934
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
 

Thread Starter

tekinzen

Joined Nov 22, 2009
30
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.
 

thatoneguy

Joined Feb 19, 2009
6,359
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.

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.
 

Thread Starter

tekinzen

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

Markd77

Joined Sep 7, 2009
2,806
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
Rich (BB code):
;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;   on the PIC16F627A. This file contains the basic code              *
;   building blocks to build upon.                                    *
;                                                                     *
;   Refer to the MPASM User's Guide for additional information on     *
;   features of the assembler (Document DS33014).                     *
;                                                                     *
;   Refer to the respective PIC data sheet for additional             *
;   information on the instruction set.                               *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:        xxx.asm                                           *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:                                                          *
;    Company:                                                         *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P16F627A.INC                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;**********************************************************************

    list      p=16f627A           ; list directive to define processor
    #include <p16F627A.inc>       ; processor specific variable definitions

    errorlevel  -302              ; suppress message 302 from list file

    __CONFIG   _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT 

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.




;***** VARIABLE DEFINITIONS
w_temp        EQU     0x7E        ; variable used for context saving 
status_temp   EQU     0x7F        ; variable used for context saving





;**********************************************************************
    ORG     0x000             ; processor reset vector
    goto    main              ; go to beginning of program
    

    ORG     0x004             ; interrupt vector location
    movwf   w_temp            ; save off current W register contents
    movf    STATUS,w          ; move status register into W register
    movwf    status_temp       ; save off contents of STATUS register

; isr code can go here or be located as a call subroutine elsewhere


    movf    status_temp,w     ; retrieve copy of STATUS register
    movwf    STATUS            ; restore pre-isr STATUS register contents
    swapf   w_temp,f
    swapf   w_temp,w          ; restore pre-isr W register contents
    retfie                    ; return from interrupt


main

; remaining code goes here

    goto    main          ;loop forever, remove this instruction, for test only


; initialize eeprom locations

    ORG    0x2100
    DE    0x00, 0x01, 0x02, 0x03


    END                       ; directive 'end of program'
 

t06afre

Joined May 11, 2009
5,934
Maybe that IS my problem.
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.
 
Top