Weight_Bed/Uart_Init.c

157 lines
3.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "H/Function_Init.H"
void Uart_Init(uint Freq,unsigned long int baud);
long Cclt_Power(uchar a, long b);
bit UartSendFlag = 0; //发送中断标志位
bit UartReceiveFlag = 0; //接收中断标志位
//乘方a幂b底
long Cclt_Power(uchar a, long b)
{
uchar i;
long temp;
temp = 1;
for(i=0;i<a;i++)
temp = temp*b;
return temp;
}
/*****************************************************
*函数名称void Uart_Test(void)
*函数功能Uart测试
*入口参数void
*出口参数void
*****************************************************/
void Uart_Test(void)
{
Uart_Init(32,9600);
}
/*****************************************************
*函数名称void Uart_Init(uint Freq,unsigned long int baud)
*函数功能Uart中断初始化
*入口参数Freq-主频baud-波特率
*出口参数void
*****************************************************/
void Uart_Init(uint Freq,unsigned long int baud) //选择Timer1作为波特率信号发生器
{
P2CON &= 0xFC; //TX/RX设置为输入带上拉
P2PH |= 0x03;
SCON |= 0X50; //设置通信方式为模式一,允许接收
TXCON |= 0X03; //UART0时钟来源为定时器1
TH1 = (Freq*1000000/baud)>>8; //波特率为T1的溢出时间
TL1 = Freq*1000000/baud;
TR1 = 0; //SC95F761xB系列UARTO使用定时器1作为时钟源TR1为0
ET1 = 0;
EUART = 1; //开启Uart中断
EA = 1;
}
/*
void Uart_Init(uint Freq,unsigned long int baud) //选择Timer2作为波特率信号发生器
{
P2CON &= 0xFC; //TX/RX设置为输入带上拉
P2PH |= 0x03;
SCON |= 0X50; //设置通信方式为模式一,允许接收
TXINX = 0x02;
TMCON |= 0X04;
TXMOD = 0X00;
TXCON = 0X30;
RCAPXH = Freq*1000000/baud/256;
RCAPXL = Freq*1000000/baud%256;
TRX = 0;
ET2 = 0;
EUART = 1; //开启Uart中断
EA = 1;
}
*/
/*****************************************************
*函数名称void UartInt(void) interrupt 4
*函数功能Uart中断函数
*入口参数void
*出口参数void
*****************************************************/
void UartInt(void) interrupt 4
{
if(TI) //UART0发送中断
{
TI = 0;
UartSendFlag = 1;
}
if(RI) //UART0接收中断
{
RI = 0;
UartReceiveFlag = 1;
}
}
void UART_SendByte(uchar Byte)//字节发送
{
SBUF = Byte;
while(!UartSendFlag);
UartSendFlag = 0;
}
void UART_SendLong(long Dat)//32位从高到低发送
{
uchar i;
uchar temp[4];
temp[0] = (Dat >> 24) & 0xFF;
temp[1] = (Dat >> 16) & 0xFF;
temp[2] = (Dat >> 8) & 0xFF;
temp[3] = Dat & 0xFF;
for(i=0;i<4;i++)
{
UART_SendByte(temp[i]);
}
}
void UART_Send_VOFA(long uart_buff)
{
uchar j;
uchar buff;
uchar vofa_buff[8];
bit negative=0;
if(uart_buff<0)//负数补码转源码
{
uart_buff=~uart_buff+1;
negative = 1;
}
for(j=0;j<8;j++)
{
vofa_buff[j] = (uart_buff % Cclt_Power(j+1,10)) / Cclt_Power(j,10);//16进制数转10进制采用小端法存低位在低字节
}
if(negative)
UART_SendByte(45);//输出负数
for(j=0;j<8;j++)
{
buff = vofa_buff[7-j]+48;//输出为字符串,需要先送高位
UART_SendByte(buff);
}
}
void UART_SendAll(long* Dat)//4个传感器原始数据都发送
{
uchar i,j;
uchar temp[4];
for(j=0;j<4;j++)
{
temp[0] = (Dat[j] >> 24) & 0xFF;
temp[1] = (Dat[j] >> 16) & 0xFF;
temp[2] = (Dat[j] >> 8) & 0xFF;
temp[3] = Dat[j] & 0xFF;
for(i=0;i<4;i++)
{
UART_SendByte(temp[i]);
}
}
}