📄 uart.c
字号:
#include <avr/interrupt.h>
#include <inttypes.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "uart.h"
#include "myDef.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 )
{
/* Set the baud rate */
UBRR0H = (unsigned char) (baudrate>>8);
UBRR0L = (unsigned char) baudrate;
//cbi(UCSR0A,U2X0) ;
/* Enable UART receiver and transmitter */
UCSR0B = ((1<<RXCIE0) | (1<<RXEN0) | (1<<TXEN0));
/*For devices without Extended IO*/
UCSR0C = (0<<USBS0) | (0<<UCSZ02)| (1<<UCSZ01)| (1<<UCSZ00);
USART_RxTail = 0;
USART_RxHead = 0;
USART_TxTail = 0;
USART_TxHead = 0;
}
SIGNAL( USART_RECEIVE_INTERRUPT )
{
unsigned char tmphead;
if(!(UCSR0A & (0<<RXC0)))
{
tmphead = ( USART_RxHead + 1 ) & USART_RX_BUFFER_MASK;
USART_RxHead = tmphead;
USART_RxBuf[tmphead] = UDR0;
}
}
SIGNAL( USART_TRANSMIT_INTERRUPT )
{
unsigned char tmptail;
if ( USART_TxHead != USART_TxTail )
{
tmptail = ( USART_TxTail + 1 ) & USART_TX_BUFFER_MASK;
USART_TxTail = tmptail;
UDR0 = USART_TxBuf[tmptail];
}
else
{
UCSR0B &= ~(1<<UDRIE0);
}
}
unsigned int usart_getc(void)
{
unsigned char tmptail;
unsigned char data;
if ( USART_RxHead == USART_RxTail ) {
return USART_NO_DATA;
}
tmptail = (USART_RxTail + 1) & USART_RX_BUFFER_MASK;
USART_RxTail = tmptail;
data = USART_RxBuf[tmptail];
return data;
}
void usart_puts(const char *s )
{
while (*s)
usart_putc(*s++);
}
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 ){
;
}
USART_TxBuf[tmphead] = data;
USART_TxHead = tmphead;
USART_CONTROL |= (1<<UDRIE0);
}
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 tempHex;
tempHex = hexDigits[hexIn >> 4];
usart_putc(tempHex);
tempHex = hexDigits[hexIn & 0x0F];
usart_putc(tempHex);
}
/*
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_8bitHex(unsigned int 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);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -