# include <iom16v.h>
# include <macros.h>
unsigned int temperture[]={49,46,48,48,45,0};
unsigned int smg_8_show[]={0x81,0xb7,0xc2,0x92,0xb4,0x98,0x88,0xb3,0x80,0x90};
void delay(unsigned int x)
{
unsigned int i,j;
for(i=x;i>0;i--)
{
for(j=0;j<1141;j++)
{
;;
}
}
}
void Port_init()//端口初始化
{
DDRA&=~BIT(0);
PORTA&=~BIT(0);
}
void USART_Init( unsigned int baud )//串口初始化
{
UBRRH = (unsigned char )(baud>>8);/*设置波特率,baud的值查数据手册*/
UBRRL = (unsigned char ) baud;/* 接收器与发送器使能*/
UCSRB = (1<<RXEN)|(1<<TXEN);/*设置帧格式: 8 个数据位, 1个停止位*/
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
}
void USART_Receive(unsigned int r)
{
while ( !(UCSRA & (1<<UDRE)) );/*等待发送数据*/
UDR=r;
}
//AD转换初始化函数
void ADC_Init()
{
ADMUX |= (1<<REFS0); //AVCC提供参考电压、ADC结果右对齐、选择通道ADC0
//ADMUX |= (1<<REFS0)|(1<<ADLAR);//左对齐
ADCSRA |=(1 <<ADEN) | (1 <<ADPS2) | (1 <<ADPS1); //使能AD转换,ADC时钟64分频
}
//AD转换函数
unsigned int ADC_GetData()
{
ADCSRA |= (1 <<ADSC); //开始AD转换
while(!(ADCSRA & (1 <<ADIF))); //等待转换完成
ADCSRA |= (1 <<ADIF); //清零ADC中断标志位
return ADC; //返回ADC值
}
void put_s_1(unsigned int *s)
{
while(*s)
{
USART_Receive(*s);
s++;
}
}
void main()
{
unsigned int res,cr;
//unsigned int re_0,re_1,re_2,re_3;
DDRD =0Xff;
Port_init();//初始化PA通道
USART_Init(51);//串口初始化
ADC_Init();//ADC初始化
while(1)
{
res = ADC_GetData(); //变换得到电压整数位
cr = ((int)((long)res*5000/1024));
temperture[0]=cr/1000+48;
temperture[1]=(cr%1000)/100+48;
temperture[2]=(cr%100)/10+48;
temperture[3]=(cr%10)+48;
put_s_1(temperture);
delay(1000);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>