i haven't seen any code posted for the HC-SR04 ultrasonic distance sensor connected to the nRF24LE1, so i rolled my own, see below. it works well enough. the sensor runs on 5V (does not work well at 3.3V) and its echo pin output is connected to the nRF24LE1 via a 1K resistor so as not to blow the uC. it's dirty, but works fine. really should be using a voltage divider with 2K/1K resistors. has anyone been able to convert the value to an accurate distance? thanks!
#define trig_pin P12 // P12 is yellow, trigger, output. so, 0 in P1CON. #define echo_pin P14 // P14 is green, echo, input. so 1 in P1CON. #define US_timeout 30000 // ultrasonic sensor timeout #define LO 0 #define HI 1 /** set pin directions & properties. */ void hcsr04_init() { // P1CON = xxxI-xOxx P1DIR |= 0x10; // set the bit corresponding to P14. INPUT. xxx1-xxxx P1CON = 0x14; // 0001-0100. bits 6:5, 00 Digital input buffer on, no pull up/down resistors. bit 4, inOut = 1 - Operate on the input configuration. bit 3, not useful here. bits 2:0, bitAddr = 100 - P1.4 // #define PIN_READ_FLOATING (0X00 | 0x10) // bits 7:5 is 000. b00: Digital input buffer on, no pull up/down resistors P1DIR &= 0xFB; // set the bit corresponding to P12. OUTPUT. xxxx-xOxx P1CON = 0x02; // p. 143, table 83. // bits 7:5, 000 Digital output buffer normal drive strength. bit 4, 0 - Operate on the output configuration. bit 3, not useful here. bits 2:0, bitAddr = 010 - P1.2 } /** get the raw counter which corresponds to the distance from the target. calculation to actual cm or inches not implemented. */ uint16_t getRawDuration() { uint16_t acount = 0, bcount = 0, ccount = 0; bool atimeout = false, btimeout = false, ctimeout = false; trig_pin = LO; // clear the trigger pin delay_us(20); trig_pin = HI; // toggle the trigger pin high delay_us(14); trig_pin = LO; // toggle the trigger pin low // delay_us(20); // why? from some other Nordic nRF board's code. remove it. while (echo_pin) if (acount++ > US_timeout) {atimeout = true; break;} while (!echo_pin) if (bcount++ > US_timeout) {btimeout = true; break;} while (echo_pin) if (ccount++ > US_timeout) {ctimeout = true; break;} dprintf(("a: echo_pin HI->LO, acount %d, atimeout: %d\n", (int) acount, (int) atimeout)); dprintf(("b: echo_pin LO->HI, bcount %d, btimeout: %d\n", (int) bcount, (int) btimeout)); dprintf(("c: echo_pin HI->LO, ccount %d, ctimeout: %d\n", (int) ccount, (int) ctimeout)); // duration = counttoUs+ tcount; // is this code meaningful for the nRF? at what CLK speed? // dist = duration * 0.017; // can i work at this backwards, by measuring the distance? return ccount; }