LIST p=16F876A
INCLUDE "p16f876a.inc"
__config _CP_OFF&_CPD_OFF&_LVP_OFF&_WDT_OFF&_XT_OSC
CBLOCK 0XF0
UNI
DEC
CEN
DISPLAY_ACTIVO
TIEMPO1
TIEMPO2
BUFFER
CONTADOR1
CONTADOR2
ENDC
ORG 0x00
GOTO INICIO
ORG 0x04
RETURN
INICIO:
BANKSEL TRISA
movlw b'00011111' ;Configura puerto A
movwf TRISA
clrf TRISB ;Puerto B completo como salidas (bus del LCD)
clrf TRISC ;Puerto C completo como salidas
movlw b'10001110' ;Configura los pines del Puerto A
movwf ADCON1 ;AN0 como única entrada análoga
BANKSEL PORTA
movlw b'01000001' ;Enciende y configura el convertidor A/D - Selecciona AN0 como entrada
movwf ADCON0
; Configurar el timer aca
clrf PORTA ;Apaga todo
clrf PORTB
clrf PORTC
CLRF DISPLAY_ACTIVO ; Ponemos a 0 el indicador de display
CICLO:
CALL CONVERSION_ADC
CALL ADC_TO_BCD
CALL MOSTRAR
goto CICLO ;Vuelve a medir y mostrar
; ------------------------------------------------------------------------
; Inicia la conversion del ADC y espera a que termine
; ------------------------------------------------------------------------
CONVERSION_ADC:
bsf ADCON0, GO ;Inicia la conversión A/D
btfsc ADCON0, GO ;Espera que termine de convertir
goto $ -1
RETURN
; ------------------------------------------------------------------------------------------
; Toma los datos del ADC y los usa.
; Toma los bits del 1 al 8 del resultado del ADC. De esa forma con un poco de error tiene
; un resultado cercano respecto a la temperatura.
; Entrada: ADRESH, ADRESL
; Salida: UNI, DEC, CEN
; ------------------------------------------------------------------------------------------
ADC_TO_BCD:
RRF ADRESH, W ; Giro para que quede el bit 8 en el carry
BANKSEL ADRESL ; Voy al banco 1
RRF ADRESL, W ; Giro nuevamente esta ves eliminando el bit de menos peso de ADRESL y poniendo el valor del carry en el bit 7, todo eso en W
BANKSEL PORTA ; Vuelvo al banco 0, para evitar posibles problemas
call DECIMAL ; Obtiene resultado en BCD: UNI, DEC y CEN
RETURN
; ------------------------------------------------------------------------------------------
; Muestra los datos en el display
; Usamos un timer con un tiempo de 5ms
; Entrada: UNI, DEC, CEN
; ------------------------------------------------------------------------------------------
MOSTRAR:
BANKSEL PORTA ; Vuelvo al banco 0, para evitar posibles problemas
BTFSS INTCON, T0IF ; Paso el tiempo del Timer?
RETURN ; No paso entonces salgo de aca
MOVLW 0x00 ; Recargo el TMR0
MOVWF TMR0
BCF INTCON, T0IF
MOVF DISPLAY_ACTIVO
ADDWF PCL, F
GOTO DISPLAY_UNI
GOTO DISPLAY_DEC
GOTO DISPLAY_CEN
; Seguridad aca
CLRF DISPLAY_ACTIVO
RETURN
DISPLAY_UNI
CLRF PORTC ; Apago cualquier valor
MOVF UNI, W ;Carga W con el valor de unidad UNIDAD
call tabla ;Vuelve W con los bits para formar el digito
movwf PORTB ;manda el digito al puerto B
bsf PORTC,2 ;enciende el bit 2 del puerto A
RETURN
DISPLAY_DEC
CLRF PORTC ; Apago cualquier valor
MOVF UNI, W ;Carga W con el valor de unidad UNIDAD
call tabla ;Vuelve W con los bits para formar el digito
movwf PORTB ;manda el digito al puerto B
bsf PORTC,1 ;enciende el bit 2 del puerto A
RETURN
DISPLAY_CEN
CLRF PORTC ; Apago cualquier valor
MOVF UNI, W ;Carga W con el valor de unidad UNIDAD
call tabla ;Vuelve W con los bits para formar el digito
movwf PORTB ;manda el digito al puerto B
bsf PORTC,0 ;enciende el bit 2 del puerto A
RETURN
; ------------------------------------------------------------------------------------------
; Convierte el valor binario en BCD
; Entrada: W
; Salida: UNI, DEC, CEN
; ------------------------------------------------------------------------------------------
DECIMAL
movwf uni ;Convierte el dato presente en W en UNI, DEC y CEN
clrf dec
clrf cen
movlw d'100' ;Determina la cant. de centenas
CENTENA
subwf uni, F
btfss STATUS, C
goto CIEN
incf cen, F
goto CENTENA
CIEN
addwf uni, F
movlw d'10' ;Determina la cant. de decenas
DECENA
subwf uni, F
btfss STATUS, C
goto DIEZ
incf dec, F
goto DECENA
DIEZ
addwf uni, 1 ;Uni queda con la cant. de unidades (sin decenas ni centenas)
return
;===========================================
; TABLA
;===========================================
tabla ; retlw b'gfedcba' para display catodo comun
addwf PCL,1 ; Se incrementa el contador del programa.-
DT 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F
end