Guest User

Untitled

a guest
Apr 5th, 2012
878
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // wiring_digital.c
  2.  
  3. void digitalWrite(uint8_t pin, uint8_t val)
  4. {
  5.     uint8_t timer = digitalPinToTimer(pin);
  6.     uint8_t bit = digitalPinToBitMask(pin);
  7.     uint8_t port = digitalPinToPort(pin);
  8.     volatile uint8_t *out;
  9.  
  10.     if (port == NOT_A_PIN) return;
  11.  
  12.     // If the pin that support PWM output, we need to turn it off
  13.     // before doing a digital write.
  14.     if (timer != NOT_ON_TIMER) turnOffPWM(timer);
  15.  
  16.     out = portOutputRegister(port);
  17.  
  18.     uint8_t oldSREG = SREG;
  19.     cli();
  20.  
  21.     if (val == LOW) {
  22.         *out &= ~bit;
  23.     } else {
  24.         *out |= bit;
  25.     }
  26.  
  27.     SREG = oldSREG;
  28. }
  29.  
  30.  
  31. // Arduino.h
  32.  
  33. #define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
  34. #define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
  35. #define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
RAW Paste Data