搜索
bottom↓
回复: 6

老师:能帮我看看我的多机通信程序错在哪了吗?谢谢

[复制链接]

出0入0汤圆

发表于 2005-3-26 16:56:45 | 显示全部楼层 |阅读模式
/*测试MPCM 收9位  中断方式 第9位为0 = PORTD=6闪灯,910=d4,911=d5 atmega8  1,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++)

   {

   ;

   }

  }

}

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

发表于 2005-3-26 18:28:11 | 显示全部楼层
先参考用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

}

出0入0汤圆

 楼主| 发表于 2005-3-26 20:30:32 | 显示全部楼层
老师:这是发送方部分,能帮我看看有没有错误吗?谢谢老师!

/*发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++)

        {

        ;

        }

  }

}

出0入0汤圆

 楼主| 发表于 2005-3-26 20:32:34 | 显示全部楼层
老师:这是我的新测试程序,仿真时还是响应中断,请问可能是什么原因?

我对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++)

   {

   ;

   }

  }

}

出0入0汤圆

发表于 2005-3-28 13:35:27 | 显示全部楼层
我觉得最好搭建硬件电路来调试,这样才容易找出问题之所在,软件仿真不可靠

出0入0汤圆

 楼主| 发表于 2005-3-30 21:06:54 | 显示全部楼层
手头没有mega8,只有两块mega48,小改了一下,烧片子试了试,正常。

该中断的中断,不该中断的~~不中断了!总之,正常。

倒!

试了试查询方式,倒是不断查得到值。

老师,对数据帧的过滤不是由硬件完成的吗?只有中断方式有效还是我的程序有问题呢?

出0入0汤圆

发表于 2005-3-30 22:39:18 | 显示全部楼层
已经是“该中断的中断,不该中断的~~不中断了!总之,正常。”了,说明MCU对数据帧进行过滤了,数据帧不会形成中断了(即RCX不会置1)。

   

    你的查询方式是查询什么?如何查询。



    另外,多机通信不建议使用查询方式。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-18 16:36

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表