Wireless with RS232 [SOLVED]

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Bonca
Posts: 319
Joined: 18 Mar 2009 07:55

Wireless with RS232 [SOLVED]

#1 Post by Bonca » 07 Jan 2011 21:33

Hi,
I am working on a simple data streaming. The problem is on the receiver side. I connected a led to be indicated if two data bytes and their checksum are ok. I have checked the digital signal on receiver side and the picture shows that incoming signals are ok too. Yellow is the transmitter digital input, blue is the receiver digital output.
Clipboard01.jpg
Clipboard01.jpg (46.56 KiB) Viewed 6266 times
This is the transmitter code (12F683):

Code: Select all

Soft_UART_Init(&GPIO, 4, 5, 1200, 0);       // receiver data rate fmax=2kHz => 1200
    delay_ms(1000);

    while(1){

        Soft_UART_Write(0xAA);    // dummy
        delay_ms(10);             // after 30ms receiver output goes to low (???)
        Soft_UART_Write(0x55);
        delay_ms(10);
        Soft_UART_Write(0xAA);
        delay_ms(10);
        Soft_UART_Write(0x55);
        delay_ms(10);
        Soft_UART_Write(0x05);    // start_byte
        delay_ms(10);
        Soft_UART_Write(0x03);    // data_byte1
        delay_ms(10);
        Soft_UART_Write(0x04);    // data_byte2
        delay_ms(10);
        Soft_UART_Write(0x07);    // checksum_byte = data_byte1 + data_byte2
        delay_ms(10);

        GPIO.F2 = ~GPIO.F2;       // indicates end of cycle
    }
This is the receiver code (18F2520):

Code: Select all

UART1_Init(1200);
    delay_ms(100);

    while(1){
        while(start_byte != 0x05){               // until 0x05 comes...
            if(UART1_Data_Ready() == 1){         // start_byte
                start_byte = UART1_Read();
            }
        }

        if(UART1_Data_Ready() == 1){             // data_byte1
            data_byte1 = UART1_Read();
        }
       
        if(UART1_Data_Ready() == 1){             // data_byte2
            data_byte2 = UART1_Read();
        }
       
        if(UART1_Data_Ready() == 1){             // checksum_byte
            checksum_byte = UART1_Read();
        }
       
        if(checksum_byte == (data_byte1 + data_byte2)){    // simple error checking
            LATB.F0 = ~LATB.F0;                  // indicates if no error in data transfer
        }

        start_byte = 0;
    }
drdoug has already told me an idea but my breadboard is not at home to try it.
I think you are just moving through your code and not reading any new data. Change your

Code: Select all

if(uart1 ready)
To

Code: Select all

while(!uart1 ready)
This will create a blocking code but can be a next step in your project. There are still other issues to resolve as well. Ultimately it will be better to use interrupts.
The problem is that receiver led always changes its state, no matter if error checking good or not. What is wrong with the code? I use mikroC PRO for PIC 4.60. Any help appreciated.

Bonca
Last edited by Bonca on 10 Jan 2011 21:26, edited 1 time in total.

Nick101
Posts: 339
Joined: 06 Aug 2006 15:32
Location: Texas
Contact:

Re: Wireless with RS232

#2 Post by Nick101 » 08 Jan 2011 00:28

Hi,
I suggest using interrupt on the receiver or follow drdoug advice using while(!UART1_Data_Ready()). Because there are 10 ms delay each sent byte while if(UART1_Data_Ready()) is not wait for data. Even if no delay on sender, data might not ready to be read on the receiver.
Nick
[url]http://www.pic_examples.byethost3.com/[/url]

Bonca
Posts: 319
Joined: 18 Mar 2009 07:55

Re: Wireless with RS232

#3 Post by Bonca » 08 Jan 2011 08:49

Hi Nick,
should I use az interrupt function or put while(!UART1_Data_Ready()) in the main function? Without 10 ms delay among bytes on transmitter side, there is same result: LATB.F0 = ~LATB.F0; always performed, and this is what I can't understand. Both controller is runnung on 4MHz. Checking the signals on scope, RF link is ok and it seems that no noise in the byte stream. I guess there is problem with receiver's code.

Bonca

Nick101
Posts: 339
Joined: 06 Aug 2006 15:32
Location: Texas
Contact:

Re: Wireless with RS232

#4 Post by Nick101 » 08 Jan 2011 16:35

Hi,
This is untest code using interrupt on the receiver. Assume sender always send start byte (0x05) follow by 2 data byte and checksum byte.

Code: Select all

char data_byte1,data_byte2,checksum_byte;
char started, count;
void interrupt(){
     char buffer;
     if (PIR1.RCIF) {
         buffer = RCREG;        //read from rcreg register
         if(started){
            count++;
            if(count == 1) data_byte1 = buffer;
            if(count == 2) data_byte2 = buffer;
            if(count == 3) {
               checksum_byte = buffer;
               started = 0;     //reset started flag
               if(checksum_byte == (data_byte1 + data_byte2)){    // simple error checking
                 LATB.F0 = ~LATB.F0;                  // indicates if no error in data transfer
               }


            }
         }else{
           if(buffer == 0x05) {
               started = 1;  //set started flag
               count = 0;
           }
         
         }
     }

}

void main(){
     UART1_Init(1200);
     delay_ms(100);
     PIE1.RCIE=1;    //enable receive interrupt
     INTCON.PEIE = 1;
     INTCON.GIE = 1;

     while(1);

}
Nick
[url]http://www.pic_examples.byethost3.com/[/url]

Bonca
Posts: 319
Joined: 18 Mar 2009 07:55

Re: Wireless with RS232

#5 Post by Bonca » 08 Jan 2011 20:30

Hi Nick,
it perfectly works in simulator! :)
I didn't know this trick: reading EUSART buffer and using its interrupt instead of using mE USART library. Ultimately I don't know why UART commands can't handle with same result. Maybe I misunderstood how these commands work.
The next step is I will try on real hardware on Monday. I thank you and drdoug!

Bonca

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: Wireless with RS232

#6 Post by drdoug » 09 Jan 2011 00:54

It can take several ms to TX/rx a single byte. Your chip can process other thongs (100s) while the receiveprocess works in the background.
Also when your system hits the real world you will need a preamble or something to know you have actually received the start bit and not some random noise.

Bonca
Posts: 319
Joined: 18 Mar 2009 07:55

Re: Wireless with RS232

#7 Post by Bonca » 10 Jan 2011 10:42

Hi,
I would like to report very good and stable results. The receiver finds the transmitter's signal. If the receiver catches signals from an unknown source (e.g. car remote), data transmission will be established again.
Thank you again for the trick of RCREG and interrupt both of you ;) This was the key of the solution.

I am expecting mE's opinion how is possible with UART1_Data_Ready() and UART1_Read() to get same result.

Bonca

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: Wireless with RS232

#8 Post by drdoug » 10 Jan 2011 14:12

You can modify Nick101s code to without the interrupt like this but I would use the interrupt. I think it would help you to read the UART section in the data sheet for a better understanding. Also, you may find Serial Port Complete helpful by Axelson.
UNTESTED...I think the {} need to be cleaned up.

Code: Select all

char data_byte1,data_byte2,checksum_byte;
char started, count;
char buffer;



}

void main(){
     UART1_Init(1200);
     delay_ms(100);
     PIE1.RCIE=1;    //enable receive interrupt
     INTCON.PEIE = 1;
     INTCON.GIE = 1;

     while(1) {
     if (UART1_Data_Ready) { // was (PIR1.RCIF)
         buffer = UART1_Read();        //read from rcreg register
         if(started){
            count++;
            if(count == 1) data_byte1 = buffer;
            if(count == 2) data_byte2 = buffer;
            if(count == 3) {
               checksum_byte = buffer;
               started = 0;     //reset started flag
               if(checksum_byte == (data_byte1 + data_byte2)){    // simple error checking
                 LATB.F0 = ~LATB.F0;                  // indicates if no error in data transfer
               } // end  if(count == 3) 


            } // end  if(started)
         }else{
           if(buffer == 0x05) {
               started = 1;  //set started flag
               count = 0;
           } // end if(buffer == 0x05) 
         
       } // end else
      } // end if (UART1_Data_Ready) 
    }   / end while(1)

} // end main()

faizanmasood
Posts: 28
Joined: 29 Feb 2012 21:33

Re: Wireless with RS232 [SOLVED]

#9 Post by faizanmasood » 29 Jun 2012 11:52

Hello.. Can i use above both tx and rx codes with pic18f452... same code as it is given? or have to change something.. i mean in rx coding about interrupt??

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

Re: Wireless with RS232 [SOLVED]

#10 Post by drdoug » 29 Jun 2012 12:08

You need to verify the registers are correct for whatever chip you use.

Code: Select all

     PIE1.RCIE=1;    //enable receive interrupt
     INTCON.PEIE = 1;
     INTCON.GIE = 1;
as well as

Code: Select all

                 LATB.F0 = ~LATB.F0;                  // indicates if no error in data transfer
since some chips do not use LAT.xx

but otherwise it should work.
Just try it, the compiler will guide you.

Post Reply

Return to “mikroC PRO for PIC General”