📄 uart._c
字号:
/********************************************************************
* 函数库说明:RS232总线驱动函数库 *
* 版本说明: 1.0 Bate *
* 作者: andylee *
* 日期: 2006年7月 *
* 平台: mega16 16M *
* 说明: 为上层提供RS232的基本读写函数 *
********************************************************************/
#include "uart.h"
#include "can.h"
unsigned char uart_b0_flag=0; //降压标志
unsigned char uart_b8_flag=0; //停止充电标志
unsigned char uart_normal_flag=0; //正常
unsigned char uart_alarm_flag=0; //报警
/*************************************
* 函数说明:串口接收中断 *
* 输入:无 *
* 输出:无 *
************************************/
#pragma interrupt_handler RX_ISR:12
void RX_ISR(void)
{
unsigned char sreg;
unsigned char comm_tempRec;
unsigned int i;
unsigned char sreg;
sreg=SREG; //保存全局中断标志
CLI();
comm_tempRec=UDR;
if(comm_tempRec==0xb0)//表示降电压
{
uart_b0_flag=1;
uartSendByte(comm_tempRec);
}
if(comm_tempRec==0xb8)//表示停止充电
{
uart_b8_flag=1;
uartSendByte(0xb8);
}
if(comm_tempRec==0xf0)//正常
{
uart_normal_flag=1;
uartSendByte(comm_tempRec);
}
if(comm_tempRec==0xa0)//报警
{
uart_alarm_flag=1;
uartSendByte(comm_tempRec);
}
SREG=sreg;
SEI();
}
/*************************************
* 函数说明:波特率设置函数 *
* 输入:波特率 *
* 输出:无 *
*************************************/
void uartSetBaudRate(unsigned long baudrate)
{
unsigned int baudratDiv;
baudratDiv = (F_CPU/(baudrate*16L)-1);
UBRRH=(unsigned char)(baudratDiv>>8);
UBRRL=(unsigned char)baudratDiv;
}
/*************************************
* 函数说明:串口化函数 *
* 输入:无 *
* 输出:无 *
*************************************/
void uartInit(void)
{
//enable RxD/TxD and interrupts
UCSRB=(1<<RXCIE)|(0<<TXCIE)|(1<<RXEN)|(1<<TXEN);
//设置波特率
uartSetBaudRate(UART_DEFAULT_BAND_RATE);
//设置帧格式: 8 个数据位, 2 个停止位
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
/************************************
* 函数说明:查询方式发送一个字节 *
* 输入:无 *
* 输出:无 *
*************************************/
void uartSendByte(unsigned char txData)
{
while(!(UCSRA&(1<<UDRE)));
// send byte
UDR=txData;
}
/*************************************
* 函数说明:串口接收例程 *
* 输入:无 *
* 输出:接受到的数据 *
*************************************/
unsigned char uartReceiveService(void)
{
unsigned char c;
c=UDR;
uartSendByte(c);
return UDR;
}
/*************************************
* 函数说明:查询方式串口接收例程 *
* 输入:无 *
* 输出:无 *
*************************************/
unsigned char uart_receive( void )
{
unsigned char c;
/* 等待接收数据*/
while ( !(UCSRA & (1<<RXC)) )
{
//rst_wdog();
}
/* 从缓冲器中获取并返回数据*/
c=UDR;
//uartSendByte(c);
return c;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -