/*****************************************************************************
*文件名: Uart_test.C
*版权: ATR XJL
*功能: 串口通信
*日期: 20090312
*************************************************************** **************/
//Head file
#include "REG938.H"
#include <INTRINS.H>
#include <MATH.H>
#include <FLOAT.H>
#include "config.h"
#include "var_def.h"
#include "func_def.h"
#include "eeprom.h"
//#define uchar unsigned char
//typedef unsigned char uchar;
//unsigned char temp;
//function
void IO_Init();
void UART_Init();
void AD_Init();
void AD_Start();
void RTC_Init();
void WatchDog_Init();
void Clear_WatchDog();
void TIMER_Init();
void DelayT0(unsigned int t);
void prtTsHHeader(unsigned char SendStatusFlag,unsigned char datlen);
void TransmitSTAck(unsigned char *buf, unsigned char datlen);
//void send_to_rs();
void RefreshPollData();
void RdADDat();
void ClrADDat();
void Alarm();
unsigned char stDataSave();
unsigned char AccessBasicInfor(unsigned char flag);
unsigned int AD7466_read();
void Delays(unsigned char no);
void POWERMX(unsigned int *data1,unsigned int *data2);
void TEMPMX(unsigned int *data1,unsigned int *data2);
void INT0_Init();
//--------I/O PORT------
//Light distribution
//add port0
sbit LED_Temp = P0^5; //pin22_LED1
sbit LED_MachineRun = P0^6; //pin20_LED2 pin_LED2
sbit LED_TXStatus = P0^7; //pin19_LED3
//add port1
sbit KGFPOWER = P1^2; //pin_KOUT2
sbit KFJ = P1^6;//pin_KOUT1
// sbit LED_Power = P1^3; // pin_KIN0 LED_Power
sbit KJLQPower = P1^4; //pin_KIN1
//add port2
//sbit pin_LED0 = P2^0; //DEBUG//定义LED控制引脚
//sbit pin_LED1 = P2^1; //定义LED控制引脚
sbit CS = P2^2; // KTEMP pin_KOUT3
sbit DI = P2^3;//KVSWR pin_KOUT4
sbit KBH = P2^4; //pin_KOUT5
sbit SK = P2^5;// SK RS485EN pin_
sbit LED_VSWR = P2^6; //debug pin27_LED4
sbit RS485EN = P2^7; // LED_Temp debug //FOR AlarmTemp pin28_LED5
//add
// sbit KABUS = P3^1;
// sbit KBBUS = P3^0;
//--------END I/O PORT -----
//-------------------------
//功能 接收中断
//-------------------------
void Rcv_ISR() interrupt 4{
//当接收完一个字节 RI置1 进入Recieve中断
unsigned char TempDat;
EA=0;
// while(!RI) //等待接收完时RI置位
TempDat = SBUF; //保存所读取的数据
RI=0; //RI清0 以等待下次发送
if(RTFlag0 == 1) //正在解析上一帧 丢掉接收到的数据
{
EA=1;
return;
}
if(RsCount != 2) //帧头检测
{
if(TempDat == mRTFIRSTBYTE) //0x7b
{
RsCount++;
}
EA=1;
return;
}
else //帧头已经检测到
{
if(RsLen0 == 0)
{
if((TempDat == mPRODADDR) || (TempDat == DevAddr)) //0xf0 0xb5
{
RsDat0[RsLen0] = TempDat;
RsLen0++;
}
else
{
RsCount = 0;
EA=1;
return;
}
}
else
{
if((TempDat == mRTENDBYTE) && (RsLen0 == RsDat0[3]+6))
{//收到数据尾 长度正确
RsLen0++;
RTFlag0 = 1;
RsCount = 0;
RS485EN = 1; //使能485发送
EA=1;
return;
//return;
}
RsDat0[RsLen0] = TempDat;
if(RsLen0 < mRSMAXLEN)
RsLen0++;
else
{
RsLen0 = 0;//数据超长,重新接收
RsCount = 0;
RTFlag0 = 0;
}
}
}
// SBUF = TempDat; //发送数据
// while(!TI); //当TI为1时 发送完毕
// TI=0; //TI清0 准备下一次发送
EA=1;
}
//-------------------------
//功能 发送中断
//-------------------------
void Send_ISR() interrupt 13{
EA = 0;
//在这里添加你需要的代码
TI = 0; //TI清0 准备下一次发送
// LED_VSRW = ~LED_VSRW;
//LED_TXStatus = ~LED_TXStatus;
TXC_FLAG = 1;
EA = 1;
}
//---------------------------
void INT0_ISR() interrupt 0{
EA=0;
// SBUF = 0x99;
bKG = ~bKG;
INT0=1; //INT1置高电平
IE0=0; //清除外部中断标志
EA=1; //开中断
//
}
//----------------------------
struct PollDataStruct xdata PollDataBuffer;
struct RsDataStructHeader xdata *prtRsH;
struct ADataAnalysisStruct xdata *prtRsA; //
struct TsDataStructHeader xdata *prtTsH;
struct PollAckStruct xdata *prtTsPollAck;
//unsigned char aa[8];
//--------------------------
//功能 主函数
//--------------------------
void main()
{
unsigned char i;
IO_Init();
UART_Init();
RTC_Init();
AD_Init();
AD_Start();
WatchDog_Init();
TIMER_Init();
Allflag = 0x00;
countfs = mSecond05;
countfs05 = mSecond05;
DevAddr = EEPROM_read(addrMDEV_ADDR);
DevID = EEPROM_read(addrMDEV_ID);
DevType = EEPROM_read(addrMDEV_TYPE);
StatusReg1 = EEPROM_read(addrMStatusReg1);
StatusReg1 = 0x00;
StatusReg2 = EEPROM_read(addrMStatusReg2);
StatusReg2 = 0x00;
//add
LimitPower = ((EEPROM_read(addrMLimitPower)<<8) & 0xff00) + EEPROM_read(addrMLimitPower+1);
LimitTemp = ((EEPROM_read(addrMLimitTemp)<<8) & 0xff00) + EEPROM_read(addrMLimitTemp+1);
LimitVSWR = EEPROM_read(addrMLimitVSWR);
SwitchStatusReg = EEPROM_read(addrMSwitchStatusReg);
POWERMX(&power02,&power05);
TEMPMX(&temp40,&temp52);
INT0_Init();
// LED_MachineRun = CLEAR; //开机前 20090525
//add
// KABUS = 0;
// KBBUS = 0;
// DelayT0(3);
LED_MachineRun = TRUE;
LED_TXStatus = CLEAR;
RS485EN = 0; //使能485接收
//LED_TXStatus = 0;
LED_VSWR = TRUE;
LED_Temp = TRUE;
bMachineStatusFlag = TRUE;
bTXInstructionsFlag = TRUE;
// KVSWR = 1;
KFJ = 1; //开机前不动作
KGFPOWER = 1; //开机前不动作
KJLQPower = 1; //开机前不动作
KBH = 1;//正常时不动作
RTFlag0 = 0;
ADWriteFlag = 0; //debug
bJLQQH = 0;
bFZQH = 0;
bKG = 0;
Clear_WatchDog();
ClrADDat();
while(1)
{
if(ADWriteFlag == 1)
{
// while(!(ADCON0 & 0x08)); //等待转换完毕
//#if 1
ClrADDat();
for(i=0;i<mCollCount;i++)
RdADDat();
// #endif
Clear_WatchDog();
RefreshPollData();
Clear_WatchDog();
// AD_Start();
}
// RTFlag0 = 1;
if(RTFlag0 == 1)
{
//CRC校验
LED_TXStatus = ~LED_TXStatus;
// prtRsA = (struct ADataAnalysisStruct *)RsDat0;//指向接收数据帧
// Delays(4); //4
DelayT0(4); //
Clear_WatchDog();
// GNflag = prtRsA->DataHead.rsGNflag;
GNflag = *(RsDat0 +1);
switch(GNflag)
{
//遥测指令
case 0x10:{
//TsStflag = RsDat0[2] | 0x80;
prtRsH = (struct RsDataStructHeader *)RsDat0;//指向接收数据帧
prtTsPollAck = (struct PollAckStruct*)TsDat0; //指向发送域
prtTsPollAck->FrameHeader.tsFrameHeader0 = mRTFIRSTBYTE;
prtTsPollAck->FrameHeader.tsFrameHeader1 = mRTFIRSTBYTE;
prtTsPollAck->FrameHeader.tsDevAddr = DevAddr;
prtTsPollAck->FrameHeader.tsGNflag = GNflag;
prtTsPollAck->FrameHeader.tsTsStflag = prtRsH->rsRsStflag | 0x80;
prtTsPollAck->FrameHeader.tsTsDatalen = sizeof(struct PollDataStruct);
prtTsPollAck->PollData = PollDataBuffer;
// TransmitSTAck((char *)prtTsPollAck,0);
TransmitSTAck(TsDat0, prtTsPollAck->FrameHeader.tsTsDatalen);
}break;
case 0x30:{
prtRsH = (struct RsDataStructHeader *)RsDat0;//指向接收数据帧
TransmitSTAck(TsDat0,AccessBasicInfor(prtRsH->rsRsStflag));
}break;
//遥控指令
case 0x81:{
prtRsA = (struct ADataAnalysisStruct *)RsDat0;//指向接收数据帧
prtTsH = (struct TsDataStructHeader*)TsDat0; //指向发送域
if(ret = EEPROM_write(addrMDEV_ADDR, prtRsA->ADataEntities.uaddress))
{
DevAddr = EEPROM_read(addrMDEV_ADDR);
prtTsHHeader(0x80, 0);
}
else
{
// DevAddr = 0x00; //debug
prtTsHHeader(0xa4, 0);
}
// prtTsH = (struct TsDataStructHeader*)TsDat0; //指向发送域
//Header();
//prtTsH->tsTsStflag = prtRsA->DataHead.rsRsStflag | 0x80;
//prtTsH->tsTsDatalen = 0;
// TransmitSTAck((char*)prtTsH ,0);
TransmitSTAck(Ts