This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

printf doesn't work !

I don't know why, I would like to implement a printf function in my C program for my T89C51CC01. When I run the debugger, my program stop during the execution of the printf function.

The manual say that printf use the putchar function. OK. I try to take a look on the disassembly windows, and the program stop just when it tries to send the first character of my string.

This is a sample of my program:

void main(void)
{
while(1)
{
// Fonctionnement normal

// Mode de configuration
modeConfig = 1;
init_mode_config();
TR0 = 1;
while(modeConfig == 1)
{
switch(niveauMenu)
{
// Attente des caractères CR,CR pour activer le menu
case 0x00:	if(flgDataRecu)		// Test si le mode config a bien été demandé ?
							{					// Attente de 2 CR
								flgDataRecu = 0;

								if(cara_recu == CR)
								{
									temps = 0;
									if(flgDemandeMenu == 1)
									{
										TR0 = 0;
										niveauMenu = 0x01;
									}
									else
										flgDemandeMenu = 1;
								}
								else
								{
									if(flgDemandeMenu == 1)
										flgDemandeMenu = 0;
									// Mauvais caractère !
								}
							}
							break;

				// Menu principal
				case 0x01:	printf("\n---------- STDI ----------", table_divers[1], "\n\n"); //
							printf("A - Vitesse de Transmission serie\n");
							printf("B - Format des Donnees\n");

Parents
  • First, fix your indenting please. 4 spaces (never tabs!) per indent only. This is my law, I expect the world to abide by it. :-)

    Second,

    printf("\n---------- STDI ----------", table_divers[1], "\n\n");
    What the heck is this? Printf needs a format specifier which you omitted, fix that first. The table_drivers[] needs its value placed somewhere. If it is an array of int's then add a %d.

    Goodness, just type this assuming that table drivers is an array of int's and please lookup printf in your C reference.

    printf("\n---------- STDI ----------\n%d\n\n", table_divers[1]);
    I suspect printf works just fine when called properly.

    Regards,

    - Mark

Reply
  • First, fix your indenting please. 4 spaces (never tabs!) per indent only. This is my law, I expect the world to abide by it. :-)

    Second,

    printf("\n---------- STDI ----------", table_divers[1], "\n\n");
    What the heck is this? Printf needs a format specifier which you omitted, fix that first. The table_drivers[] needs its value placed somewhere. If it is an array of int's then add a %d.

    Goodness, just type this assuming that table drivers is an array of int's and please lookup printf in your C reference.

    printf("\n---------- STDI ----------\n%d\n\n", table_divers[1]);
    I suspect printf works just fine when called properly.

    Regards,

    - Mark

Children
  • Hi Mark, thanks for your help!
    (I'm agree with you about the indenting but my copy and paste do that)
    the

     table_divers[1] 
    is another string, so i can accumulate it in my printf.

    It seems to me that my problem is essentially hardware! I do many investigation and i find a really strange solution!!! See what i need to do to execute my printf function:

    void main(void)
    {
    while(1)
    {
    // Fonctionnement normal

    // Mode de configuration
    modeConfig = 1;
    init_mode_config();
    TR0 = 1;
    while(modeConfig == 1)
    {
    switch(niveauMenu)
    {
    // Attente des caractères CR,CR pour activer le menu
    case 0x00:
    .
    .
    .
    break;
    // Menu principal
    case 0x01:
    ES = 0;
    TI = 1;
    printf("\n---------- STDI ----------", table_divers[1], "\n\n");
    printf("A - Vitesse de Transmission serie\n");
    printf("B - Format des Donnees\n");
    .
    .
    .
    .

    After disable the serial interruption and set the TI flag, all printf execute correctly !!!! That's really strange !!!!

  • Hi Mark, thanks for your help!
    (I'm agree with you about the indenting but my copy and paste do that)
    the

     table_divers[1] 
    is another string, so i can accumulate it in my printf.

    It seems to me that my problem is essentially hardware! I do many investigation and i find a really strange solution!!! See what i need to do to execute my printf function:
    void main(void)
    {
    while(1)
    {
    // Fonctionnement normal
    
    // Mode de configuration
    modeConfig = 1;
    init_mode_config();
    TR0 = 1;
    while(modeConfig == 1)
    {
    switch(niveauMenu)
    {
    // Attente des caractères CR,CR pour activer le menu
    case 0x00:
    .
    .
    .
    break;
    // Menu principal
    case 0x01:
    ES = 0;
    TI = 1;
    printf("\n---------- STDI ----------", table_divers[1], "\n\n");
    printf("A - Vitesse de Transmission serie\n");
    printf("B - Format des Donnees\n");
    .
    .
    .
    .
    
    After disable the serial interruption and set the TI flag, all printf execute correctly !!!! That's really strange !!!!

    sorry for the double post.

  • printf("\n---------- STDI ----------", table_divers[1], "\n\n");
    This is still wrong, at least if it works it's a surprise. Please correct this by typing this instead:
    printf("\n---------- STDI ----------%s\n\n", table_divers[1]);
    After disable the serial interruption and set the TI flag, all printf execute correctly !!!! That's really strange !!!!

    Not strange at all. Did you write a serial ISR? If so, you must re-write putchar() yourself to use your interrupt based serial port. Printf() calls putchar() so you just need to re-write putchar().

    If you did not have a serial ISR with putchar/getchar re-written to use it then printf/scanf should not work.

    - Mark

  • That's right! I write a serial ISR in my program!
    OK i understand better the putchar function and my ISR are in conflict. OK

    Sorry for my mistake about the printf parameter, your're right again.

    Thanks a lot, it will be easier now.

  • Extra arguments in the call to printf are ignored if there are no format specifiers. So it could not be the problem.

    - Mike

  • Right! That's what's happened to me, but I corrected it now...