⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 app.c

📁 AVR Devolpment Board
💻 C
字号:
#include "avr/io.h"
#include "util/delay.h"
#include "uart.h"

#define REF   1.0
#define refer ((5000.0/1024.0)*REF)


void delay_ms(unsigned int ms)
{
	while(ms--)
		_delay_ms(1);
}


unsigned int AD_Convert(unsigned char channel)
{  
	unsigned char i=10;
	unsigned int  AD_Value=0;
	DDRA &=~(1<<channel); 
	ADMUX = (0<<REFS1)|(1<<REFS0)|(0<<ADLAR)|(channel); /* REFS1,REFS0=[0,1] extern avcc ref. ADLAR=0 Right-alignment */ 
	ADCSRA= (1<<ADEN )|(1<<ADPS2)|(1<<ADPS1);           /* enable adc. ad_clk=F_CPU/64(125kHz). */
	while(i--)
	{
		ADCSRA|=(1<<ADSC);				                /* start ad convert. */
		while(!(ADCSRA&(1<<ADIF)));                     /* wait while convert end. */
		AD_Value+=ADC;                                  /* added the result. */
	}
	AD_Value/=10;                                       /* filter by average the sum. */
	AD_Value=AD_Value*refer;
	return AD_Value;                                    /* return the voltage in mV. */
}

int main(void)
{
	unsigned char CH=0;
	unsigned int  voltage;

	/* Set LED and Smg LE pin as output , databus as output */
	DDRA |=(1<<PA4)|(1<<PA5)|(1<<PA6); 
	DDRB  = 0xFF;

	/* Off the Smg display */
	PORTA|= (1<<PA5);
	PORTB = 0x00;
	PORTA&=~(1<<PA5);

	/* Off the LED display */
	PORTA|= (1<<PA6);
	PORTB = 0xFF;
	PORTA&=~(1<<PA6);


	UART_Config();
	while(1)
	{
		voltage=AD_Convert(CH);
		printf("The voltage of channel %d is : %4d mV .\r\n",CH,voltage);
		CH++;
		if(CH==8)
			CH=0;
		delay_ms(500);
	}
	return 0;
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -