AVR - software question

Discussion in 'Programmer's Corner' started by Sparky, Dec 30, 2010.

  1. Sparky

    Thread Starter AAC Fanatic!

    Aug 1, 2005
    75
    0
    Hey -

    wondering if you see something obvious wrong with my code:

    My intention - within my Receive ISR - I set a variable: comm_reception = 1; (clear it elsewhere)

    I am having a problem getting into the if statement looking for comm_reception.

    I placed a printf statement just before that if statement to print that value of comm_reception. - with that printf statement in the code - it works - it should that when I would send a character - comm_reception is a "1"

    The printf statement is:
    printf("Comm_Received = %d\n",comm_reception);

    when I comment out the statement I do not get into the if statement

    I don't see it -
    ???

    Thanks
    Sparky

    code is below:

    Code ( (Unknown Language)):
    1. [FONT=Courier New]int main(void)[/FONT]
    2. [FONT=Courier New]{[/FONT]
    3.  
    4. [FONT=Courier New]init_io_ports();[/FONT]
    5.  
    6. [FONT=Courier New]init_comm_port();[/FONT]
    7.  
    8.  
    9. [FONT=Courier New]/* set up serial printing */[/FONT]
    10. [FONT=Courier New]fdevopen(uart_putchar,NULL);[/FONT]
    11.  
    12. [FONT=Courier New]sei();[/FONT]
    13.  
    14. [FONT=Courier New]printf("System 2 Ready ...\n");[/FONT]
    15.  
    16. [FONT=Courier New]for(;;)[/FONT]
    17. [FONT=Courier New]      {[/FONT]
    18. [FONT=Courier New]        loop_counter++;[/FONT]
    19.  
    20. [FONT=Courier New]        printf("Comm_Received = %d\n",comm_reception);[/FONT]
    21.  
    22. [FONT=Courier New]        if(comm_reception == 1)[/FONT]
    23. [FONT=Courier New]        {[/FONT]
    24. [FONT=Courier New]printf("here\n");[/FONT]
    25. [FONT=Courier New]        if(receive_character == '2')[/FONT]
    26. [FONT=Courier New]        {[/FONT]
    27. [FONT=Courier New]          printf("2 Listening\n");[/FONT]
    28. [FONT=Courier New]            talking_to_me = 1;[/FONT]
    29. [FONT=Courier New]            comm_reception = 0;[/FONT]
    30. [FONT=Courier New]        }[/FONT]
    31.  
    32. [FONT=Courier New]        if(talking_to_me == 1 && receive_character == 'o')[/FONT]
    33. [FONT=Courier New]        {[/FONT]
    34. [FONT=Courier New]          printf("2 On\n");[/FONT]
    35. [FONT=Courier New]            talking_to_me = 0;[/FONT]
    36. [FONT=Courier New]            comm_reception = 0;[/FONT]
    37. [FONT=Courier New]            talking_to_me = 0;[/FONT]
    38. [FONT=Courier New]        }[/FONT]
    39.  
    40. [FONT=Courier New]        if(talking_to_me == 1 && receive_character == 'f')[/FONT]
    41. [FONT=Courier New]        {[/FONT]
    42. [FONT=Courier New]          printf("2 Off\n");[/FONT]
    43. [FONT=Courier New]            talking_to_me = 0;[/FONT]
    44. [FONT=Courier New]            comm_reception = 0;[/FONT]
    45. [FONT=Courier New]            talking_to_me = 0;[/FONT]
    46. [FONT=Courier New]        }[/FONT]
    47. [FONT=Courier New]        if(talking_to_me == 1 && receive_character == 's')[/FONT]
    48. [FONT=Courier New]        {[/FONT]
    49. [FONT=Courier New]          printf("2 Status\n");[/FONT]
    50. [FONT=Courier New]            talking_to_me = 0;[/FONT]
    51. [FONT=Courier New]            comm_reception = 0;[/FONT]
    52. [FONT=Courier New]            talking_to_me = 0;[/FONT]
    53. [FONT=Courier New]        }[/FONT]
    54.  
    55. [FONT=Courier New]        }[/FONT]
    56.  
    57.  
    58.  
    59. [FONT=Courier New]ISR(USART_RX_vect)      //COMM RECEIVER Interrupt[/FONT]
    60. [FONT=Courier New]{[/FONT]
    61. [FONT=Courier New]      receive_character = UDR0;[/FONT]
    62.  
    63. [FONT=Courier New]//    if(Comm_Test_Running == 1)[/FONT]
    64. [FONT=Courier New]//    {[/FONT]
    65. [FONT=Courier New]            //printf("Character Received = %c\n",receive_character);[/FONT]
    66. [FONT=Courier New]//    }[/FONT]
    67.  
    68. [FONT=Courier New]            comm_reception = 1;[/FONT]
    69.  
    70. [FONT=Courier New]            key_pressed = 1;[/FONT]
    71. [FONT=Courier New]}
    72.  
    73. [/FONT]

     
    Last edited by a moderator: Dec 31, 2010
  2. myforwik

    New Member

    Feb 15, 2010
    11
    0
    Please show the declaration for comm_reception. Most likly you have not declared it as volatile, so the compiler is optimising out your code because it doesn't know that it can change at a random time by the interrupt.
     
Loading...