Hello guys:
Continuing with my project, after completing my code which is rather long since include a longitude set up routine, a temperature set up, thermostat routine a 4x4 keyboard and a lcd display interface routine and other control goodies.
Even though almost everything works as planed I come up to a dead end, it so happens that using the tmr1 to measure the length of the expended foil I have a huge difference between the inputted measure and the one I get.
Between 2 and 3 times longer and never get the same, let me explain a little, let’s suppose the worst scenario
The expended roll measures 300mm in diameter (it’ll never get that big) so this is according to my math’s 300x3.1416= 942.48mm, and given that the shaft turns at 150 rpm then 150/60= 2.5 rps. Then 942.48x2.5= 2356mm per second and since every mm equals a pulse gives me a speed of 1/2356= 0.00043 us per mm and using a 4 Mhz xtal that is 1us, almost 400 times the foil speed, so I thought it wasn’t enough time for the pic to process the info, so I went o 8 Mhz, and then 16 Mhz xtal and all ways got the same.
So I decided to try counting by interrupts using Rb.0 and made a routine for that and I got the same results the only difference is that measures nearly equal, 1mean still 3 times more but with 5mm more or less, I did have a commercial incremental encoder but I don’t know what happen to it, can’t find it, so I make one and it looks like this (well they appear on the bottom)
I used a regular IR led and opt transistor, turning the roller by hand I can count the 149 pulses per revolution, the output (negative pulse) of the opt transistor is feed to a 74ls14 smith trigger and then (positive pulse) to the pic pin, so I have a doubt if the problem is in the code or in the hardware but I am missing pulses big time, any help on this mater like always be greatly appreciated, also I live some pieces of the code related.
‘THIS IS A CODE WITH THE TMR1 INTERRUPT
DEFINE INTRC_OSC_NOCLKOUT &_HS_OSC & _WDT_OFF & _PWRTE_ON & _BOREN_OFF & _LVP_OFF & _CPD_OFF &_WRT_HALF & _DEBUG_OFF & _CP_OFF
' LCD INITIALIZING
DEFINE OSC 16 ' define oscilator cristal 16 mhz
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
DEFINE ADC_BITS 10 ' define analog register 10 bit word
DEFINE ADC_CLOCK 3 ' define clock source internal rc clock
DEFINE ADC_SAMPLEUS 50 ' define adc samplign every 50 ms
pause 3000 ' pause to initialize lcd
PIE1= %00000001 ' enable timer1
INTCON= %11000000 ' enable interrupts
on interrupt goto motstop ' interrupt handler
rutpal: ' main routine
LCDOUT $FE, $01
pause 20
lcdout "TEMP " ‘display TEMP suffix on line 1
LCDOUT $FE,$C0
LCDOUT "M.U." ‘ display M.U. suffix on line 2
low l4
rpal: ‘main loop
if alcal=0 then gosub nosensor ' check for temperature sensor condition
adcin 0, calsens ‘ sample sensor
calsens = 488 * calsens ; 1024*488=499712; ‘ convert to °C
calsens = DIV32 10
LCDOUT $FE, $80 +6 ‘ display temperature on line 1
pause 20
lcdout DEC calsens DIG 4, DEC calsens DIG 3,DEC calsens DIG 2, ".", DEC calsens DIG 1, $DF,"C", " rp"
LCDOUT $FE,$C0 +5 ' display used foil length on line 2
pause 20
LCDOUT dec contador
if calsens<= temperatura-200 then high calentador ' if temperature is lower than setting, turn heater on
if calsens=> temperatura then low calentador ' and if it's higher then turn it off
button porta.1,0,0,0,boton,1,b1 ' check activation sensor if pressed go to b1
if c1= 0 then gosub debo ' if the key * was pressed go to debounce routine
if c1=0 then ‘ if still pressed sound key and go to set temperature
sound porta.5,[fr,10]
high l4
gosub tempajust
if jo=1 then ‘ on return clear flag and go to main loop
jo=0
goto rutpal
endif
endif
if c2= 0 then ' if the key 0 was pressed go to jogging routine
if jo=1 then ‘ on return clear flag and go to main loop
jo=0
goto rutpal
endif
endif
if c3= 0 then gosub debo ' if the key # was pressed go to debounce routine
if c3=0 then
sound porta.5,[fr,10] ‘if still pressed sound key and go to set length
high l4 ‘ on return clear flag and go to main loop
gosub medajust
if jo=1 then
jo=0
goto rutpal
endif
endif
if c4= 0 then gosub debo ' if the if the key D was pressed go to debounce routine
if c4=0 then ‘if still pressed sound key and go to clear contador routine
sound porta.5,[fr,10] ‘ on return clear flag and go to main loop
high l4
gosub borrar
if jo=1 then
jo=0
goto rutpal
endif
endif
goto rpal ' loop untill a subroutine is called up
b1:
counter= 0 ' routine to prevent program from seizing if sensor is not released
if porta.1= 1 then
boton=0
goto avance
endif
if porta.1= 0 then counter= counter +1
pause 50
if counter= 50 then rutpal
goto b1
avance:
t1val= 65535 – medida ‘ load tmr1
TMR1H = t1val.highbyte
TMR1L = t1val.lowbyte
T1CON=%00000111 ‘ enable tmr1
high motor ‘ turn motor on
goto rutpal ‘ return to main loop
disable ‘ disable interrupts
motstop:
low motor ‘ stop motor
high brake ‘ turn brake on and wait 300ms
pause 300
low brake ‘ turn brake off
T1CON=%00000110 ‘ disable tmr1
PIR1.0=0 ‘ clear tmr1 overflow flag
contador= contador+medida ‘ add contador to previous count and return
resume
enable ‘ enable interrupts
‘THIS IS A CODE WITH THE RB.O INTERRUPT
DEFINE INTRC_OSC_NOCLKOUT &_HS_OSC & _WDT_OFF & _PWRTE_ON & _BOREN_OFF & _LVP_OFF & _CPD_OFF &_WRT_HALF & _DEBUG_OFF & _CP_OFF
' LCD INITIALIZING
DEFINE OSC 16 ' define oscilator cristal 16 mhz
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
DEFINE ADC_BITS 10 ' define analog register 10 bit word
DEFINE ADC_CLOCK 3 ' define clock source internal rc clock
DEFINE ADC_SAMPLEUS 50 ' define adc samplign every 50 ms
pause 3000 ' pause to initialize lcd
INTCON= %10000000 ' enable interrupts
OPTION_REG.6=1 ‘ define interrupt edge (rising edge)
on interrupt goto motstop ' interrupt handler
rutpal: ' main routine
LCDOUT $FE, $01
pause 20
lcdout "TEMP " ‘display TEMP suffix on line 1
LCDOUT $FE,$C0
LCDOUT "M.U." ‘ display M.U. suffix on line 2
low l4
rpal: ‘main loop
if alcal=0 then gosub nosensor ' check for temperature sensor condition
adcin 0, calsens ‘ sample sensor
calsens = 488 * calsens ; 1024*488=499712; ‘ convert to °C
calsens = DIV32 10
LCDOUT $FE, $80 +6 ‘ display temperature on line 1
pause 20
lcdout DEC calsens DIG 4, DEC calsens DIG 3,DEC calsens DIG 2, ".", DEC calsens DIG 1, $DF,"C", " rp"
LCDOUT $FE,$C0 +5 ' display used foil length on line 2
pause 20
LCDOUT dec contador
if calsens<= temperatura-200 then high calentador ' if temperature is lower than setting, turn heater on
if calsens=> temperatura then low calentador ' and if it's higher then turn it off
button porta.1,0,0,0,boton,1,b1 ' check activation sensor if pressed go to b1
if c1= 0 then gosub debo ' if the key * was pressed go to debounce routine
if c1=0 then ‘ if still pressed sound key and go to set temperature
sound porta.5,[fr,10]
high l4
gosub tempajust
if jo=1 then ‘ on return clear flag and go to main loop
jo=0
goto rutpal
endif
endif
if c2= 0 then ' if the key 0 was pressed go to jogging routine
if jo=1 then ‘ on return clear flag and go to main loop
jo=0
goto rutpal
endif
endif
if c3= 0 then gosub debo ' if the key # was pressed go to debounce routine
if c3=0 then
sound porta.5,[fr,10] ‘if still pressed sound key and go to set length
high l4 ‘ on return clear flag and go to main loop
gosub medajust
if jo=1 then
jo=0
goto rutpal
endif
endif
if c4= 0 then gosub debo ' if the if the key D was pressed go to debounce routine
if c4=0 then ‘if still pressed sound key and go to clear contador routine
sound porta.5,[fr,10] ‘ on return clear flag and go to main loop
high l4
gosub borrar
if jo=1 then
jo=0
goto rutpal
endif
endif
goto rpal ' loop untill a subroutine is called up
b1:
counter= 0 ' routine to prevent program from seizing if sensor is not released
if porta.1= 1 then
boton=0
goto avance
endif
if porta.1= 0 then counter= counter +1
pause 50
if counter= 50 then rutpal
goto b1
avance:
INTCON=%10010000 ‘ enable RB.0 interrupts
high motor ‘start motor
goto rutpal ‘ return to main loop
disable ‘ interrupt handler routine, disable interrupts ' disable all interrupts
motstop:
counter= counter+1 ‘ increment count by one
if counter=> medida then ‘ if count matches setting turn motor off
low motor
high brake ‘ apply brake and pause 300 ms
pause 300
low brake ‘ turn brake off
contador= contador+ counter ‘ add count to previous count
counter=0 ‘ clear counter
INTCON=%10000000 ‘ disable RB.0 interrupts
else
INTCON=%10010000 ‘ if no match then enable rb.0 interrupts and return
endif
enable
![]()
Bookmarks