Gorgon_Meducer 发表于 2004-11-15 20:03:56

[菜鸟贴][古董贴][求助]谁能给我一段Mage8使用串口双机通讯的例子程序,谢谢了

谁能给我一段Mage8使用串口双机通讯的例子程序,谢谢了



说明一下,我用的是icc 通讯使用USART异步模式,硬件确认连接正确。



急急急呀!谢谢了

Gorgon_Meducer 发表于 2004-11-15 20:43:52

求救啊~!!!!!!!!!!!!!!

armok 发表于 2004-11-15 21:08:13

dfgeoff 发表于 2004-11-16 08:37:10

目前我正在做一个范例,可以试试做一个双机通讯。以前做51时做过,AVR没做过。你要什么样的性能?要中断还是查询?两者分主从吗?都一直工作等终端?还是一个工作另一个一直等待?

kkyou 发表于 2004-11-16 09:59:01

我按照双龙提供的窜口程序简单实验了一个pc和avr通讯的程序,好象比51的要容易一些,等忙完手上的任务,我下一不就要全面去做avr的串口通讯了!

以前我做过51的单机,多机232和485通讯,自己还做了很多转接卡!

armok 发表于 2004-11-16 11:39:05

Gorgon_Meducer 发表于 2004-11-16 17:33:08

我自己写了一个通讯的接收模块,保证通过(使用网卡转串口的模块测试的)

#include <iom8v.h>

#include <macros.h>

#include <eeprom.h>



char data=0;

void uart0_init(void);

void usart_rcx(void);

#pragmainterrupt_handler usart_rcx:12   //USART接受中断



const char dispCode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

                                              0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x40,0x63};

const char dispBit[] = {0b0000001,0b0000010,0b00000100,0b00001000,0b00010000,0b00100000,0b01000000,0b10000000};



int time_cont =0;



#pragmainterrupt_handler timer1_ovf:9

//***************************************

void send_data(char disp_data);

void int_dvice(void);

void int_port(void);

void int_timer(void);

void display(unsigned char bitselect ,unsigned char numbers);

void timer1_ovf(void);

//***************************************

void int_dvice(void)

{

    CLI();

        int_port();

        int_timer();

       

    SEI(); //re-enable interrupts

    TIMSK|=0B00000101;

    GICR=0x40;       

   

}

void int_port(void)

{

    DDRC =0XFF;

        PORTC =0;

       

}

void int_timer(void)

{

    //定时器1部分

   TCNT1H = 0xE0;

   TCNT1L = 0xC0;

       TCCR1B = 0x01;                     //1分频,定时1ms

}

//

void display(unsigned char bitselect ,unsigned char numbers)

{

        PORTC &=0B11111101;

        //send_data(0b00000001);

        send_data(dispBit);

        send_data(dispCode);//

        //disp_buff;

        PORTC |=0B00000010;//

        //bit_select++;

        //if(bit_select ==7)

        //   bit_select =0;

}

void send_data(char disp_data)

{

   char i;

   for(i =0;i<8;i++)

   {

       if(((disp_data <<i)& 0x80) ==0)

           {

              PORTC &=0B11110111;

           }

           else

           {

              PORTC |=0B00001000;

           }

          PORTC |=0B00000100;

           PORTC &=0B11111011;

   

          

   }



}



void timer1_ovf(void)                                      //1ms中断程序,检测主程序标志

{

       char a=0;   

           TCNT1H = 0xF8;

       TCNT1L = 0xC0;

           time_cont++;

          

           if(time_cont >1000)

           {

              time_cont =0;

           }

          

             

                      display(1,data-(data/10)*10);

                   a=data/10;

                   display(2,a-(a/10)*10);

                   a=data/100;

                   display(3,a-(a/10)*10);

             

                                                      //显示数据

          

}



void uart0_init(void) //接收初始化

{

   UCSRB = 0x00;               //disable while setting baud rate

   UCSRA = 0x00;               

   UCSRC = 0x00;

   UBRR = 12;                  //set baud rate

   UCSRB = 0x90;               //开启接收中断

       UCSRC = 0xA6;               //开启USART系统

}





void main(void)

{

       DDRD=0x00;

       PORTD=0x00;

       DDRB=0xff;

       PORTB=0xff;

       uart0_init();

       int_dvice();

       SEI();

       

       while(1)

   {                }



}



void usart_rcx(void)

{

   data =UDR;

   PORTB=data;                                              

}

Gorgon_Meducer 发表于 2004-11-16 17:36:49

但是,问题是单片机发送的程序就……

以下是我写的测试部分,可惜通不过……郁闷中

#include <iom8v.h>

#include <macros.h>



char data=0;

void uart0_init(void);

void usart_send(void);



void uart0_init(void)//发送初始化

{

UCSRB = 0x00; //disable while setting baud rate

UCSRA = 0x00;

UCSRC = 0xB6;

UBRRL = 0x0C; //set baud rate lo

UBRRH = 0x00; //set baud rate hi

UCSRB = 0x08;

}

void usart_send(void)

{//USART送数据

      UCSRA |=0X40; //标志清零

      UDR=data;

}



void main(void)

{

   char a=0;

   PORTB = 0xFF;

DDRB= 0x00;

PORTC = 0x7F; //m103 output only

DDRC= 0x00;

PORTD = 0xFF;

DDRD= 0x00;

   uart0_init();

   while(1)

   {

      for(;a<256;a++)

      {

      data =a;         /*发送数据A,但是测试的时候,最好改成一个确定的数来排除干扰给我们带来的成功的喜悦…*/

      usart_send();

          while(!(UCSRA &(1 <<UDRE))); //等待发送完成的标志

        }

   }

}


-----此内容被Gorgon Meducer于2004-11-16,20:04:24编辑过

Gorgon_Meducer 发表于 2004-11-16 17:40:47

问:目前我正在做一个范例,可以试试做一个双机通讯。以前做51时做过,AVR没做过。你要什么样的性能?要中断还是查询?两者分主从吗?都一直工作等终端?还是一个工作另一个一直等待?



答:两者不分主从,通讯对等,使用中断模式,两者始终都是处于工作状态

波特率 38400 (m8内部晶震8K)

奇效验,1停止位,8位数据

Gorgon_Meducer 发表于 2004-11-16 19:58:04

问题解决:

接收端代码略(和上面一样)

发送端:

//ICC-AVR application builder : 2004-11-16 18:45:10

// Target : M8

// Crystal: 8.0000Mhz



#include <iom8v.h>

#include <macros.h>



char data =0;

intcont =0;



void port_init(void);

void uart0_init(void);

void init_devices(void);

void usart_send(void);

void int_timer(void);





#pragmainterrupt_handler timer1_ovf:9

//---------------------------------------

void port_init(void)

{

PORTB = 0xFF;

DDRB= 0xFF;

PORTC = 0x7F; //m103 output only

DDRC= 0x00;

PORTD = 0xFF;

DDRD= 0x00;

}



void uart0_init(void)

{

UCSRB = 0x00; //disable while setting baud rate

UCSRA = 0x00;

UCSRC = 0xa6;

UBRRL = 0x0C; //set baud rate lo

UBRRH = 0x00; //set baud rate hi

UCSRB = 0x08;

}

void int_timer(void)

{

    //定时器1部分

   TCNT1H = 0xe0;

   TCNT1L = 0xc0;

       TCCR1B = 0x01;                     //1分频,定时1ms

}

//call this routine to initialise all peripherals

void init_devices(void)

{

   //stop errant interrupts until set up

   CLI(); //disable all interrupts

   port_init();

   uart0_init();

   int_timer();

   TIMSK|=0B00000101;

   GICR=0x40;       

   SEI(); //re-enable interrupts

   //all peripherals are now initialised

}







void usart_send(void)

{//USART送数据

      UCSRA |=0X40; //标志清零

      UDR=data;

}



void main(void)

{

   init_devices();

   while(1)

   {

      if(cont >500)

              PORTB =0B11111111;

          else

              PORTB =0B00000000;

    }

}





//************************************************

void timer1_ovf(void)                                      //1ms中断程序,检测主程序标志

{

      

           TCNT1H = 0xe0;

       TCNT1L = 0xc0;

           cont++;

           if( cont >1000)

           {

             cont =0;

         usart_send();

                   data++;

           }       

                      //显示数据

          

}



不过有一个小小的问题:

如果将

UCSRC = 0xa6;

改为

UCSRC = 0x86;

或者改为

UCSRC = 0xB6;



即偶效验改为无效验或者奇效验,程序依然能够通过……晕啊,谁能给我解答?




-----此内容被Gorgon Meducer于2004-11-16,20:03:38编辑过

andywu_2005 发表于 2004-12-8 11:36:54

有一個雙串口的程式,采用中斷,可以參改,采用IC:AT90S85185







点击此处下载armok0114962.TXT

sznetling 发表于 2010-7-29 12:05:51

挖坟时间到,很有纪念意义的帖子,应该在第一页现身一下

sznetling 发表于 2010-9-26 04:56:24

曾经,我在错误的时间挖了你的墓,你不理我,所以我再挖一次

sznetling 发表于 2010-9-26 04:58:24

avr已经是过去式了,想问问傻孩儿对未来有什么打算,转到哪方面呢?

kingofkings 发表于 2010-9-26 09:22:36

不厚道啊,楼上
我代傻孩子回你下,最近用AVR很欢,不带这么赚钱,唔唔唔唔.......

Gorgon_Meducer 发表于 2010-9-26 13:24:32

to【14楼】 kingofkings 技术火腿(KoK)
    不要这么说,别人会误解我在炒货。……我还是建议大家早点换别家的芯片……

BLP_July 发表于 2010-10-21 15:54:02

回复【9楼】Gorgon Meducer 傻孩子
-----------------------------------------------------------------------

04年我还在上高中呢,呵呵
额~我现在也遇到同样的问题咯,还在找啊找啊找错误
看到你的老帖子心里的纠结忽然有种少了那么一点点的感觉,原来高手也是这样一步一步走过来的^_^
参考一下下呵

Gorgon_Meducer 发表于 2010-10-21 17:03:22

to 【16楼】 BLP_July
   有此共鸣~

RightZL 发表于 2010-10-21 17:07:33

菜鸟成大师了~!

BLP_July 发表于 2010-10-22 10:47:53

回复【17楼】Gorgon Meducer 傻孩子
-----------------------------------------------------------------------

呵呵 我的今天也调出来了哦,好开心
看到你的回复更开心,啦啦啦~
页: [1]
查看完整版本: [菜鸟贴][古董贴][求助]谁能给我一段Mage8使用串口双机通讯的例子程序,谢谢了