Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic16f887

Status
Not open for further replies.

Speakerguy

Active Member
Just a note to anyone else who hadn't noticed (I hadn't), the 16F876 and 16F877 have a newer version out, the 16F886/887. Basically the same except that it has an internal oscillator (8MHz) and an internal MCLR pull up, allowing those three (OSC1, OSC2, MCLR) pins to be used as GPIO's.
 
Yes, as well as 873 and 883.
Besides, there is no LF version for 88x series. Note that the minimum supply voltage is 2 V for the oscillation frequency of less than 8 MHz. The brown out reset voltage is also adjustable.
They are also cheaper compared to 87x, twice! :D
 
Hi, can some experience help me with this very problem i am facing. i am trying to write a source code to generate a 200ms pulse at various interval, let say.. 5s, 50, 100..... using PIC16F887. I used the AD converter becos i wanted to use a rotary switch to control the frequency or speed.

i have only few days to make this work. i need to used this switch to drive a relay.

Can someone help me look at this code and the errors in it or better still help me debug or edit it. i have tried wit could not build. at an instance it did build when i started the program with

org 0x2100. but the Led did not light. i wanted to use the led to monitor the pulse time.

I tried to debug the code and i always recieve error

CORE-E0002: Stack under flow error occurred from instruction at 0x000005

Thank you


Code:
list		p=16f887	; list directive to define processor
	#include	<p16f887.inc>	; processor specific variable definitions


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

	__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

; variable decleration

     cblock 0x00
Delay1           ; Assign an address to label Delay1
Delay2
Display          ; define a variable to hold the diplay
timer1      
incf_count

Endc
        org      0x0021     ;  Start  program  at  address   
count   equ      0x20  	    ;  Counter  variable        
        nop  			    ;  required for debugger

 ;  Initialize  PORT  D  to  be  all  outputs and PORT A all inputs.

Start
        BANKSEL PORTD
        CLRF    PORTD
        BANKSEL TRISD        
        movwf   TRISA          ;  Make PortA all input
        clrf    TRISD          ;  Set PORTD for output        
        clrf    PORTD          ;  write 0s to PORTD
        clrf    count          ;  Initialize  count  to  0. 

;  Setup  timer  1 

	    bcf     T1CON,TMR1ON        ;  Turn  Timer  1  off. 
	    bsf     T1CON,T1CKPS1       ;  Set  prescaler  for  divide bsf T1CON,T1CKPS0 by  8.                	       
        bcf     T1CON,T1OSCEN       ;  Disable  the  RC  oscillator. 
        bcf     T1CON,TMR1CS        ;  Use  the  Fosc/4  source. 
        clrf    TMR1L               ;  Start  timer  at  0000h 
        clrf    TMR1H               ;  clear the pre-scalar counter
        bsf     T1CON,TMR1ON        ;  Start  the  time

Time1   btfss   PIR1,TMR1IF              ;  Did  timer  overflow? 
        
;  Timer  overflowed,  increment  counter  and  display 

        bcf    PIR1,0     ;  Clear  the  flag 
        incf   count,F    ;  Bump  the  counter 
        movf   count,W    ;  Get  the  count 
        movwf  PORTD      ;  Send  to  Port  D. 
        bcf    T1CON,0    ;  Turn  the  timer  off. 
        movlw  80h        ;  Start  timer  at  80h 
        movwf  TMR1H
        clrf   TMR1L
        bsf    T1CON,0    ;  Turn  the  timer  on. 
  
        movlw     0xFF
        movlw     0x00           ; Left Justified, Vdd-Vss referenced
        movwf     ADCON1
		BANKSEL   TRISA        
        movlw     0xFF           ; we want all Port A pins Analog
        movwf     ANSEL
        bcf       STATUS,RP0     ; back to Register Bank 0    
        movlw     0x41
        movwf     ADCON0         ; configure A2D for Fosc/8, Channel 0 (RA0), and turn on the A2D module

        nop                      ; wait 5uS for A2D amp to settle and capacitor to charge.
        nop                      ; wait 1uS
        nop                      ; wait 1uS
        nop                      ; wait 1uS 
        nop                      ; wait 1uS
        bsf       ADCON0,GO_DONE ; start conversion
        btfss     ADCON0,GO_DONE ; this bit will change to zero when the conversion is complete
        goto      $-1

        movf      ADRESH, W      ; Copy the display to the LEDs
        movwf     PORTD

        goto      timer1          

        End               ; directive 'end of program'
 
Hi,

Have not gone through all your code - but unless there is something new with those chips, the program normally expects to start at the begining of the memory.
Think you are getting your 'program' memory and 'data' memory locations mixed up
You load, ORG, it to location 0x0021 ? but do not even tell it where to go ? Try -

;****************************************************************************************
;
; Reset vector
; ============

ORG 0x0000
bootup goto Main ;go to start of main code


;****************************************************************************************
;
; Start of main program
; =====================


Main
 
Last edited:
hi iyke,
Its already been explained on your other thread why your code dosnt work.:)
 
Ok, i got it now....... it built well

look at the code below.

but i have one little problem

when i debug the program it run, but gave the following massage.

HTML:
ADC-W0001:	Tad time is less than 1.60us
ADC-W0008: No stimulus file attached to ADRESL for A/D.

Please take a look at the code
Code:
	list	p=16f887	; list directive to define processor
		#include <p16f887.inc>	; processor specific variable definitions
			 

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

		__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

			 
; variable decleration 
		udata
Delay1		res	1		; Assign an address to label Delay1
Delay2		res	1
Display		res	1		; define a variable to hold the diplay
timer1		res	1
incf_count	res	1

count		equ	0x20		;  Counter  variable 

Reset		code	0x0000		;  Start  program  at  address      
		goto	main

IRQ		code	0x0004
		retfie

		code
					;  Initialize  PORT  D  to  be  all  outputs and PORT A all inputs. 

main
		banksel	PORTD
		clrf	PORTD
		banksel	TRISD        
		
		clrf	TRISD		;  Set PORTD for output        
		clrf	PORTD		;  write 0s to PORTD
		clrf	count		;  Initialize  count  to  0. 

;  Setup  timer  1  

		bcf	T1CON,TMR1ON	;  Turn  Timer  1  off. 
		bsf	T1CON,T1CKPS1	;  Set  prescaler  for  divide bsf T1CON,T1CKPS0 by  8.                	       
		bcf	T1CON,T1OSCEN	;  Disable  the  RC  oscillator. 
		bcf	T1CON,TMR1CS	;  Use  the  Fosc/4  source. 
		clrf	TMR1L		;  Start  timer  at  0000h 
		clrf	TMR1H		;  clear the pre-scalar counter
		bsf	T1CON,TMR1ON	;  Start  the  time

Timer1		btfss	PIR1,TMR1IF	;  Did  timer  overflow? 
			 
;  Timer  overflowed,  increment  counter  and  display   

		bcf	PIR1,0		;  Clear  the  flag 
		incf	count,F		;  Bump  the  counter 
		movf	count,W		;  Get  the  count 
		movwf	PORTD		;  Send  to  Port  D. 
		bcf	T1CON,0		;  Turn  the  timer  off. 
		movlw	80h		;  Start  timer  at  80h 
		movwf	TMR1H
		clrf	TMR1L
		bsf	T1CON,0		;  Turn  the  timer  on. 
			 
		movlw	0xFF
		movlw	0x00		; Left Justified, Vdd-Vss referenced
		movwf	ADCON1
		BANKSEL	TRISA
        movwf	TRISA		;  Make PortA all input
		movlw	0xFF		; we want all Port A pins Analog
		movwf	ANSEL
		bcf	STATUS,RP0	; back to Register Bank 0    
		movlw	0x41
		movwf	ADCON0		; configure A2D for Fosc/8, Channel 0 (RA0), and turn on the A2D module

		nop			; wait 5uS for A2D amp to settle and capacitor to charge.
		nop			; wait 1uS
		nop			; wait 1uS
		nop			; wait 1uS 
		nop			; wait 1uS
		bsf	ADCON0,GO_DONE	; start conversion
		btfss	ADCON0,GO_DONE	; this bit will change to zero when the conversion is complete
		goto	$-1

		movf	ADRESH,W	; Copy the display to the LEDs
		movwf	PORTD

		goto	timer1          

		end			; directive 'end of program'
 
A quick look at your code suggests that you are running at 32kHz. When you run it in the debugger it is assuming you have a crystal attached and you have left it at a clock speed of (probably) 20MHz.

If you intend your code to run at a higher speed then you should write to OSCCON and increase your acquisition time. If not, change the oscillator speed in the simulator.

Mike.
 
Status
Not open for further replies.

Latest threads

Back
Top