This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:avr:usart [2010/02/28 14:34] – mikk.leini | en:avr:usart [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== USART ====== | ====== USART ====== | ||
| - | USART on universaalne sünkroonne jadaliides, UART aga selle lihtsustatud variant | + | USART is a universal synchronous serial interface; UART, is a simplified version |
| - | Kõikide seadistuste jaoks on olemas vastavad registrid, mida on üsna lihtne andmelehe abil seadistada. Natuke keerulisem on boodikiiruse seadmine. Taktsignaal andmete edastamiseks genereeritakse töötaktist ja kasutaja saab valida teguri | + | Every configuration option has corresponding registers, which are quite easy to configure using the datasheet. The baud rate, though, is a bit more difficult to set. The clock signal for data transmission is generated from the controller' |
| - | Kuna andmete edastus toimub protsessorist sõltumata ja oluliselt aeglasemalt, tuleb enne saatmist veenduda, et liides | + | Since data transmission takes place independently of the processor and much slower, it is necessary to confirm that the interface is ready for the next word before transmitting. This can be done by keeping an eye on the transmit buffer' |
| - | Sõna saabumist tähistab samuti spetsiaalne olekubitt. Lisaks on olekubitid vastuvõtmisel tekkiva kaadri vea, paarsuse vea ja vastuvõtja puhvri ületäitumise tähistamiseks. Puhvri ületäitumine tekib näiteks siis, kui eelmist saabunud sõna pole vastuvõtu puhvrist välja loetud | + | The arrival of a word is signified also by a special status bit. In addition to that, there are status bits to signify framing errors, parity errors and receive buffer overflows. Buffer overflow can occur when the last word is yet to be read from the buffer while a new one arrives |
| - | Muide, saatepuhver ja vastuvõtupuhver on füüsiliselt küll erinevad registrid, kuid jagavad sama mäluaadressi ja neil on ühine nimi. Ühine andmeregister toimib nii, et sellesse kirjutades jõuab sõna saatepuhvrisse ja sellest lugedes tuleb see vastuvõtupuhvrist. Veel ühe täpsustusena tuleks arvestada, et 9-bitiste andmesõnade puhul edastatakse ja loetakse üheksandat bitti hoopis ühe seaderegistri kaudu. | + | The transmit and receive buffers are physically separate registers, but share the same memory address and name. When writing to the shared register, the data is stored in the transmit buffer, and when reading from it, the data is read from the receive buffer. When using 9-bit words, the 9th bit is transmitted and read using one of the configuration registers. |
| - | ~~PB~~ | + | < |
| - | <box 100% round #EEEEEE|Näide> | + | <box 100% round #EEEEEE|Example> |
| - | Seadistada | + | Task: Configure an 8 MHz ATmega128' |
| <code c> | <code c> | ||
| Line 22: | Line 22: | ||
| int main() | int main() | ||
| { | { | ||
| - | // Boodi kiiruseks | + | // Set baud rate to 9600 bps. Formula: |
| - | // Jagamistegur | + | // multiplier |
| // UBRR = 8000000 / 16 / 9600 - 1 = ~51 | // UBRR = 8000000 / 16 / 9600 - 1 = ~51 | ||
| UBRR0H = 0; | UBRR0H = 0; | ||
| UBRR0L = 51; | UBRR0L = 51; | ||
| - | // Saatja lubamine | + | // Allow transmitting |
| UCSR0B = (1 << TXEN0); | UCSR0B = (1 << TXEN0); | ||
| - | // Asünkroonse režiimi seadistamine, andmesõna pikkuseks | + | // Configure asynchronous mode, set the word size to 8 bits |
| - | // 1 stop-bitt, keelatud paarsuse bitt. | + | // 1 stop bit, no parity bits. |
| UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); | UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); | ||
| - | // Ootame, kuni andmepuhver on tühi ehk eelmine sõna on saadetud | + | // Wait for the data buffer to empty (previous word to transmit) |
| - | // Selles näites ei oma see küll mõtet, sest saadetakse alles | + | // In this example there is no need to wait, as the first symbol is yet |
| - | // esimest märki, küll tasub seda teha rohkemate märkide korral. | + | // to be sent, but it should be done when transmitting more symbols. |
| while (!(UCSR0A & (1 << UDRE))) continue; | while (!(UCSR0A & (1 << UDRE))) continue; | ||
| - | // Märgi kirjutamine puhvrisse, kust see ka teele saadetakse | + | // Write the symbol to the buffer for transmitting |
| UDR0 = ' | UDR0 = ' | ||
| - | // Lõputu tsükkel | + | // Endless loop |
| while (1) continue; | while (1) continue; | ||
| } | } | ||