打印

一个AVR的片内AD从ICC移植都WINAVR

[复制链接]
356|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
Clovee|  楼主 | 2017-8-8 12:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一个AVR的片内AD从ICC移植都WINAVR

//ICC-AVR application builder : 2009-3-10 14:59:10
// Target : M8
// Crystal: 8.0000Mhz

#include <avr/io.h>
#include <avr/interrupt.h>

void port_init(void)
{
PORTB = 0x00;
DDRB  = 0x00;
PORTC = 0x00; //m103 output only
DDRC  = 0x00;
PORTD = 0x00;
DDRD  = 0x00;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
cli(); //disable all interrupts
port_init();

MCUCR = 0x00;
GICR  = 0x00;
TIMSK = 0x00; //timer interrupt sources
sei(); //re-enable interrupts
//all peripherals are now initialized
}

/************************************
用    途:微秒级延时程序
Taget   :mega8
crystal :8M
介    绍:在8M的晶振上进行us级的延时
入口参数:
*************************************/
void delay_us(int time)
{
     do
     {
         time--;
     }
    while (time > 1);
}


/************************************
用    途:两位数码管显示一个数
Taget   :mega8
crystal :8M
介    绍:共阳数码管
                    1-PC1(片选)
                  2-PC0
                  -----
                  a-PB0(数据)
                  b-PB1
                  ...
                  h-PB6
                  DP-PB7
入口参数:要显示的数,十进制表示
*************************************/
const unsigned char num[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
                                    0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void show_2_digit(unsigned char digi)
{
unsigned char i;
DDRD=0xff;
DDRB=0xff;
PORTD=0;//关片选
PORTB=~num[(unsigned char )(digi/10)];//显示十位
PORTD=(0x1<<1);//开十位的显示
delay_us(200);
PORTD=0;//关显示
PORTB=~num[(unsigned char )(digi%10)];//显示个位
PORTD=(0x1<<0);//开个位的显示
delay_us(200);
PORTD=0x0;//关显示
}

/************************************
用    途:adc初始化
target  :atmega8
crystal :8M
介    绍:ADC0端口输入
                    10位精度,最高为5V
入口参数:
出口参数:
*************************************/
void adc_init(void)
{
ACSR=0x80;//关掉模拟比较器的电源(禁用模拟比较器)
ADMUX=(1<<REFS0);
                   //ADMUX=7,6参考电源选择,5左对齐选择,3-0输入端口选择
ADCSRA=(1<<ADEN);//这里选择的是连续模式
                                  //ADCSRA=7,adc使能,6,adc开始转换,5,连续转换模式选择
                   //4,中断标志,3,中断允许,2-0,预分频选择
SFIOR=0x00;//4,adc高速模式选择
}

/************************************
用    途:进行一次adc转换
target  :atmega8
crystal :8M
介    绍:10位精度
入口参数:
出口参数:电压值(0-50)表示0-5.0V
*************************************/
unsigned char adc_get(void)
{
unsigned int temp;

ADCSRA|=(1<<ADSC);//开始转换
while(!(ADCSR&(1<<ADIF))){;}//等待转换完成
ADCSR|=(1<<ADIF);//清标志
temp=ADC;//读数
temp=temp*51/0x03ff;//转换成0-50(表示0-5.0)
return (unsigned char)(temp);
}

int main(void)
{
port_init();
init_devices();

adc_init();
while(1)
{
   show_2_digit(adc_get());

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

708

主题

1032

帖子

2

粉丝