daniel2004
Newbie level 1
tmr0 in 16f84a
Hi I have a dumb question, i think I am missing something very basic here.
I cant seem to get TMR0 to increment in MPLAB, and i don't think it is working in the PIC either. First I setup the Option_Reg, and set T0CS, in order to turn the timer off. When I want the timer to count I clear T0CS so the timer will increment off the internal clock. This dosnt happen. By my reading of the data sheet the Timer should increment in this mode no matter what. I have checked in MPLAB that all bits are been set and cleared as they should be, still no luck. I hope someone here could shed some light on this.
Thanks
Daniel
#define Processor 16F84A
LIST P=16F84A;, F=INHX8M
#include <P16F84A.inc>
__config _CP_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC
;************************************************************
; MEMORY EQUATES
;************************************************************
W_Temp EQU 0x20
STATUS_Temp EQU 0x21
Delay EQU 0x22
;************************************************************
; Reset and Interrupt Vectors
ORG 0x0000 ;Reset Vector
GOTO Start
ORG 0x0004 ;Interrupt vector
GOTO IntVector
;************************************************************
; Program begins here
ORG 0x005
Start
BSF STATUS, RP0
MOVLW h'FF'
MOVWF TRISA
CLRF TRISB
BSF TRISB, 7
BSF TRISB, 6
BSF OPTION_REG, T0CS ;Timer0 not running
BSF OPTION_REG, T0SE ;Rising edge
BCF OPTION_REG, PSA ;Use tmr0 for prescaler
BSF OPTION_REG, PS2 ;Maximum prescaler
BSF OPTION_REG, PS1
BSF OPTION_REG, PS0
CLRF INTCON ;Start fresh
BCF INTCON, T0IE ;Enable TMR0 Interupt
BCF INTCON, T0IF ;Clear TMR0 Flag
BCF STATUS, RP0 ;Bank0
CLRF TMR0
BSF INTCON, GIE
GOTO Main
Main
; BTFSS PORTA, 1
; GOTO Main
CLRF PORTB
BSF PORTB, 0
BSF STATUS, RP0
BCF OPTION_REG, T0CS ;Start TMR0
BCF STATUS, RP0
a MOVF Delay, W
SUBLW d'76'
BTFSS STATUS, Z
GOTO a
BSF STATUS, RP0
BSF OPTION_REG, T0CS ;Stop TMR0
BCF STATUS, RP0
CLRF TMR0
BSF PORTB, 1
; GOTO Main
Main_a
GOTO Main_a
IntVector
BSF PORTB, 3
MOVWF W_Temp ;Copy W to TEMP register
SWAPF STATUS,W ;Swap status to be saved into W
CLRF STATUS ;bank 0, regardless of current bank, Clears IRP,RP1,RP0
MOVWF STATUS_Temp ;Save status to bank zero STATUS_TEMP register
INCF Delay
CLRF TMR0
BCF INTCON, T0IF
SWAPF STATUS_Temp,W ;Swap STATUS_TEMP register into W
;(sets bank to original state)
MOVWF STATUS ;Move W into STATUS register
SWAPF W_Temp,F ;Swap W_TEMP
SWAPF W_Temp,W ;Swap W_TEMP into W
RETFIE
end
Hi I have a dumb question, i think I am missing something very basic here.
I cant seem to get TMR0 to increment in MPLAB, and i don't think it is working in the PIC either. First I setup the Option_Reg, and set T0CS, in order to turn the timer off. When I want the timer to count I clear T0CS so the timer will increment off the internal clock. This dosnt happen. By my reading of the data sheet the Timer should increment in this mode no matter what. I have checked in MPLAB that all bits are been set and cleared as they should be, still no luck. I hope someone here could shed some light on this.
Thanks
Daniel
#define Processor 16F84A
LIST P=16F84A;, F=INHX8M
#include <P16F84A.inc>
__config _CP_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC
;************************************************************
; MEMORY EQUATES
;************************************************************
W_Temp EQU 0x20
STATUS_Temp EQU 0x21
Delay EQU 0x22
;************************************************************
; Reset and Interrupt Vectors
ORG 0x0000 ;Reset Vector
GOTO Start
ORG 0x0004 ;Interrupt vector
GOTO IntVector
;************************************************************
; Program begins here
ORG 0x005
Start
BSF STATUS, RP0
MOVLW h'FF'
MOVWF TRISA
CLRF TRISB
BSF TRISB, 7
BSF TRISB, 6
BSF OPTION_REG, T0CS ;Timer0 not running
BSF OPTION_REG, T0SE ;Rising edge
BCF OPTION_REG, PSA ;Use tmr0 for prescaler
BSF OPTION_REG, PS2 ;Maximum prescaler
BSF OPTION_REG, PS1
BSF OPTION_REG, PS0
CLRF INTCON ;Start fresh
BCF INTCON, T0IE ;Enable TMR0 Interupt
BCF INTCON, T0IF ;Clear TMR0 Flag
BCF STATUS, RP0 ;Bank0
CLRF TMR0
BSF INTCON, GIE
GOTO Main
Main
; BTFSS PORTA, 1
; GOTO Main
CLRF PORTB
BSF PORTB, 0
BSF STATUS, RP0
BCF OPTION_REG, T0CS ;Start TMR0
BCF STATUS, RP0
a MOVF Delay, W
SUBLW d'76'
BTFSS STATUS, Z
GOTO a
BSF STATUS, RP0
BSF OPTION_REG, T0CS ;Stop TMR0
BCF STATUS, RP0
CLRF TMR0
BSF PORTB, 1
; GOTO Main
Main_a
GOTO Main_a
IntVector
BSF PORTB, 3
MOVWF W_Temp ;Copy W to TEMP register
SWAPF STATUS,W ;Swap status to be saved into W
CLRF STATUS ;bank 0, regardless of current bank, Clears IRP,RP1,RP0
MOVWF STATUS_Temp ;Save status to bank zero STATUS_TEMP register
INCF Delay
CLRF TMR0
BCF INTCON, T0IF
SWAPF STATUS_Temp,W ;Swap STATUS_TEMP register into W
;(sets bank to original state)
MOVWF STATUS ;Move W into STATUS register
SWAPF W_Temp,F ;Swap W_TEMP
SWAPF W_Temp,W ;Swap W_TEMP into W
RETFIE
end