📄 ultrasoundcontrolm.nc
字号:
/** * Cricket v2 Ultrasound Control implementation. * @author David Moore */module UltrasoundControlM { provides { interface UltrasoundControl; }}implementation { /* * Resets the timer that measures the number of microseconds until * each incoming pulse of the ultrasound receiver. The * PulseDetected event will be generated for each rising edge of * the pulse. timeout specifies the number of microseconds before * the timer is disabled, at which time the DetectorTimeout event * is generated. */ async command result_t UltrasoundControl.StartDetector(uint16_t timeout) { atomic { /* Enable ultrasonic receiver */ //cbi(PORTB, 4); /* Reset the counter to zero */ outp(0x00, TCNT1H); outp(0x00, TCNT1L); /* Set the maximum count */ __outw(OCR1AL, timeout); /* Clear the input capture and output compare flags. We don't * use sbi() because that could possibly clear other pending * interrupts that use the TIFR register. */ outp(1 << ICF1, TIFR); outp(1 << OCF1A, TIFR); /* Activate input capture noise canceler (0x80), select rising edge capture (0x40), and use a prescaler factor of 8 (0x02) */ outp(0xC2, TCCR1B); /* Enable the input capture and output compare interrupts */ sbi(TIMSK, TICIE1); sbi(TIMSK, OCIE1A); } return SUCCESS; } /* * Stops the ultrasound timer and prevents the generation of further * events. */ async command result_t UltrasoundControl.StopDetector() { atomic { /* Disable the timer interrupts */ cbi(TIMSK, TICIE1); cbi(TIMSK, OCIE1A); /* Turn off the timer */ outp(0xC0, TCCR1B); /* Disable ultrasonic receiver */ //sbi(PORTB,4); } return SUCCESS; } default async event result_t UltrasoundControl.PulseDetected(uint16_t time) { return SUCCESS; } default async event result_t UltrasoundControl.DetectorTimeout() { return SUCCESS; } /* When the timeout is reached, the timer is stopped and the * DetectorTimeout event is generated. */ TOSH_INTERRUPT(SIG_OUTPUT_COMPARE1A) { call UltrasoundControl.StopDetector(); signal UltrasoundControl.DetectorTimeout(); } /* Upon receiving the rising edge of a pulse, generate an event * with the captured time. */ TOSH_INTERRUPT(SIG_INPUT_CAPTURE1) { signal UltrasoundControl.PulseDetected(__inw_atomic(ICR1L)); } uint8_t cycle_count; /* * Generate a 150 us pulse of 40 KHz ultrasound using the transducer. */ async command result_t UltrasoundControl.SendPulse() { atomic { cycle_count = 0; /* Enable ultrasonic trasmitter */ cbi(PORTG,2); /* Set the OC2 port to output */ cbi(PORTB, 7); sbi(DDRB, 7); /* Disable the overflow interrupt */ cbi(TIMSK, TOIE2); /* Reset the timer to 0 */ outp(0, TCNT2); /* Set the timer frequency to 40 KHz (39.68 Khz with the 7.3 Mhz Crystal)*/ outp(0x5B, OCR2); /* Enable the output compare interrupt */ sbi(TIMSK, OCIE2); /* Enable the timer in CTC mode with toggling of OC2 at reset */ outp(0x19, TCCR2); } return SUCCESS; } command result_t UltrasoundControl.SetGain(uint8_t val) { uint8_t i; atomic { TOSH_MAKE_CC_PDATA_OUTPUT(); cbi(PORTB, 6); //enable pot control cbi(PORTB, 5); //send address bit (0 for pot A) cbi(PORTD, 7); sbi(PORTB, 6); cbi(PORTB, 6); //send the data MSB first for(i =0; i< 8; i++){ if(val & 0x80){ //bit is 1 sbi(PORTD, 7); } else {//bit is zero cbi(PORTD, 7); } //generate clock edge sbi(PORTB, 6); cbi(PORTB, 6); //get next MSB val <<= 1; } //disable pot control sbi(PORTB, 5); } return SUCCESS; } TOSH_INTERRUPT(SIG_OUTPUT_COMPARE2) { atomic { cycle_count++; /* We allow 12 cycles of the wave, or 6 periods. At 25 us per period, that makes a total pulse length of 150 us. */ if (cycle_count == 12) { cbi(TIMSK, OCIE2); outp(0x00, TCCR2); outp(0x00, TCNT2); /* Disable ultrasonic trasmitter */ sbi(PORTG,2); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -