;************************************************************************************************************** ; ; -=[ KFZ-Innenlichtdimmer v4 ]=- V1.10b 16.02.2006 ; ; (C) by CoolChip ; ;************************************************************************************************************** ; ; Taktfrequenz: 20MHz ; ; Pinbelegung ; ----------------------------------------------------------------------------- ; PORTA: 0 OUT LED ; 1 COMP1 sense ; 2 COMP2 LDR ; 3 OUT relais ; 4 IN jump_fflash ; 5 * ; 6 OSC2 ; 7 OSC1 ; ; PORTB: 0 IN Anlasser an(1) - aus(0) ; 1 * ; 2 * ; 3 PWM *LICHT* ; 4 IN Schloss_auf aktiv(1) - nope(0) ; 5 IN Schloss_zu aktiv(1) - nope(0) ; 6 IN SU Schlüssel steckt(1) - abgezogen(0) ; 7 IN Tür auf(1) - zu(0) ; ;************************************************************************************************************** ; Befehle für COMPILER oder BURNER ; processor 16f628 #include ERRORLEVEL -302 ;SUPPRESS BANK SELECTION MESSAGES ; ; Boden off, Code Protection off, Data Protectioon off, Power-on-Timer on, ; Watchdog off, Low-Power-Programming off, Master Cear off, HS-Oscillator ; __CONFIG _BODEN_OFF & _CP_OFF & _DATA_CP_OFF & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _MCLRE_OFF & _HS_OSC ; ;************************************************************************************************************** ; Variablen festlegen 20h ... 7Fh ; loops equ 0x20 ; Zähler für Warteschleife loops2 equ 0x21 ; Zähler für untere Warteschleife loops3 equ 0x29 ; Zähler für Langzeittimer loops4 equ 0x30 xflags equ 0x22 ; Platz für zusätzliche Flags pb_save equ 0x23 ; letzter Zustand von PortB licht_zeit equ 0x24 ; Zähler für Licht_an Timer cfh_zeit equ 0x25 ; Zähler für CFH-Timer todo equ 0x26 ; Was machen nach Interrupt? todo_buffer equ 0x27 ; todo ............ xxxx xxxx ; ^ 0. dimmen [run/pause] ; ^ 1. dimmen [auf/ab] ; ^ 2. speed [schnell/langsam] ; ^ 3. timer [an/aus] ; ^ 4. f_flash [an/aus] ; ^ 5. motor an, tür offen ; ^ 6. following-coming-home [an/aus] ; ^ 7. langzeit-timer [an/aus] licht_buffer equ 0x40 ; Speicher für die abschalt-zeit cfh_buffer equ 0x41 ; Speicher für die cfh-zeit W_save equ 0x70 ; Backup für Akku Status_save equ 0x71 ; Backup für Status-Bits ; Feste Werte ; #define licht_sek d'30' ; Zeit bis zum Abdimmen des Innenlichts (sek.) #define cfh_sek d'15' ; Zeit, die die Coming-Following Home Funktion an ist (sek.) #define langzeit_min d'15' ; Zeit, die das Licht anbleibt wenn die Tür offen ist (min.) ; Flag bits (FLAGS) ; #define motor xflags, 0 ; Motorzustand-Flag #define cfh_an xflags, 1 ; cofoho war an #define fflash_an xflags, 2 #define led PORTA, 0 ; LED Ausgang #define jumper PORTA, 4 ; f_flash an/aus #define relais PORTA, 3 ; Relais-Ausgang ; ;************************************************************************************************************** ; Sprung zum Programmanfang org 0x0000 goto init ;************************************************************************************************************** ; Interruptroutine org 0x0004 movwf W_save ; erstmal Backup machen swapf STATUS,W bcf STATUS, RP0 ; Bank0 movwf Status_save ; --------------------------I-N-T-R-O--------------------------- ; Überprüfung auf Störung call pause50 ; Störungswartezeit (entprellen) ; *** PortB & Anlasser Interrupt *** btfsc INTCON, INTF ; RB0_Int gesetzt? goto anlasser_int btfsc INTCON, RBIF ; PortB_Int gesetzt? goto portb_int anlasser_int btfss PORTB, 0 goto int_noaction bsf motor clrf cfh_zeit ; cofoho aus, und timer zurücksetzen bcf todo, 6 bcf cfh_an bcf relais btfss PORTB, 7 movlw b'00000101' ; schnell ausdimmen btfsc PORTB, 7 movlw b'00100000' ; Motor an, Tür offen movwf todo goto int_ende portb_int movfw pb_save xorwf PORTB, w btfsc STATUS, Z ; Hat sich was geändert goto int_noaction ; Nein movwf pb_save btfsc pb_save, 4 ; Schloss auf? goto lock_open_routine btfsc pb_save, 5 ; Schloss zu? goto lock_close_routine btfsc pb_save, 6 ; SU? goto key_out_routine btfsc pb_save, 7 ; Türänderung? goto door_rumble goto int_noaction ; Fehler !?! lock_open_routine btfss PORTB, 4 goto int_noaction btfsc motor goto int_noaction ; nix (Motor ist an) movlw b'01001011' ; normal andimmen [timer], folcomhom (Motor ist aus) goto portb_end lock_close_routine btfss PORTB, 5 goto int_noaction btfsc motor goto int_noaction ; nix (Motor ist an) movlw b'01010001' ; normal ausdimmen [fflash], folcomhom (Motor ist aus) goto portb_end key_out_routine btfsc PORTB, 6 goto int_noaction btfss motor goto int_noaction ; nix (Motor war aus) bcf motor ; Motor war an btfss PORTB, 7 ; Tür auf? movlw b'00001011' ; nö - normal andimmen [timer] btfsc PORTB, 7 ; Tür auf? movlw b'10000011' ; jo - normal andimmen [langzeit-timer] goto portb_end door_rumble btfsc PORTB, 7 goto door_open goto door_close door_open clrf cfh_zeit ; cofoho aus, und timer zurücksetzen bcf todo, 6 bcf cfh_an bcf relais btfss motor movlw b'10000011' ; normal andimmen (Motor ist aus) [langzeit-timer] btfsc motor movlw b'00000111' ; schnell andimmen (Motor ist an) goto portb_end door_close btfss motor movlw b'00001000' ; timer aktivieren (Motor ist aus) btfsc motor movlw b'00000101' ; schnell ausdimmen (Motor ist an) goto portb_end portb_end movwf todo_buffer movf todo, w andlw b'01000000' iorwf todo_buffer, w movwf todo ; todo speichern goto int_ende ; ... und raus ; --------------------------O-U-T-R-O--------------------------- int_noaction movfw PORTB movwf pb_save bcf INTCON, INTF ; RB0_Int löschen bcf INTCON, RBIF ; PortB_Int löschen swapf Status_save, w ; Backup einspielen ;) movwf STATUS swapf W_save,f swapf W_save,w retfie int_ende movfw PORTB movwf pb_save bcf INTCON, INTF ; RB0_Int löschen bcf INTCON, RBIF ; PortB_Int löschen ; swapf Status_save, w ; Backup einspielen ;) ; movwf STATUS ; swapf W_save,f ; swapf W_save,w bsf INTCON, GIE ; Global Int enable goto main ; ;************************************************************************************************************** ; Initialisierung ; ;************************************************************************************************************** init ; ** Ports vorbereiten ** ; PIC16F628 RA1 und RA2 Comparator, RA0 und RA3 digital bsf CMCON, CM0 bcf CMCON, CM1 bsf CMCON, CM2 bcf CMCON, C2INV ; Comparatorausgang nicht invertieren ; ** Port für Ein-/Ausgang vorbereiten ** bsf STATUS, RP0 ; Bank1 movlw B'11110110' ; 0 = Output movwf TRISA bcf STATUS, RP0 ; Bank0 clrf PORTA ; PortA = 0 ; ** PWM vorbereiten ** ; Frequenz auf 19.53 kHz kHz einstellen bsf STATUS,RP0 ; Bank1 movlw h'FE' ; eigentlich FF... movwf PR2 ; 19.53 kHz bcf STATUS,RP0 ; Bank0 ; Tastverhältnis auf 0% einstellen movlw d'0' movwf CCPR1L ; 0% ; PWM MODE mit CCP1 initialisieren clrf CCP1CON ; CCP1-Modus aus bsf CCP1CON,CCP1M3 ; CCP1-Modus PWM-Mode bsf CCP1CON,CCP1M2 ; RC2/CCP1 Hauptschalter an bsf STATUS,RP0 ; Bank1 bcf TRISB, 3 ; TRISB<3> clear enables Output bcf STATUS,RP0 ; Bank0 ; ** Timer vorbereiten ** ; Vorteiler 1:1 und Timer2 einschalten bcf T2CON,T2CKPS0 ; Vorteiler 1:1 bcf T2CON,T2CKPS1 bsf T2CON,TMR2ON ; Timer2 ein ; ** Sonstige Register/Flags löschen/laden ** movfw PORTB ; PortB auslesen und movwf pb_save ; in pb_save speichern clrf todo clrf xflags ; ** Interrupts vorbereiten ** bsf STATUS, RP0 ; Bank 1 bsf OPTION_REG, INTEDG ; RB0 Interrupt bei positiver Flanke bcf STATUS, RP0 ; Bank 0 bsf INTCON, INTE ; RB0 Interrupt erlauben bsf INTCON, RBIE ; PortB Interrupt erlauben ; ** einzelne Interrupts löschen und Global einschalten ** bcf INTCON, INTF ; RB0_Int löschen ; movf PORTB, w ; Man muss das erst lesen bevor man RBIF löschen kann bcf INTCON, RBIF ; PortB_Int löschen bsf INTCON, GIE ; Global Int enable ; ** ENDE der Initalisierung call pause250 ; kleine Pause call pause250 ; ;************************************************************************************************************** ; HAUPTPROGRAMM ; ;************************************************************************************************************** main movf todo, f btfss STATUS, Z goto arbeiten movf CCPR1L, f btfss STATUS, Z goto arbeiten schlafen sleep nop nop arbeiten test_comfolhom btfss todo, 6 ; comfolhom? goto dimm ; nö btfsc CMCON, C2OUT ; dunkel genug? goto dimm ; nö bsf relais ; SW an ; Hier wird gedimmt dimm btfss todo, 0 ; dimmen? goto nodimm ; nö doordimm btfss todo, 5 ; Tür nich ganz zu? goto andimm ; nö call tuer_error bcf todo, 5 andimm btfss todo, 1 ; andimmen? goto ausdimm ; nö btfss todo, 2 ; schnell? call dimm_an btfsc todo, 2 ; langsam? call dimm_fan bcf todo, 0 bcf todo, 1 bcf todo, 2 goto nodimm ausdimm btfss todo, 2 ; schnell? call dimm_aus btfsc todo, 2 ; langsam? call dimm_faus bcf todo, 0 bcf todo, 1 bcf todo, 2 goto nodimm ; Hier werden sonstige Funktionen durchgeführt nodimm test_fflash btfss todo, 4 ; fflash? goto cfh_pause ; nö bcf todo, 4 btfss jumper ; Jumper gesetzt? goto cfh_pause ; jo bsf fflash_an ; f_flash war an call frank_flash ; pause damit die Scheinwerfer lang genug leuchten cfh_pause btfss todo, 6 ; comfolhom noch gesetzt? goto test_timer ; nö call comfolhom bcf relais ; SW wieder aus bcf todo, 6 bsf cfh_an ; cfh_an bit setzen test_timer btfss todo, 3 ; timer aktivieren? goto test_tueroffen ; nö call ltimer bcf todo, 3 test_tueroffen btfss todo, 5 ; tür offen, motor an? goto test_langtimer ; nö call tuer_error bcf todo, 5 test_langtimer btfss todo, 7 ; langzeittimer? goto test_end ; nö bcf todo, 7 call langzeit_timer call dimm_aus test_end bsf led call pause50 bcf led call pause50 goto main ; ;************************************************************************************************************** ; Dimmer-Routinen ; ;************************************************************************************************************** ; Andimmen dimm_an movlw h'FF' subwf CCPR1L, w btfsc STATUS, Z ; Zero Flag an? return ; Ja incf CCPR1L, f ; PWM um 1 erhöhen call pause10 goto dimm_an ; Schnell Andimmen dimm_fan movlw h'FF' subwf CCPR1L, w btfsc STATUS, Z ; Zero Flag an? return ; Ja incf CCPR1L, f ; PWM um 1 erhöhen call pause5 goto dimm_fan ; Ausdimmen dimm_aus movlw d'0' subwf CCPR1L, w btfsc STATUS, Z ; Zero Flag an? return ; Ja decf CCPR1L, f ; PWM um 1 verkleinern call pause10 goto dimm_aus ; Schnell Ausdimmen dimm_faus movlw d'0' subwf CCPR1L, w btfsc STATUS, Z ; Zero Flag an? return ; Ja decf CCPR1L, f ; PWM um 1 verkleinern call pause5 goto dimm_faus ; ;************************************************************************************************************** ; Frankreich-Blinker ; ;************************************************************************************************************** frank_flash call pause250 call pause250 call pause250 call f_flash call f_flash call f_flash return f_flash ; Kurz aufblitzen clrf CCPR1L ; Lampe aus movlw h'FF' ; Lampe an movwf CCPR1L bsf led call pause30 ; Pause call pause30 clrf CCPR1L ; Lampe aus bcf led ; Kurze Pause call pause50 ; Auf die Hälfte hochdimmen ff_andimm movlw d'4' addwf CCPR1L, f ; PWM einstellen call pause15 movlw h'88' subwf CCPR1L, w btfss STATUS, Z ; Zero Flag an? goto ff_andimm ; Nein! bsf led ; Kurze Pause call pause50 ; Wieder abdimmen ff_abdimm movlw d'4' subwf CCPR1L, f ; PWM einstellen call pause15 btfss STATUS, Z ; Zero Flag an? goto ff_abdimm ; Nein! bcf led return ; ;************************************************************************************************************** ; Coming-Following-Home Funktion ; ;************************************************************************************************************** comfolhom movf cfh_zeit, w btfsc STATUS, Z ; cfh-timer = 0 ? goto cfh_time_mod ; jo movwf cfh_buffer goto start_timer3 cfh_time_mod movlw cfh_sek movwf cfh_buffer movlw 0x00 btfsc fflash_an movlw d'04' ; Zeit vom f_flash subwf cfh_buffer, f start_timer3 ; Countdown starten bcf fflash_an bcf led movf cfh_buffer, w movwf cfh_zeit warten3 call pause250 call pause250 call pause250 call pause250 bsf led call pause50 bcf led decfsz cfh_zeit, f ; Zeit vorbei? goto warten3 ; nein, noch nicht! return ; ;************************************************************************************************************** ; lTimer - Countdown bis das Licht wieder aus geht ; ;************************************************************************************************************** ltimer movf CCPR1L, f btfsc STATUS, Z ; Licht schon aus? return ; Ja enddimm ; Vollständig andimmen movlw h'FF' subwf CCPR1L, w btfsc STATUS, Z ; Zero Flag an? goto ltimer_mod ; Ja incf CCPR1L, f ; PWM um 1 erhöhen call pause10 goto enddimm ltimer_mod ; Dimmzeit anpassen movlw licht_sek movwf licht_buffer movlw 0x00 btfsc cfh_an movlw cfh_sek ; Zeit vom cfh subwf licht_buffer, f start_timer ; Countdown starten bcf cfh_an bcf led movf licht_buffer, w ; Licht-Zeit movwf licht_zeit warten call pause250 call pause250 call pause250 call pause250 bsf led call pause50 bcf led decfsz licht_zeit, f ; Zeit vorbei? goto warten ; nein, noch nicht! call dimm_aus ; normal ausdimmen return ; ;************************************************************************************************************** ; Tür auf, Motor an ; ;************************************************************************************************************** ; tuer_error movlw d'70' ; Herunterdimmen bis ca. 30% subwf CCPR1L, w btfsc STATUS, Z ; Zero Flag an? return ; Ja decf CCPR1L, f ; PWM um 1 verkleinern call pause5 goto tuer_error ; ;************************************************************************************************************** ; Tür auf, Motor an ; ;************************************************************************************************************** ; langzeit_timer movlw langzeit_min ; Zeitkonstante für Langzeittimer movwf loops3 wai3 movlw d'60' movwf loops4 wai4 call pause250 bsf led call pause250 call pause250 call pause250 bcf led decfsz loops4, f ; 1 min vorbei? goto wai4 ; nein, noch nicht decfsz loops3, f ; x min vorbei? goto wai3 ; nein, noch nicht return ; ;************************************************************************************************************** ; Pause-Routine ; ;************************************************************************************************************** ; Pause von 250 ms pause250 movlw D'250' movwf loops call wai return ; Pause von 50 ms pause50 movlw D'50' movwf loops call wai return ; Pause von 40 ms pause40 movlw D'40' movwf loops call wai return ; Pause von 30 ms pause30 movlw D'30' movwf loops call wai return ; Pause von 15 ms pause15 movlw D'15' movwf loops call wai return ; Pause von 10 ms pause10 movlw D'10' movwf loops call wai return ; Pause von 5 ms pause5 movlw D'5' movwf loops call wai return ; Warteschleife für 1 ms wai movlw D'249' ; Zeitkonstante für 1 ms movwf loops2 wai2 nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop decfsz loops2, f ; 1 ms vorbei? goto wai2 ; nein, noch nicht decfsz loops, f ; xxx ms vorbei? goto wai ; nein, noch nicht return ; das Warten hat ein Ende ; ;************************************************************************************************************** ; ENDE ; ;************************************************************************************************************** ; org 0x2100 ; de "KFZ-Innenlichtdimmer v4 (c)2006 by coolchip.de " ende end