PIC16F84A Timer Problems

Started by Paul_Lav March 27, 2003
Hello, i just can't seem to get the built in timer to work properly. I am using a PIC16F84A with 4Mhz Clock. Here is my code:
 
 list PF84A
 include "C:\PAUL\INC\P16F84A.INC"
 ERRORLEVEL -302
 
;Declarations:
 
Base_Value  EQU 0Ch ;Base Unit For One Time Loop
On_Value  EQU 0Dh ;Pulse On Time
Off_Value  EQU 0Eh ;Pulse Off  Time
Done_Value  EQU 0Fh ;Pulse Done Time
TimeOut_Value  EQU 10h ;TimeOut Value
 
Timer   EQU 11h
Total   EQU 12h
 org 0
 goto Start
 
;Subroutines:
 
Init
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Timing Values
 
 movlw d'1'  ;Base Unit For One Time Loop
 movwf Base_Value
 
 movlw d'20'  ;Pulse On Time
 movwf On_Value
 
 movlw d'20'  ;Pulse Off Time
 movwf Off_Value
 
 movlw d'40'  ;Pulse Done Time
 movwf Done_Value
 
 movlw d'30'  ;TimeOut Value
 movwf TimeOut_Value
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 clrf PORTA
 clrf PORTB
 
 bsf STATUS,5 ;Selects Bank 1
 
 movlw b'0000'  ;All A Outputs (0 = Output)
 movwf TRISA
 
 movlw b'00001111' ;All B Inputs (1 = Input)
 movwf TRISB
 
 movlw b'11000111'
 movwf OPTION_REG
 
 bcf STATUS,5 ;Selects Bank 0
 
 clrf INTCON  ;Clears Interrupts File Register
 
 retlw 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
On_Del
 movfw On_Value
 movwf Total
On_Del2
 call Delay
 decf Total,f
 btfss STATUS,Z ;Zero?
 goto On_Del2  ;No
 retlw 0  ;Yes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Off_Del
 movfw Off_Value
 movwf Total
Off_Del2
 call Delay
 decf Total,f
 btfss STATUS,Z ;Zero?
 goto Off_Del2 ;No
 retlw 0  ;Yes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Delay
 clrf Timer
 movfw Base_Value
 addwf Timer,f
 movfw TMR0
 addwf Timer,f
Delay2
 movfw TMR0
 subwf Timer,w
 btfss STATUS,Z ;Zero?
 goto Delay2  ;No
 retlw 0  ;Yes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Pulse
 bsf PORTA,0  ;Signal On
 bsf PORTA,1
 call On_Del  ;On Delay
 bcf PORTA,0  ;Signal Off
 bcf PORTA,1
 call Off_Del  ;Off Delay
 
 retlw 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Program Start:
 
Start call Init
 
Main
 btfsc PORTB,0  ;Tests Button 1
 call Sw1  ;On
 goto Main
 
Sw1
 call Pulse  ;One Pulse
 retlw 0
 
 END
 
I don't seem to be getting the times i would expect:
Is this correct:
 
Delay loop should last 256microsecond (4Mhz / 4 = 1Mhz = 0.000001microsecond with scaler 256 = 256microsecond
On Time should last 20 x 256us = 5.12ms
Off Time should last 20 x 256us = 5.12ms
 
Is this ok because the output stays on longer than 5ms, i can see it blink a good 1/4 second, at least
 
Many thanks,
Paul



Hi Paul,

I could be wrong about this, and I hope someone will correct me if I
am, but... I don't think the PIC16F84a supports a built-in
oscillator. I have never tried using the PIC's internal oscillator,
but I think it is considered a special feature and only available in
certain series.

When I went to digi-key to search for PICs with internal
oscillators, it seemed to filter the list down to just 16F81x,
16F87, and 16F88 in the mid range.

Also, as I look at page 22 of the data sheet for the 16F84, I don't
see anything about configuring it for internal oscillator. There is
also information there on how to connect a crystal or ceramic
resonator if you need that.

The datasheet is available on line at Microchip's website in case
you don't have it already:
http://www.microchip.com/download/lit/pline/picmicro/families/16f8x/3
0430c.pdf

Hope it helps!

Cheers,
Vincent --- In , "Paul_Lav" <paul_lav@y...> wrote:
> Hello, i just can't seem to get the built in timer to work
properly. I am using a PIC16F84A with 4Mhz Clock. Here is my code:
>
> list PF84A
> include "C:\PAUL\INC\P16F84A.INC"
> ERRORLEVEL -302
>
> ;Declarations:
>
> Base_Value EQU 0Ch ;Base Unit For One Time Loop
> On_Value EQU 0Dh ;Pulse On Time
> Off_Value EQU 0Eh ;Pulse Off Time
> Done_Value EQU 0Fh ;Pulse Done Time
> TimeOut_Value EQU 10h ;TimeOut Value
>
> Timer EQU 11h
> Total EQU 12h
>
> org 0
> goto Start
>
> ;Subroutines:
>
> Init
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> ;Timing Values
>
> movlw d'1' ;Base Unit For One Time Loop
> movwf Base_Value
>
> movlw d'20' ;Pulse On Time
> movwf On_Value
>
> movlw d'20' ;Pulse Off Time
> movwf Off_Value
>
> movlw d'40' ;Pulse Done Time
> movwf Done_Value
>
> movlw d'30' ;TimeOut Value
> movwf TimeOut_Value
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> clrf PORTA
> clrf PORTB
>
> bsf STATUS,5 ;Selects Bank 1
>
> movlw b'0000' ;All A Outputs (0 = Output)
> movwf TRISA
>
> movlw b'00001111' ;All B Inputs (1 = Input)
> movwf TRISB
>
> movlw b'11000111'
> movwf OPTION_REG
>
> bcf STATUS,5 ;Selects Bank 0
>
> clrf INTCON ;Clears Interrupts File Register
>
> retlw 0
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> On_Del
> movfw On_Value
> movwf Total
> On_Del2
> call Delay
> decf Total,f
> btfss STATUS,Z ;Zero?
> goto On_Del2 ;No
> retlw 0 ;Yes
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> Off_Del
> movfw Off_Value
> movwf Total
> Off_Del2
> call Delay
> decf Total,f
> btfss STATUS,Z ;Zero?
> goto Off_Del2 ;No
> retlw 0 ;Yes
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> Delay
> clrf Timer
> movfw Base_Value
> addwf Timer,f
> movfw TMR0
> addwf Timer,f
> Delay2
> movfw TMR0
> subwf Timer,w
> btfss STATUS,Z ;Zero?
> goto Delay2 ;No
> retlw 0 ;Yes
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> Pulse
> bsf PORTA,0 ;Signal On
> bsf PORTA,1
> call On_Del ;On Delay
> bcf PORTA,0 ;Signal Off
> bcf PORTA,1
> call Off_Del ;Off Delay
>
> retlw 0
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> ;Program Start:
>
> Start call Init
>
> Main
> btfsc PORTB,0 ;Tests Button 1
> call Sw1 ;On
> goto Main
>
> Sw1
> call Pulse ;One Pulse
> retlw 0
>
> END
>
> I don't seem to be getting the times i would expect:
> Is this correct:
>
> Delay loop should last 256microsecond (4Mhz / 4 = 1Mhz =
0.000001microsecond with scaler 256 = 256microsecond
> On Time should last 20 x 256us = 5.12ms
> Off Time should last 20 x 256us = 5.12ms
>
> Is this ok because the output stays on longer than 5ms, i can see
it blink a good 1/4 second, at least
>
> Many thanks,
> Paul




The 16F628 is pin compatible with the 16F84, has a built in oscillator, a built-in USART, and is about half the price of a 16F84.
 
For any new project where you might be considering a 16F84, I would 100% recommend choosing a 16F628 instead.
----- Original Message -----
From: vincearovola
To: p...@yahoogroups.com
Sent: Friday, March 28, 2003 7:03 PM
Subject: [piclist] Re: PIC16F84A Timer Problems

Hi Paul,

I could be wrong about this, and I hope someone will correct me if I
am, but... I don't think the PIC16F84a supports a built-in
oscillator.  I have never tried using the PIC's internal oscillator,
but I think it is considered a special feature and only available in
certain series.

When I went to digi-key to search for PICs with internal
oscillators, it seemed to filter the list down to just 16F81x,
16F87, and 16F88 in the mid range.

Also, as I look at page 22 of the data sheet for the 16F84, I don't
see anything about configuring it for internal oscillator.  There is
also information there on how to connect a crystal or ceramic
resonator if you need that.

The datasheet is available on line at Microchip's website in case
you don't have it already:
http://www.microchip.com/download/lit/pline/picmicro/families/16f8x/3
0430c.pdf

Hope it helps!

Cheers,
Vincent--- In p...@yahoogroups.com, "Paul_Lav" <paul_lav@y...> wrote:
> Hello, i just can't seem to get the built in timer to work
properly. I am using a PIC16F84A with 4Mhz Clock. Here is my code:
>
>  list PF84A
>  include "C:\PAUL\INC\P16F84A.INC"
>  ERRORLEVEL -302
>
> ;Declarations:
>
> Base_Value  EQU 0Ch ;Base Unit For One Time Loop
> On_Value  EQU 0Dh ;Pulse On Time
> Off_Value  EQU 0Eh ;Pulse Off  Time
> Done_Value  EQU 0Fh ;Pulse Done Time
> TimeOut_Value  EQU 10h ;TimeOut Value
>
> Timer   EQU 11h
> Total   EQU 12h
>
>  org 0
>  goto Start
>
> ;Subroutines:
>
> Init
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> ;Timing Values
>
>  movlw d'1'  ;Base Unit For One Time Loop
>  movwf Base_Value
>
>  movlw d'20'  ;Pulse On Time
>  movwf On_Value
>
>  movlw d'20'  ;Pulse Off Time
>  movwf Off_Value
>
>  movlw d'40'  ;Pulse Done Time
>  movwf Done_Value
>
>  movlw d'30'  ;TimeOut Value
>  movwf TimeOut_Value
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
>  clrf PORTA
>  clrf PORTB
>
>  bsf STATUS,5 ;Selects Bank 1
>
>  movlw b'0000'  ;All A Outputs (0 = Output)
>  movwf TRISA
>
>  movlw b'00001111' ;All B Inputs (1 = Input)
>  movwf TRISB
>
>  movlw b'11000111'
>  movwf OPTION_REG
>
>  bcf STATUS,5 ;Selects Bank 0
>
>  clrf INTCON  ;Clears Interrupts File Register
>
>  retlw 0
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> On_Del
>  movfw On_Value
>  movwf Total
> On_Del2
>  call Delay
>  decf Total,f
>  btfss STATUS,Z ;Zero?
>  goto On_Del2  ;No
>  retlw 0  ;Yes
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> Off_Del
>  movfw Off_Value
>  movwf Total
> Off_Del2
>  call Delay
>  decf Total,f
>  btfss STATUS,Z ;Zero?
>  goto Off_Del2 ;No
>  retlw 0  ;Yes
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> Delay
>  clrf Timer
>  movfw Base_Value
>  addwf Timer,f
>  movfw TMR0
>  addwf Timer,f
> Delay2
>  movfw TMR0
>  subwf Timer,w
>  btfss STATUS,Z ;Zero?
>  goto Delay2  ;No
>  retlw 0  ;Yes
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> Pulse
>  bsf PORTA,0  ;Signal On
>  bsf PORTA,1
>  call On_Del  ;On Delay
>  bcf PORTA,0  ;Signal Off
>  bcf PORTA,1
>  call Off_Del  ;Off Delay
>
>  retlw 0
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> ;Program Start:
>
> Start call Init

> Main
>  btfsc PORTB,0  ;Tests Button 1
>  call Sw1  ;On
>  goto Main
>
> Sw1
>  call Pulse  ;One Pulse
>  retlw 0
>
>  END
>
> I don't seem to be getting the times i would expect:
> Is this correct:
>
> Delay loop should last 256microsecond (4Mhz / 4 = 1Mhz =
0.000001microsecond with scaler 256 = 256microsecond
> On Time should last 20 x 256us = 5.12ms
> Off Time should last 20 x 256us = 5.12ms
>
> Is this ok because the output stays on longer than 5ms, i can see
it blink a good 1/4 second, at least
>
> Many thanks,
> Paul



to unsubscribe, go to http://www.yahoogroups.com and follow the instructions

">Yahoo! Terms of Service.


Hi, i didn't mean the internal oscillator as such, i meant the TMR0 timer/counter. Sorry!
 
I have sorted it out anyway. Bit daft really, i have borrowed works PIC Demo board and on it there is space for both RC osc and Crystal osc, when i got the board both the crystal and the resistor was in the board. The bloke at work told me that he could only get the pic to work if you programmed it RC. Would seem that the resistor for the RC was stopping the crystal working, Well i have now removed the resistor and programmed the pic as XT and all works as i would expect. By the way, i have told the bloke at work :o)
 
Cheers!
Paul
----- Original Message -----
From: vincearovola
To: p...@yahoogroups.com
Sent: Saturday, March 29, 2003 1:03 AM
Subject: [piclist] Re: PIC16F84A Timer Problems

Hi Paul,

I could be wrong about this, and I hope someone will correct me if I
am, but... I don't think the PIC16F84a supports a built-in
oscillator.  I have never tried using the PIC's internal oscillator,
but I think it is considered a special feature and only available in
certain series.

When I went to digi-key to search for PICs with internal
oscillators, it seemed to filter the list down to just 16F81x,
16F87, and 16F88 in the mid range.

Also, as I look at page 22 of the data sheet for the 16F84, I don't
see anything about configuring it for internal oscillator.  There is
also information there on how to connect a crystal or ceramic
resonator if you need that.

The datasheet is available on line at Microchip's website in case
you don't have it already:
http://www.microchip.com/download/lit/pline/picmicro/families/16f8x/3
0430c.pdf

Hope it helps!

Cheers,
Vincent--- In p...@yahoogroups.com, "Paul_Lav" <paul_lav@y...> wrote:
> Hello, i just can't seem to get the built in timer to work
properly. I am using a PIC16F84A with 4Mhz Clock. Here is my code:
>
>  list PF84A
>  include "C:\PAUL\INC\P16F84A.INC"
>  ERRORLEVEL -302
>
> ;Declarations:
>
> Base_Value  EQU 0Ch ;Base Unit For One Time Loop
> On_Value  EQU 0Dh ;Pulse On Time
> Off_Value  EQU 0Eh ;Pulse Off  Time
> Done_Value  EQU 0Fh ;Pulse Done Time
> TimeOut_Value  EQU 10h ;TimeOut Value
>
> Timer   EQU 11h
> Total   EQU 12h
>
>  org 0
>  goto Start
>
> ;Subroutines:
>
> Init
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> ;Timing Values
>
>  movlw d'1'  ;Base Unit For One Time Loop
>  movwf Base_Value
>
>  movlw d'20'  ;Pulse On Time
>  movwf On_Value
>
>  movlw d'20'  ;Pulse Off Time
>  movwf Off_Value
>
>  movlw d'40'  ;Pulse Done Time
>  movwf Done_Value
>
>  movlw d'30'  ;TimeOut Value
>  movwf TimeOut_Value
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
>  clrf PORTA
>  clrf PORTB
>
>  bsf STATUS,5 ;Selects Bank 1
>
>  movlw b'0000'  ;All A Outputs (0 = Output)
>  movwf TRISA
>
>  movlw b'00001111' ;All B Inputs (1 = Input)
>  movwf TRISB
>
>  movlw b'11000111'
>  movwf OPTION_REG
>
>  bcf STATUS,5 ;Selects Bank 0
>
>  clrf INTCON  ;Clears Interrupts File Register
>
>  retlw 0
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> On_Del
>  movfw On_Value
>  movwf Total
> On_Del2
>  call Delay
>  decf Total,f
>  btfss STATUS,Z ;Zero?
>  goto On_Del2  ;No
>  retlw 0  ;Yes
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> Off_Del
>  movfw Off_Value
>  movwf Total
> Off_Del2
>  call Delay
>  decf Total,f
>  btfss STATUS,Z ;Zero?
>  goto Off_Del2 ;No
>  retlw 0  ;Yes
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> Delay
>  clrf Timer
>  movfw Base_Value
>  addwf Timer,f
>  movfw TMR0
>  addwf Timer,f
> Delay2
>  movfw TMR0
>  subwf Timer,w
>  btfss STATUS,Z ;Zero?
>  goto Delay2  ;No
>  retlw 0  ;Yes
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> Pulse
>  bsf PORTA,0  ;Signal On
>  bsf PORTA,1
>  call On_Del  ;On Delay
>  bcf PORTA,0  ;Signal Off
>  bcf PORTA,1
>  call Off_Del  ;Off Delay
>
>  retlw 0
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;
> ;Program Start:
>
> Start call Init

> Main
>  btfsc PORTB,0  ;Tests Button 1
>  call Sw1  ;On
>  goto Main
>
> Sw1
>  call Pulse  ;One Pulse
>  retlw 0
>
>  END
>
> I don't seem to be getting the times i would expect:
> Is this correct:
>
> Delay loop should last 256microsecond (4Mhz / 4 = 1Mhz =
0.000001microsecond with scaler 256 = 256microsecond
> On Time should last 20 x 256us = 5.12ms
> Off Time should last 20 x 256us = 5.12ms
>
> Is this ok because the output stays on longer than 5ms, i can see
it blink a good 1/4 second, at least
>
> Many thanks,
> Paul



to unsubscribe, go to http://www.yahoogroups.com and follow the instructions

">Yahoo! Terms of Service.