📄 ch4_uart_int.c
字号:
#include<avr/io.h>
#include<avr/interrupt.h>
#include<avr/signal.h>
#define uchar unsigned char
#define uint unsigned int
uchar g_bTxdPos = 0; //发送定位计数器
uchar g_bTxdLen = 0; //等待发送字节数
uchar g_bRxdPos = 0; //接收定位计数器
uchar g_bRxdLen = 0; //等待接收字节数
uchar g_aSendBuff[16]; //发送数据缓冲区
uchar g_aRecvBuff[16]; //接收数据缓冲区
//接收中断
SIGNAL(SIG_UART1_RECV)
{
uchar c = UDR0 ;
if(g_bRxdLen>0)
{
g_aRecvBuff[g_bRxdPos] = c;
g_bRxdLen--;
}
}
//发送中断
SIGNAL(SIG_UART0_TRANS)
{
if(--g_bTxdLen>0)
{
UDR0 = g_aSendBuff[++g_bRxdPos];
}
}
//receive complete or not
uchar IsRecvComplete(void)
{
return g_bTxdLen == 0;
}
void SendToUart(uchar size)
{
g_bRxdPos = 0;
g_bTxdLen = size;
UDR0 = g_aSendBuff[0];
while(g_bTxdLen>0);
}
//接收指定长度数据到接收缓冲区
void RecvFromUart(uchar size, uchar bwait)
{
g_bTxdPos = 0;
g_bRxdLen = size;
if(bwait)
{
while(g_bRxdLen>0)
;// empty body
}//end if
}
int main()
{
uchar i;
//uart inintilization
//enable receive transmit and interrupt
UCSR0B = (1<<RXCIE0) | (1<<TXCIE0) | (1<<RXEN0) | (1<<TXEN0);
UBRR0L = 51; //baud = 9600
sei(); //always permit interrupt
while(1)
{
//asyno receive 16 byte data
RecvFromUart(16,0);
//copy the data into the buffer of send
for(i=0;i<16;i++)
{
g_aSendBuff[i] = g_aRecvBuff[i];
}
SendToUart(16);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -