#include "STC12C.H"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "EEPROM.H"
#include <intrins.h>
sbit In1 = P2^7;
sbit In2 = P2^6;
sbit In3 = P2^5;
sbit In7 = P2^2;
sbit In8 = P2^3;
sbit In9 = P2^4;
sbit In4 = P1^4;
sbit In5 = P1^3;
sbit In6 = P1^2;
//#define ADDRESS (P4&0x0f)
#define FOSC 11059200L //11059200L
#define T1ms (FOSC/24)
#define uint unsigned int
void UART_INIT()
{
SCON = 0x50; // uart 模式1 (8 bit), REN=1;
TMOD = TMOD | 0x20 ; // 定时器1 模式2;
TH1 = 0xFD; // 9600 Bds at 11.059MHz
TL1 = 0xFD;
ES = 1; //允许uart中断;
EA = 1; //允许CPU中断;
TR1 = 1; //运行定时器1:
}
unsigned int idata value;
//unsigned int uart_count=0;
unsigned char send_count=0;
unsigned char xdata recive_buf[30];
unsigned char xdata send_buf[30];
//unsigned char recive_stat=0;
unsigned char idata recive_num=0;
unsigned char idata send_num=0;
unsigned char IRAdd=0;
void serial_IT(void) interrupt 4
{
if (RI == 1) //如果是接收;
{
RI = 0; //清接收标志以便可以接收下一个数据;
if(recive_num<30)
recive_buf[recive_num++]=SBUF;
}
else if(TI) //如果是发送;
{
if(send_num>0)
{
SBUF = send_buf[send_count++];
send_num--;
if(send_num==0)
{
send_count=0;
}
}
else
{
//EN485 =0;
}
TI = 0; //清发送标志;
}
}
void init_timer0(void)//定时器0中断初始化;
{
TMOD &= 0xF0; //定时器0模式2软件启动;
TMOD |= 0x01; /* GATE0=0; C/T0#=0; M10=1; M00=0; */
TL0 = 0x00; //初始值; //5ms @ 22.1184MHz
TH0 = 0xee; //重装值
ET0=1; //允许定时器0中断;
EA=1; //允许CPU中断;
TR0=1; //启动定时器0;
}
unsigned int second_count=0;
unsigned int delay_count=0;
void it_timer0(void) interrupt 1 //定时中断;
{
//char i;
TL0 = 0x00; //初始值; //5ms @ 11.0592MHz
TH0 = 0xee; //重装值
second_count++;
delay_count++;
}
uint crc16(unsigned char *puchMsg, unsigned int usDataLen);
unsigned int mycrc16(unsigned char *puchMsg, unsigned int usDataLen);
void beginsend(void);
bit check_modbus(void);
/*
void PCA_init()
{
unsigned int value;
CCON =0;
CL =0;
CH =0;
CMOD =0x00; //系统时钟,Fosc/12
value=T1ms;
CCAP0L= value;
CCAP0H= value>>8;
value +=T1ms;
CCAPM0 =0x49; //16位软件定时器
CR=0; //启动计数器
EA=1;
}
void PCA_isr() interrupt 7 using 1//大概5ms处理一次
{
CCF0=0;
CR=0;
// receive_count=0;
} *//*
void InitADC()
{
P1ASF = 0xC0; //开启AD4、AD5
ADC_RES =0;
ADC_CONTR = ADC_POWER|ADC_SPEEDLL;
}
*/
void delay(unsigned int t)
{
unsigned int tt=1800;
while(t--);
while(tt--&&recive_num>8);
}
unsigned char IntFromStr(char *s)//3a
{
unsigned char a=0,b=0;
a =s[0]>='A'? 10+(s[0]-'A'):s[0]-'0';
b =s[1]>='A'? 10+(s[1]-'A'):s[1]-'0';
return a*16+b;
}
unsigned int send_time=0;
unsigned char work_mod='0';
unsigned char Str_to_int(char s[]);
unsigned char num[8]; //用于保存按键对应的数码管值
void main()
{
unsigned int second =0;
unsigned char *p;
unsigned char temp[6];
unsigned int crc_temp;
unsigned int jiami=0;
{
UART_INIT();
init_timer0();
read_eeprom_code();
while(1)
{
if(recive_num>0)
{
delay(5000);
if(!check_modbus())
if(IRAdd==IntFromStr(recive_buf))
{
send_buf[0]=recive_buf[0];
send_buf[1]=recive_buf[1];
if(recive_buf[2]=='G' && recive_buf[3]=='I' && recive_buf[4]=='O' ) //获取状态
{
send_buf[0]= recive_buf[0];
send_buf[1]= recive_buf[1];
send_buf[2]= 'I';
send_buf[3]= 'O';
send_buf[4]= '=';
memset(&send_buf[5],'0',9);
if(In1)
send_buf[5]= '1';
if(In2)
send_buf[6]= '1';
if(In3)
send_buf[7]= '1';
if(In4)
send_buf[8]= '1';
if(In5)
send_buf[9]= '1';
if(In6)
send_buf[10]= '1';
if(In7)
send_buf[11]= '1';
if(In8)
send_buf[12]= '1';
if(In9)
send_buf[13]= '1';
send_num = 14;
beginsend();
}
}
recive_num=0;
}
}
}
}
/*
unsigned char Str_to_int(char s[]) //二进制字符转为10进制数
{
unsigned char i=0,j=0,m=1,sum=0;
for(i=8;i>0;i--)
{
for(j=0;j<8-i;j++)
{m=2*m;}
sum=sum+(s[i-1]-0x30)*m;
m=1;
}
return sum;
} */
void beginsend(void)
{
send_count=0;
//led=0;
//EN485 =1;
TI=1;
}
void com_error(unsigned char *p)
{
unsigned int crc;
p[1]=p[1]+0x80;
memcpy(send_buf,p,6);
crc=crc16(send_buf,6);
send_buf[6]=crc>>8;
send_buf[7]=crc&0xff;
send_num=8;
beginsend();
}
int response_io_status( int address, int nb, unsigned char *response, int offset)
{
int shift = 0;
int byte = 0;
int i;
unsigned char io_status[9];
memset(io_status, 0, 9);
if(In1)
io_status[0] =0x01;
if(In2)
io_status[1] =0x01;
if(In3)
io_status[2] =0x01;
if(In4)
io_status[3] =0x01;
if(In5)
io_status[4] =0x01;
if(In6)
io_status[5] =0x01;
if(In7)
io_status[6] =0x01;
if(In8)
io_status[7] =0x01;
if(In9)
io_status[8] =0x01;
for (i = address; i <address+ nb; i++)
{
byte |= io_status[i] << shift;
if (shift == 7)
{
/* Byte is full */
response[offset++] = byte;
byte = shift = 0;
}
else
{
shift++;
}
}
if (shift != 0)
response[offset++] = byte;
return offset;
}
unsigned char get_io_num()
{
unsigned char num=9;
if(In1)
num--;
if(In2)
num--;
if(In3)
num--;
if(In4)
num--;
if(In5)
num--;
if(In6)
num--;
if(In7)
num--;
if(In8)
num--;
if(In9)
num--;
return num;
}
bit check_modbus(void)
{
unsigned int crc=0;
unsigned char CRCH,CRCL;
// 0f 04 07 cf 00 01 01 af
if( recive_num==8)////////////////////address 03 00 00 00 01 crc crc
{
crc=crc16(recive_buf,6);
CRCH =crc>>8;
CRCL =crc&0xff;
if(recive_buf[6]==CRCH&&recive_buf[7]==CRCL)
if(recive_buf[0]==IRAdd)// &&(crc==((a[7]+(a[6]<<8))))
{
if(recive_buf[1]==0x01)
{
unsigned int aa=recive_buf[2]*256+recive_buf[3];
unsigned char len =recive_buf[4]*256+recive_buf[5];
if(aa<1999)
return 0;
send_buf[0]=recive_buf[0];//设备地址
send_buf[1]=recive_buf[1];//功能码
if(len<9)
{
send_buf[2]=1;
response_io_status(aa-1999, len, send_buf, 3);
crc=crc16(send_buf,4);
send_buf[4]=crc>>8;
send_buf[5]=crc&0xff;
send_num=6;
}
else
{
send_buf[2]=2;
response_io_status( aa-1999, len, send_buf, 3);
crc=crc16(send_buf,5);
send_buf[5]=crc>>8;
send_buf[6]=crc&