Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

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
 

Joined
Jul 25, 2012
Messages
1,192
Helped
170
Reputation
340
Reaction score
161
Trophy points
1,343
Activity points
0
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.
 

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

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
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.
 

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

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
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.
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
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:

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
Yes I have. thanks..I did understand your code I just didn't realize my mistake. thanks a lot.
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top