The file contains definitions and macros for the single-wire software UART. If a different microcontroller than the ATmega32 is used, this file needs to be changed. Port and baud rate settings may also be changed in this file.
Definition in file single_wire_UART.h.
#include "stdint.h"
Include dependency graph for single_wire_UART.h:
This graph shows which files directly or indirectly include this file:
Go to the source code of this file.
Defines | |
#define | CLEAR_FLAG(flag_register, flag_bit) ( (flag_register) &= ~(1 << (flag_bit)) ) |
Use this macro when clearing a flag in a register. | |
#define | CLEAR_UART_EXTERNAL_INTERRUPT_FLAG() (GIFR = (1<<INTF0)) |
#define | CLEAR_UART_PIN() ( SW_UART_DDR |= (1<<SW_UART_PIN_NUMBER) ) |
Set pin output low. | |
#define | CLEAR_UART_TIMER() (TCNT0 = 0x00) |
#define | CLEAR_UART_TIMER_INTERRUPT_FLAG() (TIFR = (1<<OCF0)) |
#define | CLEAR_UART_TIMER_ON_COMPARE_MATCH() (TCCR0 |= (1<<WGM01)) |
Set timer control register to clear timer on compare match (CTC). | |
#define | DISABLE_UART_EXTERNAL_INTERRUPT() (GICR &= ~(1<<INT0)) |
#define | DISABLE_UART_TIMER_INTERRUPT() (TIMSK &= ~(1<<OCIE0)) |
#define | ENABLE_UART_EXTERNAL_INTERRUPT() (GICR |= (1<<INT0)) |
#define | ENABLE_UART_TIMER_INTERRUPT() (TIMSK |= (1<<OCIE0)) |
#define | FALSE 0 |
#define | INITIALIZE_UART_EXTERNAL_INTERRUPT() (MCUCR |= (1<<ISC01)) |
#define | INITIALIZE_UART_PIN() ( SW_UART_PORT &= ~(1<<SW_UART_PIN_NUMBER) ) |
Clear port. | |
#define | PRESCALER 1 |
Prescaler setting. Must be set according to the baud rate setting. | |
#define | READ_FLAG(flag_register, flag_bit) ( (flag_register) & (1 << (flag_bit)) ) |
Use this macro when reading a flag in a register. | |
#define | READ_UART_PIN() ( SW_UART_PIN & (1<<SW_UART_PIN_NUMBER) ) |
#define | RECEIVE_DELAY 76 |
Cycles from the start bit is detected to the timer is started plus cycles in timer interrupt before first data bit is received. | |
#define | SET_FLAG(flag_register, flag_bit) ( (flag_register) |= (1 << (flag_bit)) ) |
Use this macro when setting a flag in a register. | |
#define | SET_UART_PIN() ( SW_UART_DDR &= ~(1<<SW_UART_PIN_NUMBER) ) |
Tri-state pin. | |
#define | SET_UART_TIMER_COMPARE_START_RECEIVE() (OCR0 = WAIT_ONEHALF - (RECEIVE_DELAY/PRESCALER)) |
Sets the timer compare register to the correct value when a reception is started. | |
#define | SET_UART_TIMER_COMPARE_START_TRANSMIT() (OCR0 = WAIT_ONE - (TRANSMIT_DELAY/PRESCALER)) |
Sets the timer compare register to the correct value when a transmission is started. | |
#define | SET_UART_TIMER_COMPARE_WAIT_ONE() (OCR0 = WAIT_ONE) |
Sets the timer compare register to one period. | |
#define | START_UART_TIMER() (TIMER_PRESCALER_CONTROL_REGISTER |= (1<<CS00)) |
#define | STOP_UART_TIMER() (TIMER_PRESCALER_CONTROL_REGISTER &= ~(1<<CS00)) |
#define | SW_UART_DDR DDRD |
Data direction register. Not available for high voltage ports. | |
#define | SW_UART_EXTERNAL_INTERRUPT_VECTOR INT0_vect |
UART external interrupt vector. Make sure this is in accordance to the defined UART pin. | |
#define | SW_UART_FRAME_ERROR 7 |
Set if a frame error has occured. | |
#define | SW_UART_PIN PIND |
Set pin for communication. | |
#define | SW_UART_PIN_NUMBER 2 |
Set pin number for communication. | |
#define | SW_UART_PORT PORTD |
Set port for communication. | |
#define | SW_UART_RX_BUFFER_FULL 5 |
Set if data is ready to be received from the Rx buffer. | |
#define | SW_UART_RX_BUFFER_OVERFLOW 6 |
Set if receive buffer is overflowed. Indicates data loss. | |
#define | SW_UART_TIMER_COMPARE_INTERRUPT_VECTOR TIMER0_COMP_vect |
UART compare interrupt vector. | |
#define | SW_UART_TX_BUFFER_FULL 4 |
Set if data is ready to be sent from the Tx buffer. | |
#define | TIMER_PRESCALER_CONTROL_REGISTER TCCR0 |
Define the timer control register according to the timer used for the UART. | |
#define | TRANSMIT_DELAY 70 |
Cycles from the start bit is sent (from UART_transmit) to the timer is started plus cycles in the timer interrupt before first data bit is sent. | |
#define | TRUE !FALSE |
#define | WAIT_ONE 103 |
Half bit period compare setting. See the application note for calculation of this value. Make sure timer prescaler is set to the intended value. | |
#define | WAIT_ONEHALF (WAIT_ONE + WAIT_ONE/2) |
Functions | |
void | SW_UART_Disable (void) |
Disable the UART. | |
void | SW_UART_Enable (void) |
Enable the UART. | |
uint8_t | SW_UART_Receive (void) |
Receive one byte. | |
void | SW_UART_Transmit (uint8_t) |
Transmit one byte. | |
Variables | |
volatile uint8_t | SW_UART_status |
Byte holding status flags. |
#define CLEAR_FLAG | ( | flag_register, | |||
flag_bit | ) | ( (flag_register) &= ~(1 << (flag_bit)) ) |
Use this macro when clearing a flag in a register.
Definition at line 118 of file single_wire_UART.h.
Referenced by SW_UART_Receive(), SW_UART_Transmit(), and Timer_SW_UART_ISR().
#define CLEAR_UART_EXTERNAL_INTERRUPT_FLAG | ( | ) | (GIFR = (1<<INTF0)) |
Definition at line 108 of file single_wire_UART.h.
Referenced by SW_UART_Enable(), and Timer_SW_UART_ISR().
#define CLEAR_UART_PIN | ( | ) | ( SW_UART_DDR |= (1<<SW_UART_PIN_NUMBER) ) |
Set pin output low.
Definition at line 85 of file single_wire_UART.h.
Referenced by SW_UART_Transmit(), and Timer_SW_UART_ISR().
#define CLEAR_UART_TIMER | ( | ) | (TCNT0 = 0x00) |
Definition at line 99 of file single_wire_UART.h.
Referenced by External_SW_UART_ISR(), SW_UART_Transmit(), and Timer_SW_UART_ISR().
#define CLEAR_UART_TIMER_INTERRUPT_FLAG | ( | ) | (TIFR = (1<<OCF0)) |
Definition at line 102 of file single_wire_UART.h.
Referenced by External_SW_UART_ISR(), SW_UART_Transmit(), and Timer_SW_UART_ISR().
#define CLEAR_UART_TIMER_ON_COMPARE_MATCH | ( | ) | (TCCR0 |= (1<<WGM01)) |
Set timer control register to clear timer on compare match (CTC).
Definition at line 95 of file single_wire_UART.h.
Referenced by SW_UART_Enable().
#define DISABLE_UART_EXTERNAL_INTERRUPT | ( | ) | (GICR &= ~(1<<INT0)) |
Definition at line 107 of file single_wire_UART.h.
Referenced by External_SW_UART_ISR(), SW_UART_Transmit(), and Timer_SW_UART_ISR().
#define DISABLE_UART_TIMER_INTERRUPT | ( | ) | (TIMSK &= ~(1<<OCIE0)) |
#define ENABLE_UART_EXTERNAL_INTERRUPT | ( | ) | (GICR |= (1<<INT0)) |
Definition at line 106 of file single_wire_UART.h.
Referenced by SW_UART_Enable(), and Timer_SW_UART_ISR().
#define ENABLE_UART_TIMER_INTERRUPT | ( | ) | (TIMSK |= (1<<OCIE0)) |
Definition at line 100 of file single_wire_UART.h.
Referenced by External_SW_UART_ISR(), and SW_UART_Transmit().
#define FALSE 0 |
Definition at line 32 of file single_wire_UART.h.
#define INITIALIZE_UART_EXTERNAL_INTERRUPT | ( | ) | (MCUCR |= (1<<ISC01)) |
#define INITIALIZE_UART_PIN | ( | ) | ( SW_UART_PORT &= ~(1<<SW_UART_PIN_NUMBER) ) |
#define PRESCALER 1 |
Prescaler setting. Must be set according to the baud rate setting.
Definition at line 47 of file single_wire_UART.h.
#define READ_FLAG | ( | flag_register, | |||
flag_bit | ) | ( (flag_register) & (1 << (flag_bit)) ) |
Use this macro when reading a flag in a register.
Definition at line 119 of file single_wire_UART.h.
Referenced by main(), Print_String(), and Timer_SW_UART_ISR().
#define READ_UART_PIN | ( | ) | ( SW_UART_PIN & (1<<SW_UART_PIN_NUMBER) ) |
Definition at line 81 of file single_wire_UART.h.
Referenced by External_SW_UART_ISR(), and Timer_SW_UART_ISR().
#define RECEIVE_DELAY 76 |
Cycles from the start bit is detected to the timer is started plus cycles in timer interrupt before first data bit is received.
Definition at line 56 of file single_wire_UART.h.
#define SET_FLAG | ( | flag_register, | |||
flag_bit | ) | ( (flag_register) |= (1 << (flag_bit)) ) |
Use this macro when setting a flag in a register.
Definition at line 117 of file single_wire_UART.h.
Referenced by SW_UART_Transmit(), and Timer_SW_UART_ISR().
#define SET_UART_PIN | ( | ) | ( SW_UART_DDR &= ~(1<<SW_UART_PIN_NUMBER) ) |
#define SET_UART_TIMER_COMPARE_START_RECEIVE | ( | ) | (OCR0 = WAIT_ONEHALF - (RECEIVE_DELAY/PRESCALER)) |
Sets the timer compare register to the correct value when a reception is started.
Definition at line 98 of file single_wire_UART.h.
Referenced by External_SW_UART_ISR().
#define SET_UART_TIMER_COMPARE_START_TRANSMIT | ( | ) | (OCR0 = WAIT_ONE - (TRANSMIT_DELAY/PRESCALER)) |
Sets the timer compare register to the correct value when a transmission is started.
Definition at line 97 of file single_wire_UART.h.
Referenced by SW_UART_Transmit(), and Timer_SW_UART_ISR().
#define SET_UART_TIMER_COMPARE_WAIT_ONE | ( | ) | (OCR0 = WAIT_ONE) |
Sets the timer compare register to one period.
Definition at line 96 of file single_wire_UART.h.
Referenced by Timer_SW_UART_ISR().
#define START_UART_TIMER | ( | ) | (TIMER_PRESCALER_CONTROL_REGISTER |= (1<<CS00)) |
Definition at line 62 of file single_wire_UART.h.
Referenced by External_SW_UART_ISR(), SW_UART_Transmit(), and Timer_SW_UART_ISR().
#define STOP_UART_TIMER | ( | ) | (TIMER_PRESCALER_CONTROL_REGISTER &= ~(1<<CS00)) |
Definition at line 63 of file single_wire_UART.h.
Referenced by External_SW_UART_ISR(), and Timer_SW_UART_ISR().
#define SW_UART_DDR DDRD |
Data direction register. Not available for high voltage ports.
Definition at line 53 of file single_wire_UART.h.
#define SW_UART_EXTERNAL_INTERRUPT_VECTOR INT0_vect |
UART external interrupt vector. Make sure this is in accordance to the defined UART pin.
Definition at line 91 of file single_wire_UART.h.
#define SW_UART_FRAME_ERROR 7 |
Set if a frame error has occured.
Definition at line 114 of file single_wire_UART.h.
Referenced by main(), Print_String(), and Timer_SW_UART_ISR().
#define SW_UART_PIN PIND |
#define SW_UART_PIN_NUMBER 2 |
#define SW_UART_PORT PORTD |
#define SW_UART_RX_BUFFER_FULL 5 |
Set if data is ready to be received from the Rx buffer.
Definition at line 112 of file single_wire_UART.h.
Referenced by main(), SW_UART_Receive(), and Timer_SW_UART_ISR().
#define SW_UART_RX_BUFFER_OVERFLOW 6 |
Set if receive buffer is overflowed. Indicates data loss.
Definition at line 113 of file single_wire_UART.h.
Referenced by main(), and Timer_SW_UART_ISR().
#define SW_UART_TIMER_COMPARE_INTERRUPT_VECTOR TIMER0_COMP_vect |
#define SW_UART_TX_BUFFER_FULL 4 |
Set if data is ready to be sent from the Tx buffer.
Definition at line 111 of file single_wire_UART.h.
Referenced by Print_String(), SW_UART_Transmit(), and Timer_SW_UART_ISR().
#define TIMER_PRESCALER_CONTROL_REGISTER TCCR0 |
Define the timer control register according to the timer used for the UART.
Definition at line 60 of file single_wire_UART.h.
#define TRANSMIT_DELAY 70 |
Cycles from the start bit is sent (from UART_transmit) to the timer is started plus cycles in the timer interrupt before first data bit is sent.
Definition at line 55 of file single_wire_UART.h.
#define TRUE !FALSE |
Definition at line 33 of file single_wire_UART.h.
#define WAIT_ONE 103 |
Half bit period compare setting. See the application note for calculation of this value. Make sure timer prescaler is set to the intended value.
Definition at line 46 of file single_wire_UART.h.
#define WAIT_ONEHALF (WAIT_ONE + WAIT_ONE/2) |
Definition at line 58 of file single_wire_UART.h.
void SW_UART_Disable | ( | void | ) |
Disable the UART.
void SW_UART_Enable | ( | void | ) |
Enable the UART.
This function initialize the timer and buffers, and enables the external interrupt to sense for incoming data. It is important to run this function before calling any of the other UART functions. Received data will automatically be put in the RX buffer.
Definition at line 77 of file single_wire_UART.c.
References CLEAR_UART_EXTERNAL_INTERRUPT_FLAG, CLEAR_UART_TIMER_ON_COMPARE_MATCH, ENABLE_UART_EXTERNAL_INTERRUPT, INITIALIZE_UART_EXTERNAL_INTERRUPT, INITIALIZE_UART_PIN, SW_UART_status, UART_counter, and UART_STATE_IDLE.
Referenced by main().
00078 { 00079 //Tri-state communication pin. 00080 INITIALIZE_UART_PIN(); 00081 00082 SW_UART_status = 0x00; 00083 UART_counter = UART_STATE_IDLE; 00084 CLEAR_UART_TIMER_ON_COMPARE_MATCH(); 00085 00086 //Set up interrupts 00087 INITIALIZE_UART_EXTERNAL_INTERRUPT(); //Set up the external interrupt to wait for incoming start bit on correct pin. 00088 CLEAR_UART_EXTERNAL_INTERRUPT_FLAG(); //Clear flag in case it is already set for some reason. 00089 ENABLE_UART_EXTERNAL_INTERRUPT(); 00090 }
uint8_t SW_UART_Receive | ( | void | ) |
Receive one byte.
This function receives one byte of data by accessing the Rx buffer.
Definition at line 156 of file single_wire_UART.c.
References CLEAR_FLAG, SW_UART_RX_BUFFER_FULL, SW_UART_status, and UART_Rx_buffer.
Referenced by main().
00157 { 00158 uint8_t data; 00159 data = UART_Rx_buffer; 00160 CLEAR_FLAG( SW_UART_status, SW_UART_RX_BUFFER_FULL ); 00161 return data; 00162 }
void SW_UART_Transmit | ( | uint8_t | data | ) |
Transmit one byte.
This function sends one byte of data by putting it in the TX data and initialize a transmission if state is IDLE (UART_counter == 0)
data | Data to be sent. |
Definition at line 121 of file single_wire_UART.c.
References CLEAR_FLAG, CLEAR_UART_PIN, CLEAR_UART_TIMER, CLEAR_UART_TIMER_INTERRUPT_FLAG, DISABLE_UART_EXTERNAL_INTERRUPT, ENABLE_UART_TIMER_INTERRUPT, SET_FLAG, SET_UART_TIMER_COMPARE_START_TRANSMIT, START_UART_TIMER, SW_UART_status, SW_UART_TX_BUFFER_FULL, TRANSMIT_FIRST_DATA, UART_counter, UART_STATE_IDLE, UART_Tx_buffer, and UART_Tx_data.
Referenced by Print_String().
00122 { 00123 SET_FLAG( SW_UART_status, SW_UART_TX_BUFFER_FULL ); 00124 UART_Tx_buffer = data; 00125 00126 //Start transmission if no ongoing communication. 00127 if( UART_counter == UART_STATE_IDLE ) 00128 { 00129 //Copy byte from buffer and clear buffer full flag. 00130 UART_Tx_data = UART_Tx_buffer; 00131 CLEAR_FLAG( SW_UART_status, SW_UART_TX_BUFFER_FULL ); 00132 00133 DISABLE_UART_EXTERNAL_INTERRUPT(); 00134 CLEAR_UART_PIN(); //Write start bit. 00135 UART_counter = TRANSMIT_FIRST_DATA; //Update counter so the 1. data bit is the next bit to be transmitted. 00136 CLEAR_UART_TIMER(); //Clear timer. 00137 SET_UART_TIMER_COMPARE_START_TRANSMIT(); //Set timer compare value. 00138 CLEAR_UART_TIMER_INTERRUPT_FLAG(); //Make sure timer interrupt flag is not set. 00139 ENABLE_UART_TIMER_INTERRUPT(); 00140 START_UART_TIMER(); 00141 } 00142 }
volatile uint8_t SW_UART_status |
Byte holding status flags.
Definition at line 55 of file single_wire_UART.c.
Referenced by main(), Print_String(), SW_UART_Enable(), SW_UART_Receive(), SW_UART_Transmit(), and Timer_SW_UART_ISR().