; Keith Neufeld ; 25-Mar-2006 ; ; LogoChip program to oscillate 24kHz ultrasonic transducer using ; PIC PWM output. Uses PWM channel 1 on port C2. ;------------------------------------------------------------------------------ ; Routines for manipulating CCP1 and timer 2 for PWM. ;------------------------------------------------------------------------------ constants [ [CCP1CON $fbd] ; capture/compare/PWM 1 control reg [T2CON $fca] ; timer 2 control register [TMR2ON 2] ; timer 2 enable bit [T2CKPS1 1] ; timer 2 prescale bit 1 [T2CKPS0 0] ; timer 2 prescale bit 0 [CCPR1L $fbe] ; C/C/P 1 register low byte [PR2 $fcb] ; timer 2 period register [pwm-port-ddr portc-ddr] ; PWM 1 outputs on port C2 [pwm-bit 2] [period 82] ; timer period to use for 24kHz ] ; Initialize PWM by configuring timer 2 for PWM period and enabling PWM mode. to pwm-init clearbit pwm-bit pwm-port-ddr ; set PWM port to output ;setbit T2CKPS1 T2CON ; set timer 2 prescalar to 16 (1x) ;clearbit T2CKPS1 T2CON ; set timer 2 prescalar to 4 (01) ;setbit T2CKPS0 T2CON clearbit T2CKPS1 T2CON ; set timer 2 prescalar to 1 (00) clearbit T2CKPS0 T2CON write PR2 period ; set timer period for 24kHz write CCPR1L (period / 2) ; set 50% duty cycle write CCP1CON 12 ; set PWM mode end ; Enable PWM by turning on timer 2. to pwm-on setbit TMR2ON T2CON ; turn on timer 2 end ; Disable PWM by shutting off timer 2. to pwm-off write PR2 0 ; disable PWM by setting period to 0 wait 1 clearbit TMR2ON T2CON ; and turn off timer 2 end ;------------------------------------------------------------------------------ ; Routines for manipulating CCP2 and timer 3 for capture. ;------------------------------------------------------------------------------ constants [ [CCP2CON $fba] ; capture/compare/PWM 2 control reg [CCPxM-CAP-EVERY #0100] ; capture every falling edge [CCPR2H $fbc] ; CCP 2 register low byte [CCPR2L $fbb] ; CCP 2 register low byte [PIR2 $fa1] ; peripheral interrupt request 2 [CCP2IF 0] ; timer 3 interrupt bit [CCP2-PORT portc] ; uses portc pin 1 [CCP2-BIT 1] [CCP2-DDR portc-ddr] [T3CON $fb1] ; timer 3 control register [T3CCP2 6] ; timer 3 CCP mode bit 2 [T3CCP1 3] ; timer 3 CCP mode bit 1 ; 1x src CCP1+2 ; 01 src CCP2 [T3CKPS1 5] ; timer 3 prescale bit 1 [T3CKPS0 4] ; timer 3 prescale bit 0 ; 11 = 1:8 [TMR3ON 0] ; timer 3 enable bit [TMR3H $fb3] ; timer 3 high byte [TMR3L $fb2] ; timer 3 low byte ] ; Initialize CCP by configuring timer 3 and enabling capture mode. to ccp-init write CCP2CON CCPxM-CAP-EVERY ; config to capture every falling edge clearbit T3CCP2 T3CON ; set T3CCP to 01 to make T3 src CCP2 setbit T3CCP1 T3CON ; and T1 src CCP1 setbit T3CKPS1 T3CON ; set T3CKPS to 11 to use 1:8 setbit T3CKPS0 T3CON ; prescalar setbit TMR3ON T3CON ; enable timer 3 setbit CCP2-BIT CCP2-DDR ; set CCP2 (RC1) pin for input end ; Record starting value of timer 3 and clear capture flag. global [ccp-before] to ccp-start setccp-before ((read TMR3H) * 256) + read TMR3L clearbit CCP2IF PIR2 end ; Wait for capture event and return duration. global [ccp-after] to ccp-wait waituntil [testbit CCP2IF PIR2] ; wait for capture interrupt setccp-after ((read CCPR2H) * 256) + read CCPR2L output ccp-after - ccp-before end ;------------------------------------------------------------------------------ ; Top-level routines to tie it all together. ;------------------------------------------------------------------------------ ; For the sake of testing, activate on powerup. constants [[standalone 0]] to powerup pwm-init ccp-init if standalone [ pwm-on ] end ; Send a short burst of tone. to ping pwm-on mwait 1 pwm-off end ; Ping and measure time to reply. to range ccp-start ; record start time ping output ccp-wait ; wait for echo and return duration end