Advertisement
xerpi

Arduino Processing functions in C

Sep 15th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.28 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3.  
  4. enum
  5. {
  6.     HIGH = 1,
  7.     LOW  = 0,
  8. };
  9.  
  10. enum
  11. {
  12.     OUTPUT = 1,
  13.     INPUT  = 0,
  14. };
  15.  
  16. void pinMode(int pin, int mode);
  17. void digitalWrite(int pin, int mode);
  18. int digitalRead(int pin);
  19.  
  20. int main (void)
  21. {
  22.     pinMode(13, OUTPUT);
  23.  
  24.     while(1)
  25.     {
  26.         digitalWrite(13, HIGH);
  27.         _delay_ms(250);
  28.  
  29.         digitalWrite(13, LOW);
  30.         _delay_ms(250);
  31.     }
  32.  
  33. return 0;
  34. }
  35.  
  36. void pinMode(int pin, int mode)
  37. {
  38.     if( (pin >= 0) && (pin < 8) )       //PORTD
  39.     {
  40.         mode ? (DDRD |= _BV(pin)) : (DDRD &= ~_BV(pin));
  41.     }
  42.     else if( (pin > 7) && (pin < 14) )  //PORTB
  43.     {
  44.         pin -= 8;
  45.         mode ? (DDRB |= _BV(pin)) : (DDRB &= ~_BV(pin));
  46.     }
  47. }
  48.  
  49. void digitalWrite(int pin, int mode)
  50. {
  51.     if( (pin >= 0) && (pin < 8) )       //PORTD
  52.     {
  53.         mode ? (PORTD |= _BV(pin)) : (PORTD &= ~_BV(pin));
  54.     }
  55.     else if( (pin > 7) && (pin < 14) )  //PORTB
  56.     {
  57.         pin -= 8;
  58.         mode ? (PORTB |= _BV(pin)) : (PORTB &= ~_BV(pin));
  59.     }
  60. }
  61.  
  62. int digitalRead(int pin)
  63. {
  64.     if( (pin >= 0) && (pin < 8) )       //PIND
  65.     {
  66.         return (PIND & _BV(pin));
  67.     }
  68.     else if( (pin > 7) && (pin < 14) )  //PINB
  69.     {
  70.         return (PINB & _BV(pin - 8));
  71.     }
  72.     return LOW;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement