PROGRAMA CONTADOR DE PULSOS DE UN MEDIDOR DE FLUJO
DEFINE  LCD_BITS    4
Define	LCD_DREG	PORTC        
Define	LCD_DBIT	4
Define	LCD_RWREG	PORTD         
Define	LCD_RWBIT	6               
Define	LCD_RSREG	PORTD         
Define	LCD_RSBIT	5             
Define	LCD_EREG	PORTD         
Define	LCD_EBIT	7            
DEFINE  ADC_BITS 8
define  OSC 4
define ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 30
;TRISD = %00000000 
TRISB = %01001111
TRISA = 255
ADCON1 = 2
caubaj var byte
caumini var byte
volt var byte
oxbajo var byte
x  var byte
dato var word
;pulsos var byte
datoE var byte
datoS var byte
botsubir var portb.1
botbajar var portb.2
enter  var portb.3
led var portd.3
alerta var PORTD.2
Qnormal var PORTD.0
Qbaja var PORTD.1
sensor1 var portb.6
frec var byte            
bandera var bit
frec = 10
eeprom 0,[5,10,1,90]         ;contenido inicial de la EEPROM
inicio:                ;3 parpadeos del led indica que funciona
for x =1 to 3
    HIGH led
    pause 200
    low led
    pause 200
next
READ 0, caubaj             ;lee la EEPROM 0 y lo guarda en tempbaj
READ 1, caumini            ;lee la EEPROM 1 y lo guarda en tempalt
read 2, volt
read 3, oxbajo
medir:
    pwm PORTC.2,FREC,100
    low led 
    bandera = 0
    count PORTC.1,125,dato 
    adcin 1, datoE
    adcin 2, datoS
	Lcdout $fe, 1 ,"Q.mi Q.lt/m Q.ma"
    dato = dato*2 
    datoE = datoE/2
    datoS = datoS/2              
    Lcdout $fe,$c6,DEC dato	
    Lcdout $fe,$c0,DEC caubaj	
    Lcdout $fe,$cc,DEC caumini 
    lcdout $fe,$d5,dec oxbajo,"%"
    lcdout $fe,$9f,"V"
    lcdout $fe,$90,"Oent":lcdout $fe,$95,"Oref":lcdout $fe,$9a,"Osal"
    lcdout $fe,$d0,DEc datoE,"%":Lcdout $fe,$da,dec datoS,"%"
    lcdout $fe,$de,dec volt
    if sensor1 = 1 then medir
    for x = 1 to 3             
    if enter = 0 then grabar1a
    pause 10
    next  
    if botsubir = 0 then SUBIR
    if botbajar = 0 then BAJAR
    if datoS < Oxbajo then high alerta
    if datoS > oxbajo then low alerta
    if caubaj > dato then disminuir  
    if caumini > dato > caubaj then mantener
    goto medir
aviso:
   high led
    goto medir
mantener:
    low Qbaja: high Qnormal  
    if frec > (volt*10) then medir
    if bandera = 1 then medir
    high led
    bandera = 1
    frec = frec + 10
    goto medir    
disminuir:
    low Qnormal:  high Qbaja
    if botsubir = 0 then SUBIR
    if frec < (volt*7) then medir
    if bandera = 1 then medir
    high led
    bandera = 1
    frec = frec - 10
    goto medir
SUBIR:
    if frec > 249 then aviso
    if bandera = 1 then medir
    high led
    bandera = 1
    frec = frec+10
    goto medir
BAJAR:
    if frec < 11 then aviso
    if bandera = 1 then medir
    high led
    bandera = 1 
    frec = frec - 10
    goto medir
grabar1a:
    gosub soltar 
grabar1:
    Lcdout $fe, 1 ,"Programar Q."
    lcdout $fe,$c0,"bajo= ",dec caubaj ,"lt/m"
    pause 100
    if botbajar = 0 then restar1
    if botsubir = 0 then sumar1
    if enter = 0 then grabarA
goto grabar1
restar1:
    gosub soltar                  ;programa antirrebote de tecla
    if caubaj < 1 then grabar1
    caubaj = caubaj - 1
goto grabar1
sumar1:
    gosub soltar
    if caubaj > 24 then grabar1
    caubaj = caubaj + 1
goto grabar1
grabarA:
    gosub soltar
    write 0, caubaj       ;escribir en la dirección 0 de la EEPROM
grabar2:   
    Lcdout $fe, 1 ,"Programar Q."
    lcdout $fe,$c0,"alto= ",dec caumini ,"lt/m"
    pause 100
    if botbajar = 0 then restar2
    if botsubir = 0 then sumar2
    if enter = 0 then grabarB
goto grabar2
restar2:
    gosub soltar
    if caumini < 5 then grabar2
    caumini = caumini - 1
goto grabar2
sumar2:
    gosub soltar
    if caumini > 24 then grabar2
    caumini = caumini + 1
goto grabar2 
grabarB:
    gosub soltar
    write 1, caumini          ;escribir en la dirección 1 de la EEPROM
grabar3:
    Lcdout $fe, 1 ,"Programar Volt."
    lcdout $fe,$c0,"Volt= ",dec vOLT ,"V"
    pause 100
    if botbajar = 0 then restar3
    if botsubir = 0 then sumar3
    if enter = 0 then grabarC
goto grabar3
restar3:
    gosub soltar                  ;programa antirrebote de tecla
    if Volt < 1 then grabar3
    Volt = Volt - 1
goto grabar3
sumar3:
    gosub soltar
    if Volt > 23 then grabar3
    Volt = Volt + 1
goto grabar3
grabarC:
    gosub soltar
    write 2,Volt 
grabar4:
    Lcdout $fe, 1 ,"Programar O."
    lcdout $fe,$c0,"O.bajo= ",dec oxbajo,"%"
    pause 100
    if botbajar = 0 then restar4
    if botsubir = 0 then sumar4
    if enter = 0 then grabarD
goto grabar4
restar4:
    gosub soltar                  ;programa antirrebote de tecla
    if oxbajo < 1 then grabar4
    oxbajo = oxbajo - 1
goto grabar4
sumar4:
    gosub soltar
    if oxbajo > 99 then grabar4
    oxbajo = oxbajo + 1
goto grabar4
grabarD:
    gosub soltar
    write 3,oxbajo   
goto inicio
soltar:                       ;programa antirrebote de tecla
    high led
    pause 150
    low led
soltar2:
    if botbajar = 0 then soltar2
    if botsubir = 0 then soltar2
    if enter = 0 then soltar2
    pause 100
    return
end