00001 /* 00002 * Copyright (c) 2005-2006 David A. Mellis 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU Lesser General Public License as published by 00006 * the Free Software Foundation, either version 2.1 of the License, or 00007 * (at your option) any later version. 00008 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU Lesser General Public License for more details. 00013 00014 * You should have received a copy of the GNU Lesser General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 00019 #include "wiring_private.h" 00020 #include "pins_waspmote.h" 00021 00022 int analogRead(uint8_t pin) 00023 { 00024 uint8_t low, high, ch = analogInPinToBit(pin); 00025 00026 // the low 4 bits of ADMUX select the ADC channel 00027 ADMUX = (ADMUX & (unsigned int) 0xf0) | (ch & (unsigned int) 0x0f); 00028 00029 // without a delay, we seem to read from the wrong channel 00030 //delay(1); 00031 00032 // start the conversion 00033 sbi(ADCSRA, ADSC); 00034 00035 // ADSC is cleared when the conversion finishes 00036 while (bit_is_set(ADCSRA, ADSC)); 00037 00038 // we have to read ADCL first; doing so locks both ADCL 00039 // and ADCH until ADCH is read. reading ADCL second would 00040 // cause the results of each conversion to be discarded, 00041 // as ADCL and ADCH would be locked when it completed. 00042 low = ADCL; 00043 high = ADCH; 00044 00045 // combine the two bytes 00046 return (high << 8) | low; 00047 } 00048 00049 // Right now, PWM output only works on the pins with 00050 // hardware support. These are defined in the appropriate 00051 // pins_*.c file. For the rest of the pins, we default 00052 // to digital output. 00053 void analogWrite(uint8_t pin, int val) 00054 { 00055 // We need to make sure the PWM output is enabled for those pins 00056 // that support it, as we turn it off when digitally reading or 00057 // writing with them. Also, make sure the pin is in output mode 00058 // for consistenty with Wiring, which doesn't require a pinMode 00059 // call for the analog output pins. 00060 pinMode(pin, OUTPUT); 00061 00062 if (digitalPinToTimer(pin) == TIMER1A) { 00063 // connect pwm to pin on timer 1, channel A 00064 sbi(TCCR1A, COM1A1); 00065 // set pwm duty 00066 OCR1A = val; 00067 } else if (digitalPinToTimer(pin) == TIMER1B) { 00068 // connect pwm to pin on timer 1, channel B 00069 sbi(TCCR1A, COM1B1); 00070 // set pwm duty 00071 OCR1B = val; 00072 #if defined(__AVR_ATmega168__) 00073 } else if (digitalPinToTimer(pin) == TIMER0A) { 00074 // connect pwm to pin on timer 0, channel A 00075 sbi(TCCR0A, COM0A1); 00076 // set pwm duty 00077 OCR0A = val; 00078 } else if (digitalPinToTimer(pin) == TIMER0B) { 00079 // connect pwm to pin on timer 0, channel B 00080 sbi(TCCR0A, COM0B1); 00081 // set pwm duty 00082 OCR0B = val; 00083 } else if (digitalPinToTimer(pin) == TIMER2A) { 00084 // connect pwm to pin on timer 2, channel A 00085 sbi(TCCR2A, COM2A1); 00086 // set pwm duty 00087 OCR2A = val; 00088 } else if (digitalPinToTimer(pin) == TIMER2B) { 00089 // connect pwm to pin on timer 2, channel B 00090 sbi(TCCR2A, COM2B1); 00091 // set pwm duty 00092 OCR2B = val; 00093 #elif defined(__AVR_ATmega1281__) 00094 } else if (digitalPinToTimer(pin) == TIMER0A) { 00095 // connect pwm to pin on timer 0, channel A 00096 sbi(TCCR0A, COM0A1); 00097 // set pwm duty 00098 OCR0A = val; 00099 } else if (digitalPinToTimer(pin) == TIMER0B) { 00100 // connect pwm to pin on timer 0, channel B 00101 sbi(TCCR0A, COM0B1); 00102 // set pwm duty 00103 OCR0B = val; 00104 } else if (digitalPinToTimer(pin) == TIMER2A) { 00105 // connect pwm to pin on timer 2, channel A 00106 sbi(TCCR2A, COM2A1); 00107 // set pwm duty 00108 OCR2A = val; 00109 } else if (digitalPinToTimer(pin) == TIMER2B) { 00110 // connect pwm to pin on timer 2, channel B 00111 sbi(TCCR2A, COM2B1); 00112 // set pwm duty 00113 OCR2B = val; 00114 } else if (digitalPinToTimer(pin) == TIMER3A) { 00115 // connect pwm to pin on timer 3, channel A 00116 sbi(TCCR3A, COM3A1); 00117 // set pwm duty 00118 OCR3A = val; 00119 } else if (digitalPinToTimer(pin) == TIMER3B) { 00120 // connect pwm to pin on timer 3, channel B 00121 sbi(TCCR3A, COM3B1); 00122 // set pwm duty 00123 OCR3B = val; 00124 #else 00125 } else if (digitalPinToTimer(pin) == TIMER2) { 00126 // connect pwm to pin on timer 2, channel B 00127 sbi(TCCR2, COM21); 00128 // set pwm duty 00129 OCR2 = val; 00130 #endif 00131 } else if (val < 128) 00132 digitalWrite(pin, LOW); 00133 else 00134 digitalWrite(pin, HIGH); 00135 }
1.5.6