uart.c
来自「atmega128串口通信程序」· C语言 代码 · 共 72 行
C
72 行
#include <iom128v.h>
#include "type.h"
#include "bitdef.h"
#include "global.h"
//UART0 initialize
// desired baud rate: 2400
// actual: baud rate:2404 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x67; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0xD8;
}
/*一个字符接收完成中断*///////////////
#pragma interrupt_handler uart0_rx_isr:19
void uart0_rx_isr(void)
{
//uart has received a character in UDR
BYTE com;
com=UDR0;
rx_buf[rx_point]=com;
if(com!=255)
{
rx_point++;
if(rx_point>19) rx_point=0;
}
else
{
rx_point=0;
if(rx_buf[0]!=bj_address)
{
siu_flag=FALSE;
return;
}
siu_flag=TRUE;
}
}
/*////////////////////////////////////
一个字符发送完成中断
*/////////////////////////////////////
#pragma interrupt_handler uart0_tx_isr:21
void uart0_tx_isr(void)
{
//character has been transmitted
BYTE com;
if(tx_end)
{
TXCON = 0; /*释放通讯总线,保持为接收状态,因为485通信是半双工*/
UCSR0B &= ~(1<<TXCIE0); /*禁止发送完成中断*/
return;
}
com=tx_buf[tx_point];
UDR0=com;
tx_point++;
if(com==255 || tx_point>19) /*要发送的数据发送结束*/
{
tx_point=0;
tx_end=TRUE;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?