Author Topic: serial data transfer code (program)  (Read 1143 times)

0 Members and 1 Guest are viewing this topic.

Offline yalectTopic starter

  • Regular Contributor
  • *
  • Posts: 141
serial data transfer code (program)
« on: March 13, 2018, 10:43:33 am »
Hello,
I would like to ask you that I used this two follow program to send and recive DATA serially  via port C where I use start_byte (8bit) to begin the DATA and parity byte (8bit) to check the DATA recieved is right ( maybe the parity byte is enought) where I get the parity byte right but the data recieved not correct , I won't chang my code but if there is small modifucations?
thank you

Transmitter :

void main() {
void init();

   int info=0b10010110, byte_start = 0b10110010, compare = 0b00000001, count, Parity=0b11001000, DATA=0b0, byte_end=0b11010010;

/* Configure the port C as output */
    DDRC = 0xff;


        while (DATA!=byte_end)/*!= byte_start)*/
       {
    for (count = 0; count < 8; count++)
    {
      DATA = (byte_start >> count);  // output = number shifted right * count

      if(DATA & compare)
     {
     PORTC = (1<< PINC0);
     }
     else
    {
     PORTC&= ~(1<< PINC0);
     }
     Delay_ms(1);
     }
        DATA=0;
         for (count = 0; count < 8; count++)
    {
      DATA = (info >> count);  // output = number shifted right * count

      if(DATA & compare)
     {
     PORTC = (1<< PINC0);
     }
     else
    {
     PORTC&= ~(1<< PINC0);
     }
     Delay_ms(1);
     }
         for (count = 0; count < 8; count++)
      {
       DATA = (Parity >> count);  // output = number shifted right * count
      /*PORTC = DATA; */
      if(DATA & compare)
     {
     PORTC = (1<< PINC0);
     }
     else
    {
     PORTC&= ~(1<< PINC0);
     }
     Delay_ms(1);
     }

      DDRC=0x00;
   for (count = 0; count < 8;  count++)/*; count++)*/
     {
     if(PINC & (1<<PINC0))
     {
     PORTD= (1<< PIND3);
     DATA|=(1<<count);
     }
     else
    {
     DATA &= ~(1<<count);
     }

      Delay_ms(1);

}

}
 }
**************************************************************************************************************************
Reciever   


void main() {
void init();


int info=0b10010110, byte_start =  0b10110010, count=0x00, Parity=0b11001000, DATA=0b0, byte_end=0b11010010;


     DDRC = 0x00;
     DDRA = 0xff;

          while(DATA!=byte_start)
       {
      for (count = 0; count < 8;  count++)/*; count++)*/
     {
     if(PINC & (1<<PINC0))
     {
    DATA|=(1<<count);
     }
     else
    {
     DATA &= ~(1<<count);
     }

     Delay_us(500);

}
     }
     while (DATA!=Parity)
        {
    for (count = 0; count < 8;  count++)/*; count++)*/
     {
     if(PINC & (1<<PINC0))
     {
      DATA|=(1<<count);
     }
     else
    {
     DATA &= ~(1<<count);
     }

     Delay_us(500);

 }
     PORTA=DATA;
     for (count = 0; count < 8;  count++)/*; count++)*/
     {
     if(PINC & (1<<PINC0))
     {
    DATA|=(1<<count);
     }
     else
    {
     DATA &= ~(1<<count);
     }

     Delay_us(500);

        }

         }

     DDRC = 0xff;
for (count = 0; count < 8; count++)
    {
      DATA = (byte_end >> count);  // output = number shifted right * count

      if(DATA & 1)
     {
     PORTC = (1<< PINC0);
     }
     else
    {
     PORTC&= ~(1<< PINC0);
     }
     Delay_ms(1);
     }

                     }
 

Offline Buriedcode

  • Super Contributor
  • ***
  • Posts: 1682
  • Country: gb
Re: serial data transfer code (program)
« Reply #1 on: March 14, 2018, 10:25:45 pm »
Please use the code tags
Code: [Select]
as this keeps the formatting of indentations and makes it much more readable.

 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2417
  • Country: us
Re: serial data transfer code (program)
« Reply #2 on: March 14, 2018, 11:12:03 pm »
The receive process, once synchronized by a start BIT, must receive at the SAME rate as the transmitter is sending. You are not doing this.
https://en.wikipedia.org/wiki/Asynchronous_serial_communication

Here's a tutorial on how to implement software serial:
https://www.arduino.cc/en/Tutorial/SoftwareSerial



« Last Edit: March 14, 2018, 11:18:50 pm by Nusa »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf