Спойлер
Код: Выделить всё
/*
* GccApplication1.cpp
*
* Created: 11.10.2014 18:00:12
* Author: user
*/
#define F_CPU 10000000UL
#include <avr /io.h>
#include <util /delay.h>
// delay for up to 65k milliseconds
void delayms(uint16_t millis)
{
// loop, delaying 1ms each iteration
while ( millis )
{
_delay_ms(1);
millis--;
}
}
// initialise the adc
void init_adc ( void )
{
//select internal (1.1) voltage as the reference voltage (x1xx xxxx)
ADMUX = 0x40;
//enable ADC (1000 0000)
ADCSRA |= 0x80;
}
// read the specified adc
int adc_read ( uint8_t n )
{
// set the adc to the chosen channel
ADMUX = n;
// start the ADC conversion, set the ADC clock to cpu clock / 16 (0100 0100)
ADCSRA |= 0x44;
// wait for the adc conversion to be completed
while((ADCSRA & 0x40) !=0){};
// return the adc result
return ADC;
}
int main(void)
{
// set PB3 to output
DDRB |= 1< <PB3;
// turn on the ADC
init_adc();
// set up our variables
int reading = 0;
// loop for ever
while(1) {
// take a new adc reading
reading = adc_read(2);
// if the voltage is less than 3
if ( reading < 837 )
{
// turn the output pin off
PORTB |= 1<<PB3; /* LED off */
}
else
{
// turn the output pin on
PORTB &= ~(1< <PB3);
}
// wait for 20ms
delayms(20);
}
return 0;
}


