# Problem Trying to get PORTB LED's to blink PIC16F887 - HELP PLEASE SAVE MY SANITY

Status
Not open for further replies.

#### 3BABY

##### Member level 5
Hi Guys,

i have googled and searched but the 16F887 seems to be harder to get working then a 18F4550.. can someone please point out the obvious mistake im making in the below? im only working with the 16F887 as i have run out of 18F4550's... any help would save my sanity greatly!!!!

Code:
;**************************************************************************************************
;
;	Target: PIC16F887
;	Oscillator: 8Mhz
;
;
;**************************************************************************************************

LIST	p=16F887		;tell assembler what chip we are using
#include<p16f887.inc>

__config 0x1D19			;sets the configuration settings (oscillator type etc.)

;-------- Register Definitions------

W       	EQU   H'0000'
F   		EQU   H'0001'

;-------- Register Files----

d10			EQU 	0x50
d11			EQU 	0x51
d12			EQU 	0x52
d13			EQU 	0x53

;------------ Port Settings --------------------------------------

ORG	0

;------------ Port Settings --------------------------------------

BSF			STATUS,5       	;Switch to Bank 1

CLRF PORTB			;PORTB is output
MOVLW b'00000000'
MOVWF TRISB

bcf      	STATUS,5       	;Switch back to Bank 0

;------------------------------------ MAIN PROGRAM ------------------------------------------------------------------

START

CLRF PORTB

MAIN

MOVLW b'11111111'
MOVWF PORTB

CALL DELAY_10SECS
NOP

MOVLW b'00000000'
MOVWF PORTB

CALL DELAY_10SECS
NOP

GOTO MAIN

;------ DELAY_10SECS - 10 Seccond delay --------------------------------------------------------------------------------------------

DELAY_10SECS

; Delay = 10 seconds
; Clock frequency = 8 MHz

; Actual delay = 10 seconds = 20000000 cycles
; Error = 0 %

;	cblock
;	d10
;	d11
;	d12
;	endc

;19999992 cycles
movlw	0xB5
movwf	d10
movlw	0x99
movwf	d11
movlw	0x2C
movwf	d12
Delay_10_1
decfsz	d10, f
;	goto	$+2 goto D10_2 decfsz d11, f D10_2 goto D10_3 decfsz d12, f D10_3 goto Delay_10_1 ;4 cycles ;goto$+1
;goto	$+1 NOP NOP NOP ;4 cycles (including call) return ;---------------------------------------------------------------------------------------------------------------------------------------- END #### hexreader ##### Advanced Member level 2 You really should go through the Gooligum PIC tutorials to learn better programming style. Here is your code with minimum fix, just enough to make it operate: Code: ;************************************************************************************************** ; ; Target: PIC16F887 ; Oscillator: 8Mhz ; ; ;************************************************************************************************** LIST p=16F887 ;tell assembler what chip we are using #include <p16F887.inc> ; config 0x1D19 ;sets the configuration settings (oscillator type etc.) __CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _HS_OSC __CONFIG _CONFIG2, _WRT_OFF & _BOR21V cblock 0x20 d10 d11 d12 d13 endc ORG 0 ;------------ Port Settings -------------------------------------- CLRF PORTB ;PORTB is all zeros BSF STATUS,5 ;Switch to Bank 1 MOVLW b'00000000' MOVWF TRISB bcf STATUS,5 ;Switch back to Bank 0 ;------------------------------------ MAIN PROGRAM ------------------------------------------------------------------ START CLRF PORTB MAIN MOVLW b'11111111' MOVWF PORTB CALL DELAY_10SECS NOP MOVLW b'00000000' MOVWF PORTB CALL DELAY_10SECS NOP GOTO MAIN ;------ DELAY_10SECS - 10 Seccond delay -------------------------------------------------------------------------------------------- DELAY_10SECS ; Delay = 10 seconds ; Clock frequency = 8 MHz ; Actual delay = 10 seconds = 20000000 cycles ; Error = 0 % ; cblock ; d10 ; d11 ; d12 ; endc ;19999992 cycles movlw 0xB5 movwf d10 movlw 0x99 movwf d11 movlw 0x2C movwf d12 Delay_10_1 decfsz d10, f ; goto$+2
goto	D10_2
decfsz	d11, f
D10_2
goto	D10_3
decfsz	d12, f
D10_3
goto	Delay_10_1

;4 cycles
;goto	$+1 ;goto$+1

NOP
NOP
NOP

;4 cycles (including call)
return

;----------------------------------------------------------------------------------------------------------------------------------------

END

3BABY

### 3BABY

Points: 2

#### 3BABY

##### Member level 5
You really should go through the Gooligum PIC tutorials to learn better programming style.

Here is your code with minimum fix, just enough to make it operate:

Code:

Thankyou, ill have a look through Gooligum, i had used the same config code trying to get it working the first time but always got these (see pic) errors.. and now when i try re-complie the code you provided im getting the same error (see pic).. any idea why?

obviously you cant remove the "," or you would get a missing operator..

---------- Post added at 12:10 ---------- Previous post was at 11:46 ----------

ok so.. it looks like that config was for more like the 87X? i have looked around and found the below for the 887

CONFIG1 = _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
CONFIG2 = _WRT_OFF & _BOR21V

it compiles but doesnt and programs but doesn't actually run..

Your version of config setting works fine for me, as below, and on real hardware, all PORTB LEDs flash on for 10 seconds, then off, repeatedly.

Maybe you have a hardware problem

Code:
;**************************************************************************************************
;
;	Target: PIC16F887
;	Oscillator: 8Mhz
;
;
;**************************************************************************************************

LIST	 p=16F887		;tell assembler what chip we are using
#include <p16F887.inc>

;	__CONFIG    _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _HS_OSC
;	__CONFIG    _CONFIG2, _WRT_OFF & _BOR21V

CONFIG1 = _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
CONFIG2 = _WRT_OFF & _BOR21V

cblock 0x20

d10
d11
d12
d13
endc

ORG	0

;------------ Port Settings --------------------------------------

CLRF    PORTB			;PORTB is all zeros
BSF     STATUS,5       	;Switch to Bank 1

MOVLW b'00000000'
MOVWF TRISB

bcf      	STATUS,5       	;Switch back to Bank 0

;------------------------------------ MAIN PROGRAM ------------------------------------------------------------------

START

CLRF PORTB

MAIN

MOVLW b'11111111'
MOVWF PORTB

CALL DELAY_10SECS
NOP

MOVLW b'00000000'
MOVWF PORTB

CALL DELAY_10SECS
NOP

GOTO MAIN

;------ DELAY_10SECS - 10 Seccond delay --------------------------------------------------------------------------------------------

DELAY_10SECS

; Delay = 10 seconds
; Clock frequency = 8 MHz

; Actual delay = 10 seconds = 20000000 cycles
; Error = 0 %

;	cblock
;	d10
;	d11
;	d12
;	endc

;19999992 cycles
movlw	0xB5
movwf	d10
movlw	0x99
movwf	d11
movlw	0x2C
movwf	d12
Delay_10_1
decfsz	d10, f
;	goto	$+2 goto D10_2 decfsz d11, f D10_2 goto D10_3 decfsz d12, f D10_3 goto Delay_10_1 ;4 cycles ;goto$+1
;goto	\$+1

NOP
NOP
NOP

;4 cycles (including call)
return

;----------------------------------------------------------------------------------------------------------------------------------------

END

#### betwixt

##### Super Moderator
Staff member
Observations:

You can save yourself a lot of grief by using 'banksel' instead of switching banks by changing bits in the STATUS register. See the help file for instructions.

You will have trouble if you try to use interrupts. Nothing is wrong with your code but if an interrupt occurred it would almost certainly crash the program. I'm assuming you are using MPLAB as the assembler so I suggest you start all your programs by copying the template Microchip provide into the edit window before adding your own code. It will give you the 'bare bones' of a program which you can then expand and it will include all the essential configuration information and an interrupt handler which you can use if needed. In a normal instalation and for absolute code the templates are saved in:

C:\Program Files\Microchip\MPASM Suite\Template\Code

Brian.

3BABY

### 3BABY

Points: 2

#### 3BABY

##### Member level 5
Observations:

You can save yourself a lot of grief by using 'banksel' instead of.............

Thanks betwixt.. i have had a look at those templates.. ill use them in future. i have to educate myself on bank selecting as in still abit fuzzy on what it does.

Your version of config setting works fine for me, as below, and on real hardware, all PORTB LEDs flash on for 10 seconds, then off, repeatedly.

Maybe you have a hardware problem

Code:
;

hexreader, i can compile the program with MPLAB and write it to the 887, but nothing happens, i have an EasyPIC6 and can write one of the company's demo programs onto the same uC and it works fine (attached just change from .txt to .hex).. i have an 8Mz OSC hooked up to the uC but i have nothing happening.. no leds flashing or anything.

i have compiled almost identical code for a 18F4550 using

Code:
	CONFIG FOSC = XTPLL_XT		;XT oscillator, PLL Enabled XT used by USB

CONFIG PLLDIV = 2

and the PORTB LED's blink, this is using the same EasyPIC6 not changing anyting except the uC.. but for only about 2secconds.. i have to use CONFIG PLLDIV = 12 for it to make 10secconds (timed with a stopwatch).. it seems i dont understand the PLLDIV setting as i thought = 2 would mean the CPU would be running at 8Mhz/4(instruction time) = 2 Mhz

that 10 second delay was straight from Microchip PIC, ASM Delay Code, Code Generator and i chose 8Mhz.. should i be choosing 2Mhz as this is the actual CPU clock? or am i getting confused? surely choosing 2Mhz would just make the routine shorter and having an 8MHz FOSC connected would result in a delay that is very very short in time.

as for the 16F887 just not doing anything.. im stuck? is there some setting im overlooking?

#### Attachments

• Led_Curtain.txt
563 bytes · Views: 6
Last edited:

What programmer are you using? is it the EP6 on-board "micro ICD" programmer, or an external Microchip ICD2 (or ICD3) ?

Your picture shows using debug mode. I presume you switched to release mode before assembling?

Using MikroE interface with on-board programmer means setting configuration bits manually.

Can't think what else to suggest right now....

#### 3BABY

##### Member level 5
What programmer are you using? is it the EP6 on-board "micro ICD" programmer, or an external Microchip ICD2 (or ICD3) ?

Your picture shows using debug mode. I presume you switched to release mode before assembling?

Using MikroE interface with on-board programmer means setting configuration bits manually.

Can't think what else to suggest right now....

I didnt know you had to manualy switch from Dubug mode, when i get home ill recompile and write the program,

im using the MikroE onboard programmer.. arnt we setting the config bits at the start of the code anyways? so this should not be an issue?

il let you know what happens.. cheers

#### 3BABY

##### Member level 5
What programmer are you using? is it the EP6 on-board "micro ICD" programmer, or an external Microchip ICD2 (or ICD3) ?

Your picture shows using debug mode. I presume you switched to release mode before assembling?

Using MikroE interface with on-board programmer means setting configuration bits manually.

Can't think what else to suggest right now....

and still have nothing happening on PORTB.. is this a configuration issue or what im stumped.. any chance i could send you my HEX to try on your system?

This has to be a config problem or my MPLAB is not happy, i tried using the template and just putting in the delay and main, and register defs and i get that same error "," illegal at this point after _CONFIG1, and _CONFIG2,

Code:
	__CONFIG    _CONFIG1, _LVP_OFF & _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
__CONFIG    _CONFIG2, _WRT_OFF & _BOR21V

Last edited:

I think the problem is with configuration bits.

MikroE software ignores config bit settings in code and seems to ignore config in .hex files too.

Try manually selecting the correct PIC and setting all config bits manually just before writing

If you have an ICD2 or ICD3, it is a better option for programming, as it does preserve config bit settings

#### 3BABY

##### Member level 5
I think the problem is with configuration bits.

Try manually selecting the correct PIC and setting all config bits manually just before writing

do you mean by going to the top of MPLAB and selecting "configure" => configuration bits? if so i have tried that and compiled again but still not output.

or is there a different way to manually set config bits?

thanks

No.

MPLAB does everything correctly.

It is the MikroE Programmer software that throws away the config bits, and forces you to select them manually.

How are you burning the PIC?

I assume that you use PicFlash software and the EasyPIC6 on-board Mikro-ICD hardware, but maybe I guess wrong.

Maybe you could explain your set up in more detail

#### 3BABY

##### Member level 5
No.

MPLAB does everything correctly.

It is the MikroE Programmer software that throws away the config bits, and forces you to select them manually.

How are you burning the PIC?

I assume that you use PicFlash software and the EasyPIC6 on-board Mikro-ICD hardware, but maybe I guess wrong.

Maybe you could explain your set up in more detail

im using the mikroProg Suite (link to pdf) .. i just have the EasyPIC6 connected to my PC via USB..

ok so this is strange.. when i clicked on the "config" tab i had nothing it was just blank.. so i just downloaded mikroProg suite again and reinstalled it and now i can see the settings in the config tab (please see pic) .. this is while power is on on the EasyPIC and i can load my HEX.. but if i select XT as the oscillator the PORTB leds light up but it looks like they must be going very fast as they are dim to look at.. they definitely do not go on and off for 10secs i have even tried calling the 10second delay 4 times per on/off and nothing changes.. i have a 8Mhz crystal on the board at the moment. just now as im writing this i have pulled the 8Mhz crystal out and put a 4Mhz one in.. no change to the LEDs.. i have pulled the crystal out all together and pressed the reset button.. no change to the LEDs.. whats going on? edit... check out pic.. its not the best but you can see PORTB 0-1 are bright and the rest are dim

another update.. ive just modified code to MOVLW b'10101010' PORTD , 10sec delay then MOVLW b'01010101' PORTD then 10secs and its just sitting at 01010101 and not "toggling" .. so the code must not be running?

Last edited:

Now we are getting somewhere....

I would choose HS oscillator, though XT would probably work

Disable LVP (low voltage programming). My guess is that this may be the setting that really matters. With LVP disabled, everything might start to work right - fingers crossed.

#### 3BABY

##### Member level 5
Now we are getting somewhere....

I would choose HS oscillator, though XT would probably work

Disable LVP (low voltage programming). My guess is that this may be the setting that really matters. With LVP disabled, everything might start to work right - fingers crossed.

AND WE HAVE A WINNER!! that worked! its giving me a good 10second delay with the 8Mhz crystal... ive never had to use the config tab when programming the 18F4550's, why would the programmer override the .HEX?

just on another not when using the same code on a 4550 like i mentioned before it was giving me a delay of around 2seconds.. i was using the below. i had to change the PLLDIV to 12 to get it to delay for 10secconds.. does this sound correct? wouldnt that mean the CPU was running at 250KHz?

CONFIG FOSC = XTPLL_XT ;XT oscillator, PLL Enabled XT used by USB

CONFIG PLLDIV = 2

thanks for all your help btw!!!

why would the programmer override the .HEX?
Good question, one for MikroE to answer. It is a very annoying feature of MikroE tools that configuration can only be set manually. There is no sign that MikroE will ever change this highly frustrating policy

ive never had to use the config tab when programming the 18F4550's
Then I would guess that the default settings just happened to give a working option. You just got lucky.

PIC18F4550 has many, many oscillator options which are really hard to configure correctly and to understand. It is a poor choice of chip to learn with for this reason.

PIC18 is capable of running much faster than PIC16.

I would run PIC18F4550 at maximum speed 48MHz and adjust your delay routine to suit. Rather than bring PIC18 down to PIC16 speeds.

I will leave you to study the PIC18F4550 datasheet ( oscillator section) to try and understand all of the different options. I do not have the patience. The oscillator block diagram is the best place to start.

Good luck

Status
Not open for further replies.