admvip 发表于 2010-1-24 16:43:10

求助一个AVR M16串口中断的问题

编一个小程序需要与计算机通过串口联系,由于采用串口查询接收比较浪费机器资源,所以就采用了串口接收中断的方式,按M16手册查询了寄存器的设置,编了一段小程序验证,问题来了,无论如何也收不到计算机发送的数据,验证程序设计电脑发送0x43,接收中断判断出是0x43后,点亮PB口的LED,郁闷的是怎么也无法实现,把程序发上来,高手给分析一下,看看错在哪里。 谢谢大家!

解释一下:速率4800 8数据位 1停止位 倍速通信(非倍速也试了,不行) 单片机时钟8M

//ICC-AVR application builder : 2010-1-24 16:24:18
// Target : M16
// Crystal: 8.0000Mhz

#include <iom16v.h>
#include <macros.h>

void port_init(void)
{
PORTA = 0x7F;
DDRA= 0x00;
PORTB = 0x00;
DDRB= 0xFF;
PORTC = 0x00; //m103 output only
DDRC= 0x00;
PORTD = 0x00;
DDRD= 0x00;
}

//UART0 initialize
// desired baud rate: 4800
// actual: baud rate:4808 (0.2%)
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x02;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0xCF; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x98;
}

#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
//uart has received a character in UDR
unsigned char temp;
temp = UDR;
if (temp == 0x43)
   {
    PORTB = 0x55;
   }
//else PORTB = 0;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();

MCUCR = 0x00;
GICR= 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

void main ()
{
init_devices();

while (1)
{
   
}

}

cumt_123456 发表于 2012-11-5 23:07:16

PD0,PD1没有配置好。
反正PD1应该配置输出。
PD0输入输出都可以,简单测试了一下,实际使用时自行验证哈。

admvip 发表于 2012-11-25 18:26:40

谢谢你的回复!
已经搞定了!
页: [1]
查看完整版本: 求助一个AVR M16串口中断的问题