Hello everybody!
Before asking here, I searched in google and in this forum and I didn't find a useful answer.
I am using the microcontroller ATtiny167, to compile the software I use Arduino 1.0.3 and to program I use AVRISP mkII.
I want to use the hardware serial to communicate with a terminal. I am working with a baudrate of 9600.
The code, that I will write in the bottom, works if I send or I receive something less than 16 characters. If the message is larger, I do not know if it has an overflow the buffer or an stackoverflow, but the microcontroller crash and I can only make that it works another time disconnecting it from the power supply or flashing it again.
I only want to be able to write for example the next sentence: "Press the start button:"
To be sure that I am using the hardwareserial libraries, I checked that in the library "Arduino.h" has the next line:
#define USE_SOFTWARE_SERIAL 0
In the "HardwareSerial.cpp" I can see that the SERIAL_BUFFER_SIZE. is 16 because the RAMEND is 766.
The RAMEND is calculated as:
#define RAMSTART (0x100)
#define RAMSIZE (0x1FF)
#define RAMEND (RAMSTART + RAMSIZE - 1)
I understand that here is where the size of the buffer is delimited. But I try to put as SERIAL_BUFFER_SIZE 8 and still have the same behaviour as with the value 16. The same hapens if I write 32. Notice that when I change the value of SERIAL_BUFFER_SIZE, I save the change, I compile and I reflash!
In the next code, I only check the problem in the transmision, but I have the same problem in the reception. I am not able to receive more than 16 characters. If I want to receive a message of 17 characters, then it crashes and I have to reset.
So, the code works if I receive a '1', a '2', a '3' but it crashes if I receive a '4' or a '5'.
Note: I know that it would be better to use a switch case instead if and else if, but I doubt that this will be the problem...
#include <Arduino.h>
#define TRANSMIT_SPEED 9600
void setup( )
{
Serial.begin( TRANSMIT_SPEED );
Serial.print( "\n" ); //flush
Serial.print( "P: " );
}
void loop( )
{
char key = '\0';
if ( Serial.available( ) )
{
key = Serial.read( );
if( key == '1' )
{
Serial.print( "_08_4567" );
}
else if( key == '2' )
{
Serial.print( "_15_12345678901" );
}
else if( key == '3' )
{
Serial.print( "_16_abcdefgh1234" );
}
else if( key == '4' )
{
Serial.print( "_17_4567890123456" );
}
else if( key == '5' )
{
Serial.print( "_24_defgh12345678ijklmno" );
}
else
{
//Do nothing
}
}
}
Any help appreciated. Thanks!