ATMEGA162串口接收到的数据时乱码?
ATMEGA162每隔1秒,向下位机发送命令,下位机收到命令后,向上位机发送数据,
上位机收到的全部都是十六进制的乱码。端口设置和波特率设置也都是正确的,下位机程序肯定没问题。
串口1发送数据,接收到数据以后,在通过串口0,发送出去,在电脑的串口显示出来。
程序如下,是否是程序有问题??是否是熔丝位设置有问题?
仿真的时候,收到的数据都是正常的。
#include <iom162v.h>
#include <avrdef.h>
unsigned char x,y;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xff;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0xff;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0x00;
DDRE = 0xFF;
}
void send0_data(unsigned char com)
{
while(!(UCSR0A&(1<<UDRE0)));
UDR0=com;
}
void send1_data(unsigned char com)
{
while(!(UCSR1A&(1<<UDRE1)));
UDR1=com;
}
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00; //disable while setting baud rate
UBRR0L =0x47; //set baud rate
UBRR0H = 0x00;
UCSR0C = BIT(URSEL0) | 0x06;
UCSR0A = 0x00; //enable
UCSR0B = 0x08; //enable
void uart1_init(void)
{
UCSR1B = 0x00; //disable while setting baud rate
UCSR1A = 0x00; //disable while setting baud rate
UBRR1L =0x47; //set baud rate
UBRR1H = 0x00;
UCSR1C = BIT(URSEL1) | 0x06;
UCSR1A = 0x00; //enable
UCSR1B = 0x98; //enable
}
void timer0_init(void)
{
TCCR0= 0x00; //stop
TCNT0= 0x94; //set count
OCR0= 0x6C; //set compare value
TCCR0= 0x05; //start timer
}
#pragma interrupt_handler uart1_rx_isr:iv_USART1_RXC
void uart1_rx_isr(void)
{
send0_data(UDR1);
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
TCNT0= 0x94; //reload counter value
x++;
if(x==100)
{
x=0;
send1_data(0xA0);
send1_data(0xA1);
send1_data(0x0D);
}
}
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
uart0_init();
uart1_init();
MCUCR= 0x00;
EMCUCR = 0x00;
TIMSK= 0x02; //timer interrupt sources
ETIMSK=0x00;
GICR= 0x00;
PCMSK0=0x00;
PCMSK1=0x00;
SEI(); //re-enable interrupts
}
void main(void)
{
init_devices();
while(1)
{
;
}
}
此帖出自
小平头技术问答
外接晶振:11.0592MHZ,熔丝位这样设置是否正确?
一周热门 更多>