Advertisement
RuiViana

PortManipulation.ino

Aug 18th, 2020 (edited)
1,512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1.  
  2. // Port manipulation
  3.  
  4. //  C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard
  5.  
  6. //  PORTB – The Port B Data Register
  7. //  DDRB  – The Port B Data Direction Register
  8. //  PINB  – The Port B Input Pins Address
  9.  
  10. //  PORTC – The Port C Data Register
  11. //  DDRC  – The Port C Data Direction Register
  12. //  PINC  – The Port C Input Pins Address
  13.  
  14. //  PORTD – The Port D Data Register
  15. //  DDRD  – The Port D Data Direction Register
  16. //  PIND  – The Port D Input Pins Address
  17.  
  18. //  Ex:
  19. //  PDx   bit do PORTD
  20. //  PBx   bit do PORTB
  21.  
  22. //    7    6    5    4    3    2    1    0
  23. //  PD7  PD6  PD5  PD4  PD3  PD2  PD1  PD0  <<< Port de 0 a 7  0 e 1 sao usados pela Serial
  24.  
  25. //   13  12  11  10   9   8
  26. //  PB5 PB4 PB3 PB2 PB1 PB0                 <<< Port de 8 a 13
  27.  
  28. //   A7  A6  A5  A4  A3  A2  A1  A0
  29. //  PC7 PC6 PC5 PC4 PC3 PC2 PC1 PC0         <<< Port de A0 a A7
  30.  
  31. //  (port) |= (1 << (bit));    // Set um bit apenas
  32. //  (port) &= ~(1 << (bit));   // Reset um bit apenas
  33. //  (reg)  |=  value;          // Set vários bits de uma vez
  34.  
  35. // Serial imprime bits 76543210
  36.  
  37. //-------------------------------------------------------------------------
  38. void setup()
  39. {
  40.   Serial.begin(115200);
  41.  
  42.   //  DDRD |= (1 << PD2);
  43.   //  DDRB &= ~(1 << PB4);
  44.  
  45.   // Carrega estes valores nos registradores
  46.   DDRD  |= 0xF8;              //  Port3 ao port7  OUTPUT
  47.   DDRB  |= 0x7F;              //  Port8 ao port13 OUTPUT
  48.   PORTD |= 0x04;              //  Port3 INPUT PULLUP
  49.  
  50.   Serial.println(" Carrega estes valores nos registradores ");
  51.   Serial.print(DDRD, HEX); Serial.print(" "); Serial.println(DDRD, BIN);
  52.   Serial.print(DDRB, HEX); Serial.print(" "); Serial.println(DDRB, BIN);
  53.   Serial.print(PORTD, HEX); Serial.print(" "); Serial.println(PORTD, BIN);
  54.  
  55.   Serial.println(" Carrega este valor no registrador ");
  56.   PORTB |= 0xFF;              //  Set todos bits
  57.   Serial.print(PORTB, HEX); Serial.print(" "); Serial.println(PORTB, BIN);
  58.  
  59.   Serial.println(" Modifica pra este valor, apagando alguns bits ");
  60.   PORTB &= 0x73;              //  Reset alguns bits (bits 2, 3, 6, 7)
  61.   Serial.print(PORTB, HEX); Serial.print(" "); Serial.println(PORTB, BIN);
  62.  
  63.   Serial.println(" Limpa registradores ");
  64.   PORTB &= 0x00;              //  Reset todos bits
  65.   DDRB  &= 0x00;              //  Reset todos bits
  66.   Serial.print(PORTB, HEX); Serial.print(" "); Serial.println(PORTB, BIN);
  67.   Serial.print(DDRB, HEX); Serial.print(" "); Serial.println(DDRB, BIN);
  68.  
  69.   Serial.println(" Set de bits individuais");
  70.   PORTB |= (1 << PD2);        //  Set  bit 2
  71.   DDRB |= (1 << PD3);         //  Set  bit 3
  72.   Serial.print(PORTB, HEX); Serial.print(" "); Serial.println(PORTB, BIN);
  73.   Serial.print(DDRB, HEX); Serial.print(" "); Serial.println(DDRB, BIN);
  74.  
  75.   Serial.println("  Liga todos bits dos registradores");
  76.   PORTB = 0xFF;              //  Reset todos bits
  77.   DDRB  = 0xFF;              //  Reset todos bits
  78.   Serial.print(PORTB, HEX); Serial.print(" "); Serial.println(PORTB, BIN);
  79.   Serial.print(DDRB, HEX); Serial.print(" "); Serial.println(DDRB, BIN);
  80.  
  81.   Serial.println("  Reset de bits individuais");
  82.   PORTB &= ~(1 << PD4);        //  Resetet  bit 4
  83.   DDRB &= ~(1 << PD5);         //  Reseet  bit 5
  84.   Serial.print(PORTB, HEX); Serial.print(" "); Serial.println(PORTB, BIN);
  85.   Serial.print(DDRB, HEX); Serial.print(" "); Serial.println(DDRB, BIN);
  86. }
  87. //-------------------------------------------------------------------------
  88. void loop()
  89. {
  90.   // configurar PORTB como saída
  91.   DDRB = 0xFF;
  92.   // configurar PORTD como entrada
  93.   DDRD = 0;
  94.   // PORTD com PULLUP
  95.   PORTD = 0xFF;
  96.   // mapa PIND muda para PORTB
  97.   PORTB = PIND;
  98.   Serial.println(" Le entradas e envia para saidas");
  99.   Serial.print(PORTB, HEX); Serial.print(" "); Serial.println(PORTB, BIN);
  100.   delay(500);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement