file:schematic
[[file:schematic]] last edit on Apr 25, 2006 3:21 AM by Anonymous
Download: LaserProjContPanel.pdf
Size: 48.773 kilobytes
Uploaded : 2006-04-25 03:21:03.0


; SRF05 --- BETTER, MORE ACCURATE ; send a trigger to the SRF05 ; time how long it takes to get a falling edge back ; circuit: hook up echo output pin of srf05 to c2 ; hook up trigger input pin of srf05 to b7 ; this version uses the capture/compare module in the Logochip for super-accurate timing constants [[CCP1CON $FBD] [T3CON $FB1] [CCPR1H $FBF] [CCPR1L $FBE] [PIR1 $F9E] [TMR3H $FB3] [TMR3L $FB2] [ARRAY_SIZE 16] [SHIFT_SIZE 4]] global [total_val total_move_ave ptr temp] to init_SRF05 setbit 2 portc-ddr ; set pin C2 as input clearbit 2 portc ; set pin C2 low write CCP1CON #00000100 ; turn on capture/compare module ; see spec sheet pg. 129 for T3CON settings: setbit 0 T3CON ; turn on timer 3 setbit 6 T3CON ; use timer3 as clock source for capture/compare setbit 5 T3CON setbit 4 T3CON ; 1:8 prescale value (slowest speeed) for timer3 setptr 0 settotal_move_ave 0 setn 0 repeat ARRAY_SIZE [setarray n 0 setn (n + 1)] end to getvalue_SRF05 clearbit 2 PIR1 ; reset to trigger next event on C2 setn (((read TMR3H) * 256) + (read TMR3L)) ; get current timer 3 value clearbit 2 portc-ddr ; set C2 to output setbit 2 portc clearbit 2 portc ; trigger SRF05 for reading clearbit 2 PIR1 ; reset CCM setbit 2 portc-ddr ; set C2 for input waituntil [testbit 2 PIR1] ; have we got event on C2 yet? setm (((read CCPR1H) * 256) + (read CCPR1L)) ; what was value of timer 3 at time of event on C2? output (m - n) ; return the time it took for the event to occur end to getave ; here is a relatively easy way to average out values repeat 8 [ ; get 8 readings settotal_val (((total_val * 7) + getvalue_SRF05) / 8) ; average in the newest reading wait 1 ] output total_val ; return average end to setarray :index :value ; generic code to read and write to arrays setglobal :index + 10 :value end to array :index output global :index + 10 end to get_move_ave ; a better way to do moving average. settemp getvalue_SRF05 ; get the current value settotal_move_ave (total_move_ave + temp - (array ptr)) ; subtract the oldest value, and add the current value setarray ptr temp ; record the current value setptr (ptr + 1) % ARRAY_SIZE ; update the pointer in the circular buffer output (leftshift total_move_ave (0 - SHIFT_SIZE)) ; return the output of the moving average end to for_pd send leftshift (get_move_ave - 290) -4 wait 1 for_pd end #!