Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Timer1 PIC16f887 (ASSEMBLY)

Status
Not open for further replies.

julianjesuthasan

Newbie level 4
Joined
Nov 15, 2012
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,323
Problem:Timer1 PIC16f887 (ASSEMBLY)

Hi guys im using Timer1 to create a 0.5s delay to get one of the LEDs to blink however the the LED turns on but does not blink. Any suggestions?

#include <p16F887.inc>
__CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
__CONFIG _CONFIG2, _WRT_OFF & _BOR21V

cblock 0x20
Delay1 ; Define two file registers for the
Delay2 ; delay loop
endc

org 0
Start:
bsf STATUS,RP0 ; select Register Bank 1
bcf TRISD,0 ; make IO D PORT an output
MOVLW b'11110101'
bsf PIE1,TMR1IE
bsf INTCON,PEIE
bsf INTCON,GIE
bcf STATUS,RP0 ; back to Register Bank 0
MainLoop:
bsf PORTD,0 ; turn on LED RD0
Onloop:

btfss PIR1,TMR1IF ; wait here until Timer1 rolls over
goto Onloop
bcf PIR1,TMR1IF ;CLEAR FLAG
clrf TMR1H
clrf TMR1L
bcf PORTD,0
Offloop
btfss PIR1,TMR1IF ; wait here until Timer1 rolls over
goto Offloop
bcf PIR1,TMR1IF ;CLEARFLAG
clrf TMR1H
clrf TMR1L
goto MainLoop
end
 

Re: Problem:Timer1 PIC16f887 (ASSEMBLY)

After every interrupt you have to clear the TMR1IF (INTCON.TMR1IF) register so that it is reset and a new counting stsrts.
 

Re: Problem:Timer1 PIC16f887 (ASSEMBLY)

After every interrupt you have to clear the TMR1IF (INTCON.TMR1IF) register so that it is reset and a new counting stsrts.

I have cleared the TMR1IF bit using bcf PIR1,TMR1IF. TMRIF does not exist within the INTCON register. if im not mistaken
 

Try with this code and let me know.

Code:
    #include <p16F887.inc>
    __CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
    __CONFIG _CONFIG2, _WRT_OFF & _BOR21V

    cblock 0x20
    Delay1 ; Define two file registers for the
    Delay2 ; delay loop
    endc

    org 0
    Start:
    bsf STATUS,RP0 ; select Register Bank 1
	banksel TRISD
    bcf TRISD,0 ; make IO D PORT an output
    ;MOVLW b'11110101'
    ;bsf PIE1,TMR1IE
    ;bsf INTCON,PEIE
    ;bsf INTCON,GIE
    banksel STATUS
	bcf STATUS,RP0 ; back to Register Bank 0
    
	bsf T1CON, TMR1ON
	MainLoop:
    bsf PORTD,0 ; turn on LED RD0
    Onloop:

    btfss PIR1,TMR1IF ; wait here until Timer1 rolls over
    goto Onloop
    bcf PIR1,TMR1IF ;CLEAR FLAG
    clrf TMR1H
    clrf TMR1L
    bcf PORTD,0
    Offloop
    btfss PIR1,TMR1IF ; wait here until Timer1 rolls over
    goto Offloop
    bcf PIR1,TMR1IF ;CLEARFLAG
    clrf TMR1H
    clrf TMR1L
    goto MainLoop
    end

I will explain the modifications if you do not understand them.

Hope this helps.
Tahmid.
 

Re: Problem:Timer1 PIC16f887 (ASSEMBLY)

I think this is a little better

#include <p16F887.inc>
__CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
__CONFIG _CONFIG2, _WRT_OFF & _BOR21V

cblock 0x20
endc

org 0
Start:
bsf STATUS,RP0 ; select Register Bank 1
bcf TRISD,0 ; make IO D PORT an output
MOVLW b'00111101'
bsf PIE1,TMR1IE
bcf STATUS,RP0 ; back to Register Bank 0
bcf PIR1,TMR1IF
clrf TMR1H
clrf TMR1L
bsf INTCON,PEIE
bsf INTCON,GIE

MainLoop:

bsf PORTD,0 ; turn on LED RD0

Onloop:

btfss PIR1,TMR1IF ; wait here until Timer1 rolls over

goto Onloop

bcf PIR1,TMR1IF ;CLEAR FLAG
clrf TMR1H
clrf TMR1L

bcf PORTD,0

Offloop
btfss PIR1,TMR1IF ; wait here until Timer1 rolls over

goto Offloop

bcf PIR1,TMR1IF ;CLEARFLAG
clrf TMR1H
clrf TMR1L

goto MainLoop

end

- - - Updated - - -

Try with this code and let me know.

Code:
    #include <p16F887.inc>
    __CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
    __CONFIG _CONFIG2, _WRT_OFF & _BOR21V

    cblock 0x20
    Delay1 ; Define two file registers for the
    Delay2 ; delay loop
    endc

    org 0
    Start:
    bsf STATUS,RP0 ; select Register Bank 1
	banksel TRISD
    bcf TRISD,0 ; make IO D PORT an output
    ;MOVLW b'11110101'
    ;bsf PIE1,TMR1IE
    ;bsf INTCON,PEIE
    ;bsf INTCON,GIE
    banksel STATUS
	bcf STATUS,RP0 ; back to Register Bank 0
    
	bsf T1CON, TMR1ON
	MainLoop:
    bsf PORTD,0 ; turn on LED RD0
    Onloop:

    btfss PIR1,TMR1IF ; wait here until Timer1 rolls over
    goto Onloop
    bcf PIR1,TMR1IF ;CLEAR FLAG
    clrf TMR1H
    clrf TMR1L
    bcf PORTD,0
    Offloop
    btfss PIR1,TMR1IF ; wait here until Timer1 rolls over
    goto Offloop
    bcf PIR1,TMR1IF ;CLEARFLAG
    clrf TMR1H
    clrf TMR1L
    goto MainLoop
    end

I will explain the modifications if you do not understand them.

Hope this helps.
Tahmid.


Hey thanks but its not working...still the same output
 

Try with this:

Code:
#include <p16F887.inc>
    __CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
    __CONFIG _CONFIG2, _WRT_OFF & _BOR21V

    cblock 0x20
    Delay1 ; Define two file registers for the
    Delay2 ; delay loop
    endc

    org 0
    Start:
    bsf STATUS,RP0 ; select Register Bank 1
	banksel TRISD
    bcf TRISD,0 ; make IO D PORT an output
    ;MOVLW b'11110101'
    ;bsf PIE1,TMR1IE
    ;bsf INTCON,PEIE
    ;bsf INTCON,GIE
    banksel STATUS
	bcf STATUS,RP0 ; back to Register Bank 0
    
	movlw 0x31
	movwf T1CON
	;bsf T1CON, TMR1ON
	MainLoop:
    bsf PORTD,0 ; turn on LED RD0
    Onloop:

    btfss PIR1,TMR1IF ; wait here until Timer1 rolls over
    goto Onloop
    bcf PIR1,TMR1IF ;CLEAR FLAG
    clrf TMR1H
    clrf TMR1L
    bcf PORTD,0
    Offloop
    btfss PIR1,TMR1IF ; wait here until Timer1 rolls over
    goto Offloop
    bcf PIR1,TMR1IF ;CLEARFLAG
    clrf TMR1H
    clrf TMR1L
    goto MainLoop
    end

The problem with the last code was the delay time was too small, so it appeared to your eyes as though the LED is just on.
 
You're welcome. Yes, I'll explain this by breaking it down:

This part is fine:
Code:
#include <p16F887.inc>
__CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
__CONFIG _CONFIG2, _WRT_OFF & _BOR21V

cblock 0x20
Delay1 ; Define two file registers for the
Delay2 ; delay loop
endc

org 0
Start:
bsf STATUS,RP0 ; select Register Bank 1

We have a problem here:
Code:
bcf TRISD,0 ; make IO D PORT an output
MOVLW b'11110101'

STATUS is in bank 0, but TRISD is not. So, you must move on to the bank where TRISD is - bank changing is required.

Code:
bsf PIE1,TMR1IE
bsf INTCON,PEIE
bsf INTCON,GIE
In the above 3 lines, you enabled interrupts, whereas in the next part of the code, you are checking status of the interrupt flag by polling, and you're not actually using interrupts. This is a problem. The PIC tries to jump to the interrupt upon interrupt, but you don't have any interrupt service routine (ISR).

Code:
bcf STATUS,RP0 ; back to Register Bank 0
STATUS is in bank 0. So you need to move back to Bank 0 after you've switched to Bank 1.

Code:
MainLoop:
bsf PORTD,0 ; turn on LED RD0
Onloop:

btfss PIR1,TMR1IF ; wait here until Timer1 rolls over
goto Onloop

You never started the Timer. So, the interrupt flag is never raised. And the program never moves on beyond this.

Code:
bcf PIR1,TMR1IF ;CLEAR FLAG
clrf TMR1H
clrf TMR1L
bcf PORTD,0
Offloop
btfss PIR1,TMR1IF ; wait here until Timer1 rolls over
goto Offloop
bcf PIR1,TMR1IF ;CLEARFLAG
clrf TMR1H
clrf TMR1L
goto MainLoop
end

In the entire code, you've actually messed up bank switching.

Have you understood this far?
If you've understood this far, I'll explain my code and the corrections made.

Hope this helps.
Tahmid.
 
Last edited:
Yes I have. thanks..I did understand your code I just didn't realize my mistake. thanks a lot.
 

Just a tip.
Always keep the bank switching in mind. You have the map on page 26 in the datasheet. Keep it in front of you whenever you're coding.

Enjoy coding! :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top