因为需要两串口,初次接触162,才发现不是想象的那样顺。让串口接收的数据立即让另一个发出去,protues
仿真结果只有80和00,想请高手指点迷津,下面是程序:
#include"mega162.h" //7.3728MHZ-38.4K;
#define uchar unsigned char
#define uint unsigned int
uint uartdat0,uartdat1; //uartdat:串口接收,pardat并口接收 len=0,
void USART0_Transmit(uint sdat0);
void USART1_Transmit(uint sdat1);
interrupt[USART0_RXC]void usart0_rx_isr(void) //串口0接收
{
uartdat0=UDR0;
PORTA=uartdat0;USART1_Transmit(uartdat0);
}
void USART0_Transmit(uint sdat0) //串口0发出
{ while(!(UCSR0A&(1<<5)));
UDR0=sdat0;
}
interrupt[USART1_RXC]void usart1_rx_isr(void) //串口1接收
{
uartdat1=UDR1;
PORTC=uartdat1; USART0_Transmit(uartdat1);
}
void USART1_Transmit(uint sdat1) //串口1发出
{ while(!(UCSR1A&(1<<5)));
UDR1=sdat1;
}
void main(void)
{
DDRA=0xff;
PORTA=0xff;
DDRB=0x08;
PORTB=0;
DDRC=0xff;
PORTC=0xff;
DDRD=0x02;
PORTD=0;
UCSR0B = 0x00;
UCSR0A=0x00;
UCSR0B = 0x98;
// UCSRB.7=1;UCSRB.4=1;UCSRB.3=1;//允许接收中断、发送和接收
UBRR0L=11; //(fosc/16/(baud+1))%256; 11.0592MHZ-38.4k-17;11.0592-4800-143;
UBRR0H=0; //(fosc/16/(baud+1))/256; 7.3728MHZ-38.4k-11;
UCSR0C=0x86; //8位数据+1位STOP位-86;寄校验-B6
UCSR1B = 0x00;
UCSR1A=0x00;
UCSR1B = 0x98;
UBRR1L=11; //(fosc/16/(baud+1))%256; 11.0592MHZ-38.4k-17;11.0592-4800-143;
UBRR1H=0; //(fosc/16/(baud+1))/256; 7.3728MHZ-38.4k-11;
UCSR1C=0x86; //8位数据+1位STOP位-86;
#asm("sei") //SREG = 0x80; //开总中断
while(1)
{
#asm("nop");
}
}