From 0ff109f1fd0bcd39b1ce4e950dba3f1c7523a861 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sat, 3 May 2025 07:21:28 +0800 Subject: Initial commit. --- Soft_UART_Timer1.h | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 Soft_UART_Timer1.h (limited to 'Soft_UART_Timer1.h') diff --git a/Soft_UART_Timer1.h b/Soft_UART_Timer1.h new file mode 100644 index 0000000..4ad8669 --- /dev/null +++ b/Soft_UART_Timer1.h @@ -0,0 +1,115 @@ +/* + * Soft_UART_Timer1.h + * + * Software UART for ATmega328p using Timer1 + * + * Now only tranmission, no receiving + */ + +#ifndef SOFTUARTTIMER1_H_ +#define SOFTUARTTIMER1_H_ + +////////////////////////////////////////////////////////////////////////// +// Usage (in your .c file): +// 1. define Tx pin: DDR register, PORT register, PIN number: +// #define Soft_UART_TX_PORT PORTD +// #define Soft_UART_TX_DDR DDRD +// #define Soft_UART_TX_PIN 3 +// 2. define UART speed +// #define Soft_UART_Baud 9600 +// 3. include .h file: +// #include "Soft_UART_Timer1.h" +// 3. initialize software UART, required to call before usage other functions: +// static void Soft_UART_init(void); +// 4. send byte +// static void Soft_UART_send_byte(char byte); +// this function send byte in background, +// only one byte in background, other will wait. +////////////////////////////////////////////////////////////////////////// + + +////////////////////////////////////////////////////////////////////////// +// From this line begins code to the end of file. +// All code written in .h file for better inline optimization. +// Try to include this file only once per project. +////////////////////////////////////////////////////////////////////////// + +volatile char Soft_UART_busy_flag=0; +char Soft_UART_next_byte=0; + +////////////////////////////////////////////////////////////////////////// +// Initialization software UART, required to call before usage +////////////////////////////////////////////////////////////////////////// +static void Soft_UART_init(void) +{ // Pin init + bit_set(Soft_UART_TX_DDR,Soft_UART_TX_PIN); + bit_set(Soft_UART_TX_PORT,Soft_UART_TX_PIN); + // Timer1 init + TCCR1A=(0<