Hi there:
I apologize for taken so long to reply but in between attending a job and a family my time left to do other things is from very slim to none.
Anyways after messing around with the code I came out with something that works at least on the isis proteus simulator, one thing that I noticed is that when I press the encoder button the tmr1 value increment in more than one unit, but I think that’s because I didn’t put any debouncing time on the code I’ll check that out.
I going to leave de code and a snapshoot of the circuit for you to look at it and if you will comment about it, but please don´t be to rough on me, since this is my first code for something useful other than blinking leds.
About the code (this is just part of it) is to be used with an incremental encoder, I mean it only send pulses and not difference which way is turning, it is only to measure distance and to cause an interrupt whenever the set distance has been reached, ahhh and one other thing, does any body can tell me how to increase the count, because the tmr1 only counts up to little more than 65 meters before it overflows, providing than each pulse equals one millimeter
'************************************************* **************************
'Name : TAPE MEASURING
'Author : [Don Luis]
'Notice : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS]
'All Rights Reserved
'Date : 02/02/2013
'Version : 1.0
'Notes : PROGRAM CODE TO TEST THE TMR1 OPERATION
'************************************************* *********************************
' FUSES DEFINE
DEFINE HS_OSC,WDT_OFF,PWRT_ON, BOD_OFF,LVP_OFF,CPD_OFF,WRT_HALF,DEBUG_OFF,PROTECT _OFF
' LCD INITIALIZING
DEFINE OSC 4
DEFINE LCD_DREG PORTB ' set portb for lcd
DEFINE LCD_DBIT 4 ' set data bit from 4 to seven
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 2 ' set bit 1 as RS bit
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 3 ' set bit 2 as enable bit
DEFINE LCD_BITS 4 ' define number of data bits
DEFINE LCD_LINES 2 ' define number of lines
DEFINE LCD_COMMANDUS 2000 ' set time for lcd to start
DEFINE LCD_DATAUS 50 ' set time for lcd data transfer
pause 2000
'INITIALIZING SFR'S
adcon1= %10001110
PIE1= %00000001 ' Periferal interrupt enable tmr1
INTCON= %11000000 ' TMR1 interrupt control off
on interrupt goto motstop ' Interrupt handdler
'SETTING TRIS
trisa= %11110011
trisb= %00000001
trisc= %11111111
trisd= %11110000
trise= %000
' PORT DEFINING
encoder var portc.0 ' variable for encoder signal input
motor var porta.3 ' variable for motor control
brake var porta.2 ' variable for brake control
' SETTING VARIABLES
t1val var word ' set variable to store tmr1 value
tempo var byte ' set variable for timing pause
medida var word ' set variable to store required travel distance
tctval var word ' set variable used to display the actual tmr1 value
'INITIALIZING VARIABLES
tempo= 50
t1val=0
medida= 10
tctval= 0
'INITIALIZING PORTS
low brake
low motor
lcdout $FE,$01 ' clear lcd display line 1
pause 20 ' small pause to give time to lcd
lcdout " SYSTEM READDY" ' display introduction message
pause 5000 ' during 5 seconds
main:
t1val=65535-medida ' load tmr1 whith FFFF- the value of medida
TMR1H = t1val.highbyte
TMR1L = t1val.lowbyte
T1CON=%00000111 ' enable tmr1
high motor ' turn motor on
rutpal:
tctval.highbyte= TMR1H
tctval.lowbyte= TMR1L ' load tctval variable with actual timer value
if TMR1H=0 & TMR1L=0 then ' this command is used to prevent the program from
goto main ' going into an endless loop after a return from
endif ' an interrupt routine
lcdout $FE,$01 ' clear display line 1
lcdout "TIMER VALUE" ' display message on line 1
pause 20 ' pause to give the lcd some time to execute
lcdout $FE,$C0 ' clear display line 2
lcdout dec tctval ' display decimal value of tctval on line 2
pause 100 ' small pause
goto rutpal ' loop untill a interrupt ocurrs
disable ' disable all interrupts
motstop: 'interrupt handler
low motor ' stop the motor
high brake ' turn brake on
pause 2000 ' pause just we can see de led on for a moment
low brake ' turn brake off
TMR1H=0 ' clear tmr1
TMR1L=0
T1CON=%00000110 ' disable tmr1
lcdout $FE,$01 ' clear lcd display line 1
lcdout " CYCLE" ' display message on line 1
pause 20 ' small pause to give time to the lcd to execute
lcdout $FE,$C0 ' clear line two of lcd
lcdout " CONCLUDED" ' display message on line two of lcd
PIR1.0=0 ' clear flag
pause 2000 ' two second pause
resume ' end interrupt routine
enable ' enable interrupts
![]()
Bookmarks