;************************************************************************************************************************** ; PIC12F683 Noodverlichting ; clockfreq = 2MHz ; File: NV3.asm ; Version: 1.0 ; ; Date: 21/08/06 ; Author: ; Description: PIC regelt middels PWM-uitgang en AD-ingang step-up converter-schakeling. ; converter 1.2V naar 4.1 V om 2 witte LEDs te voeden vanuit 1 1.2 V cel. ; converter wordt ingeschakeld wanneer schakelaar wordt uitgezet, ; converter wordt uitgeschakeld wanneer schakelaar wordt aangezet of bat. leeg is ;*************************************************************************************************************************** list p=12F683 ;#include "c:\velleman\include_files\p12F683.INC" #include "p12f683.inc" __CONFIG _FCMEN_ON & _IESO_OFF & _CP_ON & _CPD_OFF & _BOD_SBODEN & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT ;Instruction shortcuts #define BANK0 bcf STATUS,RP0 ;Bank0 #define BANK1 bsf STATUS,RP1 ;Bank1 halfsecs EQU H'20' ; secs EQU H'21' ;Seconds mins EQU H'22' ;Minutes hours EQU H'23' ;Hours counter EQU H'24' ;counter halfsecs RESULTH EQU H'25' ; TEMP EQU H'26' W_TEMP EQU H'27' STATUS_TEMP EQU H'28' RESULTL EQU H'29' dcnt0 EQU H'2A' dcnt1 EQU H'2B' days EQU H'2C' Uout EQU H'2D' ;********************************************************************** ;Reset Vector ;********************************************************************** ORG 0x000 goto INIT ;adres 000 NOP ;adres 001 zorgt ervoor dat NOP ;adres 002 bij interrupt naar NOP ;adres 003 RTCISR wordt gesprongen goto RTCISR ;adres 004 ;*********************************************************************** ;Initialization ;*********************************************************************** ORG 0x005 INIT movlw VRCON movwf FSR clrf INDF ; Turn Off VREF movlw OSCCON ; movwf FSR movlw b'01010001' ; 2 MHz , internal oscillator movwf INDF movlw PCON ; enable software BOD movwf FSR ; disable Ultra low power wake-up movlw b'00010011' movwf INDF BANK0 clrwdt clrf GPIO bcf GPIO, 2 ; enable oscillator movlw WDTCON movwf FSR movlw b'00010100' ;00010100 = WDT time 2 sec, WDT on movwf INDF ;00011011 = WDT time 268 sec, WDT on ;00010101 = WDT time 134 sec, WDT on movlw .7 ;Turn Off comparator movwf CMCON0 ;for lowest power consumption movlw ANSEL movwf FSR movlw b'00011010' ;AD-oscillator, AN0=0, AN1=1, AN2=0, AN3=1 movwf INDF ;GP1 analoge ingang, GP3 analoge ingang movlw TRISIO movwf FSR movlw b'11111011' ;GP0-INput, GP1-INput, GP2-OUTput, GP3-INput movwf INDF ;GP4-INput, GP5-INput movlw OPTION_REG movwf FSR movlw b'10001111' ;Pull Ups Disabled, Rising Edge, Internal clock Assigned to movwf INDF ;Timer0, Prescaler to WDT , Prescaler 1:128 movlw IOC ;disable interrupt on change movwf FSR clrf INDF ;************************************************************** ;*REAL TIME CLOCK INIT***************************************** ;************************************************************** ;************************************************************** BANK0 clrf T1CON ; has to clrf TMR1H ; be done clrf TMR1L ; before INTCON is written clrf INTCON ; clear INTCON movlw PIE1 ;clear movwf FSR ;PIE1 movlw d'0' movwf INDF BANK0 clrf PIR1 MOVLW b'00001011' ; Preload TMR1 register pair MOVWF TMR1H ; For 1 second overflow MOVLW b'11011100' ; clock= 2 MHz. 2MHz:4 = 500kHz. MOVWF TMR1L MOVLW b'00110000' ; Configure for internal clock, MOVWF T1CON ; prescaler=8 500kHz: 8 = 62500Hz. 65536-62500= 3036 = ; 2048 + 512 + 256 + 128 + 64 + 16 + 8 + 4 = b00001011 11011100 CLRF secs ; Initialize timekeeping registers CLRF mins ; CLRF hours CLRF days bsf T1CON, TMR1ON ;Timer1 on movlw PIE1 ;Timer1 movwf FSR ;interrupt movlw b'00000001' ;enable movwf INDF ;activeren BANK0 bsf INTCON,PEIE ;interrupts bsf INTCON,GIE ;vrijgeven movlw CCP1CON movwf FSR movlw b'00001100' ;PWM-mode movwf INDF movlw PR2 movwf FSR movlw .255 ;PWM-period time movwf INDF movlw T2CON movwf FSR movlw b'00000100' ;TMR2 on, no prescale movwf INDF movlw CCPR1L movwf FSR movlw b'10000001' ;TMR2 duty cycle movwf INDF movlw WPU movwf FSR movlw b'00000001' ; enable Weak pull-up GP0 movwf INDF ;************************************************************************************* ;Main Program ;************************************************************************************* ; ;;;;;;;schak. 'uit' ? ; ja; meet Ubat ; Ubat< 1,1 V? ; ja; LEDs uit, wacht 10 sec ; nee; LEDs aan ;************************************************************************************** ;********************************************************************************************************** ;************************************************************************************************ ; ; Voedingsspanning PIC: 2V4 ; spanningsdeler 560K : 1M5 = factor .2718 ; U1 Ubat = 1.05 V, Uadc = 0.2718 x 1.05 V = 0.286 V ; aantal stappen = 1024 ; U/stap= 2.40 : 1024 = 0.00234 ; Uadc = 0.286 V ; stapnr = 0.286 V : 0.00234 = stap 122 64+32+16+8+2 = b'01111010' ; ; U2 Uout = 4.1 V, Uadc = (18k : 2k2 = 1:0.109) = 4.1 x 0.109 = 0.4465 V ; aantal stappen = 1024 ; U/stap= 2.70 : 1024 = 0.002637 ; Uadc = 0.4465 V ; stapnr = 0.4465 V : 0.002637 = stap 169 128+32+8+1 = b'10101001' ; ; ; ;**************************************************************************************************** ;Initialize ANSEL is configured in main INIT ; ; ADCON is configured in ADC ; ; U1 EQU b'01111010' U2 EQU b'10110001' CCPlowmax EQU b'00000100' ;laagst toegestane waarde CCPhighmax EQU b'11110000' ; ; ;************************************************************************************************ MAIN BANK0 clrwdt movlw TRISIO ;oscillator ON movwf FSR movlw b'11110011' ;GP0-INput, GP1-INput, GP2-INput, GP3-OUTput movwf INDF ;GP4-INput, GP5-INput ;;bsf GPIO, 5 ; oscillator ON bcf GPIO, 2 ; oscillator ON TESTSW ;;;;btfsc GPIO, 0 ;;;;;goto MAIN movlw TRISIO ; oscillator on movwf FSR movlw b'11110011' ;GP0-INput, GP1-INput, GP2-OUTput, GP3-OUTput movwf INDF ;GP4-INput, GP5-INput BANK0 movlw b'10000101' ; 1 = right justified, channel 1 , ADC on movwf ADCON0 call ADC BANK0 movf RESULTL, w ;haal ad waarde binnen SUBLW U2 ;U > U2? BTFSS STATUS, C ; ADC-PWM goto Uoutdown ;ja goto Uoutup Uoutdown movf CCPR1L, w movwf Uout SUBLW CCPhighmax ;U > CCPhighmax? BTFSC STATUS, C ; incf CCPR1L,f ;nee goto ADCBAT Uoutup movf CCPR1L, w movwf Uout SUBLW CCPlowmax ;U < CCPlowmax? BTFSS STATUS, C ; decf CCPR1L,f ;nee goto ADCBAT ADCBAT BANK0 movlw b'10001101' ; 1 = right justified, channel 3 , ADC on movwf ADCON0 call ADC BANK0 movf RESULTL, w ;haal ad waarde binnen SUBLW U1 ;U > U1? BTFSS STATUS, C ; goto TESTSW ;ja, bat nog niet leeg goto LOWBAT ; nee, bat is leeg LOWBAT movlw TRISIO movwf FSR movlw b'11011010' ;GP0-OUTput, GP1-INput, GP2-INput, GP3-INput movwf INDF ;GP4-INput, GP5-OUTput clrf T2CON ; stop PWM and bsf GPIO, 2 ; set GP2; bsf GPIO,5 ; oscillator off bcf INTCON, GIE clrf secs bsf INTCON, GIE TIMETEST BANK0 clrwdt movf secs,w sublw .10 ;1+ aantal sec dat gewacht moet worden btfsc STATUS, C goto TIMETEST goto INIT ;*************************************** ADC BANK0 CLRWDT ;delay for acquisition time 21 us. CLRWDT ; clock = 2 MHz CLRWDT ; 2 MHz : 4 = 500 kHz = 2 usec CLRWDT ; one instruction takes 2 usec CLRWDT ; 11 instructions = 22 usec CLRWDT CLRWDT CLRWDT CLRWDT CLRWDT CLRWDT bsf ADCON0, GO ;start AD conversion WAIT1 btfsc ADCON0, GO ;conversion is completed? GOTO WAIT1 ;no, wait some more bcf PIR1, ADIF ;reset AD vlag movlw ADRESH ;yes, haal ad waarde binnen movwf FSR ;en zet weg movf INDF,w ;moet movwf RESULTH ;via movlw ADRESL ;indirect movwf FSR ;adressing movf INDF,w ;anders movwf RESULTL ;gaat het fout RETURN ;*************************************************************************************** ;REALTIME CLOCK INTERRUPT ROUTINE ;*************************************************************************************** RTCISR movwf W_TEMP swapf STATUS,W clrf STATUS movwf STATUS_TEMP BANKSEL TMR1H ; MOVLW b'00001011' ; Preload TMR1 register pair MOVWF TMR1H ; for MOVLW b'11011100' ; one second MOVWF TMR1L ; overflow BCF PIR1, TMR1IF ; Clear interrupt flag ;bsf GPIO, 4 INCF secs, F ; Increment seconds MOVF secs, w SUBLW .60 BTFSS STATUS, Z ; 60 seconds elapsed? GOTO STOP ; No, done CLRF secs ; Clear seconds INCF mins, f ; Increment minutes MOVF mins, w SUBLW .60 BTFSS STATUS, Z ; 60 minutes elapsed? GOTO STOP ; No, done CLRF mins ; Clear minutes INCF hours, f ; Increment hours MOVF hours, w SUBLW .24 BTFSS STATUS, Z ; 24 hours elapsed? GOTO STOP ; No, done CLRF hours ; Clear hours INCF days, f ; Increment days STOP SWAPF STATUS_TEMP,W MOVWF STATUS SWAPF W_TEMP,F SWAPF W_TEMP,W RETFIE ; Done ;******************************************************************************************* ;DELAY SUBROUTINE for 250 cycles, = 500 usec = 0.5 msecs. ;******************************************************************************************** ADCdelay movlw .250 movwf counter notzero decfsz counter, f goto notzero RETURN ;******************************************************************************************* ;DELAY SUBROUTINE for ... cycles, =... msecs. ;******************************************************************************************** DELAY movlw d'1' ; delay 0,1 sec movwf dcnt0 ; 2 x 255 udelay0 movlw d'244' movwf dcnt1 udelay1 decfsz dcnt1, F goto udelay1 decfsz dcnt0, F goto udelay0 RETURN ;********************************************************************************************************** END