老师:能帮我看看我的多机通信程序错在哪了吗?谢谢
/*测试MPCM 收9位中断方式 第9位为0 = PORTD=6闪灯,910=d4,911=d5 atmega81,9,1格式*//*用的是proteus仿真,结果是能区分第九位为0,1,以及点名是否匹配,问题是无论第九位是0还是1,都响应中断*/
#include <iom8v.h>
#include <macros.h>
static void io_init(void)
{
PORTB = 0x0;
DDRB = 0x0;
PORTC = 0x0;
DDRC = 0x7F;
PORTD = 0x0;
DDRD = 0xfa;
}
void usart_s(unsigned char baud)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x21;
UCSRC=0x86;
UBRRL = 0x19;//(unsigned int) (baud);//1M晶振,2400波特率
UBRRH = 0x00;//(unsigned int ) (baud>>8);
UCSRB=0x9c;
}
#pragma interrupt_handler usart_r:12
void usart_r(void)
{
unsigned char r,r1,r2;
unsigned int r3,name=0x44;
CLI();
r=UDR;
PORTC^=0x02;//pc.1为中断标志位
if (r2==1)
{
if (r==name)
{
PORTD=((PORTD&0x0F)|0xa0);
UCSRA &=(~(1<<MPCM));
UCSRB &=(~(1<<RXB8));
}
else PORTD=((PORTD&0x0F)|0x90);
}
else PORTD=((PORTD&0x0F)|0x40);
SEI();
}
void main(void)
{
unsigned char baud=25;//1M晶振,2400波特率
int k;
CLI();
io_init( );
usart_s(baud);
PORTD=0;
PORTC=0;
SEI();
for ( ; ; )
{
PORTC^=0x08;//pc.3为主程序标志位
for (k=1;k<0xefff;k++)
{
;
}
}
} 先参考用ICC向导生成的初始化程序试试(仅接收),
//ICC-AVR application builder : 2005-03-26 18:29:23
// Target : M8
// Crystal: 1.0000Mhz
#include <iom8v.h>
#include <macros.h>
void port_init(void)
{
PORTB = 0x00;
DDRB= 0x00;
PORTC = 0x00; //m103 output only
DDRC= 0x00;
PORTD = 0x00;
DDRD= 0x00;
}
//UART0 initialize
// desired baud rate: 2400
// actual: baud rate:2404 (0.2%)
// char size: 9 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x01;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x19; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x94;
}
#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
//uart has received a character in UDR
}
//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
} 老师:这是发送方部分,能帮我看看有没有错误吗?谢谢老师!
/*发9位*/
#include <iom8v.h>
#include <macros.h>
static void io_init(void)
{
PORTB = 0x0;
DDRB = 0x0;
PORTC = 0x0;
DDRC = 0x0f;
PORTD = 0xff;
DDRD = 0x0;
}
void usart_s(unsigned char baud)
{
UBRRH = (unsigned int ) (baud>>8);
UBRRL = (unsigned int) (baud);
//UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE)|(1<<UCSZ2);
//UCSRC = (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
UCSRA=0X00;
UCSRB=0x9C;
UCSRC=0x86;
}
void main(void)
{
unsigned char baud=25,j0,j1,j2;
int i,j;
//CLI();
io_init( );
usart_s(baud);
MCUCR = 0x00;
GICR= 0x00;
TIMSK = 0x00;
//SEI();
PORTC=0x0;
for ( ; ; )
{
//j0=PORTD;
//j1=((j0>>7)&0x01);
j1=0;//NO.9
j2=0x55;
while (!(UCSRA & (1<<UDRE)) )
{
;//当udre为1时
}
UCSRB &= 0xfe;
PORTC^=0x04;
if (j1==1)
{
UCSRB|=(1<<TXB8);//if NO.9=1,set bit
PORTC|=0x08;//pc.3 is NO.9
}
UDR=j2;
for (i=0;i<0xffff;i++)
{
;
}
}
} 老师:这是我的新测试程序,仿真时还是响应中断,请问可能是什么原因?
我对proteus和MEGA8都不熟,会不会不一定是程序本身的问题?谢谢老师!
#include <iom8v.h>
#include <macros.h>
void port_init(void)
{
PORTB = 0x00;
DDRB= 0x0F;
PORTC = 0x00; //m103 output only
DDRC= 0x00;
PORTD = 0x00;
DDRD= 0x00;
}
//UART0 initialize
// desired baud rate: 2400
// actual: baud rate:2404 (0.2%)
// char size: 9 bit
// parity: Disabled
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x01;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x19; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x94;
}
#pragma interrupt_handler uart0_rx_isr:12
void uart0_rx_isr(void)
{
//uart has received a character in UDR
PORTB^=0x02;// take pb.1 as a interrupt sign
}
//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 (void)
{
int i;
while (1)
{
init_devices();
PORTB^=0x01;//take pb.0 as a program sign
for (i=0;i<0x0fff;i++)
{
;
}
}
} 我觉得最好搭建硬件电路来调试,这样才容易找出问题之所在,软件仿真不可靠 手头没有mega8,只有两块mega48,小改了一下,烧片子试了试,正常。
该中断的中断,不该中断的~~不中断了!总之,正常。
倒!
试了试查询方式,倒是不断查得到值。
老师,对数据帧的过滤不是由硬件完成的吗?只有中断方式有效还是我的程序有问题呢? 已经是“该中断的中断,不该中断的~~不中断了!总之,正常。”了,说明MCU对数据帧进行过滤了,数据帧不会形成中断了(即RCX不会置1)。
你的查询方式是查询什么?如何查询。
另外,多机通信不建议使用查询方式。
页:
[1]