file:compiler txt
[[file:compiler_txt]] last edit on Apr 11, 2006 3:10 AM by Anonymous
Download: compiler.txt
Size: 13.854 kilobytes
Uploaded : 2006-04-11 03:10:35.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 val] to startup clearbit 2 portb-ddr loop [val2 wait 1] ; read avg end to powerup clearbit 2 portb-ddr loop [avg] end to avg init_SRF05 ; get the chip ready to read from ports setval getave ifelse val < 100 [if val > 10 [ setbit 2 portb ] ][clearbit 2 portb] ; read avg end to val2 init_SRF05 ; get the chip ready to read from ports setval getvalue_SRF05 ifelse val < 150 [if val > 10 [ setbit 2 portb ] ][clearbit 2 portb] end to init_SRF05 setbit 2 portc-ddr ; set pin C2 as input clearbit 7 portb-ddr ; set pin B7 as input clearbit 6 portb-ddr ; set pin B6 as input clearbit 5 portb-ddr ; set pin B5 as input 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_1 clearbit 2 PIR1 ; reset to trigger next event on C2 setn (((read TMR3H) * 256) + (read TMR3L)) ; get current timer 3 value setbit 7 portb clearbit 7 portb ; trigger SRF05 for reading 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) / 100) ; return the time it took for the event to occur divided by 100 end to getvalue_SRF05_2 clearbit 2 PIR1 ; reset to trigger next event on C2 setn (((read TMR3H) * 256) + (read TMR3L)) ; get current timer 3 value setbit 6 portb clearbit 6 portb ; trigger SRF05 for reading 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) / 100) ; return the time it took for the event to occur divided by 100 end to getvalue_SRF05_3 clearbit 2 PIR1 ; reset to trigger next event on C2 setn (((read TMR3H) * 256) + (read TMR3L)) ; get current timer 3 value setbit 5 portb clearbit 5 portb ; trigger SRF05 for reading 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) / 100) ; return the time it took for the event to occur divided by 100 end to getave settotal_val (getvalue_SRF05_1) ; set total_val to the current distance value repeat 4 [ ; get 8 readings settotal_val (((total_val) + getvalue_SRF05_2 / 2)) ; trigger each SRF05 in turn settotal_val (((total_val) + getvalue_SRF05_3 / 2)) settotal_val (((total_val) + getvalue_SRF05_1 / 2)) 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