Advertisement
AntonioVillanueva

Test GPIO RASPBERRY B+

Jul 26th, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. //Compilar como gcc -std=c11 -lwiringPi gpio2.c gpio2
  2. //Test Raspberry B+
  3. #include <stdio.h>    // Used for printf() statements
  4. #include <wiringPi.h> // Include WiringPi library!
  5.  
  6. #define max(array) (sizeof (array)/sizeof(array[0]))//Size
  7. #define MAX_TEMPO 500
  8.  
  9. //GPIO in raspberry B+
  10. const int IN[]={17,27,22,10,9,11,5,6,13,19,26};//IN
  11. int IN_ANT[max(IN)];//Guarda el estado actual de la IN
  12. const int OUT[]={23,24,25,8,7,12,16,20,21};//OUT
  13.  
  14. //const int pwmValue = 75; // Use this to set an LED brightness
  15.  
  16. int main(void)
  17. {
  18.     int tempo;
  19.     int boolean;
  20.     int nLed=0;//Numero de OUT o Led
  21.     int nIn=0;//Numero de IN o boton
  22.     wiringPiSetupGpio(); // Initialize wiringPi -- using Broadcom pin numbers
  23. //Configure inputs
  24.     for (int i=0;i<max(IN);i++){
  25.         pinMode (IN[i],INPUT);//CONFIGURE INPUTS
  26.         //pinMode (IN[i],PUD_DOWN);//Pull down
  27.         pinMode (IN[i],PUD_OFF);
  28.     }
  29. //Configure outputs
  30.     for (int i=0;i<max(OUT);i++){
  31.         pinMode (OUT[i],OUTPUT);//Configure outputs
  32.     }
  33.  
  34. //Bucle principal no intrusivo no  blocante como los de tipo delay(xxx)
  35.     while(1)
  36.     {
  37.         if (IN_ANT[nIn]!=(digitalRead(IN[nIn])& 0x01)){//Si ha cambiado, la entrada la muestra
  38.             printf ("IN[%d]=%d\n",nIn,(digitalRead(IN[nIn]) & 0x01));
  39.         }
  40.         IN_ANT[nIn]=(digitalRead(IN[nIn]) & 0x01);//Memoria IN.Lee y guarda estado actual
  41.  
  42.  
  43.         if (tempo>=MAX_TEMPO){
  44.             boolean ? digitalWrite(OUT [nLed],HIGH ):digitalWrite(OUT[nLed],LOW);
  45.             //printf ("Led[%d]=%d\n",nLed,boolean & 0x01);
  46.             boolean =~boolean;
  47.             tempo=0;//Reset cronometro ...
  48.         }else {tempo++;}
  49.  
  50. //  printf (" Led[%d]=%d ",nLed,boolean &  0x01);
  51.  
  52.     nIn++;//Proxima entrada
  53.     if (nIn>=max(IN)){nIn=0;}
  54.  
  55.     nLed++;//Proximo Led o salida
  56.     if (nLed>=max(OUT)){nLed=0;}
  57.  
  58.     }
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement