www.openedv.com
您好,欢迎您!    会员注册 | 登入 
公告:欢迎访问www.openedv.com开源电子网,开源共享共同进步,祝您新年快乐,万事如意!
设为首页 | 加入收藏
论坛首页 » AVR单片机 前往: 
我的AVR资料[程序篇] 利用ADC通道0做的电压表(源码17)
发表人 内容
[Down] [Up]
[楼主位] 正点原子


等级:NO
注册时间:
2010/12/02 10:41
文章: 8089
来自: 湖南
在线

    这段代码利用ADC的通道0采集AD数据,参考电压为内部2.56V电压.然后将采集到的值取平均值,然后将该值转换为电压(mv),并显示在数码管上,以实现一个简单的电压表的功能.代码如下:
#include <mega16.h>
#include <delay.h>

#define ADC_VREF_TYPE 0xC0

// Read the AD conversion result
unsigned int 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 ADCW;
}

void display(unsigned char number,unsigned char position)//digtial show function
{  
    DDRB=0Xff;//set PORTA output
 PORTB=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
 }       
}
//V messure
void main(void)
{      
    unsigned char b1,b2,b3,b4,j;
 unsigned int t; 
 
    ADMUX=ADC_VREF_TYPE & 0xff;
    ADCSRA=0x83;//use inside ref 2.56V,1000KHz
 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);PORTB.7=0;
   j++;
   if(j>150)
   {
       j=0;t=0;
       for(j=0;j<20;j++)t+=read_adc(0);
       t=t/20;
       t=t*2.5;
       j=0; 
   }
   else delay_us(1000);
   PORTB.7=1;
    }
}



这篇文章被编辑了 1 次. 最近一次更新是在 2010/12/22 00:52


我的淘宝小店:http://shop62103354.taobao.com
[加为好友] 回复 引用回复
 
前往: 

Powered by ALIENTEK工作室 © 粤ICP备12000418号-1