AD转换的程序,大意是按一下接在INT0口的键,启动一次ADC,转换完成中端中把转换得到的值分成3个数,在main中让这3个数在LCD上显示出来。用的CVAVR,ATMEA16片子。搞了一个下午啊。
#include <mega16.h>
#include <delay.h>
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
unsigned char adc_value[3];
interrupt [EXT_INT0] void ext_int0_isr(void)
{
ADCSRA|=0X40;
}
#define ADC_VREF_TYPE 0x40
void adc_conv(unsigned int adc )
{
unsigned char i;
for(i=0;i<3;i++)
{
adc_value[2-i]=adc%10;
adc=adc/10;
}
}
interrupt [ADC_INT] void adc_isr(void)
{
unsigned int adc_data;
adc_data=ADCW;
adc_data=adc_data*500/1024;
adc_conv(adc_data);
}
void main(void)
{
GICR|=0x40;
MCUCR=0x02;
MCUCSR=0x00;
GIFR=0x40;
ADMUX=ADC_VREF_TYPE & 0x00;
ADCSRA=0x8D;
lcd_init(16);
#asm("sei")
while (1)
{
lcd_clear();
lcd_gotoxy(1,1);
lcd_putchar(adc_value[0]+0x30);
lcd_gotoxy(2,1);
lcd_putchar('.');
lcd_gotoxy(3,1);
lcd_putchar(adc_value[1]+0x30);
lcd_gotoxy(4,1);
lcd_putchar(adc_value[2]+0x30);
};
} |