AVR单片机 ATMEGA8单片机 ADC电压采样 数码管输出

1. 先看原理图,使用内部的ADC电压采样,通过数码管输出

AVR单片机 ATMEGA8单片机 ADC电压采样 数码管输出

 

2. 相关代码

#include <avr/io.h>

//1 总的来说,ADC总共有3个寄存器,
//  这个ADC是10位的,原来找到了寄存器的头文件
#define  LED_DATA PORTD
#define  LED_CONTROL PORTB
#define  LED_0    PORTB0
#define  LED_1    PORTB1
#define  JI_DIAN_QI PORTC1
#define  DIAN_JI PORTC3
#define  LED_2    PORTB2
#define  JI_DIAN_QI_ON  PORTC |= (1<<PORTC1)
#define  JI_DIAN_QI_OFF PORTC &= ~(1<<PORTC1)

#define  DIAN_JI_ON  PORTC |= (1<<PORTC3)
#define  DIAN_JI_OFF PORTC &= ~(1<<PORTC3)

//1 目前看来引脚的顺序发生了变化
//  ADC采样用的是ADC2
//  电机控制是PC3
//  继电器控制是PC1
char buf[3] = {0};
char adc_buf[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};


void AD_Init(void)
{
    //参考电压AREF,左对齐,ADC2单端输入
    ADMUX |= (1 << ADLAR) | (1 << MUX1);
    //AD使能, 连续转换,中断使能,32分频, 6M / 32 = 187.5khz
    ADCSRA |= (1 << ADEN) | (1 << ADFR) | (1 << ADPS2) | (1 << ADPS0);
    //AD开始转换
    ADCSRA |= (1 << ADSC);
}

void delay(unsigned int j)
{
    while(j--)
    {
            int i = 20000;
            while(i--);    
    }

}

void gpio_init()
{
    DDRD = 0XFF;
    DDRB |= (1<<LED_1) + (1<<LED_2) + (1<<LED_0);

    DDRC |= (1<<JI_DIAN_QI) + (1<<DIAN_JI);
    JI_DIAN_QI_OFF;
    DIAN_JI_OFF;
}

void led_display()
{
    int i =10000;
    while(i--)
    {
        
        LED_DATA = buf[0];
        LED_CONTROL |= 1<<LED_0;
        delay(50000);
        delay(50000);
        delay(50000);
        delay(50000);
        LED_CONTROL &= ~(1<<LED_0); 
        
        LED_DATA = buf[1];
        LED_CONTROL |= 1<<LED_1;
        delay(50000);
        delay(50000);
        delay(50000);
        delay(50000);
        LED_CONTROL &= ~(1<<LED_1);
                
        LED_DATA = buf[2];
        LED_CONTROL |= 1<<LED_2;
        delay(50000);
        delay(50000);
        delay(50000);
        delay(50000);
        LED_CONTROL &= ~(1<<LED_2);    
    }    
}
int main(void)
{
    unsigned  long int result = 0;
    
    unsigned char i;
    unsigned char flag = 0;
    gpio_init();
    //AD初始化
    AD_Init();

     while(1)
     {
         i = 5;
         result = 0;
         //1 循环10次采样,求平均值
         AD_Init();
         while(i--)
         {
            while((ADCSRA & (1 << ADIF)) == 0);
            //读取转换值
            result += ADCH; 
         }

         //result = result/10;
    
         result = ((result * 50 * 3)/5)/254;
         
        //JI_DIAN_QI_ON;
        //DIAN_JI_ON;

         
                 if(result < 108)
                 {
                     flag = 1;
                     JI_DIAN_QI_ON;
                 }
                 else if(result > 144)
                 {
                     flag = 2;
                     JI_DIAN_QI_OFF;
                 }
                 else
                 {
                     if(flag == 1)
                     JI_DIAN_QI_ON;
                     else if(flag == 2)
                     JI_DIAN_QI_OFF;     
                 }
                 
                 if(result > 110)
                  DIAN_JI_ON;
                 else
                  DIAN_JI_OFF;
            
        
         //数据更新
         buf[2] = adc_buf[result / 100];
         buf[1] = adc_buf[result % 100 / 10];
         buf[0] = adc_buf[result % 10 ]; 
         //数据显示
         led_display();
     }
}

 

上一篇:CDN之API以及SDK的调用


下一篇:A7 A8 A9 进阶日记(三)