Это программа PIO (я нашел ее где-то в Интернете, надеясь, что это сработает, но мне тоже не подходит):
Код: Выделить всё
.program ultrasonic
start:
; give a pulse to the HCSR04 Trigger pin
set pins 1 ; set the trigger to 1 and wait 10 us
in NULL 32 ; clear ISR
set x 19 ; set x to 10011
in x 5 ; shift the x into the ISR
in NULL 6 ; shift in 6 more 0 bits
mov x ISR ; move the ISR to x (which now contains 10011000000)
delay1:
jmp x-- delay1 ; count down to 0: a delay of (about) 10 us
set pins 0 ; make the trigger 0 again, completing the trigger pulse
;
wait 1 pin 0 ; wait for the echo pin to rise
; start a counting loop to measure the length of the echo pulse
set x 0 ; start with the value 0 and subtract one
jmp x-- timer ; timer is now FFFFFFFF (note: x-- is post-decrement)
timer:
jmp x-- test ; count down
jmp timerstop ; timer has reached 0, stop count down
test:
jmp pin timer ; test if the echo pin is still 1, if so, continue counting down
timerstop: ; echo pulse is over (or timer has reached 0)
mov ISR x ; move the value in x to the ISR
push noblock ; push the ISR into the RX FIFO
; wait 60ms (advice from datasheet to prevent triggering on echos)
in NULL 32 ; clear ISR
set x 28 ; set x to 10011
in x 5 ; shift the x (10011) into the ISR
in NULL 18 ; shift in 18 more 0 bits
mov x ISR ; move the ISR to x
delay2:
jmp x-- delay2 ; delay (about) 60 ms
jmp start ; start over
Код: Выделить всё
Ultrasonic::Ultrasonic(int echoPin, int trigPin) {
this->echoPin = echoPin;
this->trigPin = trigPin;
this->pio = pio0;
this->sm = pio_claim_unused_sm(pio, true);
pio_gpio_init(pio, trigPin);
pio_gpio_init(pio, echoPin);
uint offset = pio_add_program(pio, &ultrasonic_program);
pio_sm_config config = ultrasonic_program_get_default_config(offset);
sm_config_set_in_pins(&config, echoPin);
sm_config_set_jmp_pin(&config, echoPin);
pio_sm_set_consecutive_pindirs(pio, sm, trigPin, 1, true);
pio_sm_set_consecutive_pindirs(pio, sm, echoPin, 1, false);
sm_config_set_set_pins(&config, trigPin, 1);
sm_config_set_in_shift(&config, false, false, 0);
pio_sm_init(pio, sm, offset, &config);
pio_sm_set_enabled(pio, sm, true);
}
которая должна читать все, что я отправляю в rx, но я всегда получаю
Код: Выделить всё
if (pio_sm_is_rx_fifo_empty(pio, sm)) {
printf("Timeout waiting for echo\n");
return 0;
}
Мне интересно, я что-то забыл в конструкторе, или весь PIO просто неправильный? Я уже перешел на другой ультразвуковой датчик, чтобы проверить, возможно, это аппаратная проблема, но проблема все та же.
Кроме того, я работаю с SDK c/c++, а не с micropython
Подробнее здесь: https://stackoverflow.com/questions/798 ... -on-rp2040
Мобильная версия