AVR之简单的ADC程序
原创
©著作权归作者所有:来自51CTO博客作者maoge_2010的原创作品,请联系作者获取转载授权,否则将追究法律责任
最近看了ATmega16的ADC,写下这个简单的程序,了解下ADC的简单控制。
程序用Proteus仿真的,用的是ICCAVR7 的编译器(头文件有所不同,自己掂量)
程序的内容是:通过PA1外部电压的读入并动态的显示在3位数码管上
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
#include<iom16v.h>
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
#include<AVRdef.h>
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
unsigned
char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//数码管0~9
void display(unsigned
char smg);
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
unsigned
char temp;
void delay_1ms(unsigned
int z)
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
{
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
unsigned
int i,j;
for(i=0;i<z;i++)
for(j=0;j<150;j++);
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
}
void main()
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
{
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
DDRC=0xff;
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
PORTC=0;
//PC输出数码管段选 ![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
DDRD=0xff;
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
PORTD=0;
//PD5~PD7为数码管位选 ![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
ADCSRA=0;
//关闭ADC ![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
ADMUX=(1<<ADLAR)|(1<<REFS0)|(1);
//左对齐,参考电压AVCC,通道1 ![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADIE)|(7);
//使能adc和中断,预分频128,启动ADC,不设置连续转化模式(|(1<<ADATE))
//SFIOR=0;//连续转换模式 ![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
SEI();
while(1)
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
{
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
display(temp);
//动态显示数码管 ![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
}
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
}
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
#pragma interrupt_handler adc:iv_ADC
void adc()
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
{
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
temp=ADCH;
//读取转换的数据 ![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
ADCSRA|=0X40;
//重新启动ADC,设置ADCSRA的ADATE和连续转换模式时不用 ![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
}
void display(unsigned
char smg)
//3位数码管动态显示 ![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
{
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
unsigned
char bai,shi,ge;
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
bai=smg/100;
//百位 ![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
shi=smg%100/10;
//十位 ![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
ge=smg%10;
//个位 ![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
PORTC=tab[bai];
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
PORTD=0x80;
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
PORTD=0;
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
delay_1ms(2);
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
PORTC=tab[shi];
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
PORTD=0x40;
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
PORTD=0;
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
delay_1ms(2);
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
PORTC=tab[ge];
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
PORTD=0x20;
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
PORTD=0;
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
delay_1ms(1);
![AVR之简单的ADC程序_ADC](https://s2.51cto.com/images/editer/InBlock.gif)
}