single_wire_UART.h

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00029 #include "stdint.h"     //Integer types.
00030 
00031 #ifndef FALSE
00032   #define FALSE 0
00033   #define TRUE !FALSE
00034 #endif
00035 
00036 // Baud rate settings (WAIT_ONE - PRESCALER):
00037 //  Baud Rate     1MHz      2Mhz      4MHz      8MHz
00038 //     4800     207 - 1    51 - 8   103 - 8   (207 - 8)
00039 //     9600     103 - 1   207 - 1    51 - 8   103 - 8
00040 //    19200       NA      103 - 1   207 - 1    51 - 8
00041 //    28800       NA         NA     138 - 1    34 - 8
00042 //    38400       NA         NA     103 - 1   207 - 1
00043 // Please note that the UART consumes about all CPU resources when WAIT_ONE*PRESCALER<100.
00044 
00045 /* Communication parameters. The WAIT_ONE definiton has to be changed according to equation 2-1 in the application note. */
00046 #define WAIT_ONE             103      
00047 #define PRESCALER             1       
00048 
00049 /* Port and pin settings. */
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 /* Pin macros.  */
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 /* Macros for standard AVR ports. */
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 /* Macros for high voltage AVR ports. */
00087 //#define SET_UART_PIN()          ( SW_UART_PORT &= ~(1<<SW_UART_PIN_NUMBER) )//!< Tri-state pin.
00088 //#define CLEAR_UART_PIN()        ( SW_UART_PORT |= (1<<SW_UART_PIN_NUMBER) ) //!< Set pin output low.
00089 
00090 /* UART interrupt vectors definitions. */
00091 #define SW_UART_EXTERNAL_INTERRUPT_VECTOR       INT0_vect             
00092 #define SW_UART_TIMER_COMPARE_INTERRUPT_VECTOR  TIMER0_COMP_vect      
00093 
00094 /* Timer macros. These are device dependent. */
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 /* External interrupt macros. These are device dependent. */
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 /* Status register defines. */
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 /* Flag macros. */
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 //Use this to put the UART_counter in a dedicated register. Remember to comment out the previous declarations (also in the
00125 //declaration in single_wire_UART.c) of the counter variable and to lock register 15 when this is used. This is done under
00126 //the code section in the C/C++ compiler category in the IAR options menu.
00127 //__no_init __regvar volatile uint8_t SW_UART_status @ 15;
00128 
00129 //Use this to put the UART_status in a dedicated register. Remember to comment out the previous declaration of the
00130 //counter variable and to lock register 14 when this is used. This is done under the code section in the C/C++ compiler
00131 //category in the IAR options menu.
00132 //__no_init __regvar volatile uint8_t UART_counter @ 14;
00133 
00134 //Use this to put the UART_status in a general purpose io register. Check device datasheet to find adress of GPIO register if available
00135 //(set to 0x1E here). Comment out the previos declarations (also in the declaration in single_wire_UART.c) of the status variable
00136 //when the GPIO register is used.
00137 //__io __no_init static volatile uint8_t SW_UART_status @ 0x1E;
00138 
00139 
00140 /* Global UART functions. */
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);      

Generated on Mon Apr 23 10:13:57 2007 for AVR274: Single-wire Software UART by  doxygen 1.5.1-p1