Posted on Leave a comment

AVR timer overflow calculator











seconds
seconds

Code for Calculations

<form>
  <label for="F_CPU">Main Clock Frequency (F_CPU):</label>
  <input type="number" id="F_CPU" step="0.01" min="0" value="16000000">
  <hr />

  <label for="Tres">Timer Resolution(8bit or 16bit):</label>
  <input type="number" id="Tres" step="8" value="8" min="8" max="16">
  <hr />
  
  <label for="TCNT">Timer Counter Value:</label>
  <input type="number" id="TCNT" step="1" min="0" max="255" value="0">
  <hr />


  <label for="prescaler">Timer Clock Prescaler:</label><br>
  <input type="radio" id="prescaler1" name="prescaler" value="1" checked>
  <label for="prescaler1">1</label><br>
  <input type="radio" id="prescaler8" name="prescaler" value="8">
  <label for="prescaler8">8</label><br>
  <input type="radio" id="prescaler64" name="prescaler" value="64">
  <label for="prescaler64">64</label><br>
  <input type="radio" id="prescaler256" name="prescaler" value="256">
  <label for="prescaler256">256</label><br>
  <input type="radio" id="prescaler1024" name="prescaler" value="1024">
  <label for="prescaler1024">1024</label><br>
  <hr />
  <label for="stepperiod">Timer Step Period:</label>
  <input type="number" id="stepperiod"  readonly>seconds</br>
  <label for="ovfperiod">Overflow Period:</label>
  <input type="number" id="ovfperiod"  readonly>seconds
  <hr />

  <button type="button" onclick="calculateValue()">Calculate Value</button>
  <button type="button" onclick="clearFields()">Clear Values</button>
</form>

<script>
function calculateValue() {
  // Retrieve input values
  var F_CPU = parseFloat(document.getElementById("F_CPU").value);
  var Tres = parseInt(document.getElementById("Tres").value);
  var TCNT = parseInt(document.getElementById("TCNT").value);
  var prescaler = parseFloat(document.querySelector('input[name="prescaler"]:checked').value);

  // Calculate Timer Clock and Step Period
  var Timer_Clock = F_CPU / prescaler;
  var Step_Period = 1 / Timer_Clock;

  // Calculate Overflow Period based on Timer Resolution
  var ovfperiod;
  if (Tres === 8) {
    ovfperiod = (255 - TCNT + 1) * Step_Period;
  } else if (Tres === 16) {
    ovfperiod = Math.pow(2, 16) * Step_Period;
  }

  // Update the Overflow Period input field
  document.getElementById("stepperiod").value = Step_Period;
  document.getElementById("ovfperiod").value = ovfperiod;
}

function clearFields() {
  document.getElementById("F_CPU").value = "";
  document.getElementById("Tres").value = "";
  document.getElementById("ovfperiod").value = "";
}
</script>
Posted on Leave a comment

How to Transmit Data via UART with ATmega328P in AVR C using Arduino IDE

EnglishDeutschHindi

To transmit data via UART with the ATmega328P microcontroller in AVR C using the Arduino IDE, you can follow these steps:

  1. Include the necessary header files:
   #include <avr/io.h>
   #include <util/delay.h>
   #include <avr/interrupt.h>
   #include <stdlib.h>
  1. Define the UART settings:
   #define BAUDRATE 9600
   #define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
  1. Initialize UART by configuring the baud rate and other settings:
   void uart_init() {
     UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
     UBRR0L = (uint8_t)(BAUD_PRESCALLER);

     UCSR0B = (1 << TXEN0);  // Enable transmitter
     UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  // 8-bit data format
   }
  1. Implement the function to transmit data:
   void uart_transmit(uint8_t data) {
     while (!(UCSR0A & (1 << UDRE0)));  // Wait for empty transmit buffer
     UDR0 = data;  // Put data into the buffer, sends the data
   }
  1. Call the uart_init() function in your setup() function to initialize UART communication:
   void setup() {
     // Other setup code...
     uart_init();
     // More setup code...
   }
  1. Use uart_transmit() to send data:
   void loop() {
     // Transmit a character
     uart_transmit('A');

     // Delay between transmissions
     _delay_ms(1000);
   }

Make sure to connect the appropriate UART pins (TX) to the corresponding pins on your Arduino board. Additionally, set the correct BAUDRATE value based on your desired communication speed.

Complete Code
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
void uart_init() {
  UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);

  UCSR0B = (1 << TXEN0);  // Enable transmitter
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  // 8-bit data format
}
void uart_transmit(uint8_t data) {
  while (!(UCSR0A & (1 << UDRE0)));  // Wait for empty transmit buffer
  UDR0 = data;  // Put data into the buffer, sends the data
}
void setup() {
  // Other setup code...
  uart_init();
  // More setup code...
}
void loop() {
  // Transmit a character
  uart_transmit('A');

  // Delay between transmissions
  _delay_ms(1000);
}

Demo Program That Transmits String of Characters

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

#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

void uart_init() {
  UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);

  UCSR0B = (1 << TXEN0);  // Enable transmitter
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  // 8-bit data format
}

void uart_transmit(uint8_t data) {
  while (!(UCSR0A & (1 << UDRE0)));  // Wait for empty transmit buffer
  UDR0 = data;  // Put data into the buffer, sends the data
}

void uart_transmit_string(const char* str) {
  for (size_t i = 0; str[i] != '\0'; ++i) {
    uart_transmit(str[i]);
    _delay_ms(100);  // Delay between character transmissions
  }
}

void setup() {
  // Initialize UART
  uart_init();
}

void loop() {
  // Transmit a string
  uart_transmit_string("Hello, world!");

  // Delay before transmitting again
  _delay_ms(2000);
}

Um Daten über UART mit dem ATmega328P Mikrocontroller in AVR C unter Verwendung der Arduino IDE zu übertragen, können Sie die folgenden Schritte befolgen:

Fügen Sie die erforderlichen Header-Dateien ein:

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

Definieren Sie die UART-Einstellungen:

   #define BAUDRATE 9600
   #define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

Initialisieren Sie UART, indem Sie die Baudrate und andere Einstellungen konfigurieren:

   void uart_init() {
     UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
     UBRR0L = (uint8_t)(BAUD_PRESCALLER);

     UCSR0B = (1 << TXEN0);  // Sender aktivieren
     UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  // 8-Bit Datenformat
   }

Implementieren Sie die Funktion zum Senden von Daten:

   void uart_transmit(uint8_t data) {
     while (!(UCSR0A & (1 << UDRE0)));  // Warten auf leeren Sendepuffer
     UDR0 = data;  // Daten in den Puffer schreiben und senden
   }

Rufen Sie die Funktion uart_init() in Ihrer setup() Funktion auf, um die UART-Kommunikation zu initialisieren:

   void setup() {
     // Andere Setup-Code...
     uart_init();
     // Weitere Setup-Code...
   }

Verwenden Sie uart_transmit(), um Daten zu senden:

   void loop() {
     // Ein Zeichen übertragen
     uart_transmit('A');

     // Verzögerung zwischen den Übertragungen
     _delay_ms(1000);
   }

Stellen Sie sicher, dass Sie die entsprechenden UART-Pins (TX) mit den entsprechenden Pins auf Ihrem Arduino-Board verbinden. Stellen Sie außerdem den korrekten BAUDRATE-Wert entsprechend Ihrer gewünschten Kommunikationsgeschwindigkeit ein.

Vollständiger Code
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
void uart_init() {
  UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);

  UCSR0B = (1 << TXEN0);  // Enable transmitter
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  // 8-bit data format
}
void uart_transmit(uint8_t data) {
  while (!(UCSR0A & (1 << UDRE0)));  // Wait for empty transmit buffer
  UDR0 = data;  // Put data into the buffer, sends the data
}
void setup() {
  // Other setup code...
  uart_init();
  // More setup code...
}
void loop() {
  // Transmit a character
  uart_transmit('A');

  // Delay between transmissions
  _delay_ms(1000);
}

Demo-Programm zum Senden einer Zeichenkette von Zeichen

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

#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

void uart_init() {
  UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);

  UCSR0B = (1 << TXEN0);  // Enable transmitter
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  // 8-bit data format
}

void uart_transmit(uint8_t data) {
  while (!(UCSR0A & (1 << UDRE0)));  // Wait for empty transmit buffer
  UDR0 = data;  // Put data into the buffer, sends the data
}

void uart_transmit_string(const char* str) {
  for (size_t i = 0; str[i] != '\0'; ++i) {
    uart_transmit(str[i]);
    _delay_ms(100);  // Delay between character transmissions
  }
}

void setup() {
  // Initialize UART
  uart_init();
}

void loop() {
  // Transmit a string
  uart_transmit_string("Hello, world!");

  // Delay before transmitting again
  _delay_ms(2000);
}

एटीमेगा328पी माइक्रोकंट्रोलर के साथ AVR C और Arduino IDE का उपयोग करके UART के माध्यम से डेटा भेजने के लिए, आप निम्नलिखित चरणों का पालन कर सकते हैं:

आवश्यक हेडर फ़ाइलें शामिल करें:

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

UART सेटिंग्स को परिभाषित करें:

   #define BAUDRATE 9600
   #define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

बॉड दर और अन्य सेटिंग्स को कॉन्फ़िगर करके UART को प्रारंभ करें:

   void uart_init() {
     UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
     UBRR0L = (uint8_t)(BAUD_PRESCALLER);

     UCSR0B = (1 << TXEN0);  // ट्रांसमीटर सक्षम करें
     UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  // 8-बिट डेटा फ़ॉर्मेट
   }

डेटा भेजने के लिए फ़ंक्शन को लागू करें:

   void uart_transmit(uint8_t data) {
     while (!(UCSR0A & (1 << UDRE0)));  // खाली ट्रांसमिट बफर के लिए प्रतीक्षा करें
     UDR0 = data;  // डेटा को बफर में डालें, डेटा भेजें
   }

आपकी सेटअप() फ़ंक्शन में uart_init() फ़ंक्शन को कॉल करें, UART संचार को प्रारंभ करने के लिए:

   void setup() {
     // अन्य सेटअप कोड...
     uart_init();
     // अधिक सेटअप कोड...
   }

डेटा भेजने के लिए uart_transmit() का उपयोग करें:

   void loop() {
     // एक वर्ण भेजें
     uart_transmit('A');

     // भेजने के बीच में विलंब
     _delay_ms(1000);
   }

उचित UART पिन (TX) को अपने Arduino बोर्ड के संबंधित पिनों से कनेक्ट करने का सुनिश्चित करें। इसके अलावा, अपनी इच्छित संचार गति के आधार पर सही BAUDRATE मान सेट करें।

पूरा कोड:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
void uart_init() {
  UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);

  UCSR0B = (1 << TXEN0);  // Enable transmitter
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  // 8-bit data format
}
void uart_transmit(uint8_t data) {
  while (!(UCSR0A & (1 << UDRE0)));  // Wait for empty transmit buffer
  UDR0 = data;  // Put data into the buffer, sends the data
}
void setup() {
  // Other setup code...
  uart_init();
  // More setup code...
}
void loop() {
  // Transmit a character
  uart_transmit('A');

  // Delay between transmissions
  _delay_ms(1000);
}

वर्ण स्ट्रिंग भेजने का डेमो प्रोग्राम

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

#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

void uart_init() {
  UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);

  UCSR0B = (1 << TXEN0);  // Enable transmitter
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  // 8-bit data format
}

void uart_transmit(uint8_t data) {
  while (!(UCSR0A & (1 << UDRE0)));  // Wait for empty transmit buffer
  UDR0 = data;  // Put data into the buffer, sends the data
}

void uart_transmit_string(const char* str) {
  for (size_t i = 0; str[i] != '\0'; ++i) {
    uart_transmit(str[i]);
    _delay_ms(100);  // Delay between character transmissions
  }
}

void setup() {
  // Initialize UART
  uart_init();
}

void loop() {
  // Transmit a string
  uart_transmit_string("Hello, world!");

  // Delay before transmitting again
  _delay_ms(2000);
}

Posted on Leave a comment

How to Initialize UART Communication with ATmega328P in AVR C using Arduino IDE

EnglishDeutschHindi

To initialize UART communication with the ATmega328P microcontroller in AVR C using the Arduino IDE, you can follow these steps:

Include the necessary header files:

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

Define the UART settings:

#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

Initialize UART by configuring the baud rate and other settings:

void uart_init() {
  UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);

  UCSR0B = (1 << RXEN0) | (1 << TXEN0);  // Enable receiver and transmitter
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  // 8-bit data format
}

Implement functions for transmitting and receiving data:

void uart_transmit(uint8_t data) {
  while (!(UCSR0A & (1 << UDRE0)));  // Wait for empty transmit buffer
  UDR0 = data;  // Put data into the buffer, sends the data
}

uint8_t uart_receive() {
  while (!(UCSR0A & (1 << RXC0)));  // Wait for data to be received
  return UDR0;  // Get and return received data from buffer
}

(Optional) Implement a function to transmit a string:

void uart_transmit_string(const char* str) {
  for (size_t i = 0; str[i] != '\0'; ++i) {
    uart_transmit(str[i]);
  }
}

Finally, call the uart_init() function in your setup() function to initialize UART communication:

void setup() {
  // Other setup code...
  uart_init();
  // More setup code...
}

Now, you can use uart_transmit() to send individual characters and uart_receive() to receive data. If needed, you can use uart_transmit_string() to send strings.

Note that the above code assumes you are using the default hardware UART (UART0) on the ATmega328P, and the UART pins (RX and TX) are connected to the corresponding pins on your Arduino board.

Make sure to set the correct BAUDRATE value based on your desired communication speed. Also, ensure that the F_CPU macro is defined with the correct clock frequency of your microcontroller.

Remember to include the necessary header files in your Arduino sketch and define the setup() and loop() functions as required for Arduino compatibility.

Please note that configuring the UART in this way bypasses some of the Arduino’s built-in serial functionality, so you won’t be able to use Serial.print() or Serial.read() with this setup.

Code for a demo program

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

#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

void uart_init() {
  UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);

  UCSR0B = (1 << RXEN0) | (1 << TXEN0);
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}

void uart_transmit(uint8_t data) {
  while (!(UCSR0A & (1 << UDRE0)));
  UDR0 = data;
}

uint8_t uart_receive() {
  while (!(UCSR0A & (1 << RXC0)));
  return UDR0;
}

void uart_transmit_string(const char* str) {
  for (size_t i = 0; str[i] != '\0'; ++i) {
    uart_transmit(str[i]);
  }
}

void setup() {
  // Initialize UART
  uart_init();

  // Set PB5 (Arduino digital pin 13) as output
  DDRB |= (1 << PB5);
}

void loop() {
  // Read input from UART
  uint8_t receivedData = uart_receive();

  // Echo back received data
  uart_transmit(receivedData);

  // Toggle the built-in LED (PB5) on each received character
  PORTB ^= (1 << PB5);

  // Delay for a short period to observe the LED blink
  _delay_ms(500);
}

In this demo program, the ATmega328P’s UART is initialized in the setup() function, and then in the loop() function, it continuously reads incoming data and sends it back (echo) over UART. Additionally, it toggles the built-in LED (PB5) on each received character, providing a visual indication.

Um die UART-Kommunikation mit dem ATmega328P-Mikrocontroller in AVR C mithilfe der Arduino IDE zu initialisieren, können Sie den folgenden Schritten folgen:

Fügen Sie die erforderlichen Header-Dateien hinzu:

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

Definieren Sie die UART-Einstellungen:

#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

Initialisieren Sie UART, indem Sie die Baudrate und andere Einstellungen konfigurieren:

void uart_init() {
  UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);

  UCSR0B = (1 << RXEN0) | (1 << TXEN0);  // Empfänger und Sender aktivieren
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  // 8-Bit-Datenformat
}

Implementieren Sie Funktionen zum Senden und Empfangen von Daten:

void uart_transmit(uint8_t data) {
  while (!(UCSR0A & (1 << UDRE0)));  // Auf leeren Sendepuffer warten
  UDR0 = data;  // Daten in den Puffer schreiben und senden
}

uint8_t uart_receive() {
  while (!(UCSR0A & (1 << RXC0)));  // Auf empfangene Daten warten
  return UDR0;  // Empfangene Daten aus dem Puffer lesen und zurückgeben
}

(Optional) Implementieren Sie eine Funktion zum Senden einer Zeichenkette:

void uart_transmit_string(const char* str) {
  for (size_t i = 0; str[i] != '\0'; ++i) {
    uart_transmit(str[i]);
  }
}

Rufen Sie schließlich die Funktion uart_init() in Ihrer setup() Funktion auf, um die UART-Kommunikation zu initialisieren:

void setup() {
  // Anderer Setup-Code...
  uart_init();
  // Weitere Setup-Code...
}

Jetzt können Sie uart_transmit() verwenden, um einzelne Zeichen zu senden, und uart_receive() zum Empfangen von Daten. Bei Bedarf können Sie uart_transmit_string() verwenden, um Zeichenketten zu senden.

Beachten Sie, dass der obige Code davon ausgeht, dass Sie die standardmäßige Hardware-UART (UART0) des ATmega328P verwenden und dass die UART-Pins (RX und TX) mit den entsprechenden Pins auf Ihrem Arduino-Board verbunden sind.

Stellen Sie sicher, dass Sie den korrekten BAUDRATE-Wert entsprechend Ihrer gewünschten Übertragungsgeschwindigkeit festlegen. Stellen Sie außerdem sicher, dass die F_CPU-Makrodefinition die richtige Taktgeschwindigkeit Ihres Mikrocontrollers enthält.

Vergessen Sie nicht, die erforderlichen Header-Dateien in Ihren Arduino-Sketch einzufügen und die setup() und loop() Funktionen gemäß den Anforderungen der Arduino-Kompatibilität zu definieren.

Bitte beachten Sie, dass durch die Konfiguration der UART auf diese Weise einige der integrierten seriellen Funktionen des Arduino umgangen werden. Sie können daher nicht Serial.print() oder Serial.read() mit dieser Konfiguration verwenden.

Code für ein Demo-Programm

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

#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

void uart_init() {
  UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);

  UCSR0B = (1 << RXEN0) | (1 << TXEN0);
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}

void uart_transmit(uint8_t data) {
  while (!(UCSR0A & (1 << UDRE0)));
  UDR0 = data;
}

uint8_t uart_receive() {
  while (!(UCSR0A & (1 << RXC0)));
  return UDR0;
}

void uart_transmit_string(const char* str) {
  for (size_t i = 0; str[i] != '\0'; ++i) {
    uart_transmit(str[i]);
  }
}

void setup() {
  // UART initialisieren
  uart_init();

  // PB5 (Arduino digitaler Pin 13) als Ausgang festlegen
  DDRB |= (1 << PB5);
}

void loop() {
  // Eingabe von UART lesen
  uint8_t receivedData = uart_receive();

  // Empfangene Daten zurückschicken (Echo)
  uart_transmit(receivedData);

  // Die integrierte LED (PB5) bei jedem empfangenen Zeichen umschalten
  PORTB ^= (1 << PB5);

  // Eine kurze Verzögerung, um das Blinken der LED zu beobachten
  _delay_ms(500);
}

In diesem Demo-Programm wird die UART des ATmega328P im setup() Funktion initialisiert. In der loop() Funktion liest es kontinuierlich eingehende Daten und sendet sie zurück (Echo) über UART. Darüber hinaus wird die integrierte LED (PB5) bei jedem empfangenen Zeichen umgeschaltet, um eine visuelle Anzeige zu bieten.

ATmega328P माइक्रोकंट्रोलर के साथ AVR C और Arduino IDE का उपयोग करके UART संचार को प्रारंभ करने के लिए, आप निम्नलिखित चरणों का पालन कर सकते हैं:

आवश्यक हैडर फ़ाइलें शामिल करें:

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

UART सेटिंग्स को परिभाषित करें:

#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

बॉडरेट और अन्य सेटिंग्स को कॉन्फ़िगर करके UART को प्रारंभ करें:

void uart_init() {
  UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);

  UCSR0B = (1 << RXEN0) | (1 << TXEN0);  // प्राप्तकर्ता और प्रेषक को सक्षम करें
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);  // 8 बिट डेटा प्रारूप
}

डेटा भेजने और प्राप्त करने के लिए फ़ंक्शन को अमल में लाएं:

void uart_transmit(uint8_t data) {
  while (!(UCSR0A & (1 << UDRE0)));  // खाली भेजने वाले बफ़र के लिए प्रतीक्षा करें
  UDR0 = data;  // डेटा बफ़र में डेटा डालें, डेटा भेजें
}

uint8_t uart_receive() {
  while (!(UCSR0A & (1 << RXC0)));  // डेटा प्राप्त होने की प्रतीक्षा करें
  return UDR0;  // बफ़र से प्राप्त और डेटा लौटाएं
}

(ऐच्छिक) स्ट्रिंग भेजने के लिए एक फ़ंक्शन को अमल में लाएं:

void uart_transmit_string(const char* str) {
  for (size_t i =

 0; str[i] != '\0'; ++i) {
    uart_transmit(str[i]);
  }
}

अंतिम रूप में, अपने सेटअप() फ़ंक्शन में uart_init() फ़ंक्शन को बुलाएं और UART संचार को प्रारंभ करें:

void setup() {
  // अन्य सेटअप कोड...
  uart_init();
  // अधिक सेटअप कोड...
}

अब, आप uart_transmit() का उपयोग अक्षरों को भेजने के लिए कर सकते हैं और uart_receive() का उपयोग डेटा प्राप्त करने के लिए कर सकते हैं। आवश्यकता होने पर, आप uart_transmit_string() का उपयोग स्ट्रिंग भेजने के लिए कर सकते हैं।

ध्यान दें कि ऊपर का कोड मानता है कि आप ATmega328P पर डिफ़ॉल्ट हार्डवेयर UART (UART0) का उपयोग कर रहे हैं, और UART पिन (RX और TX) आपके Arduino बोर्ड पर संबंधित पिनों से कनेक्ट हैं।

अपनी Arduino स्केच में आवश्यक हैडर फ़ाइलें शामिल करने और Arduino संगतता के लिए सेटअप() और लूप() फ़ंक्शन को परिभाषित करने के लिए याद रखें।

कृपया ध्यान दें कि इस तरीके से UART को कॉन्फ़िगर करने से अर्डुइनो के कुछ निर्मित सीरियल कार्यक्षमता का बाहर जाना होता है, इसलिए इस सेटअप के साथ Serial.print() या Serial.read() का उपयोग नहीं कर सकेंगे।

डेमो प्रोग्राम के लिए कोड

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

#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

void uart_init() {
  UBRR0H = (uint8_t)(BAUD_PRESCALLER >> 8);
  UBRR0L = (uint8_t)(BAUD_PRESCALLER);

  UCSR0B = (1 << RXEN0) | (1 << TXEN0);
  UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}

void uart_transmit(uint8_t data) {
  while (!(UCSR0A & (1 << UDRE0)));
  UDR0 = data;
}

uint8_t uart_receive() {
  while (!(UCSR0A & (1 << RXC0)));
  return UDR0;
}

void uart_transmit_string(const char* str) {
  for (size_t i = 0; str[i] != '\0'; ++i) {
    uart_transmit(str[i]);
  }
}

void setup() {
  // UART को प्रारंभ करें
  uart_init();

  // PB5 (Arduino डिजिटल पिन 13) को आउटपुट के रूप में सेट करें
  DDRB |= (1 << PB5);
}

void loop() {
  // UART से इनपुट पढ़ें
  uint8_t receivedData = uart_receive();

  // प्राप्त डेटा को वापस भेजें (इको)
  uart_transmit(receivedData);

  // प्राप्त वर्णकों पर बिल्ट-इन LED (PB5) को टॉगल करें, यह दिखाने के लिए
  PORTB ^= (1 << PB5);

  // LED ब्लिंक को देखने के लिए थोड़ी सी देरी के लिए विलंब करें
  _delay_ms(500);
}

इस डेमो प्रोग्राम में, ATmega328P का UART सेटअप() फ़ंक्शन में प्रारंभित होता है, और फिर लूप() फ़ंक्शन में, यह निरंतर आने वाले डेटा को पढ़ता है और उसे वापस (इको) UART के माध्यम से भेजता है। इसके अलावा, यह प्राप्त प्रत्येक आंकड़े पर बिल्ट-इन LED (PB5) को टॉगल करता है, जिससे एक लैंप एफेक्ट प्रदर्शित होता है।

Posted on Leave a comment

Tiny2313 Dev Board

This is a Tiny development board I made. It is a small size and is ideal for quick prototyping. I made for the Attiny2313 microcontrollers. I started programming with this microcontroller board. Back then I did it on a breadboard. But since now I can know a few things about PCB etching I made this PCB.


Attiny2313 Datasheet
https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2543-AVR-ATtiny2313_Datasheet.pdf

I made this circuit schematic.

Schematic Diagram of Tiny2313 Dev Board
Schematic Diagram of Tiny2313 Dev Board

I changed a few components from the schematic. The current limiting resistor for the LED is 330 ohm in the schematic whereas I used 1.8k ohm. While performing few tests on the LED I found that the LED is visible at that lower current value.

For a 5 V supply and an 1800 ohm resistor, the current will be 2.7 mA. Which is enough for visual identification at short distances.

The Two buttons are not yet populated. They are provided as a future provision.

The Tiny2313 Dev Board is programmed using the USBASP v2 programmer.

The 6-pin male header is connected to the programmer. When not doing the programming the pins can be used as GPIO.

Here is the table of components I used for this PCB.

ComponentsDescriptionQuantityPopulated
ATtiny2313 PDIPMicrocontroller1yes
PDIP 20 Pin IC SocketMicrocontroller IC Socket1yes
8Mhz Crystal Oscillator HC49Crystal Oscillator1yes
12pf Ceramic Disc CapacitorCrystal Load Capacitors2yes
1×2 Male Berg HeaderFor Power Connection1yes
1×3 Male Berg HeaderFor UART (RX, TX) and INT01yes
1×6 Male Berg HeaderFor Programming and as GPIO1yes
LED 3mm Red clear lensLED1yes
1.8k Resistor 1/8w LED Current Limiting Resistor1yes
10k Resistor 1/4wPull Up Resistor For RESET1yes
10k Resistor 1/8wPull Up resistors2no
Tactile Switch 2 pinPush Buttons 2no

I designed the PCB in KiCad. And made the PCB using etching photoresist.

You can read How to make single-layer PCB using Dry Photoresist Film Etching Method

As you can see in the image bellow . Home PCB making is not a perfect solution. But it gives you immense learning about the process.

Posted on Leave a comment

What are AVR microcontrollers?

AVR microcontrollers, developed by Atmel Corporation (now Microchip Technology), are designed for embedded applications that require low power consumption, high performance, and a small footprint. The AVR family includes a range of microcontrollers, each with varying processing power, memory capacity, and peripheral features.

  • ATmega series: This popular series of 8-bit AVR microcontrollers offers up to 256KB of flash memory, 16-channel 10-bit ADC, and up to 86 general-purpose I/O pins.
    • Example: ATmega328P, which is commonly used in the Arduino Uno board. It has 32KB of flash memory, 2KB of SRAM, and 1KB of EEPROM.
  • ATtiny series: This low-power, low-cost series of 8-bit AVR microcontrollers is ideal for simple applications that require basic processing and control functions. They typically have less flash memory and fewer peripheral features compared to the ATmega series.
    • Example: ATtiny85, which is commonly used in the Digispark development board. It has 8KB of flash memory, 512 bytes of SRAM, and 512 bytes of EEPROM.
  • ATxmega series: This series of 8/16-bit AVR microcontrollers offers higher processing power, more memory, and advanced features such as DMA, DAC, and RTC.
    • Example: ATxmega128A1, which has 128KB of flash memory, 8KB of SRAM, and 2KB of EEPROM.
  • AT91SAM series: This series of ARM-based microcontrollers combines the low power consumption and high performance of the AVR architecture with the advanced features and processing power of the ARM architecture.
    • Example: AT91SAM9G25, which is based on the ARM926EJ-S core and has 64KB of SRAM, 32KB of ROM, and a variety of peripheral features such as Ethernet and USB.
  • AVR32 series: This series of 32-bit AVR microcontrollers offers high processing power and advanced features such as floating-point processing, DMA, and high-speed connectivity.
    • Example: AVR32 UC3A0512, which has 512KB of flash memory, 64KB of SRAM, and a variety of peripheral features such as Ethernet, USB, and CAN.

Overall, AVR microcontrollers are versatile and widely used in a variety of applications, such as automotive electronics, home automation, industrial automation, robotics, and consumer electronics. They can be programmed using a variety of programming languages and development environments, including C, C++, Assembly, and Arduino.

Comprehensive Atmel Microcontroller Series

AT90 Series:
AT90CAN128	AT90CAN32	AT90CAN64
AT90PWM1	AT90PWM161	AT90PWM2
AT90PWM261	AT90PWM2B	AT90PWM3
AT90PWM316	AT90PWM3B	AT90PWM81
AT90USB1286	AT90USB1287	AT90USB162
AT90USB646	AT90USB647	AT90USB82

ATmega Series:
ATmega128	ATmega1284	ATmega128A
ATmega128x	ATmega16	ATmega1608
ATmega1609	ATmega162	ATmega164
ATmega164P	ATmega165A	ATmega165P
ATmega165PA	ATmega168	ATmega168A
ATmega168P	ATmega168PA	ATmega168PB
ATmega169A	ATmega169P	ATmega169PA
ATmega16A	ATmega16M1	ATmega16U2
ATmega16U4	ATmega256x	ATmega32
ATmega3208	ATmega3209	ATmega324
ATmega324P	ATmega324PB	ATmega325
ATmega3250	ATmega3250A	ATmega3250P
ATmega3250PA	ATmega325A	ATmega325P
ATmega325PA	ATmega328	ATmega328P
ATmega328PB	ATmega329	ATmega3290
ATmega3290A	ATmega3290P	ATmega3290PA
ATmega329A	ATmega329P	ATmega329PA
ATmega32A	ATmega32M1	ATmega32U2
ATmega32U4	ATmega406	ATmega48
ATmega4808	ATmega4809	ATmega48A
ATmega48P	ATmega48PA	ATmega48PB
ATmega48V	ATmega64	ATmega640
ATmega644	ATmega644P	ATmega645
ATmega6450	ATmega6450A	ATmega6450P
ATmega645A	ATmega645P	ATmega649
ATmega6490	ATmega6490A	ATmega6490P
ATmega649A	ATmega649P	ATmega64A
ATmega64M1	ATmega8		ATmega808
ATmega809	ATmega8515	ATmega8535
ATmega88	ATmega88A	ATmega88P
ATmega88PA	ATmega88PB	ATmega8A
ATmega8U2	ATtiny10	ATtiny102

ATtiny Series:
ATtiny104	ATtiny12	ATtiny13
ATtiny13A	ATtiny1604	ATtiny3217
ATtiny1606  	ATtiny3217	ATtiny1607
ATtiny3217	ATtiny1614  	ATtiny3217
ATtiny1616  	ATtiny3217	ATtiny1617
ATtiny3217	ATtiny1634	ATtiny167
ATtiny20	ATtiny202	ATtiny212
ATtiny214	ATtiny2313	ATtiny24
ATtiny25	ATtiny26	ATtiny28L
ATtiny3216	ATtiny3217	ATtiny3217
ATtiny4
ATtiny40	ATtiny402	ATtiny406
ATtiny412	ATtiny414	ATtiny416
ATtiny417	ATtiny43	ATtiny4313
ATtiny44	ATtiny441	ATtiny45
ATtiny48	ATtiny5		ATtiny806
ATtiny807	ATtiny814	ATtiny816
ATtiny817	ATtiny828	ATtiny84
ATtiny841	ATtiny85	ATtiny87
ATtiny88	ATtiny9		ATtinyx04
ATtinyx61	ATtinyx61A	XMEGA A1

XMEGA Series:
XMEGA A1U	XMEGA A3	XMEGA A3B
XMEGA A3BU	XMEGA A3U	XMEGA A4
XMEGA A4U	XMEGA B1	XMEGA B3
XMEGA C3	XMEGA C4	XMEGA D3

Posted on Leave a comment

AVR Input Output Port Programming

In AVR microcontroller programming, input/output ports are used to interface with external devices such as sensors, switches, LEDs, motors, and other peripherals. Here’s an example of how to program AVR input/output ports using C language:

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

int main(void)
{
    // Set PORTB as output and PORTC as input
    DDRB = 0xFF;
    DDRC = 0x00;

    while(1)
    {
        // Read the value of PINC3
        if(PINC & (1 << PINC3))
        {
            // If PINC3 is high, turn on LED connected to PB0
            PORTB |= (1 << PB0);
        }
        else
        {
            // If PINC3 is low, turn off LED connected to PB0
            PORTB &= ~(1 << PB0);
        }
    }
}

In this example, we set PORTB as an output port by setting all of its pins to output mode. We set PORTC as an input port by setting all of its pins to input mode. Then, we use a while loop to continuously check the value of PINC3. If PINC3 is high, we turn on an LED connected to PB0 by setting the corresponding bit in PORTB to high. If PINC3 is low, we turn off the LED by setting the corresponding bit in PORTB to low.

Note that the & and | operators are used to manipulate individual bits in the port registers. The << operator is used to shift the binary value of 1 to the left by a certain number of bits to set a particular bit high or low. The ~ operator is used to invert the value of a bit. The util/delay.h library is used to create a delay between each loop iteration.

Posted on Leave a comment

Memory Space in ATmega16A

To effectively program AVR based microcontroller you must have knowledge about its memory space. Since there is not just one memory space. There are three different address spaces. They are:

  1. Data memory (SRAM)
    Here all your the intermediate results are stored and all the run time calculation get carried in this memory space.
  2. Program memory
    It is the memory where your application code is stored. And also it stores the constants. It could be divided into two sections by setting appropriate fuse bits. The two section will be:
    1. Application Section
    2. Boot Section
  3. EEPROM
    This is the memory where you can save the run time data such as configuration options, intermediate result for future processing. But it is very limited in size. So proper managment had to be taken care. Since it limits the size of certain applications.

Out of the three memories present in the ATmega16a, only the SRAM is volatile.

Size and address of memory space

  1. Data Memory
    1024 Bytes (Starting Address: 0x0060 – Last Address: 0x045F)
    96 Address which contain two section General purpose Register and I/O Registers.
    General Purpose Register ( Starting Address: 0x0000 – Last address: 0x001F)
    I/O register (Starting Address: 0x0020 – Last address: 0x005F)
  2. Program Memory
    Flash Type memory organised into 8000 memory location each pointing to a 16 bit wide data.
    Starting Address: 0x0000
    Last Address: 0x1FFF

NOTE: Care must be taken while Read/Write operations of EEPROM. It is very sensitive to operating conditions. Variations outside the tolerance limits could result in corruption of data in the worst case total loss of stored data. It is highly recommended that a stable power source must be used if performing frequent EEPROM operations.

Posted on Leave a comment

Compile AVR Code in Raspberry Pi

I have recorded a video showing the steps to compile AVR code.

In this video, you will see how to compile AVR led blinking code for the atmega32 microcontroller is being compiled using AVR-GCC in raspberry pi 3 b+.

Step 1. : Create a file and put your avr c code in that file. Save that file with a “.c” extension

Step 2. : Run the following commands one by one

avr-gcc -g -0s -mmcu=atmega32 -c led.c
avr-gcc -g -mmcu=atmega32 -o led.elf led.o
avr-objcopy -j .text -j .data -o ihex led.elf led.hex

Step 3. : The step 2 will create a hex file. which can be uploaded to the microcontroller with the help of a programmer.

Posted on Leave a comment

How to install AVR-GCC Compiler in raspberry pi

In this video, you will see that how avr compiler can be installed in raspberry pi model 3 b+.

For steps on how to install avrdude in raspberry pi you can go here.

http://www.exasub.com/component/integrated-circuit/microcontroller/avr/using-usbasp-v2-0-avr-programmer-with-raspberry-pi-3/

AVR compiler and avrdude together on raspberry pi make it a development environment for embedded systems involving Avr microcontroller.

Posted on Leave a comment

Using USBASP V2.0 AVR Programmer with Raspberry Pi 3

Raspberry pi or Rpi can be used for programming certain AVR microcontrollers. I have made a video showing you the steps.

Step 1: you have to install AVRDUDE

sudo apt-get install avrdude -y

Step 2: You can check if the avrdude is installed properly or not using

avrdude

step 3: connect your USBASP v2.0 to your microcontroller

avrdude -c usbasp -p m32

Microcontroller Programmed:

Atmega32, atmega32a, Atmega16, Atmega16a,