📄 uart.c
字号:
/*************************************************************/
/* 串口操作函数库 */
/* 环境: WinAVR-20071221 */
/* 作者: hhsj(胡双江) */
/* E-mail:hsj210@tom.com */
/* 日期: 2008年1月12日 */
/*************************************************************/
#include <iom64v.h>
#include <macros.h>
#include "uart.h"
#define _BV(X) (1<<(X))
#define FOCS 8000000 // 晶振8M
#define BAUD 9600 // 波特率9600 bps
#define BAUD_RATE (FOCS / 16 / (BAUD + 1))
#define UART1
#ifdef UART0
#define UCSRA UCSR0A
#define UCSRB UCSR0B
#define UCSRC UCSR0C
#define UBRRL UBRR0L
#define UBRRH UBRR0H
#define UDR UDR0
#else
#define UCSRA UCSR1A
#define UCSRB UCSR1B
#define UCSRC UCSR1C
#define UBRRL UBRR1L
#define UBRRH UBRR1H
#define UDR UDR1
#define RXC 7
#define UDRE 5
#endif
/////////////////////////////////////////////////////////////////////
void USART_Init (void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = 0x06;
UBRRL = (unsigned char)(BAUD_RATE & 0xFF); //set baud rate low
UBRRH = (unsigned char)(BAUD_RATE >> 8); //set baud rate high
UCSRB = 0x18;
}
/////////////////////////////////////////////////////////////////////
void USART_Transmit(unsigned char val)
{
while(!(UCSRA & _BV(UDRE))); // wait the transmit buffer empty
UDR = val; // send data
}
/////////////////////////////////////////////////////////////////////
unsigned char USART_Receive(void)
{
while(!(UCSRA & _BV(RXC))); // wait the receive buffer has a new data
return UDR; // return the data
}
/////////////////////////////////////////////////////////////////////
void USART_String(char *p)
{
while(*p != '\0')
{
USART_Transmit(*p);
p++;
}
}
void USART_Hex(unsigned char temp)
{
unsigned char *buff="0x00\n";
unsigned char hex_table[16]={'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F'};
*(buff+2) = hex_table[temp>>4];
*(buff+3) = hex_table[temp&0x0F];
USART_String(buff);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -