try 2 send n receive nrz ax 25 frame

Help
p1245
2005-07-15
2013-03-12
  • p1245
    p1245
    2005-07-15

    I need help, I appriaciate if any one could help me
    I try to send & receive NRZ ax.25 frame between  two AT89s52 via pin data (I used P1_2)
    I adapt the routine from John Hansen article, which use PIC 16 and CCS compiler.
    So I adapt his code to SDCC, but the result is very unstable.
    I predict the problem is in 'receive' routine ,but a month I try to solve this problem
    I didn't make it.I'm very sure with 'send' routine but not with 'receive' one.
    I want to examine this 'send & receive' system with logic analyzer, but I dont have one,
    I only used Osciloscope. Help me plzzzzz,( here below the send & receive routine , I wrote)*/

    /*##############################################################################
    ################################################################################
         try 2 receive frame through pin P1_2 with 1200 bps rate
        n the received frame send 2 hyperterminal via serial
    ##############################################################################
    ###############################################################################*/

    #include <at89x52.h>
    #include <hser_ir.h>

    unsigned char buffer[15];
    unsigned char bitcome(void);
    unsigned char shift2r(unsigned char chr,unsigned char bitin);
    void Loop_Delay(const unsigned int DELAY);

    unsigned char input(void);

    unsigned char cbyte;

    main() {
    unsigned char i;
    unsigned char test,newbit,indxbyte,ones;   
    hser_init();
    P1_2=1;
       while(1) {
        P1_0=1;
        hser_puts((unsigned char *)"\r\n start receiving frame");
        while (cbyte!=0x7e){
            newbit=bitcome();
            cbyte=shift2r(cbyte,newbit);PutASCII(cbyte);
            }
        P1_0=0; //turn the light, flag has been captured
           
        /*while (cbyte == 0x7e){              //find the other flags
            for(i=0;i<8;i++)           
            {
                newbit=bitcome();
                cbyte=shift2r(cbyte,newbit); }   
               
                               
        }*/   
                       
        //buffer[0] = cbyte;
        //PutASCII(buffer[0]);
            test=ones=0;
        indxbyte=0;
        while (test != 1)
          {        
            for(i=0;i<8;i++)
                {       
                   newbit = bitcome();
                   if (newbit == 1) ones++;
                      else ones = 0;
                   if (ones == 5)
                       {
                    test = bitcome();
                    ones = 0;
                    }
                   cbyte = shift2r(cbyte,newbit);
                }
            if (test == 0)
              {
               buffer[indxbyte++] = cbyte;
               buffer[indxbyte]='\0';
              }
          }
       
        buffer[indxbyte]='\0';
       
        hser_puts((unsigned char *)"\r\n Received frame:");
        hser_puts(buffer);
        hser_put_ret(1);
        PutASCIIs(buffer);
        if(P1_2)
            hser_puts((unsigned char *)"Last:P1.2 HIGH");
              else
            hser_puts((unsigned char *)"Last:P1.2 LOW");
        } 
    }

    unsigned char bitcome(void)  //lets read bit by bit
    {
    static unsigned char oldstate;
    unsigned char k;

        for (k=0;k<34;k++){ //almost 34
               if (input() != oldstate){
            oldstate = input();
            Loop_Delay(1);
            return 0;
                }
           }
        return 1;
    }

    void Loop_Delay(const unsigned int DELAY)
    {
        unsigned int x,y;
        for (x=0;x<=DELAY ;x++ )
            for (y=0;y<=13 ;y++ );//almost 13
    }

    unsigned char input(void)
    {
         if(!P1_2) return 0;
        
         return 1;   
        
    }

    unsigned char shift2r(unsigned char chr, unsigned char bitin)
    {
         unsigned char a;
         a=chr>>1;
        //b=bitin&0x01;
        if (bitin) a|=0x80;
       
        return(a);
           
    }

    /*##############################################################################
    ################################################################################
            try to send frame through pin P1_2 ,that written via Hyperterminal
    ################################################################################
    ################################################################################*/
    #include <at89x52.h>
    #include <hser_ir.h>
    #include <ctype.h>
    #include <string.h>
    #define false 0
    #define true 1

    #define MAX_frame_LEN 15
    #define NULL 0
    char cmdbuf [1 + MAX_frame_LEN];     
    unsigned char cmdndx,stuff;
    char flag;

    char * frame_scan (void);
    void cmdb_init (void);
    void sendFrame(unsigned char *frame);
    void sendByte(unsigned char inchar);
    void flip(void);
    void Loop_Delay(const unsigned int DELAY);

    main() {
    char *cmd;  
    hser_init();

    while (1)
    {
       cmdb_init ();
      hser_puts((unsigned char *)"\r\n Send frame:");
    // cmd = frame_scan ();
     
    for (cmd = NULL; cmd == NULL; cmd = frame_scan ())   //get command
      {
       //hser_putc('y');
      }           
        sendFrame(cmdbuf);
        hser_puts((unsigned char *)"frame have been sent");
        hser_put_ret(1);
        PutASCIIs(cmdbuf);
        if(P1_2)
            hser_puts((unsigned char *)"Last:P1.2 HIGH");
          else
            hser_puts((unsigned char *)"Last:P1.2 LOW");     
      }
    }

    char * frame_scan (void) //input from hyperterminal
    {
      unsigned char c;

      while (1)
        {
          if (!ser_charAvail())        //No character
          break;                                          //Exit NOW
     
          c = hser_getc();
          hser_putc(c);
          //cmdbuf [cmdndx] = c;
          //cmdndx++;
          //cmdbuf [cmdndx]   = '\0';
          if (c == '\r')                                    //Newline?
            {
              hser_puts((unsigned char *)"\r\n");                         //Output it and
              return (cmdbuf);                              //Return the buffer address
            }

          if ((c == '\b') && (cmdndx != 0))                 //Backspace?
            {
              hser_puts((unsigned char *)"\b");
              cmdbuf [--cmdndx] = '\0';
              continue;
            }

          if (!isprint (c))                                 //Printable character?
            {
              BEEPCHAR:
              continue;
            }

          if (cmdndx >= MAX_frame_LEN)                        //Past buffer length?
            goto BEEPCHAR;
          
          cmdbuf [cmdndx++] = c;
          //cmdndx++;
          cmdbuf [cmdndx]   = '\0'; //to make sure the next buffer space is empty
         }
      return (NULL);
    }

    void cmdb_init (void)
    {
    cmdndx = 0;
    cmdbuf [0] = '\0';
    }

    void sendFrame(unsigned char *frame)
    {
         unsigned char c;
         flag=true;
           sendByte(0x7E);   
       
        flag=false;
        while(c=*frame++) {
         
           sendByte(c);
          
             }
            flag=true;
        sendByte(0x7E);   
       
        flag=false;
    }

    void sendByte(unsigned char inchar)
    {
        unsigned char k,bt;
        for(k=0;k<8;k++)
        {
          bt=inchar&0x01;
          if(!bt)flip();
             else
             {
              stuff++;
              if((stuff==5)&&(flag==false))
               {
                 Loop_Delay(2);
                 flip();
               }
             }   
          inchar>>=1;
          Loop_Delay(2);       
        }
       
    }

    void flip(void) // it's make NRZ signal
    {
         stuff=0;
         if (!P1_2)
         P1_2=1;
         else
         P1_2=0;
        
        }
       
       
    void Loop_Delay(const unsigned int DELAY) //this is delay for 833 us for reach 1200 bps rate
    {
        unsigned int x,y;
        for (x=0;x<=DELAY ;x++ )
            for (y=0;y<=34 ;y++ );
           
    }