Trouble sending a Char using the SCI module of MC9S08SH4MPJ with Device Initialization.

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

Trouble sending a Char using the SCI module of MC9S08SH4MPJ with Device Initialization.

971 Views
elkin_granados
Contributor I

Hi every one!

 

A few days ago I bought a micro MC9S08SH4MPJ for making a application that reads an ADC channel value and send it to PC with the SCI module. I'm using Codewarriro 6.3, with device initialization. I set the microcontroller for working with the internal clock, I set the internal oscillator frequency in 32.7K and the internal bus clock in 16,74 Mhz. The SCI module is setting for basic use, 8 bit data, no parity, the baud rate divisor in 9 for a baud rate of 116626 bds/s.

 

In the main.c routine I have the following for making a test:

 

#include <hidef.h> /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

 

#ifdef __cplusplus

extern "C"

#endif

 

char var[]= "Hello world!";  //declaring a char array for test

int i = 0;                   //declaring an int for sending bucle

 

void MCU_init(void); /* Device initialization function declaration */

 

void main(void) {

 

  MCU_init(); /* call Device Initialization */

 

  /* include your code here */

 

//bucle for sending each character of the array

 

for(i=0;i<sizeof(var);i++) {


   sendCharSCI(var[i]);


   }

 

  for(;;) {

    /* __RESET_WATCHDOG(); by default, COP is disabled with device init. When enabling, also reset the watchdog. */

  } /* loop forever */

  /* please make sure that you never leave main */

}

 

//Funtion that sends character for SCI module

 

void sendCharSCI(unsigned char data){

 

  char read = 0;     //variable for rear SCIS1 register

 

  SCIC2_TE = 1;     //enable SCI data transmition

 

  read = SCIS1;     //read SCIS1 register

  SCID = data;     //write the data for sending in SCID register

 

      while (SCIS1_TDRE == 0){ }  //waiting buffer empty

    

      while (SCIS1_TC == 0){ }    // waiting transmition complete

    

  SCIC2_TE = 0;    //disable SCI data transmition

 

}

 

I'm testing the program using and FT232 for simulate a COM PORT. When I check in the hyperteminal the PC is receiving data, but no the same chars of the array. I noted that the received char is the ASCII char + 96. for example, the 'H' is 72 in decimal, then I'm receiving the char 72+96 = 168 that is ' ¨ '.

I don't know if you need any more info, if it, please tell me.

 

I Have a micro coldfire V1 JM128VLD, and I tried to do the same test and works fine!

 

If anybody could help me with this I'll be really grateful!

 

Sorry for my english, Thank you so much and greetings from Colombia!

Labels (1)
4 Replies

485 Views
elkin_granados
Contributor I

Hi every one!


Thank you so much for your answers, I'll do the test in the next days because I'm now a quite bussy, but I'll tell you how it works!


Best Regards!

485 Views
bigmac
Specialist III

Hello,

Part of your code -

void sendCharSCI( byte data)
{
   char read = 0;              // variable for rear SCIS1 register
  
   SCIC2_TE = 1;               // enable SCI data transmition
   read = SCIS1;               // read SCIS1 register

   SCID = data;                // write the data for sending in SCID register
   while (SCIS1_TDRE == 0){ }  // waiting buffer empty
   while (SCIS1_TC == 0){ }    // waiting transmition complete
   SCIC2_TE = 0;               // disable SCI data transmition
}

  1. The control bit SCIC2_TE should be enabled within the SCI initialisation process, and remain enabled.  The SCI transmitter should not be enabled, and then disabled, for the sending of each character.  Every time the TE bit is set, an "idle" byte is sent to the buffer, with the result that one character period of idle state will be interspersed between each send character, thus halving the character throughput.
  2. The SCIS1_TDRE flag should be checked prior to the sending of each character.  No additional read of the SCIS1 register is necessary
  3. You normally do not need to monitor the TC flag with full-duplex operation.  For a half-duplex operation, you would need to monitor when the final character of the data packet or data sequence had been sent.  Presumably you are using full-duplex.

Taking these issues into account, should give the following code -

void sendCharSCI( byte data)
{
   while (SCIS1_TDRE == 0); // wait for transmit buffer empty
   SCID = data;             // write character to buffer
}


Additionally, if you are using the internal reference within the ICS module (rather than an external crystal), the non-volatile trim value will need to be calibrated, and programmed to flash (normally when the code is programmed).  The trim value stored in flash is then transferred to the actual trim register during initialisation of the MCU.  If this is not correctly done, baud rate error will occur.  It is possible that an incorrect baud rate may account for the problems you are observing.

Regards,

Mac

0 Kudos

485 Views
RodBorras
NXP Employee
NXP Employee

Elkin,

You probably have a problem in the initialization.

I have been using the following routines for years:

// SCI FUNCTIONS //////////////////////////////////////////////////////

//---------------------------------------------------------------------

void SCI_Init(void)          {SCIBD=14; SCIC1=0x00; SCIC2=0x0C; SCIS2=0x00; SCIC3=0x00;}   // 8.388MHz/16/14=38,400 baud (actually 37,449)

//---------------------------------------------------------------------

void SCI_CharOut(byte data)  {while (!SCIS1_TDRE); SCID=data;}

//---------------------------------------------------------------------

void SCI_NibbOut(byte data)  {if (data>0x09) SCI_CharOut(data+0x37); else SCI_CharOut(data+0x30);}

//---------------------------------------------------------------------

void SCI_ByteOut(byte data)  {SCI_NibbOut(data>>4); SCI_NibbOut(data&0x0F);}

//---------------------------------------------------------------------

void SCI_TextOut(byte *data) {while (*data!=0x00) {SCI_CharOut(*data); data++;}}

//---------------------------------------------------------------------

byte SCI_CharIn(void)        {while (!SCIS1_RDRF); return SCID;}

//---------------------------------------------------------------------

byte SCI_ByteIn(void)        {byte h,l; h=SCI_CharIn()-48; if (h>9) h-=7; l=SCI_CharIn()-48; if (l>9) l-=7; return (h&15)*16+(l&15);}

//---------------------------------------------------------------------

void SCI_CRLF(void)          {SCI_CharOut(0x0D); SCI_CharOut(0x0A);}

Just use SCI_Init() and change the baudrate divisor: SCIBD=9; then use SCI_CharOut() to send your characters.

Regards,

Rod.

0 Kudos

485 Views
admin
Specialist II

Hi could you show me what are doing in void MCU_init(void){???}

0 Kudos