The Pico/W In C: GPIO Input
Written by Harry Fairhead   
Monday, 08 July 2024
Article Index
The Pico/W In C: GPIO Input
Basic Input Functions
Press Or Hold
How Fast Can We Measure?

How Fast Can We Measure?

Buttons are one form of input, but often we want to read data from a GPIO line driven by an electronic device and decode the data. This usually means measuring the width of the pulses and this raises the question of how fast can we accept input?

The simplest way to find out how quickly we can take a measurement is to perform a pulse width measurement. Applying a square wave to GP22 we can measure the time that the pulse is high using:

#include <stdio.h>
#include "pico/stdlib.h"
int main()
{ stdio_init_all(); gpio_set_function(22, GPIO_FUNC_SIO); gpio_set_dir(22, false); uint64_t t;
while (true) { while (gpio_get(22) == 1) {}; while (gpio_get(22) == 0) {}; t = time_us_64(); while (gpio_get(22) == 1) {}; t = (uint64_t)(time_us_64() - t); printf("%llu\n", t); sleep_ms(1000); }
}

This might look a little strange at first. The inner while loops are responsible for getting us to the right point in the waveform. First we loop until the line goes low, then we loop until it goes high again and finally measure how long before it goes low. You might think that we simply have to wait for it to go high and then measure how long till it goes low, but this misses the possibility that the signal might be part way through a high period when we first measure it. This can be measured down to around 1 microsecond with an accuracy of 0.5 µs with the accuracy poor at the low end of the range.

Notice that in either case if you try measuring pulse widths much shorter than the lower limit that works, you will get results that look like longer pulses are being applied. The reason is simply that the Pico will miss the first transition to zero but will detect a second or third or later transition. This is the digital equivalent of the aliasing effect found in the Fourier Transform or general signal processing. 

In chapter but not in this extract

  • The Finite State Machine
  • FSM Button
  • FSM Hold Button
  • FSM Ring Counter

Summary

  • Input is hard because things happen at any time, irrespective of what your program might be doing.

  • You can call gpio_get at any time to discover the state of a GPIO line – the problem is when and how often to call it.

  • You can chose between external or internal pull-up/pull-down resistors.

  • Mechanical input devices such as buttons have to be debounced.

  • The power of software is that it can enhance a simple device. A simple button is either pushed or released, but you can use this to generate a third “held” state.

  • Using a polling loop you can handle inputs as short as a few tens of microseconds.

  • Most IoT programs are best written as a polling loop.

  • The Finite State Machine (FSM) is one way of organizing a complex polling loop so that inputs are tested and outputs are set once for each time through the loop.

  • Ideally the states of a FSM should not be simple statements of the inputs of outputs that determine the state, but for simple systems this can be difficult to achieve.

  • It can be difficult to work out what constitutes the events of a FSM. Ideally they should be localized in time so that they indicate the moment that something happens.

Programming the Raspberry Pi Pico In C

By Harry Fairhead

picoC2E360

Buy from Amazon.

Contents

  • Preface
  • Chapter 1 The Raspberry Pi Pico – Before We Begin
  • Chapter 2 Getting Started
  • Chapter 3 Getting Started With The GPIO
  • Chapter 4 Simple Output
  • Chapter 5 Some Electronics
  • Chapter 6 Simple Input
        Extract:   GPIO Input ***NEW!
  • Chapter 7 Advanced Input – Events and Interrupts
  • Chapter 8 Pulse Width Modulation
        Extract: Basic PWM
  • Chapter 9 Controlling Motors And Servos
  • Chapter 10 Getting Started With The SPI Bus
  • Chapter 11 A-To-D and The SPI Bus
  • Chapter 12 Using The I2C Bus
  • Chapter 13 Using The PIO
        Extract: A 1-Wire PIO Program  
  • Chapter 14 The DHT22 Sensor Implementing A Custom Protocol
  • Chapter 15 The 1‑Wire Bus And The DS1820
  • Chapter 16 The Serial Port
  • Chapter 17 Using the Pico W
       Extract: Simple Web Client
       Extract:A Better Connect
  • Chapter 18 The Pico/W In C: Direct To Hardware 

Extra: Adding WiFi To The Pico 2

<ASIN:1871962803>

<ASIN:187196279X>

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Mozilla Getting New Logo?
30/08/2024

Keen watchers of industry trends think that Mozilla is working on changing its logo, and have been debating just what this means for the company behind Firefox.



Microsoft Donates Mono To WineHQ
29/08/2024

Microsoft has donated Mono, the open source, cross-platform, implementation of the .NET framework, to WineHQ. The donation was made quietly, with the only real sign being a small paragraph on the [ ... ]


More News

kotlin book

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Wednesday, 10 July 2024 )