Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7489365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:14:11+00:00 2026-05-29T15:14:11+00:00

I am running a C program on an AVR chip. Whenever a serial signal

  • 0

I am running a C program on an AVR chip. Whenever a serial signal is heard, it runs the serial interrupt ISR (USART_RX_vect). In this method it should turn on change to = 1;. Then in my main while loop, it should clear the LCD and display it and then set change = 0 again.

This is to stop it continually doing the calulations, and displaying the result on the LCD a million times a minute..

However, when the interrupt method changes the change variable to 1, it does not seem to change it “globally” and in the main method it is always 0..

There is a bit of stuff in here that is for debugging purposes.

/* LCD DEFINES */
#define LED PB5
#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)

/* UART SERIAL DEFINES */
#define F_CPU 16000000UL
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1

#define STARTCHAR 'R'
#define ENDCHAR 'E'

char reading;
char inputBuffer[12];
char readStatus;
uint8_t position;
int change;

char output;
int result;

struct Axis
{
    uint8_t axisNumber;
    uint16_t position;
    uint16_t oldPosition;

} axis1, axis2, axis3;


/* SETUP UART */

void USART_Init( unsigned int ubrr)
{
   /*Set baud rate */
   UBRR0H = (unsigned char)(ubrr>>8);
   UBRR0L = (unsigned char)ubrr;

  /*Enable receiver and transmitter */
   UCSR0B = (1<<RXEN0)|(1<<TXEN0);

   /* Set frame format: 8data, 2stop bit */
   UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}

void USART_Transmit( unsigned char data )
{
    UDR0 = data;
}

unsigned char USART_Receive( void )
{
   return UDR0;
}

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

int main(void)
{
    /* INITALISE SERIAL */
    USART_Init(MYUBRR);

    /* Turn on Receive Complete Interrupt */
    UCSR0B |= (1 << RXCIE0);

    /* Turn On GLobal Interrupts */
    sei();

    position = 0;
    change = 0;

    /* Initialise LCD */
    lcd_init(LCD_DISP_ON);  /* Initialize display, cursor off. */
    lcd_clrscr();
    lcd_puts("READY");

    //Turn on LED 13
    set_output(PORTB,LED);
    output_low(PORTB,LED);

    while (1)               /* Loop forever */
    {
        if (change == 1)
        {
            //If not reading, display the result on the LCD display.
            axis1.position  = (inputBuffer[0]<< 8) | inputBuffer[1];
            axis2.position  = (inputBuffer[2]<< 8) | inputBuffer[3];
            axis3.position  = (inputBuffer[4]<< 8) | inputBuffer[5];

            char axis1Printout[12];
            char axis2Printout[12];
            char axis3Printout[12];

            sprintf(axis1Printout,"%u ", axis1.position);
            sprintf(axis2Printout,"%u ", axis2.position);
            sprintf(axis3Printout,"%u ", axis3.position);

            char output[40] = "";
            strcat(output, axis1Printout);
            strcat(output, axis2Printout);
            //strcat(output, axis3Printout);

            lcd_clrscr();  /* Clear the screen*/
            lcd_puts(output);
            _delay_ms(300);
            change = 0;
        }
    }
}

/* INTERRUPTS */

ISR (USART_RX_vect)
{
    change = 1;
    unsigned char input = USART_Receive();

    if (input == 'R')
    {
        readStatus = 0; //Reading
        position = 0;
    }
    else if ((input != 'E') && (position < 12) && (position > -1))
    {
        inputBuffer[position] = input;
        position++;
    }
    else if (input == 'E')
    {
        readStatus = 1; //Stop Reading
        position = -1;
        output_high(PORTB,LED);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:14:12+00:00Added an answer on May 29, 2026 at 3:14 pm

    You need to declare change using the volatile keyword:

    volatile int change;
    

    This tells the two ‘threads’ (main execution loop and your ISR code) to not ‘cache’ the value in a register, but always retrieve it from memory.

    Edit: There’s another problem with the code – in your main loop, by the time you set changed to 0, you may have already had another interrupt which should have triggered your loop to run again. The easy-but-not-guaranteed fix is to immediately set changed to 0 straight after you check it. The proper way would be to use a lock – but depending on your situation, the first option might do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a method to monitor a running program that I have
I am getting this R6034 error when running program that I just updated (and
I have encountered a problem in a C program running on an AVR microcontroller
Does anyone knows what signal Xcode sends to kill a running program? I need
I've heard that most lisps support saving image of running program into file. Does
I am running a program and want to see what its return code is
I have a .NET CF program (running on a Smartphone) and I have written
I now have a running Java program which only lacks of the final step,that
I have a long-running multithreaded program, and I'd like to occasionally like to call
I'm working on a project where a program running on the mobile phone needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.