P.S. файл proteus не прикреплялся, поэтому загрузил в облако и прикрепил ссылку.
Код: Выделить всё
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define ADC_VREF_TYPE 0x40
unsigned int time = 0;
// Timer 0 output compare A interrupt service routin
ISR(TIMER0_CMP_vect)
{
if(time <= 1152)
time++;
else
time = 0;
if(time >= 0 && time <= 63)
{
PORTC = 4*time;
}
else
if(time > 63 && time <= 576)
{
PORTC = 255;
}
else
if(time > 576 && time <= 832)
{
PORTC = -time + 832;
}
else
{
PORTC = 0;
}
}
/// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=(adc_input & 0x1f) | (ADC_VREF_TYPE & 0xff);
if (adc_input & 0x20) ADCSRB |= 0x08;
else ADCSRB &= 0xf7;
// Delay needed for the stabilization of the ADC input voltage
int i=0;
while(i<1000)
{i++;}
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
void main(void)
{
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
PORTB=0x00;
DDRB=0xFF;
PORTC=0x00;
DDRC=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125,000 kHz
// Mode: CTC top=OCR0A
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x02;
TCCR0B=0x03;
TCNT0=0x00;
OCR0A=0xC3;
OCR0B=0x00;
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x02;
// ADC initialization
// ADC Clock frequency: 125,000 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: Free Running
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA6;
ADCSRB&=0xF8;
// Global enable interrupts
while (1)
{
if(read_adc(0) >=0 && read_adc(0) <= 205)
PORTB = 0x1F;
else
if(read_adc(0) > 205 && read_adc(0) < 410)
PORTB = 0x0F;
else
if(read_adc(0) >= 410 && read_adc(0) <= 615)
PORTB = 0x07;
else
if(read_adc(0) > 615 && read_adc(0) < 820)
PORTB = 0x03;
else
if(read_adc(0) >= 820 && read_adc(0) < 1023)
PORTB = 0x01;
else
PORTB = 0x00;
}
}


