Using C in Arduino enviroment

Hello all!

I am trying to use the UART in a interrupt driven environment, but I can not get it to work. When I connect a usart signal the programs stops, and not starts, even if I press the reset button. The program is just to test the USART in/out.

If some one can help I will appreciate.

Manuel

#include <PCD8544.h>


// A custom glyph (a smiley)...
static const byte glyph[] = { B00010100, B00100000, B00100000, B00100000, B00010100 };


static PCD8544 lcd;
int inbyte=0;
#define F_CPU 16000000UL
#define USART_BAUDRATE_0 9600 
#define BAUD_PRESCALE_0 (((F_CPU / (USART_BAUDRATE_0 * 16UL))) - 1)





void setup() {
  // PCD8544-compatible displays may have a different resolution...
  lcd.begin(84, 48);

  // Add the smiley to position "0" of the ASCII table...
  lcd.createChar(0, glyph);
  
UBRR0L=BAUD_PRESCALE_0;   //Load the lower 8 bits of baude rate value into low byte 
UBRR0H=(BAUD_PRESCALE_0>>8); // load the upper 8 bits of the baude rate value  
UCSR0B =(1<<TXEN0)|(1<<RXEN0); 							// turn the tranmition on
UCSR0C =(0<<USBS0)|(3<<UCSZ00);  //S 
UCSR0B |= (1 << RXCIE0); // Enable the USART Recieve Complete interrupt (USART_RXC)  
 sei(); 
}


void loop() {
  // Just to show the program is alive...
  static uint16_t counter = 0;
  
  // Write a piece of text on the first line...
  lcd.setCursor(0, 0);
  lcd.print("Hello, World!!");

  // Write the counter on the second line...
  lcd.setCursor(0, 1);
  lcd.print(counter, DEC);
  lcd.write(' ');
  lcd.write(0);  // write the smiley

  delay(0);  
  counter++;
}


ISR(USART_RXC_vect)
{ 
   char ReceivedByte; 
   ReceivedByte = UDR0; // Fetch the recieved byte value into the variable "ByteReceived" 
   UDR0 = ReceivedByte; // Echo back the received byte back to the computer 
}

Have a look at how its done in HardwareSerial.cpp, that'll probably show you whats missing. Or just use Serial.begin()

After looking at your code, I think you might be fooled about

[quote author=Manuel Silva link=topic=65527.msg479465#msg479465 date=1309646139] When I connect a usart signal the programs stops, and not starts, even if I press the reset button. [/quote]

One thing I noticed and have no idea how it'll work is that you never check for overflow of your counter variable. That can cause your program to crash. Do you see stuff popping up on the LCD before sending data?

Next, to really test this, why not define a global variable named latest_data and do this:

char LatestData = 'C';

void loop() {
  // Just to show the program is alive...
  static uint16_t counter = 0;
  
  // Write a piece of text on the first line...
  lcd.setCursor(0, 0);
  lcd.print("Hello, World!!");

  // Write the counter on the second line...
  lcd.setCursor(0, 1);
  lcd.print(LatestData); //If everything works, when you send a char, it should appear on the LCD. 
  lcd.write(' ');
  lcd.write(0);  // write the smiley

  delay(0);  
//  counter++;
}


ISR(USART_RXC_vect)
{ 
//   char ReceivedByte; 
   LatestData = UDR0; // Fetch the recieved byte value into the variable "ByteReceived" 
//   UDR0 = ReceivedByte; // Echo back the received byte back to the computer    // enable Echo on your terminal and leave this out for now. 
}

Also, have a look at Petter Fleury's libraries at http://www.jump.to/fleury

Espero ter ajudado. ;)

Thanks for the inputs.

Bubulindo, the counter overflows, but, no problem. As you already know I am developing the telemetry project. I have already the data coming out of the RS232 port in the transmitter. I have to screen the data between a lot of data about RSSI, alarm levels, etc. I am only interested in this program just to use the NOKIA LCD 5110 library. I would like to write the code in C in the Arduino environment, and want to use the UART with the interrupts to be able to screen the data I am using. The question is: Can I use interrupts in AVr C in the Arduino environment?.

Thanks and regards,
Manuel Silva