Cleanup 3rd party samples; update README.md; add some missing copyright, fix builds for boards without certain pin definitions
This commit is contained in:
@@ -1,3 +1,9 @@
|
||||
/**
|
||||
* Copyright (c) 2021 pmarques-dev @ github
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/pio.h"
|
||||
@@ -13,7 +19,7 @@
|
||||
// updated. At any point, the main code can query the current count by using
|
||||
// the quadrature_encoder_*_count functions. The counter is kept in a full
|
||||
// 32 bit register that just wraps around. Two's complement arithmetic means
|
||||
// that it can be interpreted as a 32 bit signed or unsigned value and it will
|
||||
// that it can be interpreted as a 32-bit signed or unsigned value, and it will
|
||||
// work anyway.
|
||||
//
|
||||
// As an example, a two wheel robot being controlled at 100Hz, can use two
|
||||
@@ -26,34 +32,30 @@
|
||||
// encoder count updated and because of that it supports very high step rates.
|
||||
//
|
||||
|
||||
int main() {
|
||||
int new_value, delta, old_value = 0;
|
||||
|
||||
int main()
|
||||
{
|
||||
int new_value, delta, old_value = 0;
|
||||
// Base pin to connect the A phase of the encoder.
|
||||
// The B phase must be connected to the next pin
|
||||
const uint PIN_AB = 10;
|
||||
|
||||
// Base pin to connect the A phase of the encoder.
|
||||
// The B phase must be connected to the next pin
|
||||
const uint PIN_AB = 10;
|
||||
stdio_init_all();
|
||||
|
||||
stdio_init_all();
|
||||
PIO pio = pio0;
|
||||
const uint sm = 0;
|
||||
|
||||
PIO pio = pio0;
|
||||
const uint sm = 0;
|
||||
uint offset = pio_add_program(pio, &quadrature_encoder_program);
|
||||
quadrature_encoder_program_init(pio, sm, offset, PIN_AB, 0);
|
||||
|
||||
uint offset = pio_add_program(pio, &quadrature_encoder_program);
|
||||
quadrature_encoder_program_init(pio, sm, offset, PIN_AB, 0);
|
||||
while (1) {
|
||||
// note: thanks to two's complement arithmetic delta will always
|
||||
// be correct even when new_value wraps around MAXINT / MININT
|
||||
new_value = quadrature_encoder_get_count(pio, sm);
|
||||
delta = new_value - old_value;
|
||||
old_value = new_value;
|
||||
|
||||
while (1) {
|
||||
// note: thanks to two's complement arithmetic delta will always
|
||||
// be correct even when new_value wraps around MAXINT / MININT
|
||||
new_value = quadrature_encoder_get_count(pio, sm);
|
||||
delta = new_value - old_value;
|
||||
old_value = new_value;
|
||||
|
||||
printf("position %8d, delta %6d\n", new_value, delta);
|
||||
sleep_ms(100);
|
||||
}
|
||||
|
||||
return 0;
|
||||
printf("position %8d, delta %6d\n", new_value, delta);
|
||||
sleep_ms(100);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
;
|
||||
; Copyright (c) 2021 pmarques-dev @ github
|
||||
;
|
||||
; SPDX-License-Identifier: BSD-3-Clause
|
||||
;
|
||||
|
||||
.program quadrature_encoder
|
||||
|
||||
@@ -6,7 +11,7 @@
|
||||
.origin 0
|
||||
|
||||
|
||||
; the code works by running a loop that contiously shifts the 2 phase pins into
|
||||
; the code works by running a loop that continuously shifts the 2 phase pins into
|
||||
; ISR and looks at the lower 4 bits to do a computed jump to an instruction that
|
||||
; does the proper "do nothing" | "increment" | "decrement" action for that pin
|
||||
; state change (or no change)
|
||||
|
||||
Reference in New Issue
Block a user