External variable

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

Hello everyone.
Look at the simple code below.

// ATmega48
#include 
#include
#include

#define OSC_speed 8000000
#define BAUDE 9600
#define myUBRR (OSC_speed/(16UL*BAUDE)-1)

char BUF_count = 0x00;
char RxBuf=0;

void USART_init(unsigned char UBRR_value)
{
	cli();
	UBRR0H=(char)(UBRR_value>>8);
	UBRR0L=(char)UBRR_value;
	UCSR0B=(1<<RXEN0)|(1<<TXEN0)|(0<<UCSZ02)|(1<<RXCIE0);
	UCSR0C=(0<<UMSEL01)|(0<<UMSEL00)|(1<<UCSZ00)|(1<<UCSZ01);	
	sei();
}

void USART_transmit(char data)
{
	while(!(UCSR0A & (1<<UDRE0)));
	UDR0=data;
}

ISR(USART_RX_vect)
{
   	RxBuf=UDR0;
	BUF_count++;
} 

int main()
{	
	USART_init(myUBRR);
	while (BUF_count<0x02)
	{
		_delay_ms(100);
		USART_transmit('T');
	}						
}

Why main() function does not see changes of BUF_count variable? Anyway it's changing in ISR().

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

FAQ#1 - click on my sig - all will be revealed. What you are then looking at is the USER MANUAL - probably worth a squint through all of it while there. ;-)

(presume your ultimate plan is for RxBuf to be a circular char array with read and write pointers/indices?)

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

Search for references to the word "volatile", either here in this forum, or in your C manuals.

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

I got it.

That is why the questions are called
"Frequently Asked Questions" :-)

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

And to be clear on terminology, BUF_count and RxBuf are not "external" variables. An external variable is one defined in one file, but used in another.

Regards,
Steve A.

The Board helps those that help themselves.