MC9S08PA4 - UART example code over SCI

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

MC9S08PA4 - UART example code over SCI

Jump to solution
1,457 Views
shai_b
Senior Contributor II

Hello team,

My Customer is working with MC9S08PA4 and he wishes to add UART communication and plans to use only transmitting ASCII character (TX ch.), for ex, putchar() or printf() functions, but he did not found any useful tutorials/documentation on how to accomplish.

Could you please provide a useful example code for MC9S08PA4 (16Mhz CLK) on how to use UART ch to use "printf()" or "putchar()"? 

I am waiting for your kind response, Thanks in advance 

Regards,

Shai

Tags (4)
0 Kudos
1 Solution
1,346 Views
shai_b
Senior Contributor II

Hi @vicentegomez,

I just found my solution, I've used "wait_sci_tx_empty();" -> to wait till SCI_TX buffer is ready.

and everything works.

Thanks for your time and effort.

KR,

Shai

 

View solution in original post

0 Kudos
7 Replies
1,445 Views
vicentegomez
NXP TechSupport
NXP TechSupport

HI

 

Please check the following thread

https://community.nxp.com/t5/8-bit-Microcontrollers/MC9S08PA4-SCI-sample/m-p/252881

Also I do not recommend to use printf on a S08 device, it use a lot of memory 

 

Regards

Vicente Gomez

 

0 Kudos
1,407 Views
shai_b
Senior Contributor II

Hi @vicentegomez,

 

There is any update? Could you please review my code?

Thanks a lot.

KR,

Shai

0 Kudos
1,402 Views
vicentegomez
NXP TechSupport
NXP TechSupport

I am not seeing where are you setting the baud rate?

can you send me the 

Init_SCI0(BUS_CLK_HZ); function?

 

normally you need to set something like this

SCI2_BD |= 52; //set baud rate 9600 (8Mhz/(16 x 52))  // in your case SCI0

 

this is a simple sample code

 

#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */


void SCI2RX_ISR(void);

unsigned char u8receiver, flag,chema = 0;
void main(void) {
EnableInterrupts;
/* include your code here */
/* WDOG_CNT: CNT=0xC520 */
WDOG_CNT = 0xC520; /* First part of the WDG unlock sequence */
/* WDOG_CNT: CNT=0xD928 */
WDOG_CNT = 0xD928; /* Second part of the WDG unlock sequence */
/* WDOG_TOVAL: TOVAL=4 */
WDOG_TOVAL = 0x04;
/* WDOG_CS2: WIN=0,FLG=0,??=0,PRES=0,??=0,??=0,CLK=1 */
WDOG_CS2 = 0x01;
/* WDOG_CS1: EN=0,INT=0,UPDATE=0,TST=0,DBG=0,WAIT=0,STOP=0 */
WDOG_CS1 = 0x00; /* Disable watchdog */
/* System clock initialization */
/*lint -save -e923 Disable MISRA rule (11.3) checking. */
if (*(unsigned char*far)0xFF6FU != 0xFFU) { /* Test if the device trim value is stored on the specified address */
ICS_C3 = *(unsigned char*far)0xFF6FU; /* Initialize ICS_C3 register from a non volatile memory */
ICS_C4 = (unsigned char)((*(unsigned char*far)0xFF6EU) & (unsigned char)0x01U); /* Initialize ICS_C4 register from a non volatile memory */
}
/*lint -restore Enable MISRA rule (11.3) checking. */
/* ICS_C1: CLKS=0,RDIV=0,IREFS=1,IRCLKEN=1,IREFSTEN=0 */
ICS_C1 = 0x06U; /* Initialization of the ICS control register 1 */
/* ICS_C2: BDIV=1,LP=0 */
ICS_C2 = 0x20U; /* Initialization of the ICS control register 2 */
/* ICS_C4: LOLIE=0,CME=0 */
ICS_C4 &= (unsigned char)~(unsigned char)0xA0U;


SCI2_C1 = 0x00; /* Reset flags */
SCI2_C3 = 0x00; /* Configure the SCI */
SCI2_C2 = 0x00; /* Disable error interrupts */
SCI2_S2 = 0x00; /* Disable all interrupts */
SCI2_BD |= 52; //set baud rate 9600 (8Mhz/(16 x 52))
SCI2_C1 |= 0x00;
SCI2_C2 |= (SCI2_C2_TE_MASK | SCI2_C2_RE_MASK);//| SCI2_C2_RIE_MASK); /* Enable transmitter, Enable receiver, Enable receiver interrupt */


for(;;)
{
// __RESET_WATCHDOG(); /* feeds the dog */
while (SCI2_S1_TDRE == 1)
{
SCI2_D =('a');
}
} /* loop forever */
/* please make sure that you never leave main */
}

/***************************************************************************************/

interrupt VectorNumber_Vsci2rx void SCI2RX_ISR(void)
{

(void) SCI2_S1; // read SCIS1 register for clear RDRF flag
u8receiver = SCI2_D;
//SCI2_D = 1+u8receiver;


}

/***************************************************************************************/

 

0 Kudos
1,368 Views
shai_b
Senior Contributor II

@vicentegomez ​ Hello,

 

First of all, Thank you for opening the Ticket.

I just manage to run UART and I've uses the SCI0_D register as PutChar() func.

the problem that I facing is while I trying to send a few Chars using the following code:

while (SCI0_S1_TDRE == 1)

{

 (void) SCI0_S1;

 buf_tx = (unsigned char)tmpvalue[0];

 SCI0_D = buf_tx;

 (void) SCI0_S1;

  buf_tx = (unsigned char)tmpvalue[1];

 SCI0_D = buf_tx;

 (void) SCI0_S1;

 buf_tx = '\n';

 SCI0_D = buf_tx;

 (void) SCI0_S1;

 buf_tx = '\r';

 SCI0_D = buf_tx;

}

but in the terminal, I did not get all the prints, so I think there is a flag that is a purpose to tell the buffer is ready to TX... (maybe adding a small delay could fix it).

please advise the correct way to works with SCI/UART to overcome this issues, Thanks in advance.

 

Kind regards,

Shai

0 Kudos
1,347 Views
shai_b
Senior Contributor II

Hi @vicentegomez,

I just found my solution, I've used "wait_sci_tx_empty();" -> to wait till SCI_TX buffer is ready.

and everything works.

Thanks for your time and effort.

KR,

Shai

 

0 Kudos
1,399 Views
shai_b
Senior Contributor II

Hi @vicentegomez 

Sorry for missing the init_SCI0 functions but it is part of the 'uart.c' libs and the BUS_CLK_HZ defines as 16000000L (16Mhz).

you can find below Init_SCI0 function

void Init_SCI0(dword busCLKHz)
{
SCG_C3 |= SCG_C3_SCI0_MASK; //enable clock gate for SCI0

SCI0_BD = 104; //9600 = 16MHz/16/104

SCI0_C1 = 0; // 8bit mode, 1 stop bit, no parity
SCI0_C2 = 0x0C; // enable TX , enable RX
}//end Init_SCI0_

For some reason I do not see any signal on the TX line, I am afraid I made a mistake while configuring the pins.
Could we take the rest of the support through emails because I do not want to reveal/share the full code in public?

Please advise back, Thanks a lot.

0 Kudos
1,438 Views
shai_b
Senior Contributor II

@vicentegomez hello,

Thanks for your help, I've tried adding the SCI driver into my code but I did not get any output (I even check with the scope on the lines and did not see any signal).

Could you please review my code changed and see if I did something wrong?

I've added the uart.c and uart.h into my project 

in main.c

#include "uart.h"   //adding the UART.H
 

unsigned char uart_buf; //global uart buffer

void main()

....

PORT_PTBOE_PTBOE1 = 1;  PORT_PTBIE_PTBIE0 = 1; /* config relevant pins as TX(output) and RX(input)  */ 
Init_SCI0(BUS_CLK_HZ);    //init SCI0 for uart tx/rx

...

while(1) 

...

(void) SCI0_S1; // read SCIS1 register for clear RDRF flag
uart_buf = 'A';
SCI0_D = uart_buf; // send via uart "A" char

wait_sci_tx_empty();

(void) SCI0_S1; // read SCIS1 register for clear RDRF flag
uart_buf = '\n';
SCI0_D = uart_buf; // send via uart "\n" char

wait_sci_tx_empty();

(void) SCI0_S1; // read SCIS1 register for clear RDRF flag
uart_buf = '\0';
SCI0_D = uart_buf; // send via uart "\0" char

 

Waiting for your kind responses, Thanks in advance.

Regards,

Shai

0 Kudos