PDA

View Full Version : 18F45K22 timer/counter



alec
- 14th September 2021, 23:22
Good evening all.
I'm trying to enable timer 3 and timer 5 as counters, and clearly I have something wrong. I can't write to them to zero them out, and they don't count. Reading the datasheet as well as other examples this should be easy, I'd appreciate any help anyone can provide.

************************************************** **************
'* Name : Counter_test_1 *
'* Author : Alec Noble *
'* Notice : Copyright (c) 2021 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 9/12/2021 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************
'********************DEVICE CONFIGURATION**********************************
#CONFIG
CONFIG FOSC = INTIO67 ; Internal oscillator block
CONFIG PLLCFG = OFF ; Oscillator used directly
CONFIG PRICLKEN = OFF ; Primary clock can be disabled by software
CONFIG FCMEN = OFF ; Fail-Safe Clock Monitor disabled
CONFIG IESO = OFF ; Oscillator Switchover mode disabled
CONFIG PWRTEN = OFF ; Power up timer disabled
CONFIG BOREN = SBORDIS ; Brown-out Reset enabled in hardware only (SBOREN is disabled)
CONFIG BORV = 190 ; VBOR set to 1.90 V nominal
CONFIG WDTEN = ON ; WDT is always enabled. SWDTEN bit has no effect
CONFIG WDTPS = 32768 ; 1:32768
CONFIG CCP2MX = PORTC1 ; CCP2 input/output is multiplexed with RC1
CONFIG PBADEN = OFF ; PORTB<5:0> pins are configured as digital I/O on Reset
CONFIG CCP3MX = PORTB5 ; P3A/CCP3 input/output is multiplexed with RB5
CONFIG HFOFST = ON ; HFINTOSC output and ready status are not delayed by the oscillator stable status
CONFIG T3CMX = PORTC0 ; T3CKI is on RC0
CONFIG P2BMX = PORTD2 ; P2B is on RD2
CONFIG MCLRE = EXTMCLR ; MCLR pin enabled, RE3 input pin disabled
CONFIG STVREN = ON ; Stack full/underflow will cause Reset
#ENDCONFIG

'*************************DEFINEs***************** **************************
DEFINE OSC 16 ' Tell PBP the expected systemclock frequency

OSCCON = %01110010
OSCCON2 = %00000100


' LCD settings

' Set LCD Data port
DEFINE LCD_DREG PORTB
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 0
' Set LCD Register Select port
DEFINE LCD_RSREG PORTB
' Set LCD Register Select bit
DEFINE LCD_RSBIT 4
' Set LCD Enable port
DEFINE LCD_EREG PORTB
' Set LCD Enable bit
DEFINE LCD_EBIT 5
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 4
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
DEFINE LCD_COMMANDUS 1500
' Set data delay time in us
DEFINE LCD_DATAUS 44


ANSELA = 0 'all I/O digital
ANSELB = 0
ANSELC = 0
ANSELD = 0
ANSELE = 0

TRISA = 0 'PortA all outputs
TRISB = 0 'PortB all outputs
TRISC = $FF 'PortC all inputs

T3CON = %10000111 'timer 3 source pin or osc, 1:1 prescale, secondary osc disabled, not synchronized, 16 bit operation, timer enabled
T5CON = %10000111 'timer 5 source pin or osc, 1:1 prescale, secondary osc disabled, not synchronized, 16 bit operation, timer enabled


'********************aliases********************** *************

Counter_1 var word
Counter_2 var word

reset var PortC.7





'********************inititialization************* *************


LCDOUT $FE, $01
LCDout $FE, $80, "hello world"
pause 5000
LCDOUT $FE, $01
pause 1000
TMR3H = $00 'zero both counters
TMR3L = $00

TMR5H = $00
TMR5L = $00

'*******************program*********************** **************
main:
TMR3L = Counter_1.LOWBYTE
TMR3H = Counter_1.highbyte

TMR5L = Counter_2.lowbyte
TMR5H = Counter_2.highbyte



lcdout $FE, $80, DEC5 counter_1
lcdout $FE, $C0, dec5 Counter_2

if reset = 0 then
LCDOUT $FE, $01
pause 50
lcdout $FE, $80, "hold to clear"
pause 2500
if reset = 0 then
TMR3H = 0 'zero both counters
TMR3L = 0
TMR5H = 0
TMR5L = 0
'while reset = 0 | wend
LCDOUT $FE, $01
endif

pause 50
endif
pause 250
goto main
end


Thanks to all in advance
Take care, stay safe and talk to you soon
Alec

richard
- 15th September 2021, 00:00
main:
TMR3L = Counter_1.LOWBYTE
TMR3H = Counter_1.highbyte

TMR5L = Counter_2.lowbyte
TMR5H = Counter_2.highbyte



lcdout $FE, $80, DEC5 counter_1
lcdout $FE, $C0, dec5 Counter_2

late night programming ?

try

main:
Counter_1.LOWBYTE =TMR3L
Counter_1.highbyte =TMR3H


Counter_2.lowbyte =TMR5L
Counter_2.highbyte =TMR5H






lcdout $FE, $80, DEC5 counter_1
lcdout $FE, $C0, dec5 Counter_2

alec
- 15th September 2021, 14:47
Damn, I knew it was something simple but my eyes kept going over it!

Now that it works time to complicate it. Thank you very much!

Take care, stay safe and talk to you soon