taking console inputs

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

taking console inputs

Jump to solution
2,225 Views
Yuping
Contributor I

Hello All,

 

I am working with CodeWarrior v5.1 the special edition for HCS12.

I figured that I could not use some stdio functions directly here for microcontrollers.

I included the TERMIO.c and TERMIO.h for "printf", it worked. But now I want to take inputs from terminal. I used "scanf" which I have implemented with getchar() in TERMIO. But when I try to simulate the program, I cannot type in anything at terminal.

Anybody know the reason for this problem? Do I have to specify communication port just for taking console inputs? Attached is a simple project.

 

Also, I'm confused about using those ANSI C functions. It seems most of them are not supported by CodeWarrior compiler. Anyone could direct me to some place that talks about how I could use those functions?

 

Thanks a lot!!

Labels (1)
Tags (1)
0 Kudos
1 Solution
1,336 Views
kef
Specialist I

I tried it. scanf is working, but for my surprise it requires tons of stack space. scanf routine is defined in lib\src\embedded.c . At top of routine you may see local variable   char outbuf[LIBDEF_SCANF_BUF_SIZE]; . LIBDEF_SCANF_BUF_SIZE is set to 255 by default in include\libdefs.h. This means you need to have stack much larger than 255. Either increase stack size in *.prm file or recompile library with reduced LIBDEF_SCANF_BUS_SIZE. You may just add embedded.c to your project and change link priority so that linker links with embedded.c from your project, but not from then library.

View solution in original post

0 Kudos
9 Replies
1,336 Views
Yuping
Contributor I

Is anyone here to help??

I checked the Debugger_HC12.pdf file and found that I could change terminal connections to "keyboard --> Virtual SCI".

But it seemed not working...

I think the problem might be from using the "scanf()" function. Could anyone help me with this problem?

 

Thanks!

 

Yuping

0 Kudos
1,336 Views
kef
Specialist I

Is getchar really working?

(If you wonder, yes, simulator is working and you can do serial I/O to/from terminal window and keyboard).

0 Kudos
1,336 Views
Yuping
Contributor I

kef,

 

Thanks for replying!

TERMIO_GetChar() is working. Inspired from your concern, I used this TERMIO_GetChar() function instead of scanf(). The program took the inputs from keyboard.

 

Below is the output I got from Terminal

 

Input a 16-character string:

The string is Test message 01.ZZZZZZZZZZZZZZZZZZZZZZZZZZZZ

84 101 115 116 32 109 101 115 115 97 103 101 32 48 49 46 

 

It is weird that there are some Z's following the message. Do you have any idea about why it happened?

Also, I am confused by the use of TERMIO. Why can I use printf() without any problem, but not scanf()?

 

Below is my main.c

 

#include <hidef.h>      /* common defines and macros */#include "derivative.h"      /* derivative-specific definitions */#include <TERMIO.h>#include <stdio.h>#include <stdlib.h>void main(void) {  /* put your own code here */  int i;  char words[16];  int asc[16];  TERMIO_Init();    printf("Input a 16-character string:\n");  for(i = 0; i < 16; i++)    words[i] = TERMIO_GetChar();  //scanf("%s",&words);  //fgets(words,strsize,stdin);  printf("The string is %s\n", words);  i = 0;  while(words[i]!='\0') {       asc[i] = (int)words[i];       printf("%d ",asc[i]);       i++;  } EnableInterrupts;  for(;;) {    _FEED_COP(); /* feeds the dog */  } /* loop forever */  /* please make sure that you never leave main */}

 Thanks!

0 Kudos
1,337 Views
kef
Specialist I

I tried it. scanf is working, but for my surprise it requires tons of stack space. scanf routine is defined in lib\src\embedded.c . At top of routine you may see local variable   char outbuf[LIBDEF_SCANF_BUF_SIZE]; . LIBDEF_SCANF_BUF_SIZE is set to 255 by default in include\libdefs.h. This means you need to have stack much larger than 255. Either increase stack size in *.prm file or recompile library with reduced LIBDEF_SCANF_BUS_SIZE. You may just add embedded.c to your project and change link priority so that linker links with embedded.c from your project, but not from then library.

0 Kudos
1,336 Views
Yuping
Contributor I

Hi Kef,

 

Thanks a lot for helping out!

Now I'm able to use scanf correctly.

 

I have another question though...

Since scanf reads console input until it sees a whitespace, I think it may be better to use fgets for convenience...

I couldn't find the file which defines fgets either... I checked CodeWarrior InfoCenter, and did not find the information.

 

Could you give an insight about where to find function definitions in general? It's really annoying when I have to find those ANSI C functions which cannot be used by just including stdio.h...

 

Thanks again!!

 

Yuping

0 Kudos
1,336 Views
CrasyCat
Specialist III

Hello

 

The Compiler_HC12.pdf reference manual states that

 

"The ANSI–C library contains a fairly large interface for file I/O. In microcontroller
applications however, one usually does not need file I/O. In the few cases where one
would need it, the implementation depends on the actual setup of the target system.
Therefore, is therefore impossible for Freescale to provide an implementation for these
features that the user has to specifically implement."

 

Now if you want to read data from a file you can use the terminal redirection feature.

 

This is explained in Debugger_HC12.pdf chapter Book I Debugger Engine section Debugger Components > General Debugger Components > Terminal Component >  Input and Output File & File Control Commands.

 

 

CrasyCat

0 Kudos
1,336 Views
AirDragon
Contributor III

Would it be a bad idea to make the printf(), scanf(), etc. functions use the SCI module?

0 Kudos
1,336 Views
Yuping
Contributor I

Thank you, CrasyCat!

I got it!

0 Kudos
1,336 Views
kef
Specialist I

And you get ZZZZ because your local not initialied string doesn't have '\0' at the end.

0 Kudos