2.3 延时关键子程序
由于精确延时采用的是外部晶振,则Timer 1接外部晶振的初始化程序采用如下精简程序:CLRF T1 CON ;Stop Timer1,Internal Clock Source;T1 oscillator disabled,prescaler = 1:1CLRF TMR1H ;Clear Timer1 High byte registerCLRF TMR1L ;Clear Timer1 Low byte registerCLRF INTCON ;Disable interruptsBSF STATUS,RP0 ;Bank1CLRF PIE1;Disable peripheral interruptsBCF STATUS,RP0 ;BankOCLRF PIR1;Clear pe ripheral interrupts FlagsMOVLW 0x32 ;External Clock source with 1:8 prescalerMOVWF T1CON ;Clock source is synchronized to device;Timerl is stopped and T1 OSC is disabledBSF T1CON,TMR1ON ;Timerl starts to increment;The Timerl interrupt is disabled,do poling on the overflow bitT1_OVFL_WAITBTFSS PIR1,TMR1IFGOTO T1_OVFL_WAIT;Timer has overflowedBCF PIR1,TMR1IF
根据延时方法分析,中断置初值采用如下程序:load_initial_sbcf T1CON,TMR1ONCLRF TMR1H; Clear Low byte,Ensures no rolover intoTMR1H,Value to load into TMR1HMOVLW 0X80 ;Value to load into TMR1H,Write High byteMOVWF TMR1H ;MOVLW 0X00 ;Value to load into TMR1L,Write Low byteADDWF TMR1L;one second intrupt one time run 262162 Tcy,i.e.0.262162s。soset tmr1BSF T1CON,TMR1ON
load_initial_msbcf T1CON,TMR1ONCLRF TMR1L;Clear Low byte,Ensures no rolover intoTMR1H,Value to load into TMR1HMOVLW 0Xxx;the value is preparative worked outMOVWF TMR1H;MOVLW 0Xxx ;the value is preparative worked outMOVWF TMR1L;BSF T1CON,TMR1ON
程序初始化后,通过预置初值,加上程序的其他结构。就可以实现PIC的精确延时。
3 结束语
鉴于PIC12XX单片机的功能特点和优点,采用外接晶振和Timer 1中断技术,可实现较精确的任意延时。另外。Microchip公司的PIC系列单片机具有实用、低价、易学、省电、高速和体积小等特点,还具有低功耗睡眠、掉电复位锁定、上电复位电路、看门狗电路等功能,而且外围器件少、占用空间小、成本低,保密技术也十分可靠,可最大限度地保护开发者的利益。因此,在工业控制、仪器仪表、计算机、家电等诸多领域具有极其广泛的应用前景。