📄 zhongcai.c
字号:
//#include <SM89516A.h>
#include <reg52.h>
#include <string.h>
#include <math.h>
#include <intrins.h>
#include <stdio.h>
#include "MyRam.h"
#include "ComRam.h"
#define uchar unsigned char
#define uint unsigned int
/**************************************公共RAM区地址分配*****************************************/
//公共RAM区从机采集数据包存储开始地址.
#define ComSlaveDataStart1 0x0000 //000H~2FFH 公共RAM区从机1的数据的开始地址.
#define ComSlaveLenAdd1 0x02FE //公共RAM区从机1数据字节数的存贮地址.
#define ComSlaveDataStart2 0x0300 //300H~5FFH 公共RAM区从机2的数据的开始地址.
#define ComSlaveLenAdd2 0x05FE //公共RAM区从机2数据字节数的存贮地址。
#define ComSlaveDataStart3 0x0600 //600H~8FFH 公共RAM区从机3的数据的开始地址.
#define ComSlaveLenAdd3 0x08FE //公共RAM区从机3数据字节数的存贮地址。
#define ComSlaveDataStart4 0x0900 //900H~BFFH 公共RAM区从机4的数据的开始地址.
#define ComSlaveLenAdd4 0x0BFE //公共RAM区从机4数据字节数的存贮地址。
#define ComSlaveDataStart5 0x0C00 //C00H~EFFH 公共RAM区从机5的数据的开始地址.
#define ComSlaveLenAdd5 0x0EFE //公共RAM区从机5数据字节数的存贮地址。
/*******从机的报警区********/
#define ComAlarmStart1 0x1000 //1000H-10FDH 公共RAM区从机1报警的起始地址。
#define ComAlarmLenAdd1 0x10FE //10FEH-10FFH 公共RAM区从机1报警数据总长度的存储地址。
#define ComAlarmStart2 0x1100 //1100H-10FDH 公共RAM区从机2报警的起始地址。
#define ComAlarmLenAdd2 0x11FE //11FEH-11FFH 公共RAM区从机2报警数据总长度的存储地址。
#define ComAlarmStart3 0x1200 //1200H-12FDH 公共RAM区从机3报警的起始地址。
#define ComAlarmLenAdd3 0x12FE //12FEH-12FFH 公共RAM区从机3报警数据总长度的存储地址。
#define ComAlarmStart4 0x1300 //1300H-13FDH 公共RAM区从机4报警的起始地址。
#define ComAlarmLenAdd4 0x13FE //13FEH-13FFH 公共RAM区从机4报警数据总长度的存储地址。
#define ComAlarmStart5 0x1400 //1400H-14FDH 公共RAM区从机5报警的起始地址。
#define ComAlarmLenAdd5 0x14FE //14FEH-14FFH 公共RAM区从机5报警数据总长度的存储地址。
//公共RAM区从机命令数据包存放开始地址.
#define ComOrderStart 0x1500 //1500H~4FFFH 公共RAM区发给从机命令的起始地址。
#define ComLenOrder 0x4FFE //公共RAM区从机命令字节数的存贮RE认为》256N。
#define ComNewOrderFlag 0x4FFD //公共RAM区有新从机命令的标识位。
//公共RAM区从机时间命令数据包存放开始地址.
#define ComTimeStart 0x5000 //5000H~50FFH 公共RAM区日期和时间的其实地址。
#define ComLenTimer 0x50FE //公共RAM区时间字节数存贮地址。
//公共RAM区输出数据包存放开始地址.
#define ComOutputStart 0x5100 //5100H~6FFFH 公共RAM区发给输出单片机的起始地址。
#define ComLenOutput 0x6FFE //公共RAM区输出字节数存贮地址。
//公共RAM区从机使能数据包存放开始地址.
#define ComSlaveEnable 0x7500 //公共RAM区从机使能数据包存贮地址.
#define ComLenSlaveEnable 0x7510 //公共RAM区从机使能数据包状态标识存贮地址.
#define ComSlaveEnableNewFlag 0x7511 //公共RAM区从机使能数据包状态标识存贮地址.
/**************************************公共RAM区地址分配*****************************************/
/**************************************服务器命令数据包接收信息**********************************/
//接收状态。
#define InitState 0 //开始接收状态。
#define CommandState 1 //命令包状态。
#define DataState 2 //数据状态。
//数据包头及数据包长度。
#define StartFlag 0x2A //包开始标识。
#define CmdWs 0xA1 //外设参数数据包。
#define LenCmdWs 0x0E //外设参数数据包长度。
#define CmdSlave 0xA2 //从机参数数据包。
#define LenCmdSlave 0x08 //从机参数数据包长度。
#define CmdTime 0xA3 //时间同步数据包。
#define LenCmdTime 0x09 //时间同步数据包长度。
#define CmdAlarm 0xA4 //联动配置表数据包。
#define LenCmdAlarm 0x05 //联动配置表数据包长度。
#define CmdWsControl 0xA5 //外设控制数据包。
#define LenCmdWsControl 0x05 //外设控制数据包长度。
#define CmdChannelPara 0xA6 //通道参数数据包。
#define LenCmdChannelPara 0x09 //通道参数数据包长度。
#define CmdGetChannelPara 0xA7 //取通道参数数据包。
#define LenCmdGetChannelPara 0x05 //取通道参数数据包长度。
#define CmdSlaveEnable 0xA8 //从机使能数据包。
#define LenCmdSlaveEnable 0x07 //从机使能数据包。
#define CmdSendSlave 0xB1 //提交从机数据数据包。
/**************************************服务器命令数据包接收信息**********************************/
#define LenCmdTime 0x09
#define LEN 40
#define MAX 200
#define Main 0x00
#define Output 0x01
#define ZhongCai 0x02
#define Slave1 0x03
#define Slave2 0x04
#define Slave3 0x05
#define Slave4 0x06
#define Slave5 0x07
uchar FlagEnableReaded = 1; //读到A8数据包标识。
uchar TimeCnt = 0; //定时器中断计数器。
uchar LenSlaveEnableReaded = 0; //读到公共RAM区从机使能数据包的长度。
uchar SlaveEnableRcv[7] = {0,0,0,0,0,0,0}; //5个从机的使能标识。
uchar FlagCurrent = ZhongCai; //当前使用IIC权的资源标识。
/**************************************IIC使用权用已分配标识*****************************************/
uchar FlagMainEnable = 0; //当没有读到从机配置数据包时的主机IIC使用权标识。
uchar NewFlagSlaveEnable = 0; //从机使能数据包的更新标识。
bit FlagSlave1Enable = 0;
bit FlagSlave2Enable = 0;
bit FlagSlave3Enable = 0;
bit FlagSlave4Enable = 0;
bit FlagSlave5Enable = 0;
/**************************************IIC使用权用已分配标识*****************************************/
bit FlagTime0 = 0;
uint TimeCount = 0;
sbit P12 = P1^2; //从单片机1IIC使用权使用完信号返回标识。
sbit P13 = P1^3; //从单片机2IIC使用权使用完信号返回标识。
sbit P14 = P1^4; //从单片机3IIC使用权使用完信号返回标识。
sbit P15 = P1^5; //从单片机4IIC使用权使用完信号返回标识。
sbit P16 = P1^6; //从单片机5IIC使用权使用完信号返回标识。
sbit P17 = P1^7; //主单片机IIC使用权使用完信号返回标识。
sbit P36 = P3^6; //输出单片机IIC使用权使用完信号返回标识。
sbit P20 = P2^0; //通过p2.0,p2.1,p2.2三线来分配IIC使用权。
sbit P21 = P2^1; //000-从机1;001-从机2;010-从机3;011-从机4;
sbit P22 = P2^2; //100-从机5;101-主机;110-输出单片机;111-仲裁机。
#define DelayTime 2
/*****************************************************************************************
功能:ClearComRam
描述:清零字节数存储地址
参数:address---需要清零的地址
返回:无
*****************************************************************************************/
void ClearComRam(uint address)
{
uchar temp[2];
temp[0]=0x00;
temp[1]=0x00;
ComWriteBytes(address,temp,2);
}
/****************************************************************************************
功能:Delay
描述:延时函数
参数:t
返回:无
****************************************************************************************/
void Delay(uint t)
{
while(t!=0)
t--;
}
void Time0Initial()
{
TR0=0;
TH0=0xFF; //50ms
TL0=0xFF;
// TH0=0x3C; //50ms
// TL0=0xB0;
TR0=1;
}
/***********************************************************************************************************
功能:InitSerialCom
描述:串口初始化
参数:无
返回:无
************************************************************************************************************/
void InitSerialCom(void)
{
SCON = 0x50; //SCON: serail mode 1, 8-bit UART, enable ucvr
TMOD |= 0x21; //TMOD: timer 1, mode 2, 8-bit reload
PCON |= 0x80; //SMOD=1;
TH1 = 0xF3; //Baud:4800 fosc=12MHz
IE |= 0x93; //Enable Serial Interrupt
TR1 = 1; // timer 1 run
IT0 = 0;
// TI=1;
}
/***********************************************************************************************************
功能:SendCharCom
描述:向串口发送一个字符
参数:t
返回:无
************************************************************************************************************/
void SendCharCom(unsigned char ch)
{
SBUF=ch;
while(TI==0);
TI=0;
}
/***********************************************************************************************************
功能:SendStringCom
描述:向串口发送一个字符串,strlen为该字符串长度
参数:str---要发送的字符串指针 strlen---字符串长度
返回:无
************************************************************************************************************/
void SendStringCom(unsigned char *str,unsigned int strlen)
{
unsigned int k=0;
while( (k < strlen) && (strlen > 0) )
{
SendCharCom(*(str + k));
k++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -