#include "wiring_private.h"
#include "pins_arduino.h"
uint8_t analog_reference = DEFAULT;
void analogReference(uint8_t mode)
{
analog_reference = mode;
}
int analogRead(uint8_t pin)
{
uint8_t low, high;
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
if (pin >= 54) pin -= 54; #elif defined(__AVR_ATmega32U4__)
if (pin >= 18) pin -= 18; #elif defined(__AVR_ATmega1284__)
if (pin >= 24) pin -= 24; #else
if (pin >= 14) pin -= 14; #endif
#if defined(__AVR_ATmega32U4__)
pin = analogPinToChannel(pin);
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
#elif defined(ADCSRB) && defined(MUX5)
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
#endif
#if defined(ADMUX)
ADMUX = (analog_reference << 6) | (pin & 0x07);
#endif
#if defined(ADCSRA) && defined(ADCL)
sbi(ADCSRA, ADSC);
while (bit_is_set(ADCSRA, ADSC));
low = ADCL;
high = ADCH;
#else
low = 0;
high = 0;
#endif
return (high << 8) | low;
}
void analogWrite(uint8_t pin, int val)
{
pinMode(pin, OUTPUT);
if (val == 0)
{
digitalWrite(pin, LOW);
}
else if (val == 255)
{
digitalWrite(pin, HIGH);
}
else
{
switch(digitalPinToTimer(pin))
{
#if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
case TIMER0A:
sbi(TCCR0, COM00);
OCR0 = val; break;
#endif
#if defined(TCCR0A) && defined(COM0A1)
case TIMER0A:
sbi(TCCR0A, COM0A1);
OCR0A = val; break;
#endif
#if defined(TCCR0A) && defined(COM0B1)
case TIMER0B:
sbi(TCCR0A, COM0B1);
OCR0B = val; break;
#endif
#if defined(TCCR1A) && defined(COM1A1)
case TIMER1A:
sbi(TCCR1A, COM1A1);
OCR1A = val; break;
#endif
#if defined(TCCR1A) && defined(COM1B1)
case TIMER1B:
sbi(TCCR1A, COM1B1);
OCR1B = val; break;
#endif
#if defined(TCCR2) && defined(COM21)
case TIMER2:
sbi(TCCR2, COM21);
OCR2 = val; break;
#endif
#if defined(TCCR2A) && defined(COM2A1)
case TIMER2A:
sbi(TCCR2A, COM2A1);
OCR2A = val; break;
#endif
#if defined(TCCR2A) && defined(COM2B1)
case TIMER2B:
sbi(TCCR2A, COM2B1);
OCR2B = val; break;
#endif
#if defined(TCCR3A) && defined(COM3A1)
case TIMER3A:
sbi(TCCR3A, COM3A1);
OCR3A = val; break;
#endif
#if defined(TCCR3A) && defined(COM3B1)
case TIMER3B:
sbi(TCCR3A, COM3B1);
OCR3B = val; break;
#endif
#if defined(TCCR3A) && defined(COM3C1)
case TIMER3C:
sbi(TCCR3A, COM3C1);
OCR3C = val; break;
#endif
#if defined(TCCR4A)
case TIMER4A:
sbi(TCCR4A, COM4A1);
#if defined(COM4A0)
cbi(TCCR4A, COM4A0);
#endif
OCR4A = val; break;
#endif
#if defined(TCCR4A) && defined(COM4B1)
case TIMER4B:
sbi(TCCR4A, COM4B1);
OCR4B = val; break;
#endif
#if defined(TCCR4A) && defined(COM4C1)
case TIMER4C:
sbi(TCCR4A, COM4C1);
OCR4C = val; break;
#endif
#if defined(TCCR4C) && defined(COM4D1)
case TIMER4D:
sbi(TCCR4C, COM4D1);
#if defined(COM4D0)
cbi(TCCR4C, COM4D0);
#endif
OCR4D = val; break;
#endif
#if defined(TCCR5A) && defined(COM5A1)
case TIMER5A:
sbi(TCCR5A, COM5A1);
OCR5A = val; break;
#endif
#if defined(TCCR5A) && defined(COM5B1)
case TIMER5B:
sbi(TCCR5A, COM5B1);
OCR5B = val; break;
#endif
#if defined(TCCR5A) && defined(COM5C1)
case TIMER5C:
sbi(TCCR5A, COM5C1);
OCR5C = val; break;
#endif
case NOT_ON_TIMER:
default:
if (val < 128) {
digitalWrite(pin, LOW);
} else {
digitalWrite(pin, HIGH);
}
}
}
}