element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Blog Pico PIO state machine implements a peripheral: Rotary Decoder - 1: example
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
GPIO Pinout
Raspberry Pi Wishlist
Comparison Chart
Quiz
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 6 Nov 2022 9:57 AM Date Created
  • Views 4238 views
  • Likes 3 likes
  • Comments 2 comments
Related
Recommended
  • raspberry
  • pico
  • pico_eurocard
  • PIO

Pico PIO state machine implements a peripheral: Rotary Decoder - 1: example

Jan Cumps
Jan Cumps
6 Nov 2022

The Pico has a set of PIO co-processors. They are real-time controllers that can execute logic with deterministic timing. Ideal to run strict-timed sequences and state machines. And to implement extra peripherals (like a quadrature decoder here).
The PIO engine is not to easy to program, not easy to learn. But there are some great examples out there. I'm reviewing a PIO Quadrature Encoder by Jamon Terrell.
In this post: test the example, using the rotary encoder on shabaz' PICO-EUROCARD

image

The purpose of this blog isn't to write a PIO developer tutorial. I'm trying to backtrack how the developer implemented a standard protocol using the PIO instructions. I'm using C, CMake, VSCode and the Pico C SDK. Expected skill level: you can build and run the official pico-examples.

Get the pio decode library

Clone or download the sources from Kevin's github. 

git clone https://github.com/jamon/pi-pico-pio-quadrature-encoder.git

Set an environment variable that points to this location. This will take care that we can create a make script that's not dependent on where you keep 3rd party code. I'm using VSCode, and will define the environment variable there. You can also do it in your OS settings, shell script, ...

image

Project Folder

I created a directory with a CMake file, and a source folder to contain the very simple test file, taken from here. I only made a change so that it only logs a value when it changes, instead of every second. And used GIO 7 and 6 as the rotary pins A and B - to match the schematics of the development board.

source/main.c

//  SPDX-FileCopyrightText: 2022 Jamon Terrell "jc: mail address removed, available on github"
//  SPDX-License-Identifier: MIT

// https://github.com/jamon/pi-pico-pio-quadrature-encoder/blob/main/src/quadrature.pio

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "quadrature.pio.h"

#define QUADRATURE_A_PIN 7
#define QUADRATURE_B_PIN 6
int main() {
    stdio_init_all();
    PIO pio = pio0;
    uint offset = pio_add_program(pio, &quadrature_program);
    uint sm = pio_claim_unused_sm(pio, true);
    quadrature_program_init(pio, sm, offset, QUADRATURE_A_PIN, QUADRATURE_B_PIN);

    uint x = 0;
 
    while (true) {
        sleep_ms(1000);
        pio_sm_exec_wait_blocking(pio, sm, pio_encode_in(pio_x, 32));
        uint y = pio_sm_get_blocking(pio, sm);
        if (y != x) {
          x = y;
          printf("%d\n", x);
        }
    }
}

./CMakeList.txt

cmake_minimum_required(VERSION 3.13)

# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)

project(pio_rotary_project0 C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

#I've set this to allow breakpoints on any source line
set(PICO_DEOPTIMIZED_DEBUG=1)

pico_sdk_init()


add_executable(pio_rotary_project0
        source/main.c
#        $ENV{PIO_QUADRATURE_DECODER_PATH}/src/quadrature.pio
        )

target_include_directories(pio_rotary_project0 PRIVATE
        ${CMAKE_CURRENT_LIST_DIR}/source
        $ENV{PIO_QUADRATURE_DECODER_PATH}/src
        )


target_link_libraries(pio_rotary_project0 pico_stdlib hardware_pio)

pico_add_extra_outputs(pio_rotary_project0)

Testing

To test this, you just need to connect a terminal program to the debugger*. 

image

I'm attaching a ZIP of my VSCode project. Don't forget to download the pi-pico-quadrature-encoder source and set the environment variable.

pio_quadrature_decoder_20221107.zip

  • Sign in to reply
  • Jan Cumps
    Jan Cumps over 2 years ago

    I just found out that the standard raspberry pico-examples also have a good PIO quadrature encoder example.

    To make it work with the pico Eurocard, change this line of code:

    //  const uint PIN_AB = 10;
        const uint PIN_AB = 6;
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago

    I've attached the VSCode project

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube