Programa Raspberry Pi
en
Lenguaje C
Programemos Raspberry Pi con Lenguaje C:
We Program Raspberry Pi with C:
Actualización gracias a Agusthina Oliva
¿Por qué?--Why?:
Porque es el mismo lenguaje que usa eficazmente tu Aire acondicionado, tu microondas, tu lavarropas, se comunica con mayor eficiencia que Python.Because is the more efficient Language, run in your Microwave, in your air-conditioning .
Para programar nuestros proyectos con C en Raspberry Pi tenemos 2 opciones--We have 2 options:
BCM2835
Wiring Pi
Diagrama de conexión--Diagram:
BCM2835: Pin 11 GPIO 17
Wiring Pi: Pin 11 WPi Pin 0
En ambos es el Pin fisico 11
In both is the physical Pin 11
Comencemos con la librería BCM2835--Start with BCM2835 library :
Utilizar este Fork --- Newest WiringPi Fork works in Pi400:
git clone https://github.com/WiringPi/WiringPi.git
cd WiringPi
./build
sudo reboot
Podemos confirmar con--We can check with:
gpio readall
Eso es todo saltemos a la parte de--That's all jump to Geany part.
Abrimos el Terminal y tecleamos lo siguiente--Open Terminal and choose the latest version by typing:
Método antiguo--Old method
cd
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.67.tar.gz
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.71.tar.gz
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.73.tar.gz
Documentación--Docs:
Descomprimimos la versión descargada--Untar:
tar zxvf bcm2835-*.tar.gz
rm bcm28*tar.gz
Nos situamos en el nuevo directorio--Go to the new directory:
cd bcm2835*
Ejecutamos configure--Type:
./configure
Ejecutamos make--Type:
make
Ejecutamos con permisos root make check--Check with sudo:
sudo make check
Ejecutamos con permiso root la instalación--Install:
sudo make install
Configuremos nuestro compilador Geany--Config Geany for use the library:
Debemos enlazar la librería bcm2835 a nuestros comandos, yendo a la opción Construir-> Establecer comando de construcción:
We need link the library wiringPi to our build commands, go to Build -->Set Build Commands:
Compile: gcc -Wall -c "%f" -lbcm2835
Build: gcc -Wall -o "%e" "%f" -lbcm2835
Si compilamos desde Terminal solo debemos escribir el comando--If we compile from Terminal then:
gcc -o miprograma miprograma.c -lbcm2835
Ejecutandolo así--And we execute:
./miprograma
Creamos nuestro primer Blink:
Segunda opción de Blink:
Mas ejemplos en la carpeta--More examples:
/home/pi/bcm2835-1.67/examples
o
~/bcm2835-1.71/examples
Ahora con Wiring Pi--Now with WiringPi:
Veamos si está instalado--Look if is installed typing:
gpio -v
Antes no venia instalado en Raspbian Stretch y veíamos esto.--On Raspbian we see these on the past, because not came with the OS.
Hoy en dia con Raspberry Pi OS ya viene pre-instalado.--Now came pre-installed but not in his latest version
Si tenemos alguna version instalada, la eliminamos--We purge the old version with:
sudo apt-get purge -y wiringpi
hash -r
sudo apt-get purge -y wiringpi
hash -r
Obtenemos la ultima version de Wiring Pi--Getting the latest:
cd
wget https://project-downloads.drogon.net/wiringpi-latest.deb
Instalamos el paquete .deb obtenido--Install it:
sudo dpkg -i wiringpi-latest.deb
rm wiring*.deb
Podemos observar la última versión instalada.--We can see the lates version installed
Verificamos con gpio -v la nueva--Check the difference vesion with gpio -v
Observamos la nomenclatura de pines utilizada por wiringPi (wPi) con--We can see the nomenclature of WiringPi(wPi):
gpio readall
gpio readall de una--on a Raspberry Pi 3B
gpio readall de una--on a Raspberry pi 4B
Como realizamos antes, en Geany debemos indicarle el nombre de la biblioteca utilizada--Like we do before, we need to tell to Geany the name of library used:
Debemos enlazar la librería wiringPi a nuestros comandos, yendo a la opción Construir --> Establecer comandos de construcción:
We need link the library wiringPi to our build commands, go to Build -->Set Build Commands:
Compile: gcc -Wall -c "%f" -lwiringPi
Build: gcc -Wall -o "%e" "%f" -lwiringPi
Si compilamos desde Terminal solo debemos escribir el comando--If we compile from Terminal then:
gcc -o miprograma miprograma.c -lwiringPi
Ejecutandolo así--And we execute:
./miprograma