Код: Выделить всё
#include <mega48>
#include <delay>
#include <sleep>
#define SetBit(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define ClearBit(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define ADC_VREF_TYPE 0x40
// Function prorotipes
unsigned int read_adc(unsigned char adc_input) ;
// Declare global variables
unsigned char pow ;
unsigned char per ;
interrupt [WDT] void wdt_timeout_isr(void) // Watchdog timeout interrupt service routine
{
per++ ;
sleep_disable() ;
}
void main(void)
{
// Declare your local variables here
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80 ;
CLKPR=0x00 ;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Input/Output Ports initialization
// Port B initialization
// Func7=Out Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=0 State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00 ;
DDRB=0x00 ;
// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00 ;
DDRC=0x00 ;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00 ;
DDRD=0x00 ;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00 ;
TCCR0B=0x00 ;
TCNT0=0x00 ;
OCR0A=0x00 ;
OCR0B=0x00 ;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00 ;
TCCR1B=0x00 ;
TCNT1H=0x00 ;
TCNT1L=0x00 ;
ICR1H=0x00 ;
ICR1L=0x00 ;
OCR1AH=0x00 ;
OCR1AL=0x00 ;
OCR1BH=0x00 ;
OCR1BL=0x00 ;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=FFh
// OC2A output: Disconnected
// OC2B output: Disconnected
ASSR=0x00 ;
TCCR2A=0x00 ;
TCCR2B=0x00 ;
TCNT2=0x00 ;
OCR2A=0x00 ;
OCR2B=0x00 ;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-14: Off
// Interrupt on any change on pins PCINT16-23: Off
EICRA=0x00 ;
EIMSK=0x00 ;
PCICR=0x00 ;
// Timer/Counter 0 Interrupt(s) initialization
TIMSK0=0x00 ;
// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=0x00 ;
// Timer/Counter 2 Interrupt(s) initialization
TIMSK2=0x00 ;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80 ;
ADCSRB=0x00 ;
// ADC initialization
// ADC Clock frequency: 1000,000 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: None
// Digital input buffers on ADC0: On, ADC1: Off, ADC2: On, ADC3: On
// ADC4: On, ADC5: On
DIDR0=0x02;
ADCSRA=0x03 ;
// Watchdog Timer initialization
// Watchdog Timer Prescaler: OSC/1024k (period 8 sec)
// Watchdog Timer interrupt: Off
#pragma optsize-
#asm("wdr")
WDTCSR=0x39 ;
WDTCSR=0x61 ;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Global enable interrupts
#asm("sei")
while (1)
{
if(per==KOL_PER) {
ADMUX=ADC_VREF_TYPE & 0xff ;
SetBit(ADCSRA,ADEN) ;
delay_ms(50) ;
pow_adc = read_adc(1) ;
ADMUX=0x00 ;
ClearBit(ADCSRA,ADEN) ;
per = 0 ;
}
sleep_enable() ;
powerdown() ;
};
}
unsigned int read_adc(unsigned char adc_input) // Read the AD conversion result
{
ADMUX = adc_input | (ADC_VREF_TYPE & 0xff) ;
delay_us(10) ; // Delay needed for the stabilization of the ADC input voltage
ADCSRA |= 0x40 ; // Start the AD conversion
while ((ADCSRA & 0x10)==0) ; // Wait for the AD conversion to complete
ADCSRA |= 0x10 ;
return ADCW ;
}
Потребление девайса составляет 6,5мА в спящем режиме. Что-то очень много. BOD выключен. В чем проблема?
Спасибо!