Advertisement
KRITSADA

wiring_analog.c in avr\cores\arduino for UNO R3B

Jul 29th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.77 KB | None | 0 0
  1. /*
  2.   wiring_analog.c - analog input and output
  3.   Part of Arduino - http://www.arduino.cc/
  4.  
  5.   Copyright (c) 2005-2006 David A. Mellis
  6.  
  7.   This library is free software; you can redistribute it and/or
  8.   modify it under the terms of the GNU Lesser General Public
  9.   License as published by the Free Software Foundation; either
  10.   version 2.1 of the License, or (at your option) any later version.
  11.  
  12.   This library is distributed in the hope that it will be useful,
  13.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.   Lesser General Public License for more details.
  16.  
  17.   You should have received a copy of the GNU Lesser General
  18.   Public License along with this library; if not, write to the
  19.   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20.   Boston, MA  02111-1307  USA
  21.  
  22.   Modified 28 September 2010 by Mark Sproul
  23.  
  24.   $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
  25. */
  26.  
  27. #include "wiring_private.h"
  28. #include "pins_arduino.h"
  29.  
  30. uint8_t analog_reference = DEFAULT;
  31.  
  32. void analogReference(uint8_t mode)
  33. {
  34.     // can't actually set the register here because the default setting
  35.     // will connect AVCC and the AREF pin, which would cause a short if
  36.     // there's something connected to AREF.
  37.     analog_reference = mode;
  38. }
  39.  
  40. int analogRead(uint8_t pin)
  41. {
  42.     uint8_t low, high;
  43.  
  44. #if defined(analogPinToChannel)
  45. #if defined(__AVR_ATmega32U4__)
  46.     if (pin >= 18) pin -= 18; // allow for channel or pin numbers
  47. #endif
  48.     pin = analogPinToChannel(pin);
  49. #elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  50.     if (pin >= 54) pin -= 54; // allow for channel or pin numbers
  51. #elif defined(__AVR_ATmega32U4__)
  52.     if (pin >= 18) pin -= 18; // allow for channel or pin numbers
  53. #elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
  54.     if (pin >= 24) pin -= 24; // allow for channel or pin numbers
  55. #else
  56.     if (pin >= 14) pin -= 14; // allow for channel or pin numbers
  57. #endif
  58.  
  59. #if defined(ADCSRB) && defined(MUX5)
  60.     // the MUX5 bit of ADCSRB selects whether we're reading from channels
  61.     // 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
  62.     ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
  63. #endif
  64.  
  65.     // set the analog reference (high two bits of ADMUX) and select the
  66.     // channel (low 4 bits).  this also sets ADLAR (left-adjust result)
  67.     // to 0 (the default).
  68. #if defined(ADMUX)
  69.     ADMUX = (analog_reference << 6) | (pin & 0x07);
  70. #endif
  71.  
  72.     // without a delay, we seem to read from the wrong channel
  73.     //delay(1);
  74.  
  75. #if defined(ADCSRA) && defined(ADCL)
  76.     // start the conversion
  77.     sbi(ADCSRA, ADSC);
  78.  
  79.     // ADSC is cleared when the conversion finishes
  80.     while (bit_is_set(ADCSRA, ADSC));
  81.  
  82.     // we have to read ADCL first; doing so locks both ADCL
  83.     // and ADCH until ADCH is read.  reading ADCL second would
  84.     // cause the results of each conversion to be discarded,
  85.     // as ADCL and ADCH would be locked when it completed.
  86.     low  = ADCL;
  87.     high = ADCH;
  88. #else
  89.     // we dont have an ADC, return 0
  90.     low  = 0;
  91.     high = 0;
  92. #endif
  93.  
  94.     // combine the two bytes
  95.     return (high << 8) | low;
  96. }
  97.  
  98. // Right now, PWM output only works on the pins with
  99. // hardware support.  These are defined in the appropriate
  100. // pins_*.c file.  For the rest of the pins, we default
  101. // to digital output.
  102. void analogWrite(uint8_t pin, int val)
  103. {
  104.     // We need to make sure the PWM output is enabled for those pins
  105.     // that support it, as we turn it off when digitally reading or
  106.     // writing with them.  Also, make sure the pin is in output mode
  107.     // for consistenty with Wiring, which doesn't require a pinMode
  108.     // call for the analog output pins.
  109.     pinMode(pin, OUTPUT);
  110.     if (val == 0)
  111.     {
  112.         digitalWrite(pin, LOW);
  113.     }
  114.     else if (val == 255)
  115.     {
  116.         digitalWrite(pin, HIGH);
  117.     }
  118.     else
  119.     {
  120.         switch(digitalPinToTimer(pin))
  121.         {
  122.             // XXX fix needed for atmega8
  123.             #if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
  124.             case TIMER0A:
  125.                 // connect pwm to pin on timer 0
  126.                 sbi(TCCR0, COM00);
  127.                 OCR0 = val; // set pwm duty
  128.                 break;
  129.             #endif
  130.  
  131.             #if defined(TCCR0A) && defined(COM0A1)
  132.             case TIMER0A:
  133.                 // connect pwm to pin on timer 0, channel A
  134.                 sbi(TCCR0A, COM0A1);
  135.                 OCR0A = val; // set pwm duty
  136.                 break;
  137.             #endif
  138.  
  139.             #if defined(TCCR0A) && defined(COM0B1)
  140.             case TIMER0B:
  141.                 // connect pwm to pin on timer 0, channel B
  142.                 sbi(TCCR0A, COM0B1);
  143.                 OCR0B = val; // set pwm duty
  144.                 break;
  145.             #endif
  146.  
  147.             #if defined(TCCR1A) && defined(COM1A1)
  148.             case TIMER1A:
  149.                 // connect pwm to pin on timer 1, channel A
  150.                 sbi(TCCR1A, COM1A1);
  151.                 OCR1A = val; // set pwm duty
  152.                 break;
  153.             #endif
  154.  
  155.             #if defined(TCCR1A) && defined(COM1B1)
  156.             case TIMER1B:
  157.                 // connect pwm to pin on timer 1, channel B
  158.                 sbi(TCCR1A, COM1B1);
  159.                 OCR1B = val; // set pwm duty
  160.                 break;
  161.             #endif
  162.  
  163.             #if defined(TCCR1A) && defined(COM1C1)
  164.             case TIMER1C:
  165.                 // connect pwm to pin on timer 1, channel B
  166.                 sbi(TCCR1A, COM1C1);
  167.                 OCR1C = val; // set pwm duty
  168.                 break;
  169.             #endif
  170.  
  171.             #if defined(TCCR2) && defined(COM21)
  172.             case TIMER2:
  173.                 // connect pwm to pin on timer 2
  174.                 sbi(TCCR2, COM21);
  175.                 OCR2 = val; // set pwm duty
  176.                 break;
  177.             #endif
  178.  
  179.             #if defined(TCCR2A) && defined(COM2A1)
  180.             case TIMER2A:
  181.                 // connect pwm to pin on timer 2, channel A
  182.                 sbi(TCCR2A, COM2A1);
  183.                 OCR2A = val; // set pwm duty
  184.                 break;
  185.             #endif
  186.  
  187.             #if defined(TCCR2A) && defined(COM2B1)
  188.             case TIMER2B:
  189.                 // connect pwm to pin on timer 2, channel B
  190.                 sbi(TCCR2A, COM2B1);
  191.                 OCR2B = val; // set pwm duty
  192.                 break;
  193.             #endif
  194.  
  195.             #if defined(TCCR3A) && defined(COM3A1)
  196.             case TIMER3A:
  197.                 // connect pwm to pin on timer 3, channel A
  198.                 sbi(TCCR3A, COM3A1);
  199.                 OCR3A = val; // set pwm duty
  200.                 break;
  201.             #endif
  202.  
  203.             #if defined(TCCR3A) && defined(COM3B1)
  204.             case TIMER3B:
  205.                 // connect pwm to pin on timer 3, channel B
  206.                 sbi(TCCR3A, COM3B1);
  207.                 OCR3B = val; // set pwm duty
  208.                 break;
  209.             #endif
  210.  
  211.             #if defined(TCCR3A) && defined(COM3C1)
  212.             case TIMER3C:
  213.                 // connect pwm to pin on timer 3, channel C
  214.                 sbi(TCCR3A, COM3C1);
  215.                 OCR3C = val; // set pwm duty
  216.                 break;
  217.             #endif
  218.  
  219.             #if defined(TCCR4A)
  220.             case TIMER4A:
  221.                 //connect pwm to pin on timer 4, channel A
  222.                 sbi(TCCR4A, COM4A1);
  223.                 OCR4A = val;    // set pwm duty
  224.                 break;
  225.             #endif
  226.            
  227.             #if defined(TCCR4A) && defined(COM4B1)
  228.             case TIMER4B:
  229.                 // connect pwm to pin on timer 4, channel B
  230.                 sbi(TCCR4A, COM4B1);
  231.                 OCR4B = val; // set pwm duty
  232.                 break;
  233.             #endif
  234.  
  235.             #if defined(TCCR4A) && defined(COM4C1)
  236.             case TIMER4C:
  237.                 // connect pwm to pin on timer 4, channel C
  238.                 sbi(TCCR4A, COM4C1);
  239.                 OCR4C = val; // set pwm duty
  240.                 break;
  241.             #endif
  242.                
  243.             #if defined(TCCR4C) && defined(COM4D1)
  244.             case TIMER4D:              
  245.                 // connect pwm to pin on timer 4, channel D
  246.                 sbi(TCCR4C, COM4D1);
  247.                 #if defined(COM4D0)     // only used on 32U4
  248.                 cbi(TCCR4C, COM4D0);
  249.                 #endif
  250.                 OCR4D = val;    // set pwm duty
  251.                 break;
  252.             #endif
  253.  
  254.                            
  255.             #if defined(TCCR5A) && defined(COM5A1)
  256.             case TIMER5A:
  257.                 // connect pwm to pin on timer 5, channel A
  258.                 sbi(TCCR5A, COM5A1);
  259.                 OCR5A = val; // set pwm duty
  260.                 break;
  261.             #endif
  262.  
  263.             #if defined(TCCR5A) && defined(COM5B1)
  264.             case TIMER5B:
  265.                 // connect pwm to pin on timer 5, channel B
  266.                 sbi(TCCR5A, COM5B1);
  267.                 OCR5B = val; // set pwm duty
  268.                 break;
  269.             #endif
  270.  
  271.             #if defined(TCCR5A) && defined(COM5C1)
  272.             case TIMER5C:
  273.                 // connect pwm to pin on timer 5, channel C
  274.                 sbi(TCCR5A, COM5C1);
  275.                 OCR5C = val; // set pwm duty
  276.                 break;
  277.             #endif
  278.  
  279.             case NOT_ON_TIMER:
  280.             default:
  281.                 if (val < 128) {
  282.                     digitalWrite(pin, LOW);
  283.                 } else {
  284.                     digitalWrite(pin, HIGH);
  285.                 }
  286.         }
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement