00001
00029 #include "stdint.h"
00030
00031 #ifndef FALSE
00032 #define FALSE 0
00033 #define TRUE !FALSE
00034 #endif
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #define WAIT_ONE 103
00047 #define PRESCALER 1
00048
00049
00050 #define SW_UART_PIN_NUMBER 2
00051 #define SW_UART_PORT PORTD
00052 #define SW_UART_PIN PIND
00053 #define SW_UART_DDR DDRD
00054
00055 #define TRANSMIT_DELAY 70
00056 #define RECEIVE_DELAY 76
00057
00058 #define WAIT_ONEHALF (WAIT_ONE + WAIT_ONE/2)
00059
00060 #define TIMER_PRESCALER_CONTROL_REGISTER TCCR0
00061 #if (PRESCALER == 1)
00062 #define START_UART_TIMER() (TIMER_PRESCALER_CONTROL_REGISTER |= (1<<CS00)) //Needs to be change if a different timer is used. Please refer to datasheet.
00063 #define STOP_UART_TIMER() (TIMER_PRESCALER_CONTROL_REGISTER &= ~(1<<CS00))
00064 #elif (PRESCALER == 8)
00065 #define START_UART_TIMER() (TIMER_PRESCALER_CONTROL_REGISTER |= (1<<CS01))
00066 #define STOP_UART_TIMER() (TIMER_PRESCALER_CONTROL_REGISTER &= ~(1<<CS01))
00067 #else
00068 #error PRESCALER must be set to 1 or 8
00069 #endif
00070
00071 #if ( ((WAIT_ONEHALF-(RECEIVE_DELAY/PRESCALER)) > 255) || ((WAIT_ONE) > 255))
00072 #error WAIT_ONE is set too high. Try to increase prescaler or use a higher baud rate.
00073 #endif
00074
00075 #if ( (WAIT_ONE) < (100/PRESCALER))
00076 #error A too high baud rate is used. Please check the PRESCALER and WAIT_ONE setting.
00077 #endif
00078
00079
00080 #define INITIALIZE_UART_PIN() ( SW_UART_PORT &= ~(1<<SW_UART_PIN_NUMBER) )
00081 #define READ_UART_PIN() ( SW_UART_PIN & (1<<SW_UART_PIN_NUMBER) )
00082
00083
00084 #define SET_UART_PIN() ( SW_UART_DDR &= ~(1<<SW_UART_PIN_NUMBER) )
00085 #define CLEAR_UART_PIN() ( SW_UART_DDR |= (1<<SW_UART_PIN_NUMBER) )
00086
00087
00088
00089
00090
00091 #define SW_UART_EXTERNAL_INTERRUPT_VECTOR INT0_vect
00092 #define SW_UART_TIMER_COMPARE_INTERRUPT_VECTOR TIMER0_COMP_vect
00093
00094
00095 #define CLEAR_UART_TIMER_ON_COMPARE_MATCH() (TCCR0 |= (1<<WGM01))
00096 #define SET_UART_TIMER_COMPARE_WAIT_ONE() (OCR0 = WAIT_ONE)
00097 #define SET_UART_TIMER_COMPARE_START_TRANSMIT() (OCR0 = WAIT_ONE - (TRANSMIT_DELAY/PRESCALER))
00098 #define SET_UART_TIMER_COMPARE_START_RECEIVE() (OCR0 = WAIT_ONEHALF - (RECEIVE_DELAY/PRESCALER))
00099 #define CLEAR_UART_TIMER() (TCNT0 = 0x00)
00100 #define ENABLE_UART_TIMER_INTERRUPT() (TIMSK |= (1<<OCIE0))
00101 #define DISABLE_UART_TIMER_INTERRUPT() (TIMSK &= ~(1<<OCIE0))
00102 #define CLEAR_UART_TIMER_INTERRUPT_FLAG() (TIFR = (1<<OCF0))
00103
00104
00105 #define INITIALIZE_UART_EXTERNAL_INTERRUPT() (MCUCR |= (1<<ISC01)) //< Sets falling edge of INT0 generates interrupt.
00106 #define ENABLE_UART_EXTERNAL_INTERRUPT() (GICR |= (1<<INT0))
00107 #define DISABLE_UART_EXTERNAL_INTERRUPT() (GICR &= ~(1<<INT0))
00108 #define CLEAR_UART_EXTERNAL_INTERRUPT_FLAG() (GIFR = (1<<INTF0))
00109
00110
00111 #define SW_UART_TX_BUFFER_FULL 4
00112 #define SW_UART_RX_BUFFER_FULL 5
00113 #define SW_UART_RX_BUFFER_OVERFLOW 6
00114 #define SW_UART_FRAME_ERROR 7
00115
00116
00117 #define SET_FLAG(flag_register,flag_bit) ( (flag_register) |= (1 << (flag_bit)) )
00118 #define CLEAR_FLAG(flag_register,flag_bit) ( (flag_register) &= ~(1 << (flag_bit)) )
00119 #define READ_FLAG(flag_register,flag_bit) ( (flag_register) & (1 << (flag_bit)) )
00120
00121
00122 extern volatile uint8_t SW_UART_status;
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 void SW_UART_Enable(void);
00142 void SW_UART_Disable(void);
00143 void SW_UART_Transmit(uint8_t);
00144 uint8_t SW_UART_Receive(void);