avr单片机mega644将ADC转换的结果通过串口调试显示
时间:10-02
整理:3721RD
点击:
#include<iom644v.h>
#include<macros.h>
#define SYSTEM_CLOCK 12000000
void delay(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<760;j++);
}
void Port_init()//端口初始化
{
DDRA&=~BIT(PA1);
PORTA&=~BIT(PA1);
}
void ADC_Init()
{
ADMUX |= (1<<REFS0); //AVCC提供参考电压、ADC结果右对齐、选择通道ADC0
ADCSRA |=(1 <<ADEN) | (1 <<ADPS2) | (1 <<ADPS1); //使能AD转换,ADC时钟64分频
}
unsigned int ADC_GetData()
{
unsigned int addata;
ADCSRA |= (1 <<ADSC); //开始AD转换
while(!(ADCSRA & (1 <<ADIF))); //等待转换完成
ADCSRA |= (1 <<ADIF); //清零ADC中断标志位
addata=ADCL;
addata=addata+ADCH*256;
return addata;
//return ADC; //返回ADC值
}
/*unsigned int get_adc(void)
{ unsigned int addata;
ADCSRA=0x80;
ADCSRA|=BIT(ADSC);
while(!(ADCSRA&(BIT(ADIF))));
addata=ADCL;
addata=addata+ADCH*256;
return addata;
}*/
void uart_init(int baud)
{
UCSR0A = 0x00;
UCSR0B = 0x00;
baud=SYSTEM_CLOCK/16/baud-1;
UBRR0H=(unsigned char)(baud >> 8);
UBRR0L=(unsigned char)baud;
UCSR0B=(1<<RXEN0)|(1<<TXEN0);
UCSR0C=(0<<USBS0)|(3<<UCSZ00);
SREG = 0X80;
}
void uart0_send(unsigned char data)
{
while(!(UCSR0A&(BIT(UDRE0))));
UDR0 = data;
while(!(UCSR0A&(BIT(TXC0))));
UCSR0A|=BIT(TXC0);
}
void main()
{
unsigned char j,k;
unsigned int i, data,ad[4];
Port_init();
ADC_Init();
data=ADC_GetData();
for(i=0;i<4;i++)
{
ad[3-i]=data%10;
data=data/10;
}
//i = 3;
// j = 4;
//k = 'q';
uart_init(19200);
delay(200);
while(1)
{
uart0_send(ad[0]);
delay(1000);
uart0_send(ad[1]);
delay(1000);
uart0_send(ad[2]);
delay(1000);
uart0_send(ad[3]);
delay(1000);
}
}
#include<macros.h>
#define SYSTEM_CLOCK 12000000
void delay(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<760;j++);
}
void Port_init()//端口初始化
{
DDRA&=~BIT(PA1);
PORTA&=~BIT(PA1);
}
void ADC_Init()
{
ADMUX |= (1<<REFS0); //AVCC提供参考电压、ADC结果右对齐、选择通道ADC0
ADCSRA |=(1 <<ADEN) | (1 <<ADPS2) | (1 <<ADPS1); //使能AD转换,ADC时钟64分频
}
unsigned int ADC_GetData()
{
unsigned int addata;
ADCSRA |= (1 <<ADSC); //开始AD转换
while(!(ADCSRA & (1 <<ADIF))); //等待转换完成
ADCSRA |= (1 <<ADIF); //清零ADC中断标志位
addata=ADCL;
addata=addata+ADCH*256;
return addata;
//return ADC; //返回ADC值
}
/*unsigned int get_adc(void)
{ unsigned int addata;
ADCSRA=0x80;
ADCSRA|=BIT(ADSC);
while(!(ADCSRA&(BIT(ADIF))));
addata=ADCL;
addata=addata+ADCH*256;
return addata;
}*/
void uart_init(int baud)
{
UCSR0A = 0x00;
UCSR0B = 0x00;
baud=SYSTEM_CLOCK/16/baud-1;
UBRR0H=(unsigned char)(baud >> 8);
UBRR0L=(unsigned char)baud;
UCSR0B=(1<<RXEN0)|(1<<TXEN0);
UCSR0C=(0<<USBS0)|(3<<UCSZ00);
SREG = 0X80;
}
void uart0_send(unsigned char data)
{
while(!(UCSR0A&(BIT(UDRE0))));
UDR0 = data;
while(!(UCSR0A&(BIT(TXC0))));
UCSR0A|=BIT(TXC0);
}
void main()
{
unsigned char j,k;
unsigned int i, data,ad[4];
Port_init();
ADC_Init();
data=ADC_GetData();
for(i=0;i<4;i++)
{
ad[3-i]=data%10;
data=data/10;
}
//i = 3;
// j = 4;
//k = 'q';
uart_init(19200);
delay(200);
while(1)
{
uart0_send(ad[0]);
delay(1000);
uart0_send(ad[1]);
delay(1000);
uart0_send(ad[2]);
delay(1000);
uart0_send(ad[3]);
delay(1000);
}
}
小编这个是想分享还是有问题啊?代码还是做点简单的描述比较好吧
第一次写帖子 谢谢你的提醒 以后我会注意的