📄 uart.c
字号:
#include <avr/pgmspace.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <string.h>
#include <stdlib.h>
#include "uart.h"
#include "delay.h"
#define usarthexDigits(i) pgm_read_byte(&usarthex[i])
const prog_char usarthex[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
void USART_Init( unsigned int baudrate )
{
unsigned char x;
// Set the baud rate
UBRRH = (unsigned char) (baudrate>>8);
UBRRL = (unsigned char) baudrate;
// Enable UART receiver and transmitter
UCSRB = ( ( 1 << RXCIE ) | ( 1 << RXEN ) | ( 1 << TXEN ) );
UCSRC = (1<<URSEL)|(1<<USBS)|(1<<UCSZ1)|(1<<UCSZ0); //For devices without Extended IO
x = 0;
USART_RxTail = x;
USART_RxHead = x;
USART_TxTail = x;
USART_TxHead = x;
}
SIGNAL(USART_RECEIVE_INTERRUPT)
{
unsigned char data;
unsigned char tmphead;
data = UDR;
tmphead = ( USART_RxHead + 1 ) & USART_RX_BUFFER_MASK;
USART_RxHead = tmphead; // Store new index
if ( tmphead == USART_RxTail )
{
}
USART_RxBuf[tmphead] = data; // Store received data in buffer
}
SIGNAL(USART_TRANSMIT_INTERRUPT)
{
unsigned char tmptail;
// Check if all data is transmitted
if ( USART_TxHead != USART_TxTail )
{
// Calculate buffer index
tmptail = ( USART_TxTail + 1 ) & USART_TX_BUFFER_MASK;
USART_TxTail = tmptail; // Store new index
UDR = USART_TxBuf[tmptail]; // Start transmition
}
else
{
UCSRB &= ~(1<<UDRIE); // Disable UDRE interrupt
}
}
// Read and write functions
unsigned char USART_Receive( void )
{
unsigned char tmptail;
while ( USART_RxHead == USART_RxTail ) // Wait for incomming data
;
tmptail = ( USART_RxTail + 1 ) & USART_RX_BUFFER_MASK;// Calculate buffer index
USART_RxTail = tmptail; // Store new index
return USART_RxBuf[tmptail]; // Return data
}
void USART_Transmit( unsigned char data )
{
unsigned char tmphead;
// Calculate buffer index
tmphead = ( USART_TxHead + 1 ) & USART_TX_BUFFER_MASK; // Wait for free space in buffer
USART_TxBuf[tmphead] = data; // Store data in buffer
USART_TxHead = tmphead; // Store new index
UCSRB |= (1<<UDRIE); // Enable UDRE interrupt
}
void usart_puts(const char *s )
{
while (*s)
USART_Transmit(*s++);
}
unsigned int usart_getc(void)
{
unsigned char tmptail;
unsigned char data;
if ( USART_RxHead == USART_RxTail ) {
return USART_NO_DATA; // no data available
}
// calculate /store buffer index
tmptail = (USART_RxTail + 1) & USART_RX_BUFFER_MASK;
USART_RxTail = tmptail;
// get data from receive buffer
data = USART_RxBuf[tmptail];
//return (UART1_LastRxError << 8) + data;
return data;
}
void usart_puts_p(const char *progmem_s ){ register char c; while ( (c = pgm_read_byte(progmem_s++)) ) usart_putc(c);}
void usart_putc(unsigned char data)
{
unsigned char tmphead;
tmphead = (USART_TxHead + 1) & USART_TX_BUFFER_MASK;
while ( tmphead == USART_TxTail ){
;// wait for free space in buffer
}
USART_TxBuf[tmphead] = data;
USART_TxHead = tmphead;
// enable UDRE interrupt
USART_CONTROL |= _BV(USART_UDRIE);
}
int DataInReceiveBuffer( void )
{
return ( USART_RxHead != USART_RxTail ); // Return 0 (FALSE) if the receive buffer is empty
}
unsigned int usart_ready(void)
{
if ( USART_RxHead == USART_RxTail )
return(FALSE);
else
return(TRUE);
}
void usart_SendBcd(unsigned char hexIn)
{
unsigned char MSB_byte,LSB_byte;
MSB_byte = usarthexDigits(hexIn >> 4);
LSB_byte = usarthexDigits(hexIn & 0xF);
usart_putc(MSB_byte);
usart_putc(LSB_byte);
}
void usart_16bitHex(unsigned int hexIn)
{
unsigned char B1,B2,B3,B4;
B1 = usarthexDigits((hexIn & 0xF000) >> 12);
B2 = usarthexDigits((hexIn & 0x0F00) >> 8);
B3 = usarthexDigits((hexIn & 0x00F0) >> 4);
B4 = usarthexDigits((hexIn & 0x000F));
usart_putc(B1);
usart_putc(B2);
usart_putc(B3);
usart_putc(B4);
}
void usart_SendBin(unsigned char x)
{
unsigned char i;
for (i=128;i>0;i>>=1)
{
if ((x&i)==0) usart_puts("0");
else usart_puts("1");
}
}
void usart_SendFloat( float usart_value,unsigned char usart_digit ,unsigned char usart_precision)
{
char usart_temp[15];
unsigned char i;
dtostrf(usart_value,usart_digit,usart_precision,usart_temp);
i=0;
while(usart_temp[i] != 0)
{
usart_putc(usart_temp[i]);
i++;
}
}
void usart_SendDec(unsigned int x, unsigned char n, unsigned char fillch)
{
unsigned char i;
unsigned char s[10];
for (i = 0; i < n; i++) {
s[n - i - 1] = '0' + (x % 10);
x /= 10;
}
for (i=0; i<(n - 1); i++) {
if (s[i] == '0') s[i] = fillch; else break;
}
for (i=0; i<n; i++) usart_putc(s[i]);
}
void usart_SendDecDigit(unsigned long x)
{
unsigned int y;
if (x<65535)
{
y=x/1000;usart_putc(y+0x30);x-=(y*1000);
y=x/100;usart_putc(y+0x30);x-=(y*100);
y=x/10;usart_putc(y+0x30);x-=(y*10);
usart_putc(x+0x30);
}
else usart_puts("Err");
}
void usart_SendInt(unsigned int number )
{
char temp[15];
itoa(number,temp,10);
usart_puts(temp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -