The Pico/W In C: GPIO Input |
Written by Harry Fairhead | |||||
Monday, 08 July 2024 | |||||
Page 1 of 4 Input is never simple, but this is about as simple as it gets. This is an extract from a recent book in the I Programmer Library, all about the Pico/W in C. Programming the Raspberry Pi Pico In CBy Harry FairheadBuy from Amazon. Contents
Extra: Adding WiFi To The Pico 2 <ASIN:1871962803> <ASIN:187196279X> Simple InputThere is no doubt that input is more difficult than output. When you need to drive a line high or low you are in command of when it happens, but input is in the hands of the outside world. If your program isn't ready to read the input, or if it reads it at the wrong time, then things just don't work. What is worse, you have no idea what your program is doing relative to the event you are trying to capture. Welcome to the world of input. In this chapter we look at the simplest approach to input – the polling loop. This may be simple, but it is a good way to approach many tasks. In Chapter 8 we look at more sophisticated alternatives – events and interrupts. GPIO InputGPIO input is a much more difficult problem than output from the point of view of measurement and verification. For output at least you can see the change in the signal on a logic analyzer and know the exact time that it occurred. This makes it possible to track down timing problems and fine tune things with good accuracy. Input on the other hand is "silent" and unobservable. When did you read in the status of the line? Usually the timing of the read is with respect to some other action that the device has taken. For example, you read the input line 20 µs after setting the output line high. But how do you know when the input line changed state during that 20 microseconds? The simple answer is in most cases you don’t. In some applications the times are long and/or unimportant but in some they are critical and so we need some strategies for monitoring and controlling read events. The usual rule of thumb is to assume that it takes as long to read a GPIO line as it does to set it. This means we can use the delay mechanisms that we looked at with regard to output for input as well. One common and very useful trick when you are trying to get the timing of input correct is to substitute an output command to a spare GPIO line and monitor it with a logic analyzer. Place the output instruction just before the input instruction and where you see the line change on the logic analyzer should be close to the time that the input would be read in the unmodified program. You can use this to debug and fine tune and then remove the output statement. |
|||||
Last Updated ( Wednesday, 10 July 2024 ) |