├── README.md ├── LICENSE.txt └── main.c /README.md: -------------------------------------------------------------------------------- 1 | Software ("bit-bang") UART Transmitter ( 8 data bits, 1 stop bit, no parity ) for Attiny24A 2 | this can easily be ported to other AVR8 µC's or might even work without changes on many of them 3 | 4 | the Baud rate is calculated as follows: 5 | BAUD = F_CPU / ( TIMER0_PRESCALER * (OCR0A + 1) 6 | so we can modify the prescaler and/or the OCR0A value to achieve a certain baud rate. 7 | 8 | In this example I am using the internal 8MHz oscillator as clock source, so F_CPU=8000000 9 | and a baud rate of 9600. 10 | 11 | The program can be compiled with avr-gcc and the avr-libc libraries. -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Marcel Meyer-Garcia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | /* Software ("bit-bang") UART Transmitter (8 data bits, 1 stop bit, no parity) 2 | for Attiny24A/44A/84A using the internal 8MHz oscillator as clock source 3 | 4 | (c) 2018 Marcel Meyer-Garcia 5 | see LICENCE.txt 6 | */ 7 | 8 | /* NOTE: since the internal 8MHz oscillator is not very accurate, the value for OCR0A can be tuned 9 | to achieve the desired baud rate (nominal value is 103) 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | // change these to use another pin 17 | #define TX_PORT PORTB 18 | #define TX_PIN PB0 19 | #define TX_DDR DDRB 20 | #define TX_DDR_PIN DDB0 21 | 22 | volatile uint16_t tx_shift_reg = 0; 23 | 24 | 25 | void UART_tx(char character) 26 | { 27 | uint16_t local_tx_shift_reg = tx_shift_reg; 28 | //if sending the previous character is not yet finished, return 29 | //transmission is finished when tx_shift_reg == 0 30 | if(local_tx_shift_reg){return;} 31 | //fill the TX shift register witch the character to be sent and the start & stop bits (start bit (1<<0) is already 0) 32 | local_tx_shift_reg = (character<<1) | (1<<9); //stop bit (1<<9) 33 | tx_shift_reg = local_tx_shift_reg; 34 | //start timer0 with a prescaler of 8 35 | TCCR0B = (1<>= 1; 92 | tx_shift_reg = local_tx_shift_reg; 93 | //if the stop bit has been sent, the shift register will be 0 94 | //and the transmission is completed, so we can stop & reset timer0 95 | if(!local_tx_shift_reg) 96 | { 97 | TCCR0B = 0; 98 | TCNT0 = 0; 99 | } 100 | } 101 | --------------------------------------------------------------------------------