Skip to content Skip to sidebar Skip to footer

Widget HTML #1

Serial Communication Between ATtiny UART and Computer

Serial Communication Between ATtiny UART and Computer

ATtiny13 UART How to Serial Communication Between ATtiny and Computer - When we create a project, sometimes we need a debug method.

With this debug method we will send data to the serial, so we can see the response of the program we made on the serial monitor.

ATtiny generally does not have serial communication, like Arduino Uno. On the Arduino Uno, we can see pins 0 and 1 which are the TX and RX pins.

To outsmart the serial communication on the Avr ATtiny13, we will use USI (Universal Serial Interface). With USI we can communicate between attiny arduino ideas.


ATtiny13 Serial Schematics

Before you use the circuit, note that here we are using two circuits. Circuit for programming ATtiny13 and Circuit for serial communication.

To be able to program the ATtiny13 Arduino IDE, please read How to Burn the Bootloader and Program the ATtiny13 With Arduino. Program code you can see the program code below.

After you program the ATtiny13 with the program code below, we will then test the ATtiny13 for serial communication.

In this tutorial I use the FTDI module. Apart from that module, you can use other modules such as CH340 or the like.

Then connect the pins between FTDI and ATtiny13 as shown below:

ATtiny13 Serial, ATtiny13 UART, ATtiny13 Arduino IDE, AVR ATtiny13, ATtiny13 Serial Communication, ATtiny13 Schematics, ATtiny13 Serial Schematics, ATtiny13 FTDI
Rangkaian ATtiny13 Serial

After the circuit is complete, please open the serial monitor on the Arduino, then test the program code.


ATtiny13 Serial Progam Code

This program consists of a send (TX) and receive (RX) program. However, it should be noted that this program is not the same as serial programs in general.

To transmit data, this program use a Sring. However, to read data, this program is only able to accept integers.

Development still needs to be done. Here is an attiny idea arduino program.

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

volatile uint8_t uart;
uint8_t temp;
volatile uint8_t count;
volatile uint8_t start;
volatile uint8_t c;
volatile uint8_t uart_data;
volatile uint8_t Rece_bit;
volatile uint8_t rec;
volatile uint8_t usart_r;
volatile uint8_t baudSpeed;

ISR(INT0_vect) {
  rec = 1;// Interrupt is purely to determine the start bit when receiving,
  // rarely used, you can hang something else here
}

ISR(TIM0_COMPA_vect) {
  TIMSK0 = 0x00;
  TCCR0B = 0x00; // Single Timer, used to form a clear gap
  OCR0A = 0;     // between bits, both when receiving and when transmitting
  c = 1;
  TCNT0 = 0;
  TIMSK0 = 0x04;
  TCCR0B = 0x02;
  // The value "reset on match" is loaded each time from the variable
  OCR0A = baudSpeed;    // You can quickly change the UART speeds
  Rece_bit = 1;
}

int send (uint8_t data) {
  if (count >= 8) {
    PORTB |= (1 << 4); start = 0; temp = 0; c = 0; count = 0;
    TIMSK0 = 0; TCCR0B = 0; OCR0A = 0; goto nah;
  }

  if (c == 1) {
    if (start == 0) {
      temp = 0x80; start = 1;
      count--; goto razvet;
    }
    temp = data;
    temp = temp >> count;
    temp = temp << 7;
razvet:
    switch (temp) {
      case 0x80 : PORTB &= ~(1 << 4);  break;
      case 0x00 : PORTB |= (1 << 4);   break;
    }
    count++; c = 0;
  }
nah:;
}

void mySerial_print(char *text) {
  while (*text) {
    UART_trans(*text++);
  }
}

void mySerial_println(char *text) {
  while (*text) {
    UART_trans(*text++);
  }
  UART_trans('\n');
}

void itoa(uint16_t n, char s[]) {
  uint8_t i = 0;
  do {
    s[i++] = n % 10 + '0';
  }
  while ((n /= 10) > 0);
  s[i] = '\0';
  // Reversing
  uint8_t j;
  char c;
  for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
    c = s[i];
    s[i] = s[j];
    s[j] = c;
  }
}

void send_num(char *text, uint16_t n) {
  char s[6];
  itoa((uint16_t)n, s);
  mySerial_print(text);
  mySerial_print(s);
}

int UART_trans(uint8_t data) {
  uint8_t f;
  data = ~data;
  baudSpeed = 123;
  TIMSK0 = 0x04;
  TCCR0B = 0x02;
  for (f = 0; f < 10; f++) {
    while (c == 0);
    send(data);
  }
  start = 0; temp = 0; c = 0; count = 0;
  TIMSK0 = 0; TCCR0B = 0; OCR0A = 0;
  baudSpeed = 0;
}

int UART_receiv(void) {
  uint8_t a;
  usart_r = 0;

  MCUCR = 0x02; // INT0 Interrupt
  GIMSK = 0x40; // INT0 Interrupt

  while (rec == 0); // Wait until the start bit happens
  MCUCR = 0; // INT0 Interrupt
  GIMSK = 0; // INT0 Interrupt
  baudSpeed = 123;
  TIMSK0 = 0x04;
  TCCR0B = 0x02;
  rec = 0;
  TCNT0 = 0xDC;
  for (a = 0; a < 9; a++) {
    while (Rece_bit == 0);

    if (bit_is_set(PINB, 1)) {
      usart_r |= (1 << 7);
    } else {
      usart_r &= ~(1 << 7);
    }
    usart_r = usart_r >> 1;
    Rece_bit = 0;
  }
}

int main(void)
{
  DDRB &= ~(1 << 1); // set to RX (INPUT)
  DDRB |= (1 << 4);  // set to TX (OUTPUT)

  asm("sei");
  while (1)
  {
    UART_receiv(); // Receive a byte first
    _delay_ms(10);  // Pause for clarity
    UART_trans(usart_r);  // Send back to serial
    //mySerial_println("123");
  }
}

UART_receive() is used to receive data. UART_trans() is used to send data. These two functions only receive and send 1 character.

To send String data, use the mySerial_print() or mySerial_println() functions.

Use Serial Monitor to see the response of the arduino ide attiny13a serial communication.


ATtiny13 Datasheet

If you need the ATtiny13 datasheet, you can read the following datasheet:


Hopefully the article ATtiny13 UART How to Serial Communication Between ATtiny and Computer can help your project.

Thank you for visiting the Chip Piko website. May be useful.

Source: https://habr.com/en/post/250995/

Post a Comment for "Serial Communication Between ATtiny UART and Computer"