之前用M16作串口通信一直没问题的,然后换了M162双串口就出问题了,PC机上收到数据,但是都是错误的数据,晶振选择3.6864M,波特率选择了2400,同样的程序,哪里出问题了??M162使用双串口时需要注意什么问题呢??
程序已贴上!!
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define CRYSTAL 3686400
#define BAUD 2400
#define BAUD_SETTING (unsigned int)((unsigned long)CRYSTAL/(16*(unsigned long)BAUD)-1)
#define BAUD_H (unsigned char)(BAUD_SETTING>>8)
#define BAUD_L (unsigned char)(BAUD_SETTING)
#define Beep_off PORTE|=(1<<PE0)
#define Beep_on PORTE &= 0x00
char a =0;
void delayms(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<1411;j++);
}
void port_init(void)
{
DDRE |= (1<<PE0);
PORTE |=(1<<PE0);
PORTB=0XFF;
DDRB=0X0A;//配置USART1的TX为输出,控制输出端输出1
//PORTD=0XFF;
//DDRD=0X02;//配置USART0的TX为输出
}
//串口1相关函数
void uart1_init(void)
{
UCSR1A = 0x00;
UCSR1B = 0x00;
UBRR1L=BAUD_L;
UBRR1H=BAUD_H;
UCSR1C=0x86;//(1<<URSEL1)|(1<<UCSZ11)|(1<<UCSZ01)
UCSR1A = 0x00;
UCSR1B=0x18; //(1<<RXEN1)|(1<<TXEN1) 无接受中断
}
void init_devices1(void)
{
port_init();
uart1_init();
}
void uart1_send(unsigned char i)
{
// loop_until_bit_is_set(UCSR1A,UDRE1); // Wait until the send register is empty
// UDR1 = i;
while ( !( UCSR1A & (1<<UDRE1)) ); //检测是否可以发送,UDRE=1寄存器为空;
UDR1=i;
}
void main(void)
{
unsigned char temp;
init_devices1();
DDRB |= (1<<PB1);
while(1)
{
uart1_send(0x02);
delayms(50);
}
}
|