Hello all,
I must capture the rpm of diesel engine in a real time and
control the output by parameters.
Now I've try with this following code but during the acquisition the PIC can't
control the output and I loose the output pulse.
I think that must use the Capture/Pwm function for acquire the pulse input
while control the pulse output.
Any suggest are welcome, thanks a lot

giorgio

CODE:


'RPM Engine acquisition
rpm = 0
count porta.6,300,RPM2
rpm = RPM2 * 100

GIRI2M = RPM/60
GIRIM = ABS ((inj2*100)/GIRI2M) ' TEMPO PISTONI
if rpm > 700 and inj1 >0 then

high portb.5
pause inj1
low portb.5
pause GIRIM
else
low portb.5
endif
GOTO LOOP


Full Code :

'************************************************* ***************
'* Name : Capture4x20.BAS *
'* Author : Giorgio Rossi *
'* Notice : Copyright (c) 2010 RG-Eelettronica *
'* : All Rights Reserved *
'* Date : 03/12/2010 *
'* Version : 1.0 *
'* Notes : Rpm Capture *
'* Micro : PIC16f88 *
'************************************************* ***************
@ DEVICE INTRC_OSC_NOCLKOUT
' Set clock 4 Mhz
osccon.6 = 1
osccon.5 = 1
osccon.4 = 0
osccon.2 = 1
osccon.1 = 1
osccon.0 = 0

DEFINE LCD_LINES 4
INCLUDE "MODEDEFS.BAS"

' Define LCD pins
Define LCD_DREG PORTB
Define LCD_DBIT 0
Define LCD_RSREG PORTA
Define LCD_RSBIT 3
Define LCD_EREG PORTA
Define LCD_EBIT 4


TRISA = %11100001 'RA Input
TRISB = %00000000 'all PORTB output

ANSEL = %00000001'only porta.0 is analogue
ADCON1 = 7 'right justify the result(top bits are read as zero)
CMCON = 7 'Turn off comparators

adval VAR WORD
i VAR BYTE
RPM VAR WORD
RPM2 VAR WORD
advalx VAR BYTE
girim VAR WORD
GIRI2M VAR WORD
inj1 VAR WORD
inj2 VAR WORD


'Define ADCIN parameters
Define ADC_BITS 8 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 5 ' Set sampling time in uS

low portb.5
LOW PORTB.4
pause 100 '1000
Lcdout $FE, 1
pause 1000


'start message

Lcdout $FE, 2, " "
Lcdout $FE, $c0, " RG-ELETTRONICA "
Lcdout $FE, $94, " 21040 Gerenzano "
Lcdout $FE, $D4, " ITALY "


pause 1500
Lcdout $FE, 2, " "
Lcdout $FE, $c0, " "
Lcdout $FE, $94, " loading data "
Lcdout $FE, $D4, " "

PAUSE 500

'clean display

write 2, 3 ' mS injection
write 3,100 ' % Ratio
adval = 0
advalx= 0
Lcdout $FE, 1


loop:

'init
read 2,inj1
pause 50
read 3,inj2
pause 50
low portb.5

'Injection parameters change
if porta.7 = 0 then
goto cambio
endif

'Pressure A/D sensor conversion
ADCIN 0, adval
advalx = (adval*100/255)
IF advalx < 91 THEN
HIGH PORTB.4
Lcdout $FE, 2, " ON "
endif

IF advalx > 95 THEN
LOW PORTB.4
Lcdout $FE, 2, " Off"
endif

'Dysplay data
if advalx <100 then
Lcdout $FE, 2, "pressione "," ", #advalx,"%"
endif
if advalx <10 then
Lcdout $FE, 2, "pressione "," ", #advalx,"%"
endif

if rpm <1000 then
Lcdout $FE, $c0, "giri motore "," ", #RPM," "
ELSE
Lcdout $FE, $c0, "giri motore ", #RPM," "
endif

if RPM2 <10 then
Lcdout $FE, $94, "Pulse in "," ",#RPM2
else
Lcdout $FE, $94, "Pulse in ",#RPM2
endif

Lcdout $FE, $D4, "T. PIST ",#GIRiM," ",#inj1, " mS"


'Injector overture setting

cambio :

if porta.7 = 0 and inj1=0 then 'set1
inj1 = 1
write 2,inj1
pause 200
endif

if porta.7 = 0 and inj1=1 then 'set3
inj1 = 2
write 2,inj1
pause 200
endif

if porta.7 = 0 and inj1=2 then 'set7
inj1 = 3
write 2,inj1
pause 200
endif

if porta.7 = 0 and inj1=3 then 'set7
inj1 = 4
write 2,inj1
pause 200
endif

if porta.7 = 0 and inj1=4 then 'set7
inj1 = 5
write 2,inj1
pause 200
endif

if porta.7 = 0 and inj1=5 then 'set7
inj1 = 6
write 2,inj1
pause 200
endif

if porta.7 = 0 and inj1=6 then 'set7
inj1 = 7
write 2,inj1
pause 200
endif

if porta.7 = 0 and inj1=7 then 'set0
inj1 = 0
write 2,inj1
pause 200
endif





'RPM Engine acquisition
rpm = 0
count porta.6,300,RPM2
rpm = RPM2 * 100

GIRI2M = RPM/60
GIRIM = ABS ((inj2*100)/GIRI2M) ' TEMPO PISTONI
if rpm > 700 and inj1 >0 then

high portb.5
pause inj1
low portb.5
pause GIRIM
else
low portb.5
endif
GOTO LOOP