PDA

View Full Version : TMR1 external LP xtal setup check



comwarrior
- 13th October 2009, 01:25
can someone double check my setup for TMR1 to use 32KHz external LP xtal on 16F877a?

I think i got it right, but TMR1 doesn't seem to be working...


TMR1L = 0 ' reset TMR1 low to 0
TMR1H = 0 ' reset TMR1 high to 0
PIE1.0 = 0 ' Disable TMR1 interupt (double check)
T1CON.5 = 0 ' Setup TMR1 timings 1:1
T1CON.4 = 0 ' Setup TMR1 timings 1:1
T1CON.3 = 1 ' Oscillator is ON (LP OSC Mode)
T1CON.2 = 1 ' do not Synchronize external clock input
T1CON.1 = 1 ' External clock from pin RC0
T1CON.0 = 1 ' Enables Timer1

thanks

Darrel Taylor
- 13th October 2009, 07:10
Yes, that should make Timer1 keep time with a watch crystal.
Assuming 100 other things are correct ...



T1CON.5 = 0 ' Setup TMR1 timings 1:1
T1CON.4 = 0 ' Setup TMR1 timings 1:1
T1CON.3 = 1 ' Oscillator is ON (LP OSC Mode)
T1CON.2 = 1 ' do not Synchronize external clock input
T1CON.1 = 1 ' External clock from pin RC0
T1CON.0 = 1 ' Enables Timer1


Of course, the timer will only tick every 2 seconds, unless you reload the timer each time it overflows.

You've added capacitors to the crystal right? ...
You're responding to the overflow flag? ...
The TMR1IE enable bit isn't set? ...
I'm guessing without the program again ...
<br>

comwarrior
- 13th October 2009, 15:21
Thanks darrel,

i've got a weird one here... i'm not sure what the hell is going on but something is screwed...

basically, it's a test program, every time TMR1 over flows it toggles an LED, just so that i can see it's working...

it's doing weird things to the ports so i simulated it in oshonsoft's pic simulator...
i toggle the PIR1.0 manually...
What i found is the first overrflow turns PORTB.1 on,
Second overflow turns PORTB.3 on
3 and above overflows turn PORTC3 on and off...

It's not supposed to touch PORTC at all!

Here is that BAS file


ASM
device pic16F877A
device HS_OSC
device WDT_ON
device PWRT_ON
device BOD_ON
device LVP_OFF
device WRT_OFF
device CPD_OFF
device DEBUG_OFF
ENDASM
Define OSC 24 ' Set clock speed
INTCON = 0 ' Disable all interupts
TRISE = %00000111 ' Set PortE to all inputs
TRISD = %00000000 ' Set PortD to all outputs
TRISC = %00000000 ' Set PortC to all outputs
TRISB = %00000000 ' Set PortB to all outputs
TRISA = %11111111 ' Set PORTA to all input

HIGH PORTB
HIGH PORTB.0
'pause 5000
low PORTB
gosub TMR1setup
loop:
if PIR1.0 = 1 then ' if TMR1 has overflowed
T1CON.0 = 0 ' Stop tmr 1
PIR1.0 = 0
toggle PORTB
gosub TMR1setup ' reset and restart TMR1
endif
goto loop

TMR1setup:
TMR1L = 0 ' reset TMR1 low to 0
TMR1H = 0 ' reset TMR1 high to 0
PIE1.0 = 0 ' Disable TMR1 interupt (double check)
T1CON.5 = 0 ' Setup TMR1 timings 1:1
T1CON.4 = 0 ' Setup TMR1 timings 1:1
T1CON.3 = 1 ' Oscillator is ON (LP OSC Mode)
T1CON.2 = 1 ' do not Synchronize external clock input
T1CON.1 = 1 ' External clock from pin RC0
T1CON.0 = 1 ' Enables Timer1
return

Here is the ASM file



; PicBasic Pro Compiler 2.44, (c) 1998, 2003 microEngineering Labs, Inc. All Rights Reserved.
PM_USED EQU 1

INCLUDE "16F877A.INC"


; Define statements.
#define CODE_SIZE 8
#define OSC 24

RAM_START EQU 00020h
RAM_END EQU 001EFh
RAM_BANKS EQU 00004h
BANK0_START EQU 00020h
BANK0_END EQU 0007Fh
BANK1_START EQU 000A0h
BANK1_END EQU 000EFh
BANK2_START EQU 00110h
BANK2_END EQU 0016Fh
BANK3_START EQU 00190h
BANK3_END EQU 001EFh
EEPROM_START EQU 02100h
EEPROM_END EQU 021FFh

R0 EQU RAM_START + 000h
R1 EQU RAM_START + 002h
R2 EQU RAM_START + 004h
R3 EQU RAM_START + 006h
R4 EQU RAM_START + 008h
R5 EQU RAM_START + 00Ah
R6 EQU RAM_START + 00Ch
R7 EQU RAM_START + 00Eh
R8 EQU RAM_START + 010h
FLAGS EQU RAM_START + 012h
GOP EQU RAM_START + 013h
RM1 EQU RAM_START + 014h
RM2 EQU RAM_START + 015h
RR1 EQU RAM_START + 016h
RR2 EQU RAM_START + 017h
_PORTL EQU PORTB
_PORTH EQU PORTC
_TRISL EQU TRISB
_TRISH EQU TRISC
#define _PORTB_0 PORTB, 000h
#define _PIR1_0 PIR1, 000h
#define _T1CON_0 T1CON, 000h
#define _PIE1_0 PIE1, 000h
#define _T1CON_5 T1CON, 005h
#define _T1CON_4 T1CON, 004h
#define _T1CON_3 T1CON, 003h
#define _T1CON_2 T1CON, 002h
#define _T1CON_1 T1CON, 001h
INCLUDE "XTALTE~1.MAC"
INCLUDE "PBPPIC14.LIB"


ASM?

device pic16F877A
device HS_OSC
device WDT_ON
device PWRT_ON
device BOD_ON
device LVP_OFF
device WRT_OFF
device CPD_OFF
device DEBUG_OFF


ENDASM?

MOVE?CB 000h, INTCON
MOVE?CB 007h, TRISE
MOVE?CB 000h, TRISD
MOVE?CB 000h, TRISC
MOVE?CB 000h, TRISB
MOVE?CB 0FFh, TRISA
HIGH?B PORTB
HIGH?T _PORTB_0
LOW?B PORTB
GOSUB?L _TMR1setup

LABEL?L _loop
CMPNE?TCL _PIR1_0, 001h, L00001
MOVE?CT 000h, _T1CON_0
MOVE?CT 000h, _PIR1_0
TOGGLE?B PORTB
GOSUB?L _TMR1setup
LABEL?L L00001
GOTO?L _loop

LABEL?L _TMR1setup
MOVE?CB 000h, TMR1L
MOVE?CB 000h, TMR1H
MOVE?CT 000h, _PIE1_0
MOVE?CT 000h, _T1CON_5
MOVE?CT 000h, _T1CON_4
MOVE?CT 001h, _T1CON_3
MOVE?CT 001h, _T1CON_2
MOVE?CT 001h, _T1CON_1
MOVE?CT 001h, _T1CON_0
RETURN?

END


And here is the MAC file...



NOLIST
; PicBasic Pro Compiler 2.44, (c) 1998, 2003 microEngineering Labs, Inc. All Rights Reserved.

LABEL?L macro Label
RST?RP
ifdef PM_USED
LALL
Label
XALL
else
Label
endif
endm

CMPNE?TCL macro Regin, Bitin, Cin, Label
if (Cin == 0)
CLRWDT?
BIT?GOTO 1, Regin, Bitin, Label
else
if (Cin == 1)
CLRWDT?
BIT?GOTO 0, Regin, Bitin, Label
else
L?GOTO Label
endif
endif
endm

ASM? macro
RST?RP
endm

ENDASM? macro
endm

GOSUB?L macro Label
local dlabel
ifdef DEBUG_STACK
CHK?RP DEBUG_STACK
ifdef DEBUG_STACK_ADDR
bcf STATUS, C
rlf DEBUG_STACK, W
addlw DEBUG_STACK_ADDR
movwf FSR
movlw low (dlabel)
movwf INDF
incf FSR, F
movlw (dlabel) >> 8
movwf INDF
endif
incf DEBUG_STACK, F
endif
L?CALL Label
dlabel
endm

GOTO?L macro Label
L?GOTO Label
endm

HIGH?B macro Bin
MOVE?BA Bin
L?CALL HIGHP
endm
HIGH_USED = 1

HIGH?T macro Regin, Bitin
MOVE?CT 1, Regin, Bitin
MOVE?CT 0, ((Regin) + 80h), Bitin
endm

LOW?B macro Bin
MOVE?BA Bin
L?CALL LOWP
endm
LOW_USED = 1

RETURN? macro
ifdef DEBUG_STACK
CHK?RP DEBUG_STACK
decf DEBUG_STACK, F
endif
RST?RP
return
endm

TOGGLE?B macro Bin
MOVE?BA Bin
L?CALL TOGGLE
endm
TOGGLE_USED = 1


LIST


And this is the HEX file


:1000000026280F2080040B280F20FF3A80050C289B
:100010000F2080060B28FF3A841780052128A800AE
:100020000630A8190730840000308A0028080739F4
:10003000820701340234043408341034203440344C
:100040008034831303138312640008008B0183162A
:1000500007308900880187018601FF3085008312FF
:1000600006088A010120061483160610831206086A
:100070008A0104208A110A124C2064008A110A1293
:100080000C1C4A2810100C1006088A0108208A113E
:100090000A124C208A013D288E018F0183160C1014
:1000A000831290121012901510159014101408005D
:02400E00763FFB
:00000001FF


I'm struggling to see whats gone wrong...

Darrel Taylor
- 13th October 2009, 17:17
HIGH PORTB
TOGGLE PORTB
What those do is read PORTB, takes the value and turns it into a pin number, then toggles that pin.

As the value of the PORT changes, so does the PIN that's being toggled.
<br>

comwarrior
- 13th October 2009, 18:11
indeed... i replaced portb with portb.0 and it now seems to work...

I used a stopwatch to time the LED and it was slightly over... I timed it 10 times and each one came back within 0.1 second of the same value so i've worked out a preload for TMR1...

I'm going to have a play with your (DT's) instant interupts, i need to use interupts now coz i'm dealing with HS serial coms...


Thanks Darrel