Serial Port Attiny

Posted on  by admin

An overview of the alternate pin functions of the ATtiny2313 microcontroller and the internal peripherals that connect to them.The purpose of this part of the tutorial is to get familiar with the internal peripherals that connect to external pins of the ATtiny2313 microcontroller. There are also internal peripherals that do not connect to pins of the ATtiny2313, such as the watchdog timer and EEPROM.Each peripheral will be covered individually in detail and include software examples later in this tutorial series. The information below just shows what peripherals are available and which pins of the microcontroller they use. Alternate Pin FunctionsThe image below shows the ATtiny2313 DIP package with the alternate pin functions labelled:ATtiny2313 Alternate Pin FunctionsEvery port pin of the ATtiny2313 can be used as an I/O (input / output) pin. When we refer to an alternate function of a pin, we are referring to a function other than I/O (alternate to I/O).

  1. Serial Port Attiny85

We will now look at what each of these alternate functions is. RESET / dW – Reset and debugWIREPA2 (pin 1) of the ATtiny2313 microcontroller has two alternate functions – it can be used as a RESET pin or as a debugWIRE pin. ResetPin 1 (PA2) can be used as an external active low reset pin. A fuse bit in the AVR must be programmed in order to enable this pin as a reset pin (RSTDISBL fuse).Older microcontrollers usually require an external reset circuit on a dedicated reset pin to reset the microcontroller when power to the microcontroller circuit is switched on. This is not so with AVR microcontrollers – they have an internal reset circuit that holds them in reset at power up until the power supply is stable. DebugWIREPin 1 (PA2) can be used in debugWIRE (dW) mode.

An internal fuse of the ATtiny2313 must be programmed to enable debugWIRE mode (DWEN fuse). The fuse is programmed by using a tool such as the.Once debugWIRE mode is enabled, an external tool (e.g.

AVR dragon) can be used in conjunction with software (e.g. Atmel Studio) to debug a C program running on the AVR and single-step through the source code. RXD, TXD & XCK – USARTThe USART can act as a UART using RXD and TXD, or act as a USART using RXD, TXD and XCK. XTAL1 & XTAL2 – External CrystalThe XTAL1 and XTAL2 pins can be connected to an external crystal to generate a more accurate CPU clock frequency (and a frequency from a wider range) than the default internal R/C oscillator.XTAL1 can be connected to an external oscillator and used as a source for the CPU clock frequency. CKOUT – System Clock OutputOutputs the system clock on this pin when the CKOUT fuse bit is programmed. INT0 and INT1 – External Interrupt PinsINT0 and INT1 are both external interrupt sources to the microcontroller.

Programming the ATtiny. Next, we can use the Arduino as an ISP to upload a program to the ATtiny: Open the Blink sketch from the examples menu. Change the pin numbers from 13 to 0. Select the appropriate item from the Tools Board menu (leave the serial port set to that of your Arduino board). Have read that the ATTINY 85 does not have a serial port. It depends on your definition of serial port. From a hardware perspective it has a usi. That can be configured as a uart. Whether it fits your need or not is up to your application. Read the datasheet to be sure.

T0, OC0A & OC0B – 8-bit Timer / Counter 0An 8-bit timer/counter with PWM. T0 is an input for an external clock source. OC0A and OC0B are output pins from the timer/counter on which a frequency can be output – e.g. A PWM frequency. T1, OC1A, OC1B & ICP – 16-bit Timer / Counter 1Timer/counter 1 is a 16-bit timer/counter with PWM. The T1 input is an input for an external clock source. OC1A and OC1B are two independent outputs from the timer counter that can each output a frequency.

ICP is the input capture pin for timer/counter 1. AIN0 & AIN1 – Analog ComparatorAIN0 and AIN1 are the input pins for the analog comparator. AIN0 is the positive input pin. AIN1 is the negative input pin. PCINT0 to PCINT7 – Pin Change InterruptPCINT pins can serve as an external interrupt source that generates an interrupt if the logic level on the pin changes (toggles).

Universal Serial Interface (USI)The USI can be used for two-wire and 3-wire serial communication such as SPI and TWI.SPIThree-wire mode of the USI is compatible with SPI (Serial Peripheral Interface), but does not have the SS (Slave Select) pin. TWIIn two-wire mode, the SCL and SDA pins are used.

Two-wire mode is TWI and I2C compatible. Alternative USI UsageThe USI can also be used for several alternative functions, such as:. Half-duplex Asynchronous Data Transfer. 4-bit Counter.

12-bit Timer/Counter. Edge Triggered External Interrupt. Software Interrupt.

As I mentioned in a previous post, I’m starting to look at projects that for various reasons don’t require a full arduino. There are some projects that even the Arduino Pro Mini is too large for. For this, I’m looking at using an ATtiny85 since I only need a few pins. But first, we have to figure out how to program it. There are several ways to program a stand alone IC and future posts will show these, but this post will focus on how to program using an Arduino.

There are already numerous tutorials of this online, but many of them are either incomplete or wrong. Others just don’t answer some of the questions I had when I started and this is what I hope to fix with my version.The first step is setting up the arduino to act as a programmer. Plug your arduino into your computer like normal and select the appropriate serial port and board. In the arduino program, click File Examples and select ArduinoISP.

Upload to your board like any other program.Next, we need to add the files to use the ATtiny board in the Arduino environment. The folks over at the High-Low Tech lab at MIT have created all the files needed to use the ATtiny84 and 85.

Much of their instructions are replicated here along with my comments. These guys do great work and if any of them are ever in Norman, Oklahoma I’d be happy to buy you a drink or two as a token of my thanks.Download the zip file from. Unzip it and then move the unzipped file to the hardware folder that is in the Arduino sketchbook folder. If you don’t know where you Arduino sketchbook folder is, you can find it in the Arduino preferences.

Unless you have added additional boards in the past, the hardware folder will probably not exist and you will have to create it. Restart the Arduino IDE and you should now see lots of new boards available to you.Now it’s time to wire the Arduino and ATtiny. Below is the pinout for the ATtiny85. Notice that the physical pin numbers do not correspond to the digital pin numbers.

It’ll try to differentiate them to make it easier.Now we need to connect the ATtiny to positive voltage on physical pin 8 and ground on physical pin 4 plus connect the following:ATtiny physical pin 7 to Arduino SCK – Uno pin 13, Mega pin 52ATtiny physical pin 6 to Arduino MISO – Uno pin 12, Mega pin 50ATtiny physical pin 5 to Arduino MOSI – Uno pin 11, Mega pin 51ATtiny physical pin 1 to Arduino SS – Uno pin 10, Mega pin 53And, since we need some way of detecting whether our sketch was uploaded correctly or not, lets attach a LED to physical pin 5 with a resister to connect it to ground. The resister value will change based on what voltage you’re running at. I’m using a 110 ohm resister and anything in that range should work.The final thing we need to wire is a 10 microfarad capacitor between the ground and reset pins on the arduino.

This prevents the arduino from resetting ensuring the the Arduino IDE talks to the Arduino ISP programmer and not the bootloader on the arduino. Some tutorials say you need this capacitor, others say you don’t. I tried it with an Arduino Uno R2, Duemilanove, and Mega R3. The Uno was the only one that worked without the capacitor. However, they all work with a capacitor, so I’d put one in just in case.

Serial Port Attiny85

The white stripe on the capacitor goes towards the ground pin.Now, in the Arduino IDE, open the blinky sketch from the examples folder and change pin 13 to pin 0 in the sketch. Then change the board to ATtiny85 (1 MHz internal oscillator) and the programmer to ArduinoISP and hit upload. You should get the following error message which is ok to ignore. However, if you have a sync error or programmer communication error, you have a problem. If your LED is blinking, it’s time to move onto the next step.

If it isn’t blinking but you didn’t get an error, try flipping the LED or making sure it isn’t dead.Next, we want to change from the 1MHz internal clock to the 8MHz clock. To do this, change the board to the ATtiny85 (8MHz internal oscillator) and select burn bootloader. This will configure the internal fuse to switch to 8MHz. This is a semi permanent change. You’ll only need to do this when you want to change the speed, not every time you turn it on. Why would we want 8MHz?

Well, you can do faster calculations but the big reason is that you need 8MHz to use the software serial package which we will discuss later. Note: no matter what you set the clock speed to, you can upload using either the 1MHz or 8MHz option. However, if you choose the incorrect one, things will go much faster (or slower) than you are expecting. (This with a blink sketch is a good way to test what speed you’ve set it at if you can’t remember.)Another final note is that you can do all of this with the circuit hooked up including programming the arduino as the arduinoISP and programming the attiny. The led being hooked up will not affect things at all.

Attiny85 serial port monitor

It definitely does work using the Arduino Pro Mini as the ISP programmer, but it’s easy to make a mistake so there are some things you should double check. Firstly, you need to make sure you’ve programmed the Arduino Pro Mini with the arduino as isp sketch and make sure that when you uploaded it, you upload it to the correct board (arduino pro mini). Then you need to make sure you connect the pins correctly. For instance, if you want to program an uno with a pro mini, you would connect Pin 13 on the Pro to Pin 13 on the Uno; Pin 12 on the Pro to Pin 12 on the Mini; Pin 11 on the Pro to Pin 11 on the Uno; and Pin 10 on the Pro to the reset pin on the Uno. I tested it and it works with or without a capacitor on the Pro Mini. Finally, when you upload you need to make sure that you change the board back to the board you are actually programming (in this case the Uno) and that you choose to “Upload using Programmer”.

If you have any more questions, just let me know.