Pic to Pic Usart

General discussion on mikroC.
Author
Message
Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#16 Post by Tanky321 » 20 Aug 2009 19:21

I know its been a while since ive posted here, but ive finally done some more work on the receiver end.

I included the preamble in the transmitter, and I think im on the right track on the receiver but im not sure.

Can anyone shed some light? It compiles fine, but it doesnt work what so ever. PortA should be high initially, but thats not happening?


TX Code:

Code: Select all

/*This Program recevies a USART signal via RF
from a handheld remote, that is then translated
to a binary output on PORTA

 8MHz int. Osc

*/





void main() {
CMCON = 7;             // turns of the analog comparators on the PORTA pins
                       // ensures pins are reading digital
TRISA = 0xFF;          // set PORTA to be input

// Initialize USART module
Usart_Init(4800);

  do {
     Usart_write(49);
     Usart_write(49);
     Usart_write(65);
     Usart_write(PORTA);
     Usart_write(66);
     Usart_write(35);
     delay_ms(5);
  } while (1);
}//~!

Receiver:

Code: Select all






unsigned short i, rxdata[3];

unsigned int rxnum;



void main() {
INTCON.GIE = 0;                   // Disable interrupts

cmcon = 7;                        //Declare Variables
VRCON = 0;
PORTA = PORTB.F4 = PORTB.F5 = 1;  //Init. PORTA
PORTB = 0;                        //& PORTB
TRISA = 0x00;
TRISB = 0x00;
TRISB.F1 = 1;
rxnum = 0;

Usart_Init(2400);                  //Establish USART at 2400bps


while(1){

 if(Usart_Data_Ready){

     i = Usart_Read();
  }
  
 if( i == 65){
 
  while( rxnum < 3){
          if(Usart_Data_Ready){

               rxdata[rxnum] = Usart_Read();
  rxnum++;
  }
   if(rxdata[1] == 66 && rxdata[2] == 35){
   
     PORTA = rxdata[0];}
 }}


 if ( PORTA == 239) {
      PORTB.F4 = 0;
     }

   if ( PORTA == 223) {
      PORTB.F5 = 0;
     }
   Delay_ms(100);
   PORTA = 0XFF;
   PORTB.F4 = PORTB.F5 = 1;




}
}

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

#17 Post by drdoug » 20 Aug 2009 19:42

One quick observation is the baud rates on each 2400 and 4800. Unless you have an oscillator on the uC set up wrong this won't work.

Also, make sure to always initialize variables (and arrays). This may give you strange results.

The Delay_ms(100) may also cause you to miss some data. Maybe add the delay after turning off the PORTB lights(?) which is what I think the intent is.

What happens when you connect the tx and rx to a computer?

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#18 Post by Tanky321 » 20 Aug 2009 20:41

I made the modifications....Doh! But im still having the same issues.

When I connect the Tx to the Usart terminal I get continuous streams of strange characters. When I connect the Rx and try to send numbers I get nothing.

I think the Tx is working because I hooked up the output to a scope and it seems correct, as I press a button I get a change in the output wave form.


Im still at a loss, and its driving me insane. Id pull out my hair if I had any!


Thanks!

New Rx:

Code: Select all

unsigned short i, rxdata[3];

unsigned int rxnum;



void main() {
INTCON.GIE = 0;                   // Disable interrupts

cmcon = 7;                        //Declare Variables
VRCON = 0;
PORTA = PORTB.F4 = PORTB.F5 = 1;  //Init. PORTA
PORTB = 0;                        //& PORTB
TRISA = 0x00;
TRISB = 0x00;
TRISB.F1 = 1;
rxnum = 0;
i=0;
rxdata[0] = rxdata[1] = rxdata[2] = rxdata[3] = 0;

Usart_Init(4800);                  //Establish USART at 4800bps


while(1){

 if(Usart_Data_Ready){

     i = Usart_Read();
  }
  
 if( i == 65){
 
  while( rxnum < 3){
          if(Usart_Data_Ready){

               rxdata[rxnum] = Usart_Read();
  rxnum++;
  }
   if(rxdata[1] == 66 && rxdata[2] == 35){
   
     PORTA = rxdata[0];}
 }}


 if ( PORTA == 239) {
      PORTB.F4 = 0;
     }

   if ( PORTA == 223) {
      PORTB.F5 = 0;
     }

   PORTA = 0XFF;
   PORTB.F4 = PORTB.F5 = 1;




}
}

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

#19 Post by drdoug » 20 Aug 2009 22:04

Whoa there partner. I've been down the same road you are on and I think we need to start with getting the transmitter to work properly.
Change the delay from 5 ms to 500 ms and see what you can do to get it working. My guess is its a timing/oscillator issue.

What chip are you using? Is the OSCCON set for 8 Mhz or whatever the oscillator register is.

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#20 Post by Tanky321 » 20 Aug 2009 23:48

Im using the 16f628A intosc set to 8MHz, Il try a longer delay, I was thinking that such a long delay would end up giving me a bunch of noise from the data slicer, so a short delay would "keep out" the noise.

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

#21 Post by drdoug » 21 Aug 2009 00:52

From what I can tell, the internal oscillator is 4 Mhz not 8 Mhz.

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#22 Post by Tanky321 » 21 Aug 2009 03:53

Man, I never noticed that. They helped out greatly on the transmitter it works perfect.

Tx transmits: 49 49 65 PORTA 66 35

The receiver although, not as lucky. Is it necessary to call Usart_Data_ready before each number? Or am I sending those as one combined number?


Thanks again.

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

#23 Post by drdoug » 21 Aug 2009 04:17

You need to receive each character and the usart_data_ready is a blocking call.

Initialize rxnum = 0 in your code.

I think you are receiving like this:
rxdata[0] = "your PORTA value"
rxdata[1] = 66
rxdata[2] = 35

Then try this (note the "()" change. Maybe it makes a difference, maybe not
if((rxdata[1] == 66) && (rxdata[2] == 35)){

after that rxnum continues to count and does not change the intial response.

This is untested but may help with trouble shooting.

Code: Select all

unsigned short i, rxdata[3];

unsigned int rxnum;



void main() {
INTCON.GIE = 0;                   // Disable interrupts

cmcon = 7;                        //Declare Variables
VRCON = 0;
PORTA = PORTB.F4 = PORTB.F5 = 1;  //Init. PORTA
PORTB = 0;                        //& PORTB
TRISA = 0x00;
TRISB = 0x00;
TRISB.F1 = 1;
rxnum = 0;

Usart_Init(2400);                  //Establish USART at 2400bps


while(1){

 if(Usart_Data_Ready){

     i = Usart_Read();
  }
 
 if( i == 65){
  rxnum = 0;
  while( rxnum < 3){
          if(Usart_Data_Ready){
               rxdata[rxnum] = Usart_Read();
               rxnum++;
     }
   if(rxdata[1] == 66 && rxdata[2] == 35){
   
     PORTA = rxdata[0];}
      Delay_ms(1000); // Longer delay so you dont miss anything
 }}


 if ( PORTA == 239) {
      PORTB.F4 = 0;
     Delay_ms(1000); // Longer delay so you dont miss anything
     PORTA = 0XFF;
     PORTB.F4 = 1;
     }

   if ( PORTA == 223) {
      PORTB.F5 = 0;
     Delay_ms(1000);
     PORTA = 0XFF; // Longer delay so you dont miss anything
     PORTB.F5 = 1;
     }





}
} 

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

#24 Post by Mince-n-Tatties » 21 Aug 2009 11:14

quick observation (i have not read the included threads) i noticed while glancing by...

your code

Code: Select all

PORTA = PORTB.F4 = PORTB.F5 = 1;
is that actually what you want? to just have PORTA = 1 which means PORTA bit 0 = 1.

when PORTA is used without any bit designators, it is 8 bits!


this reads a lot better

Code: Select all

PORTA = 0xFF;
PORTB.F4 = 1;
PORTB.F5 = 1;

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#25 Post by Tanky321 » 21 Aug 2009 15:30

The basic idea is to have 8bits high at all times, and then the status of PORTA that is transmitted will kind of be an inverted binary number

eg- 1110 for 1 1101 for 2 1100 for 3 etc etc


So by saying PORTA = 1 that only sets PORTA.F1 to 1? So the default output of PORTA would be 0001?


Thank you for your input!

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

#26 Post by drdoug » 21 Aug 2009 16:50

Minor correction to your statement.
PORTA = 1 is the same as PORTA.F0 = 1, not PORTA.F1.

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#27 Post by Tanky321 » 21 Aug 2009 19:07

Thats what I meant, came out wrong... :oops:

Ive been messing with this all morning and im still in the same place. I only used one 1s delay, because I dont want a delay in between the port changes.


Now Im getting warmer, before I couldnt get the ports to initialize correctly, now PORTA is high and PORTB bits 4 & 5 are high. Buuuut it still wont work correctly.

I tried using the USART terminal, but that didnt work either. I wasnt sure if I should send each number at a time or send one string?


Thanks,

Andrew

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

#28 Post by drdoug » 21 Aug 2009 19:21

Can you verify the transmitter is working properly? Can you read the output in the terminal program?

Did you make the proper oscillator changes in the receiver?
Try to code just a loopback program on your receiver chip to verify it is working.
Then add other code one step at a time.

for example:

Code: Select all

unsigned short i, rxdata[3];

unsigned int rxnum;



void main() {
INTCON.GIE = 0;                   // Disable interrupts

cmcon = 7;                        //Declare Variables
VRCON = 0;
PORTA = PORTB.F4 = PORTB.F5 = 1;  //Init. PORTA
PORTB = 0;                        //& PORTB
TRISA = 0x00;
TRISB = 0x00;
TRISB.F1 = 1;
rxnum = 0;

Usart_Init(2400);                  //Establish USART at 2400bps


while(1){

 if(Usart_Data_Ready){

     i = Usart_Read();
    Usart_Write(i);  // write data back to computer
  }
 
 if( i == 65){
  PORTB.F4 = 0;     // Flash light if "A" is received
  Delay_ms(1000);
  PORTB.F4 = 1;
     } 
} // end while
} // end main

Tanky321
Posts: 40
Joined: 07 Dec 2008 15:29

#29 Post by Tanky321 » 21 Aug 2009 19:48

The loopback works, if I send and A I get back an A, but if I send an "a" I get "aaaaaaaa" back.

Also the light wont flash if "A" is sent?


Thanks for your time!!

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

#30 Post by drdoug » 21 Aug 2009 19:52

That does not make sense(to me at least). I'll see if I can find a 628A to try out for you. Sorry it will be at least an hour or so.

Post Reply

Return to “mikroC General”