; CC5X Version 3.4, Copyright (c) B Knudsen Data ; C compiler for the PICmicro family ; ************ 24. Jun 2009 9:07 ************* processor 16C54 radix DEC TMR0 EQU 0x01 PORTA EQU 0x05 PORTB EQU 0x06 Zero_ EQU 2 TO EQU 4 millisec EQU 0x07 next EQU 0x09 n EQU 0x07 i EQU 0x08 x EQU 0x0F i_2 EQU 0x0F counter EQU 0x0A ; FILE test\delay.c ;/* ; DELAYS AND TIMING ; ================= ; ; Delays are frequently used. There are various ; ways to generate them: ; 1. Instruction cycle counting ; 2. Using the TMR0 timer ; 3. Watchdog timeout for low power consumption ; 4. Using variables achieves longer delays ;*/ ; ; ;void delay_ms( uns16 millisec) ;// Delays a multiple of 1 milliseconds at 4 MHz ;// using the TMR0 timer ;{ delay_ms ; char next = 0; CLRF next ; ; OPTION = 2; // prescaler divide TMR0 rate by 8 MOVLW 2 OPTION ; TMR0 = 2; // deduct 2*8 fixed instruction cycles delay MOVWF TMR0 ; do { ; next += 125; m001 MOVLW 125 ADDWF next,1 ; clrwdt(); // needed only if watchdog is enabled CLRWDT ; while (TMR0 != next) // 125 * 8 = 1000 (= 1 ms) m002 MOVF TMR0,W XORWF next,W BTFSS 0x03,Zero_ ; ; GOTO m002 ; } while ( -- millisec != 0); DECF millisec,1 INCF millisec,W BTFSC 0x03,Zero_ DECF millisec+1,1 MOVF millisec,W IORWF millisec+1,W BTFSS 0x03,Zero_ GOTO m001 ;} RETLW 0 ; ; ;void delay10( char n) ;/* ; Delays a multiple of 10 milliseconds using the TMR0 timer ; Clock : 4 MHz => period T = 0.25 microseconds ; 1 IS = 1 Instruction Cycle = 1 microsecond ; error: 0.16 percent ;*/ ;{ delay10 MOVWF n ; char i; ; ; OPTION = 7; MOVLW 7 OPTION ; do { ; clrwdt(); // only if watchdog enabled m003 CLRWDT ; i = TMR0 + 39; /* 256 microsec * 39 = 10 ms */ MOVLW 39 ADDWF TMR0,W MOVWF i ; while ( i != TMR0) m004 MOVF i,W XORWF TMR0,W BTFSS 0x03,Zero_ ; ; GOTO m004 ; } while ( --n > 0); DECFSZ n,1 GOTO m003 ;} RETLW 0 ; ; ;void _delay10( char x) ;/* ; Delays a multiple of 10 milliseconds ; using instruction cycle counting ; Clock : 32768 Hz => period T = 30.518 microseconds ; 1 Instruction Cycle = 1 IS = 4 * T = 122 microseconds ; 10 ms = 82 IS (81.92) => error: 0.1 percent ;*/ ;{ _delay10 MOVWF x ; do { ; char i = 26; /* 2 IS */ m005 MOVLW 26 MOVWF i_2 ; do ; while ( --i > 0); /* 26 * 3 - 1 = 77 IS */ m006 DECFSZ i_2,1 GOTO m006 ; } while ( --x > 0); /* 3 IS */ DECFSZ x,1 GOTO m005 ;} RETLW 0 ; ; ; ;char counter; ; ;void main( void) ;{ main ; if ( TO == 1) { BTFSS 0x03,TO GOTO m007 ; /* power up or MCLR */ ; PORTA = 0; /* write output latch first */ CLRF PORTA ; TRISA = 0; /* all outputs */ MOVLW 0 TRIS PORTA ; TRISB = 0xFF; /* all inputs */ MOVLW 255 TRIS PORTB ; } ; else { GOTO m008 ; /* watchdog wakeup */ ; if ( --counter > 0) { m007 DECF counter,1 BTFSC 0x03,Zero_ GOTO m008 ; OPTION = 0x0B; /* WDT divide by 16 */ MOVLW 11 OPTION ; sleep(); /* waiting 16 * 18 ms = ; 288 ms = 0.288 seconds */ SLEEP ; } ; } ; ; // .. ; ; delay_ms( 5500); /* 5.5 seconds */ m008 MOVLW 124 MOVWF millisec MOVLW 21 MOVWF millisec+1 CALL delay_ms ; ; // .. ; ; delay10( 100); /* 1 second */ MOVLW 100 CALL delay10 ; ; // .. ; ; counter = 7; // 7 * 0.288 sec. = 2 sec. totally MOVLW 7 MOVWF counter ; OPTION = 0x0B; // 0 1011: WDT divide by 16 MOVLW 11 OPTION ; // main terminates by sleep();, allows low power consumption ; // waiting for watchdog timeout: approx. 16*18 ms = 288 ms ;} SLEEP ORG 0x01FF GOTO main END ; *** KEY INFO *** ; 0x0000 20 word(s) 3 % : delay_ms ; 0x0014 14 word(s) 2 % : delay10 ; 0x0022 8 word(s) 1 % : _delay10 ; 0x002A 26 word(s) 5 % : main ; RAM usage: 4 bytes (3 local), 21 bytes free ; Maximum call level: 1 ; Total of 69 code words (13 %)