新手上路
- 积分
- 39
- 金钱
- 39
- 注册时间
- 2015-2-19
- 在线时间
- 0 小时
|
5金钱
我用的ATmega16,AD转换,结果输出至串口,可是无论我接不接电,串口数字都不变,原因是什么啊?紧急求助啊!!
#include<iom16v.h>
#include <macros.h>
#define fosc 1000000 //晶振8MHZ
#define baud 9600 //波特率
void uart0_init(void)
{
UCSRB=(1<<RXEN)|(1<<TXEN)|(1<<RXCIE);//允许收发,打开接收中断
UCSRA = 1<<U2X;
UBRRL=12;
UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);//8位数据+1位STOP
}
void delay( int a)
{
int i;
int j;
for(i=0;i<124;i++)
{
for (j=0;j<a;j++)
{
}
}
}
void USART_Transmit(char data) //发送采用查询方式
{
while(!(UCSRA&(1<<UDRE)));//上次发送有没有完成
UDR=data; //发送数据
}
void send(unsigned char *f,int b)
{
int i;
for(i=1;i<=b;i++)
{
while( !(UCSRA & (1<<5)) );
UDR=f;
delay(100);
}
}
void port_init(void)
{
DDRD = 0x02;
PORTD = 0xFF;
}
void Init_ADC(void)
{
DDRA&=~(1<<1);
ADMUX|=(1<<6);
ADCSRA|=(1<<7)|(1<<5)|(1<<2)|(1<<1);
ADCSRA|=(1<<6);
}
int main(void)
{
int teml,temh,tem;
char tema[10];
tem=0;
uart0_init();
port_init();
Init_ADC();
while(1)
{
if(ADCSRA&(1<<4))
{
temh=ADCH;
teml=ADCL;
temh=temh*16*16;
tem=temh+teml;
tem=tem*0x03;
tema[4]=tem%10+48;
tema[3]=(tem%100)/10+48;
tema[2]=(tem%1000)/100+48;
tema[1]=(tem%10000)/1000+48;
tema[5]='\r';
tema[6]='\t';
send(tema,6);
delay(1000);
}
}
return 0;
}
还请各位分析一下啊!!!谢谢啦!!! |
|