📄 main.c
字号:
//*****************************************************************************
// File........: main.c
// Author(s)...: ATMEL Norway
// Target(s)...: ATmega169 in Butterfly or STK502
// Compiler....: AVR GCC 20060421
// Description.: Main loop and initialization
// Revisions...: 1.1
// YYYYMMDD - VER. - COMMENT - SIGN.
// 20030326 - 1.0 - Created - AR
// 20060829 - 1.1 - Ported to AVR GCC - TMF, OL
//*****************************************************************************
// Include files
#include <avr/interrupt.h>
#include "Main.h"
/******************************************************************************************
* Function name: main
* returns: None
* parameters: None
* Purpose: Main function which contains the endless main loop
******************************************************************************************/
int main(void)
{
//Variables
unsigned int i;
int ADC_temp;
int adc = 0;
//Initialization
ADMUX = 2; // external AREF and ADCx
ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0); // set ADC prescaler to , 1MHz / 8 = 125kHz
while(1)
{
// To save power, the voltage over the LDR and the NTC is turned off when not used
// This is done by controlling the voltage from a I/O-pin (PORTF3)
sbi(PORTF, PORTF3); // Enable the VCP (VC-peripheral)
sbi(DDRF, PORTF3);
sbi(ADCSRA, ADEN); // Enable the ADC
//do a dummy readout first
ADCSRA |= (1<<ADSC); // do single conversion
while(!(ADCSRA & 0x10)); // wait for conversion done, ADIF flag active
for(i=0;i<32;i++) // do the ADC conversion 8 times for better accuracy
{
ADCSRA |= (1<<ADSC); // do single conversion
while(!(ADCSRA & 0x10)); // wait for conversion done, ADIF flag active
ADC_temp = ADCL; // read out ADCL register
ADC_temp += (ADCH << 8); // read out ADCH register
adc += ADC_temp; // accumulate result (32 samples) for later averaging
}
adc = adc >> 5; // average the 8 samples
cbi(PORTF, PORTF3); // disable the VCP
cbi(DDRF, PORTF3);
cbi(ADCSRA, ADEN); // disable the ADC
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -