DIY circuits on attiny2313. We connect the button to the ATtiny2313 microcontroller, a simple program. Compilation and firmware of the program in MK

How to assemble a simple circuit, how to connect a programmer to an ATtiny2313 microcontroller, how to write the simplest program in C language and how to flash the ATtiny2313 microcontroller with our program, you will find all this in this article.

First of all, we need a programmer, there are many types of programmers, which programmer should we choose?
There are ordinary programmers in which you need to insert a microcontroller, flash it, remove the microcontroller and then insert it into our board to see the result, and this sequence will have to be done hundreds of times at first, this option, in my opinion, is not convenient.
Our microcontroller ATtiny2313 Supports ISP (In-System Programming) function via SPI port, this in-circuit programming use case ISP in my opinion the most convenient and fastest, because... there is no need to remove the microcontroller from our board after each firmware update; you can program the microcontroller hundreds of times and immediately, without disconnecting the programmer from the computer and the board, see the result after flashing the microcontroller, the debugging process software installation of an amateur radio device is noticeably simplified and the time spent on it is reduced.
You can make an in-circuit ISP programmer yourself, there are many on the Internet simple circuits how this is done via LPT, COM port, for example a programmer PonyProg You can find diagrams on the Internet on how to make it.

This article will discuss working with an in-circuit ISP programmer for microcontrollers AVR (PX-400) it works via COM port.
If you do not have a COM port on your computer, you will also need an adapter with USB port to the COM port, there are also many varieties of such adapters, I recommend the adapter that I worked with: UCON-232S USB to Serial port converter board
Photo of the programmer PX-400, adapter UCON-232S USB , Datasheet ATTiny2313

Let's look at all the details of this scheme in more detail:
(Just in case, I bought all the parts, programmer, adapter (from USB to COM port) at chipdip.ru)

1 - PBD-20 board socket 2.54mm 2x10 straight- I did this for convenience, to make it easier to check signals from the microcontroller pins; this point could not be done.
2 - SCS-20 DIP panel 20 pins- we solder the panel to the board so that it is possible to replace the microcontroller in the board if necessary,
ATtiny2313-20PU, DIP20, MCU, 5V, 1K-Flash, 12MHz- We insert the microcontroller into the DIP panel.
3 - Quartz crystal 4.000 MHz (truncated) HC-49S- Quartz crystal 4 MHz
4 - Ceramic capacitor K10-17B imp. 22pF NPO,5%,0805- Two 22pF ceramic capacitors
5 - 78M05 (+5V, 0.5A) TO220- A 5V voltage stabilizer supplies the microcontroller with stabilized power of no more than +5V, in this case I got 4.4V, which is enough.
6 - NP-116 power plug 1.3x3.4x9.5mm MP-331 (7-0026c)- The power plug was soldered to the old one charger from mobile phone DC 5.7V/800mA
7 - DS-213 power socket on board- where is the power supply for the NP-116 plug, for easy power connection
8 - IDC-10MS (BH-10), straight plug- Plug for connecting an in-circuit ISP programmer
9 - Constant resistor 0.25W 150 Ohm- Three 150 Ohm resistors on the MISO, SCK, MOSI pins
10 - Constant resistor 0.25W 47 Ohm- One 47 Ohm resistor per RESET pin
11 - Tact button h=5mm, TC-0103 (TS-A2PS-130)- Button reset RESET, after pressing the button, the program in the microcontroller starts from the beginning, the button could not have been created.
12 - Green LED d=3mm, 2.5V, 2mA - Performs the function of an indicator, this item could not be done.
13 - Constant resistor 0.25W 110 Ohm- A resistor for the LED so that there is 2V on the LED, this step can be omitted
14 - Two wires connected to the LED, to check signals from the microcontroller pins, this step could not be done
15 - Dip-RM printed breadboard 100x100mm

Points 3 and 4 Work as a single unit, like an external clock generator, these points can be omitted if you do not place high demands on the accuracy and stability of the internal RC Oscillator, the internal RC Oscillator has an error of about 10% and the accuracy may be affected by change temperature.

So you have downloaded and installed Atmel Studio:
Let's launch Atmel Studio and write the simplest program in C language blinking an LED:
Click: New project... \ AVR GCC \ C \ C Executable Project
Specify the folder where to save the project and the project name, for example Test1, and click OK.
From the list, select our ATtiny2313 microcontroller and click OK.
We erase everything that appears in the window and paste our program code below:

#define F_CPU 4000000L //Specify the frequency of our external quartz 4 MHz
#include
#include
int main(void)
{
//Set all PORTB pins as outputs
DDRB=0xFF;//Register of the direction of information transfer (1-output, 0-input)
while(1)
{
//Data register PORTB (used to output information)
PORTB=0b00000001;//We supply 1 to port 12 of MK PB0 - turn on the LED
PORTB=0b00000000;//Apply 0 to port 12 of MK PB0 - turn off the LED
_delay_ms(1000);//Delay 1 sec.
}
}

Go to the menu Build\Configuration manager\Active solution configuration\
Choose Release, press Close
We did this so that we could have a folder in the project Release, which I will talk about below.

Click F7, ready, our application has compiled!
To flash the ATtiny2313 microcontroller firmware, we need only one file with the extension HEX
It is located in our project folder: ...
Please note the file Test1.hex just take it from the folder Release !
Don't get confused, because... folder Debug there is also a file Test1.hex, but this file still contains debugging information and because of this, you will not be able to flash with this file. it usually happens big size and will not fit in the memory of the MK.

We found the .hex file, now we need a program to flash the ATtiny2313 microcontroller, there are many such programs, but we will use the program: Avr-Osp II
Download:

We connect the programmer to our circuit, and be sure to supply power to the circuit!

Launch the program Avr-Osp II, specify the path to the file in the FLASH section... \Test1\Test1\Release\Test1.hex, check the boxes in the program and press the button Program that's it, microcontroller ATtiny2313 is flashed!

What is the advantage of in-circuit ISP programmers? Now, without disconnecting the wires from our circuit, you can make changes in the program, and, as described above, flash the microcontroller and immediately see the result.

Please leave questions and comments on our forum

AVR RISC architecture:

RISC (Reduced Instruction Set Computer). This architecture has large set instructions, the majority of which are executed in 1 machine cycle. It follows that compared to previous microcontrollers based on CISC architecture (for example, MCS51), RISC microcontrollers are 12 times faster.

Or if we take a certain level of performance as a basis, then to perform this condition microcontrollers based on RISC (Attiny2313) require 12 times less clock frequency of the generator, which leads to a significant reduction in power consumption. In this regard, it becomes possible to design various devices on Attiny2313, using battery power.

Operational Storage Device (RAM) and non-volatile memory of data and programs:

  • 2 KB of self-programmable Flash program memory that can provide 10,000 write/erase repeats.
  • 128 Bytes of EEPROM writeable data memory that can provide 100,000 write/erase repeats.
  • 128 Bytes SRAM memory (read-only RAM).
  • It is possible to use the function to protect program code and EEPROM data.

Peripheral properties:

  1. Microcontroller Attiny2313 equipped with an eight-bit timer-counter with a separately installed prescaler with a maximum coefficient of 256.
  2. There is also a sixteen-bit timer-counter with a separate prescaler, capture and comparison circuit. The timer is clocked - the counter can vary from external source signal, and from internal.
  3. Two channels. There is an operating mode of fast PWM modulation and PWM with phase correction.
  4. Internal analog comparator.
  5. Watchdog timer (programmable) with internal oscillator.
  6. Serial Universal Interface (USI).

Special technical indicators of Attiny2313:


  • Idle— Idle mode. In this case, only the central processor stops working. Idle does not affect the operation of the SPI, analog comparator, A/D converter, counter timer, watchdog, or interrupt system. In fact, it only stops kernel synchronization central processor and flash memory. The Attiny2313 microcontroller returns to normal operation from Idle mode by an external or internal interrupt.
  • Power-down— The most economical mode, in which the Attiny2313 microcontroller is actually switched off from power consumption. In this state, the clock generator stops and all peripherals are turned off. Only the interrupt processing module from an external source remains active. When an interrupt is detected, the Attiny2313 microcontroller exits Power-down and returns to normal operation.
  • Standby– the microcontroller switches to this power consumption standby mode using the SLEE command. This is similar to shutting down, the only difference being that the clock continues to run.

Input-output ports of the Attiny2313 microcontroller:

The microcontroller has 18 I/O pins that can be programmed based on design needs specific device. The output buffers of these ports can withstand a relatively high load.

  • Port A (PA2 - PA0) – 3 bits. Bidirectional I/O port with programmable pull-up resistors.
  • Port B (PB7 - PB0) – 8 bits. Bidirectional I/O port with programmable pull-up resistors.
  • Port D (PD6 - PD0) – 7 bits. Bidirectional I/O port with programmable pull-up resistors.

Supply voltage range:

The microcontroller operates successfully with a supply voltage from 1.8 to 5.5 volts. Current consumption depends on the operating mode of the controller:

Active mode:

  • 20 µA at a clock frequency of 32 kHz and a supply voltage of 1.8 volts.
  • 300 µA at a clock frequency of 1 MHz and a supply voltage of 1.8 volts.

Power Saving Mode:

  • 0.5 µA at a supply voltage of 1.8 volts.

(3.6 Mb, downloaded: 5,934)

The device of this article is working with SD cards. The topic is old and quite hackneyed, but the use of SD cards is worth writing about it again.
In general, SD cards (SDC, SD Card) have many advantages and are very simple and convenient to use in small embedded projects. A number of factors contribute to this:
- a very simple interface for interacting with the card (implemented via SPI);
- high operating speed (the microcontroller is capable of transferring data from an SD card at a speed close to 10 Mbit/s);
- low power consumption (literally a couple of milliamps - no more);
- small sizes;
- availability and low cost.
SD cards have virtually no drawbacks (except, perhaps, for their initialization procedure :)).

1. Introduction.

I called the device described in this article SD Card Talking Device. A little pretentious;), but the name makes it clear that this is a talking device. It is intended for voicing your projects. In short, it works as follows: numbered sound files are recorded on the SD card, which the device plays at your command. The scope of application is quite wide - warning systems, toys, robots, smart House etc. The dimensions of the device are quite modest (it could be smaller, but I deliberately chose the ATtiny2313 microcontroller, which is cheaper and easier to get). I tried to place the main emphasis on simplicity and maximum functionality.
Looking ahead, let's see what should happen in the end:

Is such a device useful? Then let's collect!

2 Memory card.

The device uses an SD memory card. I have already written about the reasons for the choice, I will only add that SD cards are becoming almost a standard memory card for mobile devices. Even manufacturers who fanatically promoted/are promoting their type of memory cards are slowly starting to use SD cards. The reason for such popularity was probably the low price of these cards. For amateur devices, the SD card is, in fact, the only card suitable for use, and the reason for this is the simple interface for working with it.

The SD card has come a long way in evolution and has several options for its implementation (MMC - as an SD card option, SD ver1, SD ver2, SDHC, SDXC). The procedure for communicating with the card is simple and universal for all types of cards, but putting it into operation (initializing the card) is a rather ambiguous and confusing process, with ritual “jerking” of the card, sending empty “dummy” commands and other incomprehensible things (in short, dancing with tambourines required:)). The specification for the SDC protocol itself describes the initialization process in quite some detail, which is understandable; there are a lot of card manufacturers, each with their own hardware, with their own characteristics... What am I getting at? - I tried to make the initialization procedure as universal as possible, but be prepared for the fact that some cards will not work. Therefore, if something is not going well with your device, try another memory card - this may be the reason.

IN this device SD cards up to 2 GB in size are supported. Everything higher (SDHC and SDXC) is not supported.
It makes no difference for the device what form factor the card is (SD, MiniSD or MicroSD), but you must connect it correctly, according to the card pinout.

3 File system.

The device uses cards with the FAT16 file system. This system is ideally suited for devices like ours, as it is simple and easy to implement (FAT12 and FAT32, in principle, are also not difficult to implement, but this is impractical due to the lack of any advantages compared to FAT16).

There are no special requirements for formatting the card - it can be formatted in any available device. Standard formatting windows quite suitable for these purposes.

For the device to operate correctly, sound files located on the SD card must meet certain requirements:
a) The file format must be uncompressed WAV.
The file parameters are as follows:
- Bitrate - sampling frequency (Frequency) - 32000 Hz;
- Number of channels (Channels) - 1 (mono);
- Sample size - 8 bits.
Another possible reduction is WAV PCM 8U

b) The file must be named in a special way. In order for the device to know which file is the first, second, third, etc. The first character of the file name must be a capital letter of the Latin alphabet (the rest of the name, like the file extension, is ignored).
For example, the following file names would be correct:
A_Lai_dog.wav - first track
B-This is the second track.wav - the second track
With Warning! Error!.wav - third track

c) To use additional capabilities of the device, files can be located in two folders named “1” and “2”. The device has a switch for selecting the active folder, that is, the same command to start playback can play tracks from folder “1” or “2”, depending on the level on the switching input (a kind of selection of the sound scheme - a very useful thing!) . If one of the folders (or both) does not exist, the files are played from the root directory.

You can store any other files along with audio tracks, provided that they do not create conflicts with their names (it is better to put them in a separate directory, then you will not have to pay attention to how they are named there).

d) Due to the small amount of SRAM on the ATtiny2313, it is impossible to create a buffer for pre-reading data, so data from the file is directly output for playback. Accordingly, there is no way (there is not enough time) to search for file fragments using the FAT table. In other words, the files written to the card must not be fragmented.

In fact, this is not a big problem, since any operating system always tries to write the file as a whole piece and as long as you have space on the card, any actions with the files (deleting, copying, renaming) will not affect their integrity. If you have a very small card or you have filled a large card to capacity, in order to be sure of the integrity of the files, simply copy them to HDD computer, format the card and get the files back.

4 Scheme. Printed circuit board.

The device diagram is as simple as possible. In fact, apart from the microcontroller itself and the SD card, there is nothing in it. For myself, I made a signet for SMD components, since I plan to use this device in a place with limited dimensions. If the dimensions are not critical for you, you can assemble the circuit on a breadboard in the DIP version. In the case of a breadboard, assembling the device will take you, at most, 15 minutes. The permissible supply voltage for an SD card is from 2.7 to 3.6 volts. The microcontroller also works normally in this interval, so there is no need to use any matching components. I checked the operation of the entire device and with a power supply of 5 volts - everything worked fine, but I do not recommend doing this on permanent basis, since different cards may react differently to overvoltage. I used an adapter as a microSD cardholder, soldering it directly to its contacts. If you need smaller dimensions, it is better to use a real cardholder for microSD.

To flash the microcontroller firmware, the same connector is used as for the SD card, so you will have to think about how to connect the programmer to it (I specially made an adapter).

After the board is soldered, you can flash the microcontroller.

A small gallery of the finished device:




A small nuance regarding the scheme.
When installing an SD card into a cardholder (connecting the card to a power source), a current surge is created and, accordingly, a voltage drop in the circuit (it seems that significant capacities are being charged in the card at this time). The drawdown is so significant that the microcontroller resets. I use this to start the card initialization procedure (installing the card restarts the microcontroller and the first thing the firmware does is search for and initialize the card). If you do not reset the microcontroller when installing a card (a powerful power supply or large smoothing capacitors), then you need to take care of the reset button in the circuit for manually resetting the microcontroller (this is if you plan to “hot” change cards).

5 Device operation.

As I wrote above, working with the device is very simple: copy the correctly named tracks to the SD card, insert the card into the cardholder, the device will automatically find the card, turn on the green LED - that’s it, the device is ready to play the tracks. Now you just need to select and start playing the track in the way that suits you best.

5.1 Device buttons and their actions.

I tried to make the device as functional as possible, so a lot of microcontroller legs are used for operating mode switches (this makes the device resemble a hedgehog :)). If you don’t need any function, just leave your leg “hanging” in the “air”.
Switch action:
- “Monster” - allows you to slow down (2 times) the playback of the track - creating the effect of a low voice. The switch works “on the fly” - the speed changes upon switching;
- “Helium” - speeds up the playback of the track (by 1/3) - creating the effect of a high-pitched voice. The switch works on the fly;
- “Repeat” if this switch is shorted to ground, the selected track will play endlessly (until the switch is opened). This can be useful, for example, if you need to create a certain sound background - the sound of rain, a burning fire, the murmuring of a stream...;
- “Select / Play” button that starts the track for playback (description below);
- “Select track” - setting the number of the track being played (description below);
- “Dir1 / Dir2” - select a sound scheme (description below).

5.2 Start playback.

There are three ways to start playing a specific track:
- by sending a capital letter of the Latin alphabet via UART, playback of the file containing this letter at the beginning of the name immediately begins;
- if using “Select track” the file number is selected (binary code 0001=”A”, 0010=”B”, etc. 1 - leg is closed to the ground, 0 - “hanging” in the “air”), then the “Select / Play” button will start the corresponding file for playback;
- if nothing is selected using “Select track” (0000 - legs “hang” in the “air”)), then by pressing the “Select / Play” button a certain number of times, we launch the corresponding track (1 time = “A”, 2 times =”B”, etc.).

5.3 Sound schemes.

A very useful feature is the function of selecting one of two sound schemes. This means that the “Dir1 / Dir2” switch selects the folder on the card from which the track will be played.

There are a lot of applications: messages in Russian and English (educational toys), children's and adult voices, noises of flowing water and burning fire, cat/dog, good and evil policeman :), calming/invigorating sounds and a bunch of other similar options.

For example, you need your device to be able to communicate in a male and female voice. It is implemented like this:
- create two sets of messages, respectively, in the female and male version;
- file numbering for both options is the same. Don’t forget that the device “sees” only the first letter in the file name, so you can make the names more understandable for yourself, for example, “S_Waiting for command_male.wav” and “S_Waiting for command_female.wav” are quite correct;
- copy the set of men’s messages into folder “1”, and the women’s messages into folder “2”.
Now, depending on the state of the “Dir1 / Dir2” switch, the same command will play tracks from the “male” or “female” folder.

5.4 Indication of device operation.

Since Teeny2313 has very few legs, and almost all of them are used for switches, I had to sacrifice a normal indication, and in return attach something NOT normal. To indicate different operating modes, only one leg of the microcontroller is used, to which two LEDs are connected - red and green (or whichever you prefer). The different operating modes of the device are indicated by a specific color code:
- red LED flashes - there is no SD card or its type is not supported by the device;
- the red LED is on - the SD card is supported and has been successfully initialized, but the card is not formatted in FAT16;
- green LED is on - the SD card has been successfully initialized, the required one has been found file system and the device is ready to play the track - waiting for a command;
- green LED flashes - the device is playing a track;
- green lights up, red lights up briefly, green lights up again - track not found;
- green lights up, goes out briefly and turns green again - the track selection key is pressed.

5.5 Debugging information.

To make it easier to find problem areas (if the device does not want to work), I duplicated each initialization stage in the program with messages via UART. After each successful step, the corresponding character is sent to the UART:
- “S” - (Start) the microcontroller peripherals are initialized normally;
- “C” - (Card Init) The SD card is initialized normally and is supported;
- “F” - (FAT Init) FAT system supported;
- “1” - (No 1 Dir) there is no folder “1” reading will be carried out from the root directory;
- “2” - (No 2 Dir) there is no folder “2” reading will be carried out from the root directory;
- “R” - (Ready) the device is completely ready - awaiting the command to start the track;
- In addition, each time a track is started, the capital letter of the track name is transmitted to the UART.

6 Tracks for dubbing your devices.

6.1 Converting tracks

If you didn’t find anything suitable in the library above, then you can get the necessary tracks on the Internet (there are many special sites for musicians and video editing, where large libraries of sounds have already been collected), in game installations (often gameplay sounds are divided into tracks and put into a separate folder). You can also cut sound effects from films and music compositions. The found tracks need to be converted into a format that the device supports. Let me remind you that the file format must be uncompressed WAV. 32000 Hz, 1 channel, 8 bit (WAV PCM 8U)
Any music editor is suitable for converting to this format, or, if you just need to convert a track without editing it -

Many household appliances and industrial automation devices of relatively recent production years have mechanical counters installed. They are products on a conveyor belt, turns of wire in winding machines, etc. In the event of a failure, finding a similar meter is not easy, and it is impossible to repair due to the lack of spare parts. The author proposes to replace the mechanical counter with an electronic one. An electronic counter, developed to replace a mechanical one, turns out to be too complex if it is built on microcircuits with a low and medium degree of integration (for example, the K176, K561 series). especially if a reverse account is needed. And in order to save the result when the power is turned off, it is necessary to provide backup battery nutrition.

But you can build a counter on just one chip - a universal programmable microcontroller, which includes a variety of peripherals and capable of solving a very wide range of problems. Many microcontrollers have a special memory area - EEPROM. Data written into it (including during program execution), for example, the current counting result, is saved even after the power is turned off.

The proposed counter uses the Attiny2313 microcontroller from the AVR family from Almel. The device implements reverse counting, displaying the result with cancellation of insignificant

hive on a four-digit LED indicator, storing the result in EEPROM when the power is turned off. An analog comparator built into the microcontroller is used to timely detect a decrease in supply voltage. The counter remembers the counting result when the power is turned off, restoring it when turned on, and, similarly to a mechanical counter, is equipped with a reset button.

The counter circuit is shown in the figure. Six lines of port B (РВ2-РВ7) and five lines of port D (PDO, PD1, PD4-PD6) are used to organize dynamic indication of the counting result on the LED indicator HL1. The collector loads of phototransistors VT1 and VT2 are resistors built into the microcontroller and enabled by software that connect the corresponding pins of the microcontroller to its power supply circuit.

An increase in the counting result N by one occurs at the moment the optical connection between the emitting diode VD1 and the phototransistor VT1 is interrupted, which creates an increasing level difference at the INT0 input of the microcontroller. In this case, the level at the INT1 input must be low, i.e., the phototransistor VT2 must be illuminated by the emitting diode VD2. At the moment of a rising differential at the INT1 input and a low level at the INT0 input, the result will decrease by one. Other combinations of levels and their differences at the inputs INT0 and INT1 do not change the counting result.

Once the maximum value of 9999 is reached, counting continues from zero. Subtracting one from the zero value gives the result 9999. If counting down is not needed, you can exclude the emitting diode VD2 and phototransistor VT2 from the counter and connect the INT1 input of the microcontroller to the common wire. The count will only continue to increase.

As already mentioned, the detector of a decrease in supply voltage is the analog comparator built into the microcontroller. It compares the unstabilized voltage at the output of the rectifier (diode bridge VD3) with the stabilized voltage at the output of the integrated stabilizer DA1. The program cyclically checks the state of the comparator. After disconnecting the meter from the network, the voltage on the rectifier filter capacitor C1 drops, and the stabilized voltage remains unchanged for some time. Resistors R2-R4 are selected as follows. that the state of the comparator in this situation is reversed. Having detected this, the program manages to write the current counting result to the EEPROM of the microcontroller even before it stops functioning due to the power being turned off. The next time you turn it on, the program will read the number written in EERROM and display it on the indicator. Counting will continue from this value.

Due to the limited number of microcontroller pins, to connect the SB1 button, which resets the counter, pin 13 was used, which serves as the inverting analog input of the comparator (AIM) and at the same time as the “digital” input of PB1. The voltage divider (resistors R4, R5) here sets the level perceived by the microcontroller as high logical. When you press the SB1 button, it will become low. This will not affect the state of the comparator, since the voltage at the AIN0 input is still greater than that at AIN1.

When the SB1 button is pressed, the program displays a minus sign in all digits of the indicator, and after releasing it, it starts counting from zero. If you turn off the power to the meter while the button is pressed, the current result will not be written to the EEPROM, and the value stored there will remain the same.

The program is designed in such a way that it can be easily adapted to a meter with other indicators (for example, with common cathodes), with different wiring printed circuit board etc. A slight correction of the program will be required when using quartz resonator to a frequency that differs by more than 1 MHz from the specified one.

When the source voltage is 15 V, measure the voltage at pins 12 and 13 of the microcontroller panel relative to the common wire (pin 10). The first should be in the range of 4...4.5 V, and the second should be more than 3.5 V, but less than the first. Next, the source voltage is gradually reduced. When it drops to 9 ... 10 V, the difference in voltage values ​​at pins 12 and 13 should become zero and then change sign.

Now you can install the programmed microcontroller into the panel, connect the transformer and apply mains voltage to it. After 1.5...2 s you need to press the SB1 button. The counter indicator will display the number 0. If nothing is displayed on the indicator, check the voltage values ​​at the AIN0.AIN1 inputs of the microcontroller again. The first must be greater than the second.

When the counter has been successfully launched, all that remains is to check the correctness of the count by alternately shading the phototransistors with a plate opaque to IR rays. For greater contrast It is advisable to cover the indicators with a red organic glass filter.

Today we try to take advantage of more simple microcontroller ATtiny2313 and connect a symbolic one to it LCD display, containing two lines of 16 characters.

We will connect the display in a standard way 4-bit way.

First, let's start, of course, with the microcontroller, since we are already very familiar with the display from previous lessons.

Let's open the controller datasheet ATtiny2313 and see its pinout

We see that this controller exists in two types of cases, but since it came into my hands in a DIP case, we will consider this particular version of the case, and in principle, they do not differ much, except in appearance, since the number of legs is the same - in 20.

Since there are 20 legs compared to the 28 legs of the ATMega8 controller, which we have been working on all along and will continue to work on, then, accordingly, there will also be fewer possibilities.

In principle, everything that the ATmega8 had is here, the only thing is that there are fewer port claws. But since the task before us is to try to connect it via the SPI bus with another controller, this does not depress us much.

There are some other differences, but they are minor and we will get to know them as needed.

Let's assemble a circuit like this (click on the picture to enlarge the image)

The display is connected to the pins of port D. PD1 and PD2 are to the control inputs, and the rest are connected to the pins of the display module D4-D7.

Let's create a project with the name TINY2313_LCD, transfer everything into it except the main module from the project for connecting the display to Atmega8.

Of course, some things will need to be redone. To do this, you need to carefully study which leg is connected to what. The E bus of the display is connected to PD2, and the RS bus is connected to PD1, so let's make changes to the file lcd.h

#definee1PORTD|=0b0000 01 00 // set line E to 1

#definee0PORTD&=0b1111 10 11 // set line E to 0

#definers1PORTD|=0b00000 01 0 // set the RS line to 1 (data)

#definers0PORTD&=0b11111 10 1 // set the RS line to 0 (command)

As we can see from the selection in bold, we haven’t had such drastic changes.

Now information inputs. Here we use legs PD3-PD6, that is, they are shifted by 1 point compared to the connection to Atmega8, so we will also correct something in the file lcd.c in function sendhalfbyte

PORTD&=0b 1 0000 111; // erase information on inputs DB4-DB7, leave the rest alone

But that is not all. We previously shifted the transmitted data by 4, but now, due to the above changes, we will only have to shift it by 3. Therefore, in the same function we will also correct the very first line

c<<=3 ;

That's all the changes. Agree, they are not that great! This is achieved by the fact that we always try to write universal code and use macro substitutions. If we had not spent time on this at one time, we would have had to correct the code in almost all the functions of our library.

In the main module, we do not touch the initialization of port D; let the whole module go into the output state, as in lesson 12.

Let's try to assemble the project and first see the result in Proteus, since I also made a project for it, which will also be in the attached archive with the project for Atmel Studio

Everything works great for us! This is how you can quickly remake a project for one controller for another.

Proteus is very good, but it’s always nicer to look at real details. The entire circuit was assembled on a breadboard, since I did not make or assemble a debug board for this controller. We will connect the programmer via a standard connector like this

Here's the whole diagram

Everything is standard here. Pull-up resistor to RESET, etc.

Now, before flashing the controller in avrdude, we need to select the controller and read its flash memory

Then go to the FUSES tab and set the fuses correctly. Since we don’t have a quartz resonator, we install the fuses this way




Top