📄 senddat.c
字号:
/*************************************************************************************************************
* 函数名称 :
* 功能描述 :
* 实际参数 :
* 返回值 :
* 说明 :
**************************************************************************************************************/
#include "define.h"
bit AllowReceipt_Flag ; // 容许接受标志位
bit Wirte_Flash_Flag ; // 改写FLASH标志
uint xdata Site_TempDat[10] ; // 设置温度数据
uchar xdata Receive_Dat[6] ; // 接收到的数据
uchar xdata Send_DatBag[6] ; // 发送数据包
uchar Receive_Finger ; // 接收指针
uchar Send_Finger ; // 发送指针
uchar Send_Counter ; // 发送计数器
uchar Breed_Dat ; // 数据种类,暂时有4种,F0,E0,D0,C0
/*************************************************************************************************************
* 函数名称 :
* 功能描述 :
* 实际参数 :
* 返回值 :
* 说明 : 处理发送温度数据子程序,变BCD码,及小数点运算,
**************************************************************************************************************/
uchar BCD(uchar temp_data)
{
uchar a ;
a = (temp_data / 10) % 10 ;
a <<= 4 ;
a |= (temp_data % 10) ;
return(a) ;
}
/*************************************************************************************************************
* 函数名称 : uint ChangeBCD(uint ack_data,uint sit_temp)
* 功能描述 :
* 实际参数 :
* 返回值 :
* 说明 : 处理发送温度数据子程序,变BCD码,及小数点运算,作假
**************************************************************************************************************/
uint ChangeBCD(uint ack_data,uint sit_temp)
{
uchar temp ;
uint temp_dat;//dat1 ;
sit_temp >>= 2 ;
temp_dat = (ack_data >> 2) ; // 取整数,去小数点
temp = (uchar)(ack_data & 0x03) ;
if (temp >= 2) // 四舍五入,2 X 0.25 = 0.5 度
temp_dat++ ;
if (sit_temp) // 设置温度不为0时
if (temp_dat > sit_temp) // 实际温度比设置温度大时,
{
if (temp_dat > (sit_temp + 3))
{
temp_dat -= 3 ;
}
else
{
temp_dat = sit_temp ;
}
// dat1 =((temp_dat - sit_temp) >> 1) ; // 过冲温度除2
// temp_dat = (sit_temp + dat1) ;
}
temp = ((temp_dat / 1000) % 10) ; // 取温度千位数
temp <<= 4 ;
temp |= ((temp_dat / 100) % 10) & 0x0f ; // 取温度百位数
ack_data = temp ;
ack_data <<= 8 ;
temp = ((temp_dat / 10) % 10) ; /* 取温度十位数 */
temp <<= 4 ;
temp |= ((temp_dat % 10) & 0x0f) ; /* 取温度十位数和个位数 */
ack_data |= temp ;
return(ack_data) ;
}
/*************************************************************************************************************
* 函数名称 :
* 功能描述 :
* 实际参数 :
* 返回值 :
* 说明 : BCD码转十进制,再左移两位,即比实际值打四倍,后两位为小数点位
**************************************************************************************************************/
uint ChangeHex(uchar *change_dat)
{
uint dat0 ;
dat0 = (*change_dat >> 4) * 1000 ; // 千位数
dat0 += (*change_dat & 0x0f) * 100 ; // 百位数
dat0 += (*(change_dat + 1) >> 4) * 10 ; // 十位数
dat0 += *(change_dat + 1) & 0x0f ; // 个位数
dat0 <<= 2 ; // 乘以4倍
return(dat0) ;
}
/*************************************************************************************************************
* 函数名称 : void SendDat(void)
* 功能描述 :
* 实际参数 :
* 返回值 :
* 说明 : 发送数据
**************************************************************************************************************/
void SendDat(void)
{
uint send_array ;
if (Send_Counter == 0) // 已发送完毕一帧数据(6组数据)
{
if (Breed_Dat) // 有发送
{
Send_Counter = 6 ; // 一帧6个数据
switch (Breed_Dat)
{
case 1:
Send_DatBag[5] = 0xd1 ; // 数据代码
Send_DatBag[4] = 0x00 ;
Send_DatBag[3] = (BCD(Check_TempTimeCycle)) ; // 时间不需要增大4倍
Breed_Dat = 2 ; // 发送下一组数据
break ;
case 2: // 温度数据
send_array = ChangeBCD(Fact_Temp[Send_Finger],Site_TempDat[Send_Finger]) ; // 转BCD码
Send_DatBag[5] = 0xf0 | Send_Finger ; // 数据代码
Send_DatBag[4] = (uchar)(send_array >> 8) ;
Send_DatBag[3] = (uchar)send_array ;
if (Send_Finger < 9)
Send_Finger++ ;
else
{
Breed_Dat = 3 ;
Send_Finger = 0 ;
}
break ;
case 3: // 热电偶状态
Send_DatBag[5] = 0xe0 | Send_Finger ; // 数据代码
Send_DatBag[4] = 0x00 ;
Send_DatBag[3] = Thermocouple_Open[Send_Finger] ;
if (Send_Finger < 9)
Send_Finger++ ;
else
{
Breed_Dat = 4 ;
Send_Finger = 0 ;
}
break ;
case 4: // 温度校正值
Send_DatBag[5] = 0xc0 | Send_Finger ; // 数据代码
Send_DatBag[4] = Road_AdhystSign[Send_Finger] ; // 校正符号
Send_DatBag[3] = BCD(Road_AdjustDat[Send_Finger] >> 2) ;// 校正值
if (Send_Finger < 9)
Send_Finger++ ;
else
{
Breed_Dat = 0 ;
Send_Finger = 0 ;
}
break ;
default:
break ;
}
Send_DatBag[2] = Send_DatBag[4] ; // 重发数据包
Send_DatBag[1] = Send_DatBag[3] ; //
Send_DatBag[0] = 0xff ; // 结束标志
SBUF = Send_DatBag[Send_Counter - 1] ;
Send_Counter-- ;
}
}
else // 继续发送,发完6个数据
{
SBUF = Send_DatBag[Send_Counter - 1] ;
Send_Counter-- ;
}
}
/*************************************************************************************************************
* 函数名称 :
* 功能描述 :
* 实际参数 :
* 返回值 :
* 说明 : 发送数据
**************************************************************************************************************/
/* void SendDat(uchar dat0,uchar dat1,road_dat)
{
uchar c ;
SBUF = road_dat ; // 段数据
while(Send_Flag == 0) ;
Send_Flag = 0 ;
for(c = 0;c < 2;c++) // 发送两次数据
{
SBUF = dat1 ; // 第一次发送00,
while(Send_Flag == 0) ;
Send_Flag = 0 ;
SBUF = dat0 ; // 加温标志或,断线标志
while(Send_Flag == 0) ;
Send_Flag = 0 ;
}
SBUF = 0xff ; // 发送结束标志
while(Send_Flag == 0) ;
Send_Flag = 0 ;
}
/*************************************************************************************************************
* 函数名称 :
* 功能描述 :
* 实际参数 :
* 返回值 :
* 说明 : 处理发送温度数据子程序,变BCD码,及小数点运算,
**************************************************************************************************************/
/* void ChangeBCB(uchar *temp_dat0,uchar *temp_dat1,uint ack_data)
{
uchar temp,temp1 ;
uint temp_dat ;
temp_dat = (ack_data >> 2) ; // 取整数,去小数点
temp = (uchar)(ack_data & 0x03) ;
if (temp >= 2) // 四舍五入,2 X 0.25 = 0.5 度
temp_dat++ ;
temp = ((temp_dat / 1000) % 10) ; // 取温度千位数
temp <<= 4 ;
temp1 = ((temp_dat / 100) % 10) & 0x0f ; // 取温度百位数
*temp_dat1 = (temp | temp1) ;
temp = ((temp_dat / 10) % 10) ; // 取温度十位数
temp <<= 4 ;
temp1 = ((temp_dat % 10) & 0x0f) ; // 取温度十位数和个位数
*temp_dat0 = (temp | temp1) ;
return ;
}
/*************************************************************************************************************
* 函数名称 :
* 功能描述 :
* 实际参数 :
* 返回值 :
* 说明 : 发送温度数据子程序,此温度为实际温度的4倍,road_data为几路0-9)
**************************************************************************************************************/
/* void SendTemp(uchar road_data) // temp_data为温度数据,road_data为路数数据
{
uchar a,open_dat,temp0,temp1 ;
uchar *temp00_dat,*temp11_dat ;
uint temp_data ;
temp00_dat = &temp0 ;
temp11_dat = &temp1 ;
if (road_data < 4)
{
a = road_data ;
temp_data = Fact_Temp0[a] ; // 0-4段温度
open_dat = Thermocouple0_Open[a] ;
}
else
{
a = road_data - 5 ;
temp_data = Fact_Temp1[a] ; // 5-9段温度
open_dat = Thermocouple1_Open[a] ;
}
ChangeBCB(temp00_dat,temp11_dat,temp_data) ; // 去小数点,转BCD码
a = (road_data | 0xf0) ; // 先发送段数据F0,F1,F2,F3
SendDat(temp0,temp1,a) ; // 发送
// 发状态标志
a = (road_data | 0xe0) ; // 段数据
SendDat(open_dat,0x00,a) ; // 发送
}
/*************************************************************************************************************
* 函数名称 :
* 功能描述 :
* 实际参数 :
* 返回值 :
* 说明 : 发送温度数据子程序,此温度为实际温度的4倍,road_data为几路0-9)
**************************************************************************************************************/
/* void SendTempDat(uchar road_data) // temp_data为温度数据,road_data为路数数据
{
SendTemp(road_data) ; // 第一个MAX6675
SendTemp(road_data + 5) ; // 第二个MAX6675
}
/*************************************************************************************************************
* 函数名称 :
* 功能描述 :
* 实际参数 :
* 返回值 :
* 说明 : 发送温度修正值
**************************************************************************************************************/
/* void SendAdjustDat(void)
{
SendDat(SiteFack_Temp,0x00,0xd0) ; // 发送几度开始运算加温
SendDat(Check_TempTimeCycle,0x00,0xd1) ; // 发送运算周期时间以秒为单位
}
/*************************************************************************************************************
* 函数名称 : void ReceiptDat(void)
* 功能描述 :
* 实际参数 :
* 返回值 :
* 说明 : 接收数据
**************************************************************************************************************/
void ReceiptDat(void)
{
uchar sbuf_dat ;
sbuf_dat = SBUF & 0xf0 ;
if ((sbuf_dat == 0xf0) || (sbuf_dat == 0xe0) || (sbuf_dat == 0xc0) || (sbuf_dat == 0xd0))
if ((SBUF & 0x0f) < 0x0a) // 判断是否是一帧数据的包头
{
AllowReceipt_Flag = 1 ; // 置位容许接收
Receive_Finger = 0 ; // 重新接收
}
if (AllowReceipt_Flag) // 判断是否容许接收
{
Receive_Dat[Receive_Finger] = SBUF ; // 存储接收到的数据,
Receive_Finger ++ ; // 存储数据指针
if (Receive_Finger == 6) // 判断是否接收完一帧数据
if (Receive_Dat[5] == 0xff) // 判断帧尾是否是结束标志
if ((Receive_Dat[1] == Receive_Dat[3]) && (Receive_Dat[2] == Receive_Dat[4])) //判断接收到的两次数据是否有错
{
AllowReceipt_Flag = 0 ; // 清容许接收标志
Receive_Finger = 0 ; // 清次数
switch(Receive_Dat[0] & 0xf0)
{
case 0xf0: // 设置温度
Site_TempDat[Receive_Dat[0] & 0x0f] = ChangeHex(&Receive_Dat[1]) ; // BCD码转十六进制
break ;
case 0xd0: // 加温运算参数
sbuf_dat = (Receive_Dat[2] >> 4) * 10 + (Receive_Dat[2] & 0x0f) ;
if ((Receive_Dat[0] & 0x0f) != 0x01) // 控温时间,不用乘于4倍
sbuf_dat <<= 2 ; // 乘以4倍
WiteFalsh_Dat[Receive_Dat[0] & 0x0f] = sbuf_dat ;
Wirte_Flash_Flag = 1 ; // 修改FLASH标志
break ;
case 0xc0: // 修正值
WiteFalsh_Dat[(Receive_Dat[0] & 0x0f) + 2] = Receive_Dat[1] & 0xff ;
sbuf_dat = (Receive_Dat[2] >> 4) * 10 + (Receive_Dat[2] & 0x0f) ;
sbuf_dat <<= 2 ;
WiteFalsh_Dat[(Receive_Dat[0] & 0x0f) + 12] = sbuf_dat ;
Wirte_Flash_Flag = 1 ;
break;
default:
break;
}
}
}
}
/*************************************************************************************************************
* 函数名称 :
* 功能描述 :
* 实际参数 :
* 返回值 :
* 说明 :
**************************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -