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.

Using Pic16f887

Status
Not open for further replies.

iyke20024

New Member
Pic16f887

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'
 
I have adjusted the code and this is what i got. i built the program again. and it runed. but when program it to my pic. the leds did not blink as expected.

when i try to debug the code by puting a breakpoint. i recieve this following massage no matter where the break point is placed.

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


Wat does this mean and how can i go about it?


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 0x0020 ; 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'
 
Change the line,
Code:
		org 0x0020 ; Start program at address

to,

Code:
		org 0x00[COLOR="Red"]0[/COLOR]0 ; Start program at address

and it should get rid of the errors.

Mike.
 
WHAT ORDER THREAD. ARE U SAYING IT MIGHT BE FROM THE ORDER OM CODE.

The code is supposed to start with org 0x000. what do you thinlk. i tried it is even worse, could not build.
 
WHAT ORDER THREAD. ARE U SAYING IT MIGHT BE FROM THE ORDER OM CODE.

The code is supposed to start with org 0x000. what do you thinlk. i tried it is even worse, could not build.

hi,
One reason why you had problems there was an equate at 0x20 , after the origin, also added 0x0004 for intr vector

It now assembles in MPLAB IDE ... OK..:)


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


Code:
; '__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

   [B]errorlevel -302, -207[/B]
; variable decleration

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

 Endc
[B]count   equ      0x20[/B]  	    ;  Counter  variable 

        [B]org      0x0000     [/B];  Start  program  at  address      
	goto 	Start
	[B]org 0x0004     
        nop  			    ;  required for debugger
	retfie[/B]

 ;  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'
 
Last edited:
Hi Eric, Thanks for you time

please the program did not compile on my MPLAB IDE. Built failed. this was the result.

did you try it?
 
Hi Eric, Thanks for you time

please the program did not compile on my MPLAB IDE. Built failed. this was the result.

did you try it?

hi,
Yes I did Build it with MPLAB,, I will retry OK.
 
Hi Eric, Thanks for you time

please the program did not compile on my MPLAB IDE. Built failed. this was the result.

did you try it?

hi,
Redone with MPLAB OK.
See clip from MPLAB output.
 

Attachments

  • esp01 Aug. 07.gif
    esp01 Aug. 07.gif
    3.7 KB · Views: 236
hi,
Redone with MPLAB OK.
See clip from MPLAB output.
Hi,

What version of MPLAB are u using. I am using MPLAB IDE v8.00.

Look at the output i am getting from my MPLAB

HTML:
Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\Program Files\Microchip\MPASM Suite\Template\Object\16F887TMPO.o".
Clean: Deleted file "C:\Program Files\Microchip\MPASM Suite\Template\Object\16F887TMPO.lst".
Clean: Done.
Executing: "C:\Program Files\Microchip\MPASM Suite\MPAsmWin.exe" /q /p16F887 "16F887TMPO.ASM" /l"16F887TMPO.lst" /e"16F887TMPO.err" /o"16F887TMPO.o"
Warning[203] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\OBJECT\16F887TMPO.ASM 66 : Found opcode in column 1. (goto)
Message[302] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\OBJECT\16F887TMPO.ASM 77 : Register in operand not in bank 0.  Ensure that bank bits are correct.
Message[302] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\OBJECT\16F887TMPO.ASM 78 : Register in operand not in bank 0.  Ensure that bank bits are correct.
Message[302] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\OBJECT\16F887TMPO.ASM 108 : Register in operand not in bank 0.  Ensure that bank bits are correct.
Message[302] C:\PROGRAM FILES\MICROCHIP\MPASM SUITE\TEMPLATE\OBJECT\16F887TMPO.ASM 111 : Register in operand not in bank 0.  Ensure that bank bits are correct.
Executing: "C:\Program Files\Microchip\MPASM Suite\MPLink.exe" "C:\Program Files\Microchip\MPASM Suite\LKR\16f887.lkr" "C:\Program Files\Microchip\MPASM Suite\Template\Object\16F887TMPO.o" /o"16F887TMPO.ASM.cof" /M"16F887TMPO.ASM.map" /W
MPLINK 4.14, Linker
Copyright (c) 2007 Microchip Technology Inc.
Error - section '.org_1' can not fit the absolute section. Section '.org_1' start=0x00000004, length=0x00000062
Errors    : 1

Link step failed.
BUILD FAILED: Fri Aug 08 09:06:42 2008

This was the code i built. i.e. from the sugestion you gave.
Code:
;****************************************************************************************************************************
;   This file is a source code used for switching. this code will generate an approximately 0.2s pulse at various interval  *
;   to drive a relay.                                                                                                       *
;                                                                                                                           *   
;   Refer to the MPASM User's Guide for additional information on features of the assembler and linker (Document DS33014).  *        
;                                                                                                                           *
;   Refer to the respective PIC data sheet for additional information on the instruction set.                               *
;                                                                                                                           *
;****************************************************************************************************************************
;                                                                                                                           *
;    Filename:	    16F887TMPO.asm                                                                                          *
;    Date:04/08/2008                                                                                                        *
;    File Version:                                                                                                          *
;                                                                                                                           *
;    Author: Jonathan Okocha                                                                                                *
;    Company: Kelman                                                                                                        *
;                                                                                                                           *
;                                                                                                                           *
;****************************************************************************************************************************
;                                                                                                                           *
;    Files required:                                                                                                        *                                                  
;                                                                                                                           *
;                                                                                                                           *                             
;**************************************************************************************************************************** 
;                                                                                                                           *
;    Notes:I was able to get a 209ms pulse, using a 10MHZ crystal This is very close to the desired 200ms.                  *
;                                                                                                                           *
;    length of an instruction @ 10MHz = 1/(10MHZ/4) = 0.4µs = T                                                             *
;                                                                                                                           *
;    periodic length of the timer1  Tp = T  x  65536  x  8  = 0.209s = 209ms    (using a                                    *
;                                                                                                                           *
;    prescaler of (1:8) 11,   16-bit    Prescaler                                                                           *
;                                                                                                                           *
;    In the interrupt service routine: >> every time the interrupt fires: ++count; >> after 25                              * 
;                                                                                                                           *
;    times > 5s, after 50 times 10s and so on                                                                               *
;                                                                                                                           *
;    But please mind that these times are not really 100% exact because the crystal is not exact                            *
;    and some interferences are also always present. For exact timing an RTC  will be connected                             *                                                                                                                  
;                                                                                                                           *
;****************************************************************************************************************************


	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
count   equ      0x20  	    ;  Counter  variable 

        org      0x0000     ;  Start  program  at  address      
goto 	main
	    org 0x0004     
        nop  			    ;  required for debugger
	    retfie

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

main
        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

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        
        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'

I dont know wats wrong. it built from yours and cannot build here.

what i did was to build from my template code, after deleting everyword or code.

why is this happenning top me now?

what do i do next?

will appreciate any help.

Thanks for the help so far
 
The code above is written as absolute code and should not have a linker script. If you remove the linker file from your project it will assemble fine.

One other thing, you shouldn't change the template files, you should copy (and rename) them to another location before including them in your project. Somewhere like My Documents/Projects.

Mike.
 
BTW, if you want to use a linker script you need to change your code to be,
Code:
;****************************************************************************************************************************  
;                                                                                                                           *  
;    Filename:	    16F887TMPO.asm                                                                                          *  
;    Date:04/08/2008                                                                                                        *  
;    File Version:                                                                                                          *  
;                                                                                                                           *  
;    Author: Jonathan Okocha                                                                                                *  
;    Company: Kelman                                                                                                        *  
;                                                                                                                           *  
;                                                                                                                           *  
;****************************************************************************************************************************  
;                                                                                                                           *  
;    Files required:                                                                                                        *                                                    
;                                                                                                                           *  
;                                                                                                                           *                               
;****************************************************************************************************************************   
;                                                                                                                           *  
;    Notes:I was able to get a 209ms pulse, using a 10MHZ crystal This is very close to the desired 200ms.                  *  
;                                                                                                                           *  
;    length of an instruction @ 10MHz = 1/(10MHZ/4) = 0.4µs = T                                                             *  
;                                                                                                                           *  
;    periodic length of the timer1  Tp = T  x  65536  x  8  = 0.209s = 209ms    (using a                                    *  
;                                                                                                                           *  
;    prescaler of (1:8) 11,   16-bit    Prescaler                                                                           *  
;                                                                                                                           *  
;    In the interrupt service routine: >> every time the interrupt fires: ++count; >> after 25                              *   
;                                                                                                                           *  
;    times > 5s, after 50 times 10s and so on                                                                               *  
;                                                                                                                           *  
;    But please mind that these times are not really 100% exact because the crystal is not exact                            *  
;    and some interferences are also always present. For exact timing an RTC  will be connected                             *                                                                                                                    
;                                                                                                                           *  
;****************************************************************************************************************************  


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

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        
		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'

Note how udata replaces cblock and code replaces org.

Mike.
 
It seems you are not using the linker file. try using the linker file and see if it will build sucessfully

hi,
As Mike explains, no linker required.

What about the other thread, please close one, its confusing.:)
 
Thanks very much. the code is now running well.

one very problem i am facing now is the result. i wanted a 200ms pulse at various interval..ie.. 5s..50s..100s..200s to drive a relay. off course i wan it to be a digital output.

i wanted to controlled the speed or frequency by an analogue input from a rotary switch.

can you look at the code? where am i wrong?



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	TRISD		
		clrf	TRISD		;  Set PORTD for output
	    BANKSEL	PORTD
		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		
		movwf	ADCON1
		BANKSEL	TRISA
        movlw	B'11111111'		; Left Justified, Vdd-Vss referenced
        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'
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top