==============================================================================================
;PROGRAMA DE UN DIMMER VARIADO DESDE UN POTENCIOMETRO
;O DESDE EL TECLADO MATRICIAL
;------------------------------------------------------------------
;CONEXION
;=====================================================================
;EL POT Cconectado a ra0
;pulsantes conectadoa al PORTB,0 PORTB,1 y PORTB,2
;EL PORTB,0 Y PORTB,1 SIVEN PARA LA INTENSIDAD DEL LED.
;EL PORTB,2 SIVE PARA SELECCIONAR CON QUE SE DESEA VARIAR LA INTENSIDAD
;CON EL POT O CON LOS PULSANTES
;==============================================================================================
list p=18f452
#include <p18f452.inc>
;============================================================================
;ZONA DE DECLARACION DE SIMBOLOS Y VARIABLES.
;============================================================================
;RAM de Display auxiliar (para conteo)
tecla = 0x06
flag = 0x07
temp= 0x08
nlamp= 0x09
cont= 0xa
compara = 0x0b
;vectores de reset e interrupcion
org 0
goto inicio
org 0x18
goto interrupt ;vector de IT (baja priorodad)
inicio
movlb 0
;---------------------------------------
;CONFIGURACION DEL PUERTO B
;----------------------------------------
clrf PORTB ;LIMPIO EL PORTB
movlw 0x07 ;CARGON 0X03 AL W
movwf TRISB ;PORTB.0 AL PORTB.1 SON ENTRADAS
bcf INTCON2,7 ;ACTIVO PULL-UPS
;--------------------------------------
clrf PORTC ;PORTC = 0000 0000 (inicialmente)
movlw b'11111110'
movwf TRISC ;PORTC,1 ES SALIDA
;---------------------------------------
;==============================================
;Temporizadores
;===============================================
;RCON (No prioridades)
bcf RCON,7
; Timer 0
;configiracion del timer0
movlw b'01100000'
movwf INTCON
movlw b'01000101'
movwf T0CON
clrf TMR0L
clrf TMR0H
;-----------------------------------
; Timer 2
;sirve para el PWM
;------------------------------------
clrf T2CON ;limpio T2
clrf TMR2
;-----------------------------------
;Configuracion del PWM
movlw d'249'
movwf PR2 ;F=1Kh
clrf CCP1CON ;CCP module apagado
bsf CCP1CON, CCP1M3 ;Modo PWM
bsf CCP1CON, CCP1M2 ;Modo PWM
movlw 0x00 ; Valor inicial
movwf CCPR1L ; CU
;-----------------------------------------
;CONFIGURACION DEL A/D
movlw B'00001110' ;RA0 es entrada analogica
movwf ADCON1 ;ADRESH
movlw B'00000000' ; AN1; F/2
movwf ADCON0 ; A/D deshabilitado
bcf PIR1,ADIF ; Limpio bandera A/D interrupt
bsf PIE1,ADIE ; Enable A/D interrupt
bsf INTCON,PEIE ; Enable peripheral interrupts
;-------------------------------------------
;Inicia Timer
bsf T0CON,7 ;enciendo timer 0
bsf T2CON,TMR2ON ;enciendo timer 2
; Interrupciones On
bsf INTCON,GIE ;GIE = 1!
;-----------------------------------------
again
goto again
;============================================
; SUBRUTINA DE INTERRUPCION
;=============================================
interrupt
;preguntar por las banderas respectivas
;¿interrumpió el timer0?
btfsc INTCON,TMR0IF
goto int_timer0
btfss PIR1,ADIF ;interrumpio el CONVERSOR A/D
goto no_it ;no hubo interrupcion
;cuando el conversor interrupte cargo el valor del AD al CCPR1L
movff ADRESH,CCPR1L ;tomo el valor A/D
goto back_AD ;salgo del conversor
;--------------------------------
;Atencion al timer 1
int_timer0
btfss PORTB,2 ;selector entre POT y el teclado
call teclado
call conv_AD
goto back_tmr0
;--------------------------------
back_tmr0
bcf INTCON,TMR0IF ;limpia bandera del tmr0
goto no_it
;----------------------------------------
back_AD
bcf ADCON0,0 ;apago AD
bcf ADCON0,1 ;paro conversor
goto no_it
;--------------------------------------
no_it
retfie 1
;----------------------------------------------
; SUBRUTINA TECLADO
;---------------------------------------------
teclado
btfss PORTB,1
goto down
btfsc PORTB,0
return
movf CCPR1L,W
sublw 0xFF
btfsc STATUS,Z
return
incf CCPR1L
return
down
movf CCPR1L,W
btfsc STATUS,Z
return
decf CCPR1L
return
;-----------------------------------------------------
conv_AD
bsf ADCON0,0 ;prendo AD
bsf ADCON0,2 ;go conversor
return
end