Maker Pro
Maker Pro

pic16f88 not working properly

ami85t

Feb 19, 2014
71
Joined
Feb 19, 2014
Messages
71
Hi,
I want to implement a project using the PIC16F88.
As a start and for needed indications later a want to lit up a led
and it doesn't work properly.
I'm using a PICKIT3 for programing and I have managed to program a PIC16F877
for another program.

I tried various variations of turning on/off and flickering led commands.
In all the cases the led (bi-colored) responded after a long time (~30 sec)
and turned off and on randomly. The mplab software messages inform
of no errors and of successful programing.
The led works well when not connected to the PIC.

What can the problem by?
I'm attaching the recent code
Thanks, Amitai

Code:
LIST	P=PIC16F88
    	
include	<P16f88.inc>
org		0x00

	__CONFIG _CONFIG1, _LVP_OFF &_WDT_OFF &_PWRTE_OFF &_CP_OFF & _INTRC_IO

	reset:
	nop
	goto	start
	org		0x20

start:

; CONFIGURATION*-*-*-*-*-*-*-*-*-
;*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

; used_ registers:
	MUX_sel			EQU H'0021'  		; MUX select value
	del1			EQU H'0030'  		; for delay
	del2			EQU H'0031'  		; for delay
	to_transmit		EQU H'0050'  		; data register
	led1			EQU H'0070'  		; for led delay
	demo_tr			EQU H'0072'  		; demo transmit
	loop_mux		EQU H'0074'  		; demo transmit
	
; oscillator
	banksel		OSCCON					; 8M internal clk, mode defined 
	movlw		0b01111000					; by fosc(2:0), ??????
	movwf		OSCCON					; system clock source from main oscillator 	
	
; led
	BANKSEL 	PORTB 
	CLRF		PORTB 
	
	banksel		ANSEL					; Configure all pins as digital inputs	
	clrf		ANSEL				
	banksel		TRISB
	clrf		TRISB					; make PORTB output

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

Led_flicker:
	banksel		PORTB
	bsf			PORTB,3	
	bcf			PORTB,2
;---end Led flicker

; END MAIN PROGRAM*-*-*-*-*-*-*-*-*-*
;*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

end
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
The first problem is that there is no instruction to loop back to the Led_flicker label. When the device gets past the last instruction, bcf portb,2, it will continue executing unprogrammed Flash ROM locations. These will normally contain values of 0x3FFF (all bits unprogrammed), which, as far as I can tell from the data sheet, will be interpreted by the PIC core as an addlw 0xFF operation. When the PIC hits the end of program memory it will wrap around to the start.

Exactly why you're getting random behaviour, I'm not sure, but you definitely need to add a goto Led_flicker instruction before the end statement.

Another problem is that your bsf and bcf instructions are operating on different bits of portb.

A third problem is that the LED will flicker far too fast to be visible. An instruction loop that consists of:
Code:
lp: bsf     PORTB,3
    bcf     PORTB,3
    goto    lp
will toggle the pin high then back low again every 4 instruction cycles. If the PIC is running at about 8 MHz (as selected through the OSCCON register), tis corresponds to a frequency of 2 MHz. A bit too fast to see!

You can insert a call to a delay function after the bsf and another call after the bcf to slow things down, and add the following code to do the actual delay:

Code:
delay:
    movlw   8
    movwf   delayvar1
    clrf    delayvar2
    clrf    delayvar3
d1: decfsz  delayvar3
    goto    d1                     ; 256 x 3 cycles
    decfsz  delayvar2
    goto    d1                     ; x 256 = ~200k cycles
    decfsz  delayvar1
    goto    d1                     ; x 8 = ~1.6M cycles = ~0.2 seconds at 8 MHz MCU clock
    return
Also I'm not confident that you're initialising all of the needed control registers. You may also need to initialise OPTION_REG and INTCON.
 
Last edited:

ami85t

Feb 19, 2014
71
Joined
Feb 19, 2014
Messages
71
Thanks,
As I have tried a few options of turning the led on and off
in a loop as you suggested. The code which I have posted
was one of the versions (and not a very good one).
I'll look up again at the OPTION_REG and INTCON initializations.

Amitai
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Have you fixed the other problems? The bcf and bsf instructions operating on a different bit in PORTB, and the need for a delay subroutine?
 

ami85t

Feb 19, 2014
71
Joined
Feb 19, 2014
Messages
71
I'm aware of that, thanks. I have a bi-colored led with
two inputs which I wanted to use for tests by flickering
or changing lights programs, that's why there's different ports commands.
I can check the circuit again only on Sunday.
If it will still not work out i'll inform you.

Thanks, Amitai
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
If you use that code as written, neither of the pins will change state. Executing a bsf PORTB,3 will set PORTB bit 3 high each time round the loop and it will always be high; executing bcf PORTB,2 will set PORTB bit 2 low each time and it will always be low.

You need to do this:
Code:
lp: bsf   PORTB,3
    bcf   PORTB,2
    call  delay
    bsf   PORTB,2
    bcf   PORTB,3
    call  delay
    goto  lp
 
Top