⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 softuart.h

📁 AVR单片机软件模拟串口实现全双工的例子。
💻 H
字号:

#if !defined(F_CPU)
#warning F_CPU not defined in makefile - now defined in softuart.h
#define F_CPU 3686400UL
#endif

#define SOFTUART_BAUD_RATE   4800

#define SOFTUART_RXPIN   PIND
#define SOFTUART_RXDDR   DDRD
#define SOFTUART_RXBIT   PD6

#define SOFTUART_TXPORT  PORTD
#define SOFTUART_TXDDR   DDRD
#define SOFTUART_TXBIT   PD7

#define SOFTUART_T_COMP_LABEL      TIM0_COMPA_vect
//#define SOFTUART_T_COMP_REG        OCR0A
//#define SOFTUART_T_CONTR_REGA      TCCR0A
//#define SOFTUART_T_CONTR_REGB      TCCR0B
#define SOFTUART_T_CONTR_REG         TCCR0    

#define SOFTUART_T_CNT_REG         TCNT0
#define SOFTUART_T_INTCTL_REG      TIMSK

//#define SOFTUART_CMPINT_EN_MASK    (1<<OCIE0A)
#define SOFTUART_CMPINT_EN_MASK    (1<<TOIE0)

//#define SOFTUART_CTC_MASKA         (1<<WGM01)
//#define SOFTUART_CTC_MASKB         (0)

/* "A timer interrupt must be set to interrupt at three times 
   the required baud rate." */
#define SOFTUART_PRESCALE (8)
// #define SOFTUART_PRESCALE (1)

#if (SOFTUART_PRESCALE==8)
//#define SOFTUART_PRESC_MASKA         (0)
//#define SOFTUART_PRESC_MASKB         (1<<CS01)
#define SOFTUART_PRESC_MASK         (1<<CS01)
#elif (SOFTUART_PRESCALE==1)
//#define SOFTUART_PRESC_MASKA         (0)
//#define SOFTUART_PRESC_MASKB         (1<<CS00)
#define SOFTUART_PRESC_MASK         (1<<CS00)
#else 
#error "prescale unsupported"
#endif


#define SOFTUART_TIMERTOP ( F_CPU/SOFTUART_PRESCALE/SOFTUART_BAUD_RATE/3 -1)

#if (SOFTUART_TIMERTOP > 0xff)
#warning "Check SOFTUART_TIMERTOP"
#endif

#define SOFTUART_IN_BUF_SIZE     32

// Init the Software Uart
void softuart_init(void);

// Clears the contents of the input buffer.
void softuart_flush_input_buffer( void );

// Tests whether an input character has been received.
unsigned char softuart_kbhit( void );

// Reads a character from the input buffer, waiting if necessary.
char softuart_getchar( void );

// To check if transmitter is busy
unsigned char softuart_can_transmit( void );

// Writes a character to the serial port.
void softuart_putchar( const char );

// Turns on the receive function.
void softuart_turn_rx_on( void );

// Turns off the receive function.
void softuart_turn_rx_off( void );

// Write a NULL-terminated string from RAM to the serial port
void softuart_puts( const char *s );

// Write a NULL-terminated string from program-space (flash) 
// to the serial port. i.e. softuart_puts_p(PSTR("test"))
void softuart_puts_p( const char *prg_s );

// Helper-Macro - "automaticly" inserts PSTR
// when used: include avr/pgmspace.h before this include-file
#define softuart_puts_P(s___) softuart_puts_p(PSTR(s___))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -