Odd problem with printf and uart

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Guys,

I am using a Atmega328 for an app. The program has a USART, timer and ADC. I am using the stdout library functions for the printf to send strings to the UART. Here is a skeleton of the program;


//stdout definitions for UART
//UART_init function definition
//ADC_init function definition
//Timer_init function definition

//TIMER_ISR
//flag = 1 set in TIMER ISR
/*************************/
//MAIN
//CALL ADC_INIT
//CALL TIMER_INIT
//CALL UART_INIT
while(1)

 {

    //post processing of ADC value
     _delay_ms(100);

      if(flag == 1)
      
        { 
          printf("Hello");
         }

       else
         {
           printf("Goodbye");
         }
  }

Ok so when I debug it in single stepping mode, the flow goes like this:


//CALL ADC_INIT
//CALL TIMER_INIT
//CALL UART_INIT
while(1)
_delay_ms(100);
Goes to printf("Hello") but does not print it
printf("Goodbye");
TIMER_ISR
flag = 1;
printf("Hello");

This is just a dummy program to present the problem.

Question is why does the program flow go straight to the printf "Hello" statement and not go to the if loop for checking the flag value?
The flag variable has been declared volatile and is initialized to zero at the very beginning.

Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You haven't said which C compiler. The _delay_ms() suggests this *might* be GCC. In which case where is your FDEV_SETUP_STREAM or did you think printf() was going to work by magic?

As for the flag - it is 'volatile' isn't it

If this is a GCC question I'll move it to the right forum for you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, Cliff it is GCC.

I do have the FDEV_SETUP_STREAM defined. I will post all the code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I will post all the code.

What a radical idea.