Problem with hardware serial on ATtiny 4313

I have a problem with ATtiny: the blink code seems to slow down when a serial device is connected (physical pins 2 and 3, RX & TX) and the Serial.begin (9600); is in the setup(). In fact, here is the code:

int led = 13;

void setup() {                
  Serial.begin (9600);
  pinMode(led, OUTPUT);     

void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second

The LED blinks about 3 times slower than expected. But If I either disconnect the serial device, or leave it connected but comment out the Serial.begin, then the blinking rate is OK. If that matters, the ATtiny 4313 is running on two AAA batteries at 1 MHz (8 MHz internal oscillator, internally divided). I don't have this problem with Nano or Pro Mini. I have seen posts related to problems with software serial on tinys, and with serial speeds above 9600 at 1 MHz. None of that seems to apply here, does it?

There is a possibility that the receive buffer exceeds available SRAM. I don't think I did any testing of HardwareSerial on the ATtinyX313 processors.

That isn't the problem...

The buffer should be 32 bytes (of 256 bytes) (12.5% of SRAM).

int led = 13;

Put const in front of that.

Does the device transmit constantly? If "yes"...

If that matters, the ATtiny 4313 is running ... at 1 MHz

1000000/(9600/10) = 1041 processor clock cycles between bytes. That may not be enough. If you can, rerun the experiment with the processor clocked at 8 MHz.

I can confirm the same problem with my t4313 @ 1MHz. Higher 8 and 16MHz seem to be okay.

However, I also tried it with a t2313 @ 1MHz, and it works fine. No delays with serial active. So it may be something with the t4313 files. I am thinking it may be something with the include\avr\iotn4313.h file.

Yep, I did a crude test by substituting the iotn2313.h in place of the iotn4313.h file and the t4313@1MHz worked fine with serial activity.

I think this is similar to other definition problems in the iotn4313.h file you had, relating to your other post.

I’ll see if the Atmel AVRToolchain is any different.

Edit: Nope, still the same files in Atmel Studio 6.0 AVRToolchain. I haven’t tried checking Atmel Studio 6.1 yet.

Hopefully I am not complicating the issue further, but with 4313 at 8 MHz (lfuse 0x64 => 0xE4, right?), I have a different problem. Watchdog behaves strange. The code I showed in the other post, which should blink a LED, works OK at 1 MHz, but at 8 MHz resets the MCU after reaching the ISR for the first time - the LED just briefly blinks every 2 s, due to being turned on in setup() … (this has nothing to do with Serial at the moment)

byte LED = 13;
boolean LEDon = true;

void setup()
   pinMode (LED, OUTPUT);
   digitalWrite (LED, LEDon); 

   // Set up and turn on the watchdog timer.
   MCUSR &= ~(1<<WDRF);               // Clear the watchdog reset flag
   WDTCSR |= (1<<WDCE) | (1<<WDE);    // Set WDE and WDCE - must be done in one operation; writing 1 to WDE starts the timed sequence
   WDTCSR = B00000111;                // Set the watchdog timeout prescaller to 2 s; must be done within 4 cycles from writing 1 to WDE
   WDTCSR |= _BV(WDIE);               // Enable the watchdog timer interrupt.

ISR(WDT_OVERFLOW_vect)   //  Watchdog Interrupt Service.
   LEDon = !LEDon;
   digitalWrite (LED, LEDon); 

void loop()

Edit: Nope, still the same files in Atmel Studio 6.0 AVRToolchain. I haven't tried checking Atmel Studio 6.1 yet.

Don't bother... Smart | Connected | Secure | Microchip Technology (At least its assigned to someone.)

The errors in the vector names are very likely the culprit.

No luck yet, with those bug changes.

However, after a bit of looking through the iotn4313.h code, I believe the problem with the USART is down to one line of definition. The file defines a USART data I/O port of UDR0, which applies to the ATmega line, not to these ATtiny 2313a/4313 chips.

It appears that HardwareSerial.cpp is checking for UDR0 before checking for UDR and thus reading the wrong I/O port.

Just comment out these lines in iotn4313.h(leave the UDR line):

#define UDR _SFR_IO8(0x00C)
//#define UDR0 0
//#define UDR1 1
//#define UDR2 2
//#define UDR3 3
//#define UDR4 4
//#define UDR5 5
//#define UDR6 6
//#define UDR7 7

Works! Thanks a lot.