这个程序利用AVR的内部adc读取通道0的电压,采用内部2.56V的参考电压。源码如下:
#include <mega16.h>
#include <delay.h>
#define ADC_VREF_TYPE 0x60 //内部2.56V参考电压
// Read the 8 most significant bits
// of the AD conversion result
unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCH;
}
void display(unsigned char number,unsigned char position)//digtial show function
{
DDRB=0Xff;//set PORTA output
 ORTB=0XFF;//set PORTA output in high level
switch(position)//choice the position
{
case 1:{PORTB&=0xf8;break;}
case 2:{PORTB&=0xf9;break;}
case 3:{PORTB&=0Xfa;break;}
case 4:{PORTB&=0Xfb;break;}
case 5:{PORTB&=0Xfc;break;}
case 6:{PORTB&=0Xfd;break;}
case 7:{PORTB&=0Xfe;break;}
case 8:{PORTB&=0Xff;break;}
}
switch(number)//show number
{
case 0:{PORTB&=0x87;break;} //0
case 1:{PORTB&=0x8f;break;} //1
case 2:{PORTB&=0x97;break;} //2
case 3:{PORTB&=0x9f;break;} //3
case 4:{PORTB&=0xa7;break;} //4
case 5:{PORTB&=0xaf;break;} //5
case 6:{PORTB&=0xb7;break;} //6
case 7:{PORTB&=0xbf;break;} //7
case 8:{PORTB&=0xc7;break;} //8
case 9:{PORTB&=0xcf;break;} //9
default:{PORTB=0xff;break;} //null
}
}
void main(void)
{
unsigned char b1,b2,b3,b4,b5,b6,b7;
unsigned char t;
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;
while(1)
{
b1=t%10;display(b1,8);delay_us(1000);
b2=(t/10)%10;display(b2,7);delay_us(1000);
b3=(t/100)%10;display(b3,6);delay_us(1000);
b4=(t/1000)%10;display(b4,5);delay_us(1000);
b5=(t/10000)%10;display(b5,4);delay_us(1000);
b6=(t/100000)%10;display(b6,3);delay_us(1000);
b7=(t/1000000)%10;display(b7,2);
t=read_adc(0);
}
}
|