AVR ATtiny USB Tutorial

28,447

Circuit Image

This is the second part of a USB tutorial for the ATtiny2313 microcontroller and the V-USB library. The first part covered how to derive 3.3V from USB to power circuits. In this section, the setup will be expanded with additional components. It has been noted that the current setup operates the ATtiny2313 at 12 MHz with only 3.3V VCC, which is outside the specified range (frequencies over 10 MHz require 4.5V or more). Although there have been no issues reported, it is recommended to power the ATtiny2313 directly from the 5V USB line and use zener diodes on the D+ and D- lines to reduce their voltage, as demonstrated in a later tutorial with the ATtiny85 microcontroller. This part will outline the necessary steps and provide images of the final result without detailing every connection. The schematic includes a regulator circuit, pull-up and line resistors for D+ and D- connected to PD2 and PD3, and the ATtiny2313 with a RESET pull-up and connections for MISO, MOSI, SCK, and RESET to a 6-pin programming header, along with a 12 MHz crystal. The connections should be made by reconstructing the power circuit from the first part in one corner of the breadboard. Instead of connecting 5V from USB to the positive power rail, it should be directly connected to the input pin of the regulator, as 5V is not needed elsewhere. For those needing a refresher on connecting the RESET pull-up and 6-pin header, it is advised to refer to a relevant tutorial, noting that pin locations differ between the ATtiny2313 and ATtiny45. The crystal connection is straightforward; connect the two legs of the crystal to PA0 and PA1, and place ceramic capacitors between each leg and ground for stability. It is recommended to test the ATtiny2313 setup with a simple test program provided, which blinks an LED on PB0. The program can be modified to verify that the LED blinks 12 times faster after updating the fuse bits for the 12 MHz clock. The low fuse value should be set to 0xEF using the avrdude command. Once the lfuse is updated, the chip should be reset or reprogrammed to confirm the LED blinks at the expected rate. The blink example's F_CPU should then be updated to 12000000L and reflashed to ensure the blinking returns to 1 Hz speed. The tutorial will continue with the third part, which will cover the basic V-USB setup for the circuit.

The schematic described involves several critical components and connections essential for the operation of the ATtiny2313 with the V-USB library. The power circuit is a key element, where a voltage regulator is utilized to step down the 5V from the USB to 3.3V, ensuring that the ATtiny2313 operates within its voltage specifications. The D+ and D- lines are equipped with pull-up resistors to ensure proper USB communication, and the use of zener diodes is recommended to protect the ATtiny2313 from voltage spikes.

The ATtiny2313 microcontroller serves as the central processing unit, with its pins configured for USB communication and programming. The RESET pin is connected to a pull-up resistor to ensure that the microcontroller can be reset correctly, while the programming header allows for easy reprogramming and debugging. The addition of a 12 MHz crystal oscillator is crucial for maintaining accurate timing and functionality, particularly when operating at higher frequencies. The ceramic capacitors connected to the crystal are essential for stabilizing the oscillator circuit, which is vital for reliable operation.

Testing the setup with the provided blink program is an effective way to ensure that all connections are functioning correctly. The adjustments to fuse settings allow for configuration of the clock speed, enabling the microcontroller to operate at its optimal frequency. This also provides a practical demonstration of the impact of clock settings on the performance of the LED blinking rate.

Overall, this schematic and setup provide a robust foundation for further development and experimentation with USB applications using the ATtiny2313 and the V-USB library, paving the way for more advanced projects in the future.This is the second part of my USB tutorial for ATtiny2313 and V-USB library. In the first part we learned how to get 3. 3V from USB to power our circuits. In this part, we will expand our setup with following parts: Update: Some people have noted that the setup I`m using here runs ATtiny2313 at 12 MHz with only 3. 3V VCC, which is outside the specif ied range (frequencies over 10 Mhz require 4. 5V or more). I`ve never had any problems, and many others have succeeded with this setup, but if you encounter persistent problems, I suggest you to power the ATtiny2313 straight from 5V of the USB line and use zener diodes on D+ and D- lines to drop their voltage, as is done in my later tutorial with the ATtiny85 microcontroller. This time I will not walk you through every connection. Instead, I`ll just outline the steps needed and show the pictures of end result. Here is the schematic we`re building: Basically it`s the regulator circuit we just built, pullups and line resistors for D+ and D- in the USB bus going to PD2 and PD3, and the ATtiny2313 with RESET pullup and MISO, MOSI, SCK and RESET connected to the 6-pin programming header, and a 12 MHz crystal.

An easy order to make these connections is to: Reconstruct the power circuit from part 1 in one corner of the breadboard. Instead of connecting 5V from USB to the positive power rail, connect it straight to the input pin of the regulator we won`t be needing 5V anywhere else By the way, if you need a refresher on how to connect the RESET pullup and 6-pin header and use the programmer, check out this excellent tutorial wiring is the same but of course pin locations in ATtiny2313 are different than in ATtiny45.

Connecting the crystal is also very easy, just connect the two legs of the crystal to PA0 and PA1, and add ceramic capacitors between each leg and ground (they need to be close to the crystal to make it stable). Now for the good stuff! If you did not already test that your ATtiny2313 setup is working, I suggest you to do that now. A good way to do it is to use the following simple test program: #include #define F_CPU 1000000UL // 1 MHz #include int main() { DDRB |= 1; // LED on PB0 while(1) { PORTB |= 1; // Turn LED on _delay_ms(500); PORTB &= ~1; // Turn LED off _delay_ms(500); } return 1; } This test program has the great additional feature that once we update the fuse bits for 12 MHz clock, it`s easy to verify that blink gets 12x faster.

Now for ATtiny2313, we want to disable the internal clock/8 division and use an over 8 MHz clock with 14 clock cycles + 4. 1 ms startup delay (CLKSEL=1111, SUT=10). This means that low fuse should be 0xEF, which is done using avrdude command -U lfuse:w:0xef:m  (without quotes).

In case you are using any other part or just want to check it yourself, use the excellent AVR Fuse Calculator. Once you have updated the lfuse, reset or reprogram the chip to verify that the LED is now blinking 12 times per second.

Update the blink example`s F_CPU to 12000000L and reflash to verify that the blinking returns to 1 Hz speed. Congratulations, everything is now ready for the V-USB -part of the tutorial! I will stop here for tonight, and post the third part of this tutorial shortly, covering the basic V-USB setup for our circuit.

🔗 External reference