AD数模转换-CVAVR程序代码
扫描二维码
随时随地手机看文章
#include
#include
//AlphanumericLCDModulefunctions
#asm
.equ__lcd_port=0x18;PORTB
#endasm
#include
intshi,ge,qian,bai,m;
intadc_v;
//Timer0outputcompareinterruptserviceroutine
interrupt[TIM0_COMP]voidtimer0_comp_isr(void)
{
//Placeyourcodehere
m++;
}
#include
#defineADC_VREF_TYPE0x00
//ADCinterruptserviceroutine
interrupt[ADC_INT]voidadc_isr(void)
{
unsignedintadc_data;
//ReadtheADconversionresult
adc_data=ADCW;
adc_v=(unsignedlong)adc_data*5000/1024.0;
//Placeyourcodehere
}
//Declareyourglobalvariableshere
voidmain(void)
{
//Declareyourlocalvariableshere
//Input/OutputPortsinitialization
//PortAinitialization
//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In
//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T
PORTA=0x0f;
DDRA=0x0f;
//PortBinitialization
//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In
//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T
PORTB=0x00;
DDRB=0x00;
//PortCinitialization
//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In
//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T
PORTC=0x00;
DDRC=0x00;
//PortDinitialization
//Func7=InFunc6=InFunc5=InFunc4=InFunc3=InFunc2=InFunc1=InFunc0=In
//State7=TState6=TState5=TState4=TState3=TState2=TState1=TState0=T
PORTD=0x00;
DDRD=0x00;
//Timer/Counter0initialization
//Clocksource:SystemClock
//Clockvalue:62.500kHz
//Mode:CTCtop=OCR0
//OC0output:Disconnected
TCCR0=0x0B;
TCNT0=0x00;
OCR0=0xFF;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
//Timer/Counter2initialization
//Clocksource:SystemClock
//Clockvalue:Timer2Stopped
//Mode:Normaltop=FFh
//OC2output:Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
//ExternalInterrupt(s)initialization
//INT0:Off
//INT1:Off
//INT2:Off
MCUCR=0x00;
MCUCSR=0x00;
//Timer(s)/Counter(s)Interrupt(s)initialization
TIMSK=0x02;
//AnalogComparatorinitialization
//AnalogComparator:Off
//AnalogComparatorInputCapturebyTimer/Counter1:Off
ACSR=0x80;
SFIOR&=0x1F;
SFIOR|=0x60;
//ADCinitialization
//ADCClockfrequency:1000.000kHz
//ADCVoltageReference:AREFpin
//ADCAutoTriggerSource:FreeRunning
ADMUX=0x47;
ADCSRA=0xAD;
//LCDmoduleinitialization
lcd_init(16);
//Globalenableinterrupts
#asm("sei")
while(1)
{lcd_clear();
qian=adc_v/1000;
bai=adc_v00/100;
shi=adc_v000/10;
ge=adc_v000;
lcd_gotoxy(0,0);
lcd_putchar(qian+48);
lcd_putchar(bai+48);
lcd_putchar(shi+48);
lcd_putchar(ge+48);
delay_ms(100);
//Placeyourcodehere
};
}