atmega128串口1能发送数据,不能进入接收中断??
用proteus仿真atmega128串口1能发送数据,但是串口助手发送过来的数据接收不到,不能进入接收中断,大家帮我看看问题出在哪?谢谢!下面是代码#include <avr/io.h>
#include <avr/interrupt.h>
#define BAUD 9600 //波特率采用9600bps
#define CRYSTAL 8000000 //系统时钟4MHz
//计算和定义波特率设置参数
#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 FRAMING_ERROR (1<<FE1)
#define PARITY_ERROR (1<<UPE1)
#define DATA_OVERRUN (1<<DOR1)
#define DATA_REGISTER_EMPTY (1<<UDRE1)//USART 数据寄存器空
void USART_Transmit(unsigned char data)
{
while (!(UCSR1A & DATA_REGISTER_EMPTY)); // 等待发送寄存器空UDREn=1
UDR1 = data; // 发送数据
}
ISR(USART1_RX_vect)// USART接收中断服务
{
unsigned char status,data;
status = UCSR1A;
data = UDR1;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)//收到后,发回.
USART_Transmit(data);
}
int main( void )
{
cli();
DDRD |= _BV(PD3);//TXD(PD3)输出
PORTD |= 0x0c;// RXD(PD2)输入,上拉有效
UCSR1B = 0x00; //设置波特率时USART停止工作
UCSR1A = 0x00; // USART初始化
UCSR1C =0x00;
UBRR1H = BAUD_H; // 设置波特率
UBRR1L = BAUD_L;
UCSR1C = (1<<UCSZ11)|(1<<UCSZ10); // 8 Data, 1 Stop, No Parity
UCSR1B = (1<<RXCIE1)|(1<<RXEN1)|(1<<TXEN1); // 允许RXC中断,接收允许,发送允许
sei(); // 开中断
USART_Transmit(0x31);
for(;;)
{
}
} #include <avr/io.h>
#include <avr/interrupt.h>
#define uchar unsigned char
#define BAUD 9600 //波特率采用9600bps
#define CRYSTAL 8000000 //系统时钟4MHz
//计算和定义波特率设置参数
#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 FRAMING_ERROR (1<<FE1)
#define PARITY_ERROR (1<<UPE1)
#define DATA_OVERRUN (1<<DOR1)
#define DATA_REGISTER_EMPTY (1<<UDRE1)//USART 数据寄存器空
void USART_Transmit(unsigned char data)
{
while (!(UCSR1A & DATA_REGISTER_EMPTY)); // 等待发送寄存器空UDREn=1
UDR1 = data; // 发送数据
}
SIGNAL(USART1_RX_vect)// USART接收中断服务
{
unsigned char status,data;
status = UCSR1A;
data = UDR1;
PORTB = data;
//if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)//收到后,发回.
//USART_Transmit(data);
UDR1 = data;
}
int main( void )
{
uchar i;
DDRB = 0xFF;
cli();
DDRD |= _BV(PD3);//TXD(PD3)输出
PORTD |= 0x0c;// RXD(PD2)输入,上拉有效
//UCSR1B = 0x00; //设置波特率时USART停止工作
//UCSR1A = 0x00; // USART初始化
//UCSR1C =0x00;
UBRR1H = BAUD_H; // 设置波特率
UBRR1L = BAUD_L;
UCSR1A &= ~_BV(7);
UCSR1C = (1<<UCSZ11)|(1<<UCSZ10); // 8 Data, 1 Stop, No Parity
UCSR1B = (1<<RXCIE1)|(1<<RXEN1)|(1<<TXEN1); // 允许RXC中断,接收允许,发送允许
sei(); // 开中断
for(i=0;i<10;i++)
{
USART_Transmit(0x31+i);
}
for(;;)
{
}
return 0;
}
恩 这样就可以了 中断接收数据并把接收到的数据再发送回去 谢谢楼上的回复,原因是proteus里atmega128模型的问题,QFP64-14X14这个封装的就没有问题,TQFP64-12X12MM这个封装的就是进不了中断,硬件我也试了没有问题,所以说有时候还是要硬件调试…… 哥们儿,我也在用m128的串口,看到你说问题解决了,能否留个qq
页:
[1]