SCI for Send Char and Receive Char

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

SCI for Send Char and Receive Char

Jump to solution
2,950 Views
SamLin
Contributor I

Hi,all.

 

The MCU i am using now is MCF51QE128. The dev board is DEMOQE.

 

What I'm trying now is to send the command to my Xbee from my MCU.

 

The send function has been tested and completed done. The receive function has been tried through different ways as well.

 

When the command(+++) send to my Xbee, the Xbee will reply "OK" through serial port.

 

I was trying to get the "OK" but unfortunately, it never got before.

 

I am pretty sure the Xbee reply a OK already since the LED on the Xbee dev board is blinking while replies a "OK".

 

There may be some registers I set wrong to cause I can not get the "OK".

 

Below is my send and receive function.

 

Hope someone who is experienced can help me to deal with this problem.

 

I try to figure out what's going out 2 weeks already but still nothing discovered.

 

----------------------------------------------------Send function--------------------------- 

void SendMsg(char msg[]) {
  byte i=0;
  char nxt_char;
 
  PTCD = 0x00;
  SCI1C2 = 0x08;    // enable Tx
  SCI1C2_TCIE =1;   //Transmission complete interrupt enable
  nxt_char = msg[i++];
  while(nxt_char != 0x00) {
    while(!SCI1S1_TDRE){}
    SCI1D = (byte) nxt_char; // 2nd half of TDRE clear procedure
    nxt_char = msg[i++];
  } //end while
} //end SendMsg 

 

----------------------------Receive function--This function is put into main function already----------------------------------

void SCI_init(){
  
  SCI1C2_RIE = 1;    //set rx interrupt enable
  SCI1C2_RE = 1;    // enable Rx
 
}

 

------------------------I use Rx ISR to start my Receive function------------------

interrupt VectorNumber_Vsci1rx void SCIrx_ISR(void){
 
  SendMsg("Rx is working now!!");       //set up the led to test if the rx is triggered.\
  PTCDD=0x02;   //set up the led to test if the Rx is triggered.
  char rec_char[3];
  static int pos = 0; 

  if (SCI1S1_RDRF)
  {
    rec_char[pos] = (char)SCI1D;  // 2nd half of RDRF clear procedure
    pos++;

    if ( pos == 3)
    {
       if ( rec_char[0] == 'O'  && rec_char[1] == 'K' && rec_char[2] == '\n') {
      
       SendMsg("You got it!!");
     
      pos =0;
       }
    
      else{
       
       SendMsg("You got wrong!!");
     
      pos = 0;
      }
   
    }
     
  }
  SCI1C2_RIE = 1;
  SCI1C2_RE = 1;    // enable Rx
}

Labels (1)
0 Kudos
Reply
1 Solution
1,008 Views
RichTestardi
Senior Contributor II

Hi Sam,

 

> Do you mean I have to change

> SCI1C2 = 0x08 to SCI1C2_TE=1; or SCI1C2=0x0C;

> Is this what you mean? 

 

Yes, I would think the bitwise "SCI1C2_TE=1;" is safest -- the whole register assignments can be risky.  You also want to make sure any time you use the bitwise assignments that a read-modify-write of the register behaves predictably, which I believe it does for SCI1C2.  (I actually did not use the bitwise assignments in my code, but then again, I don't change that register after initialization.)

 

> I visited you website and it's awesome.

 

Thanks!!!  I had a lot of fun doing that, but now I have to return to the real world for a while to get a paycheck again! :-)  My dream is still to take this stuff into High Schools...  Some day!

 

-- Rich

 

 

 

 

 

 

View solution in original post

0 Kudos
Reply
4 Replies
1,008 Views
SamLin
Contributor I

Thanks Rich,

 

Do you mean I have to change

SCI1C2 = 0x08 to SCI1C2_TE=1; or SCI1C2=0x0C;

Is this what you mean?

 

Thanks a lot.

 

I visited you website and it's awesome.

 

                                                               Sam

0 Kudos
Reply
1,009 Views
RichTestardi
Senior Contributor II

Hi Sam,

 

> Do you mean I have to change

> SCI1C2 = 0x08 to SCI1C2_TE=1; or SCI1C2=0x0C;

> Is this what you mean? 

 

Yes, I would think the bitwise "SCI1C2_TE=1;" is safest -- the whole register assignments can be risky.  You also want to make sure any time you use the bitwise assignments that a read-modify-write of the register behaves predictably, which I believe it does for SCI1C2.  (I actually did not use the bitwise assignments in my code, but then again, I don't change that register after initialization.)

 

> I visited you website and it's awesome.

 

Thanks!!!  I had a lot of fun doing that, but now I have to return to the real world for a while to get a paycheck again! :-)  My dream is still to take this stuff into High Schools...  Some day!

 

-- Rich

 

 

 

 

 

 

0 Kudos
Reply
1,008 Views
SamLin
Contributor I

Hi,Rich

 

I tried your way and it does work.

 

I still have some questions about MCF51QE128.

 

Can I ask you via email? I think this way could be more directly.

 

I found your email address on you website. I wrote an email to you and hope you can get it!

 

Thanks Rich. Your experience strongly helps me a lot.

 

: )

 

                                                                                 Sam

0 Kudos
Reply
1,008 Views
RichTestardi
Senior Contributor II

I think every time you send a message, you are disabling the receiver with:

 

 SCI1C2 = 0x08;    // enable Tx

 

Is that possible?

 

If you want to see my SCI routines (and lots of other example code for the 51QE), you can download them in sources/serial.[ch] and pin.ch from the skeleton.zip at the bottom of this page:

 

  http://www.cpustick.com/downloads.htm

 

-- Rich

 

0 Kudos
Reply