The Pico/W In C: GPIO Input |
Written by Harry Fairhead | |||||||||||||
Monday, 08 July 2024 | |||||||||||||
Page 3 of 4
Press Or HoldYou can carry on elaborating on how to respond to a button. For example, most users have grown accustomed to the idea that holding a button down for a longer time than a press makes something different happen. To distinguish between a press and a hold all you need to do is time the difference between line down and line up: #include "pico/stdlib.h" int main() In this case holding the button for 2s registers a “held” – the LED flashes 10 times and anything less is a “push” – the LED flashes just once. Notice the 1ms debounce pause between the test for no-press and press. One of the problems with all of these sample programs is that they wait for the button to be pressed or held and this makes it difficult for them to do anything else. You have to include whatever else your program needs to do within the loop that waits for the button – the polling loop. You can do this in an ad hoc way, but the best approach is to implement a finite state machine, see later. Serial DataIn the next program we need to gather some data and the simplest way of doing this is to use a serial connection between the Pico and the host machine. This topic is covered in detail in Chapter 16. If you have any problems making this work, read the section on setting up the UART. This is the short version and it should “just work”. The Pico has a default UART connected to pins 1 GP0 TX and pin 2 GP1 RX. You simply need to connect the RX/TX pins to the corresponding TX/RX pins on the host machine. In the case of a machine that doesn’t have a serial port that can work at 3.3V you need an adapter – usually a USB to serial adapter. If you are using a Raspberry Pi 4 as the development system you can simply connect the Pico’s GPIO lines to the Pi’s:
If you have already connected the Pico and the Pi together via the SWD interface you don’t need to connect the GND as there is already a ground connection. If you are only interested in transmitting data from the Pico you only need to connect Pin 1 and Pin 2. The hardware setup is simple but you also need to use the: sudo raspi-config to set up the serial port on the Pi. Select Interfaces and then disable the Linux shell and enable the serial port. The system will reboot. After this you can use serial0 to communicate with devices connected to the serial port. To make the connection use: sudo minicom -b 115200 -D /dev/serial0 Notice that you can do this in a terminal window in VS Code which is the best way to work. To make sure that the serial interface works try this simple program: #include <stdio.h> #include "pico/stdlib.h" |
|||||||||||||
Last Updated ( Wednesday, 10 July 2024 ) |