📜 ⬆️ ⬇️

Another software UART on ATtiny13



Hello to all habouiers. Quite recently, Comrade Himura published his UART article in ATtiny13 or How to get data from the MC for 52p , and then I remembered that I have, besides previous work on this topic, namely the Three-Channel UART ADC on ATtiny13 , there is something else, some of which I cut out from a rather curious project Happy Christmas and Happy New Year wishes from Attiny13 , there is also a software UART, both reading and sending, and something else on SPI, I generally recommend looking to anyone who is interested, though the resource is English-speaking.
Here is a video of work:



Cutting off all that is unnecessary for me from the code of Comrade Vinod.S , it turned out:
')
Sources
#define F_CPU 9600000 #include <avr/io.h> #include <util/delay.h> #define SOFT_TX_PIN (1<<PB1) // PB1    TXD #define SOFT_TX_PORT PORTB #define SOFT_TX_DDR DDRB int helloHabr = 0; int main (void) { uart_tx_init (); //  . UART while (1) { helloHabr++; uart_print("Hello Habr, I'm ATtiny13 "); //  num_to_str(helloHabr, 4); // 0..9999 ..  4    uart_print(" counds"); uart_print("\r\n"); //     _delay_ms(1000); } return 0; } void uart_tx_init () { TCCR0A = 1 << WGM01; // compare mode TCCR0B = 1 << CS00; // prescaler 1 SOFT_TX_PORT |= SOFT_TX_PIN; SOFT_TX_DDR |= SOFT_TX_PIN; OCR0A = 75; //115200 baudrate at prescaler 1 } //     void num_to_str(unsigned int value, unsigned char nDigit) { switch (nDigit) { case 4: uart_send_byte((value / 1000) + '0'); case 3: uart_send_byte(((value / 100) % 10) + '0'); case 2: uart_send_byte(((value / 10) % 10) + '0'); case 1: uart_send_byte((value % 10) + '0'); } } void uart_print(char *str) { byte i = 0; while (str[i]) { uart_send_byte(str[i++]); } } //bitbanged UART transmit byte void uart_send_byte (unsigned char data) { unsigned char i; TCCR0B = 0; TCNT0 = 0; TIFR0 |= 1 << OCF0A; TCCR0B |= (1 << CS00); TIFR0 |= 1 << OCF0A; SOFT_TX_PORT &= ~SOFT_TX_PIN; while (!(TIFR0 & (1 << OCF0A))); TIFR0 |= 1 << OCF0A; for (i = 0; i < 8; i++) { if (data & 1) SOFT_TX_PORT |= SOFT_TX_PIN; else SOFT_TX_PORT &= ~SOFT_TX_PIN; data >>= 1; while (!(TIFR0 & (1 << OCF0A))); TIFR0 |= 1 << OCF0A; } SOFT_TX_PORT |= SOFT_TX_PIN; while (!(TIFR0 & (1 << OCF0A))); TIFR0 |= 1 << OCF0A; } 


Arduino IDE swallowed it up without any problems:



All this takes 470 bytes, that is, less than half of the memory ATtiny13, you can still program a lot, if you skillfully use the resources of the microcontroller.

As you can see, the Hello Hbr, I'm ATtiny 13 character set is displayed using the uart_print ("...") function ; and the value of variables, for example, let the variable be called value, then the output in a line of content will be carried out using the function num_to_str (value, 4); where 4 is the number of digits, in this case, you can output a value from 0 to 9999. The lines uart_print ("\ r \ n"); finish the line when outputting to the UART and the transition to the new one is carried out, by analogy the Enter button on the keyboard. The port that will work as TXD is specified in lines #define SOFT_TX_PIN (1 << PB1) . On the ports map it looks like this:



And finally - ATtiny13 is clocked from an internal RC chain tuned to a frequency of 9.6 MHz, the divisor by 8 is disabled, this is specified by the fusion bit lf 0x7A .

Speed ​​UART'a 115200 baud (English baud).

And finally, the photo session:
image

image

To the pictures did not disappear.

References:

UART in ATtiny13 or How to display data from the MC for 52p;

Three-channel UART ADC on ATtiny13;

Happy Christmas and Happy New Year wishes from Attiny13 ;

ATtiny13 firmware and programming with Arduino (updated);

All my publications .

PS If you heat the microcontroller to ~ 60 degrees, then the garbage will start coming, checked it personally, but this is simply the price of ATtiny13.

Source: https://habr.com/ru/post/251141/


All Articles