📄 usart.c
字号:
/*************************************************************
* Target: usart communication
* File name: usart.c
* Date: 2006.11.19
* Author: Panda
************************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "usart.h"
#define byte unsigned char
#define word unsigned int
#define CR '/0'
#define BR '/n'
#define pRx &(Rx_Buffer[0])
#define Rx_Buffer_Size 33
static byte Rx_Buffer[Rx_Buffer_Size] ; //receive buffer
static byte Rx_Ctr = 0;
static byte Rx_Ix = 0;
static byte test_buffer[] = "test!\n"; //test the array sending
static volatile byte *pTx; //send pointer
static volatile byte Tx_Length = 0; //send length
static byte Tx_Ctr = 0; // send counter
//串口通信初始化
void usart_init(void)
{
UCSRB = 0x00;//禁止中断
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x0B;
UBRRH = 0x00;
UCSRB = 0xD8;
}
/***********************************************
* The receiving of the usart, by interrupt
************************************************/
SIGNAL(SIG_USART_RECV)
{
byte temp = UDR;
if(temp!='r')
{
if (Rx_Ix < 32)
{
Rx_Ctr++;
Rx_Buffer[Rx_Ix++] = temp;
}
if(Rx_Ix==32)
{
Rx_Ix=0;
Rx_Buffer[32]='\0';
}
}
else if(temp=='r')
{
printf(Rx_Buffer);
//for(temp = 0 ;temp<100;temp++)
//asm("nop");
//printf(test_buffer);
}
}
/************************************************
* the send of usart, by interrupt
************************************************/
SIGNAL(SIG_USART_TRANS)
{
if (Tx_Ctr < Tx_Length )
{
Tx_Ctr++; // the couter of send
UDR = *(pTx++); // send data to the sending
}
else end_trans();
}
/************************************************
* the interrupt when the UDR is empty
* when the UDR is empty, means that
* you can put the data into the UDR
************************************************/
/*
SIGNAL(SIG_USART_DATA)
{
if (Comm_Tx_Ctr < Comm_Tx_Size)
Comm_Tx_Ctr++; // the couter of send
UDR = Comm_Tx_Buf[Comm_Tx_Ix++]; // send data to the sending
if ( Comm_Tx_Ix == Comm_Tx_Size )
Comm_Tx_Ix = 0;
}
*/
/**********************************************************
* send string!
* 2006.11.24
**********************************************************/
void printf(unsigned char * Send_String)
{
Tx_Length = strlen(Send_String); //求发送字节数
pTx = &(Send_String[0]); //求发送字符串首地址
Start_trans(); //开始发送
}
/********************************************************
* puts char
* 2006.11.26
********************************************************/
void puts(unsigned char data)
{
while (!(UCSRA & (1<<UDRE)))
;
UDR = data; //也有可能出现傻等的情况,
}
/*********************************************************
* start the transmitt, finish on 2006.11.22
*********************************************************/
void Start_trans(void)
{
Tx_Ctr = 0; //情零标志位,从缓冲头开始发送
while (!(UCSRA & (1<<UDRE)))
;
UDR = *pTx++; //也有可能出现傻等的情况,
Tx_Ctr++; //2006.11.22 完成固定缓冲发送,该函数固定。
}
/********************************************************
* end the trans ,finish on 2006.11.22
*********************************************************/
void end_trans(void)
{
Tx_Length = 0;
Tx_Ctr = 0;
}
/********************************************************
* handle the data recieved, finish on2006.11.24
*********************************************************/
/*
void handle(void)
{
unsigned char Ix;
pRx = &Rx_Buffer[0];
for ( Ix=0 ; Ix<Rx_Ctr ; Ix++)
printf(*(pRx+Ix));
}
*/
/////////////////////////////////////////////////////////
// end
/////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -