📄 _rs485.c
字号:
/* this is the rs485.c module, all the serial prot communication is entered here. */
/* specification */
/* the system include the following module: */
/* _main.c
_edit.c
_io.c
_rs485.c */
/* define the include file */
#include <reg52.h>
#include "_main.h"
#include "_io.h"
#include "_edit.h"
#include "_rs485.h"
#include "_predit.h"
#include "_prio.h"
#include "_prmain.h"
uchar xdata *g_receive_ptr; /* the data receive pointer. */
idata uchar g_sum_of_value; /* the byte summary for check. */
idata uint g_sum_of_byte; /* the transfer byte number. */
idata uint g_rece_uint; /* the uint register. */
idata uchar g_rece_char; /* the uchar register. */
idata uchar g_rece_char0;
idata uchar g_rece_status; /* data receive status register. */
idata uchar g_rece_counter; /* the counter in data receive. */
idata uchar g_rece_flash; /* the flash identify in rece mode. */
/* the program begin here. */
/***************************************************************************
NAME: Init_Rs232
FUNCTION: This procedure will initialize the rs232 register.
INPUTS: NONE
OUTPUTS: NONE
***************************************************************************/
void Init_Rs232()
{
/* initialize the status flag. */
AD_485TR = CN_RE_EN; /* the MAX487E receive enable. */
TMOD = 0x21; /* choose mode 2 for Timer1 . */
PCON = 0x00; /* SMOD = 0, TH1 = 256 - (2^SMOD)*f/(384*BAUDRATE) */
TH1 = 0xFB; /* set baud rate is 9600. */
TL1 = 0xFB;
ET1 = 0 ; /* Timer1 interrupt disable. */
TR1 = 1 ; /* start the Timer1. */
SCON = 0xF0; /* choose serial port mode 3 and receive enable. */
PS = 1 ; /* serial port interrupt high priority. */
TI = 0 ;
RI = 0 ;
ES = 1 ; /* serial port interrupt enable. */
SM2 = 1 ; /* only address can be receive. */
EX0 = 0 ; /* disable the EX0 interrupt. */
EA = 1 ; /* enable interrupt. */
}
/***************************************************************************
NAME: Release_Rs232
FUNCTION: This procedure will release the rs232 port.
INPUTS: NONE
OUTPUTS: NONE
***************************************************************************/
void Release_Rs232()
{
TR1 = 0; /* close the Timer1. */
ES = 0; /* disable the serial port interrupt. */
SM2 = 1; /* only address can be received. */
AD_485TR = CN_RE_EN; /* the MAX487E receive enable. */
}
/***************************************************************************
NAME: Byte_Calculate
FUNCTION: This procedure will calculate the transfer byte number.
INPUTS: NONE
OUTPUTS: byte number and start address
***************************************************************************/
void Byte_Calculate()
{
uchar i;
if(g_transall!=CN_SEGSET)
{
g_char_ptr = (uchar xdata *)AD_PROGRAM;
g_sum_of_byte = AD_LASTRAM-AD_PROGRAM+1;
}
else
{
g_uint_ptr = (uint xdata *)AD_STARTTRAN;
g_uintreg0 = *g_uint_ptr;
g_uint_ptr = (uint xdata *)AD_LASTTRAN;
g_uintreg1 = *g_uint_ptr;
if(g_dotshow==CN_LASTSHOW)
{
if(g_uintreg0>=g_uintreg1) /* assure the reg1 >= reg0. */
{
g_sum_of_byte = g_uintreg0;
g_uintreg0 = g_uintreg1;
g_uintreg1 = g_sum_of_byte;
}
}
if(g_dotshow==CN_LASTSHOW)
{
i = 0;
if(g_uintreg0<CN_COMPROG)
{ i = 0; }
if(g_uintreg0>=CN_COMPROG)
{ i = 1; }
switch(i)
{
case 0:
g_char_ptr = (uchar xdata *)(AD_PROGRAM+g_uintreg0*CN_COMBYTE);
g_sum_of_byte = (g_uintreg1-g_uintreg0+1)*CN_COMBYTE;
break;
case 1:
g_char_ptr = (uchar xdata *)(AD_MIDSRAM+(g_uintreg0-CN_COMPROG)*CN_MIDBYTE);
g_sum_of_byte = (g_uintreg1-g_uintreg0+1)*CN_MIDBYTE;
break;
default:
break;
}
}
else
{
if(g_uintreg0<CN_COMPROG)
{
g_char_ptr = (uchar xdata *)(AD_PROGRAM+g_uintreg0*CN_COMBYTE);
g_sum_of_byte = CN_COMBYTE;
}
if(g_uintreg0>=CN_COMPROG)
{
g_char_ptr = (uchar xdata *)(AD_MIDSRAM+(g_uintreg0-CN_COMPROG)*CN_MIDBYTE);
g_sum_of_byte = CN_MIDBYTE;
}
}
}
}
/***************************************************************************
NAME: Tran_Handle
FUNCTION: This procedure will transmit data to another machine.
INPUTS: NONE
OUTPUTS: transmit OK or Err flag.
***************************************************************************/
bit Tran_Handle()
{
bit d_datatrok;
uchar d_charreg;
uchar d_charreg0;
uchar d_counter;
uchar i;
Init_Rs232();
Reset_Watchdog();
for(i=0;i<7;i++)
{
g_numshow[i] = CN_BUSYF;
}
Number_Show(g_itemcnter);
ES = 0; /* serial port interrupt disable. */
d_datatrok = TRUE; /* default the data can transmited successfully. */
AD_485TR = CN_TR_EN; /* the MAX487E transmit enable. */
Delay_Set(CN_2ms);
SM2 = 0; /* receive data enable. */
TB8 = 1; /* assure the firstly transmited signal is address code. */
g_char_ptr = (uchar xdata *)AD_TOMACH; /* point to the machine address. */
SBUF = *g_char_ptr; /* transmit the machine address to the sub machine.*/
while( TI!=1 ); /* waiting for the address is transmited. */
TI = 0;
i = CN_TIMER1; /* delay 1 ms . */
while ( i > 0 )
i--;
AD_485TR = CN_RE_EN; /* the MAX487E receive enable. */
for(d_charreg=0;d_charreg<=CN_DELAYTIMER;d_charreg++)
{
Delay_Set(CN_2ms);
if(RI==1)
{ RI = 0; break; }
if(d_charreg==CN_DELAYTIMER) /* timer overflow. */
{ d_datatrok = FALSE; }
}
if(d_datatrok==TRUE)
{
d_charreg = SBUF;
g_char_ptr = (uchar xdata *)AD_TOMACH;
if(d_charreg==*g_char_ptr) /* shake hands successful. */
{
TB8 = 0; /* transfer data but no address. */
for(d_charreg=0;d_charreg<3;d_charreg++)
{
Delay_Set(CN_2ms);
AD_485TR = CN_TR_EN; /* the MAX487E transmit enable. */
Delay_Set(CN_2ms);
g_sum_of_value = 0; /* clear the sum for check. */
d_datatrok = TRUE;
Byte_Calculate();
for(d_counter=0;d_counter<CN_DAHEADLENGTH+1;d_counter++)
{
switch(d_counter)
{
case 0:
d_charreg0 = g_transall;
SBUF = d_charreg0; /* transmit the all/segment flag. */
g_sum_of_value = g_sum_of_value+d_charreg0;
break;
case 1:
SBUF = g_dotshow;
g_sum_of_value = g_sum_of_value+g_dotshow;
break;
case 2:
d_charreg0 = g_uintreg0/256; /* get the high byte. */
SBUF = d_charreg0;
g_sum_of_value = g_sum_of_value+d_charreg0;
break;
case 3:
d_charreg0 = g_uintreg0%256; /* get the low byte. */
SBUF = d_charreg0;
g_sum_of_value = g_sum_of_value+d_charreg0;
break;
case 4:
d_charreg0 = g_uintreg1/256; /* get the high byte. */
SBUF = d_charreg0;
g_sum_of_value = g_sum_of_value+d_charreg0;
break;
case 5:
d_charreg0 = g_uintreg1%256; /* get the low byte. */
SBUF = d_charreg0;
g_sum_of_value = g_sum_of_value+d_charreg0;
break;
case 6:
SBUF = g_sum_of_value; /* transmit the byte sum value for check. */
break;
default:
break;
}
while( TI!=1 ); /* waiting for the data is transmited. */
TI = 0;
}
i = CN_TIMER1; /* delay 1 ms . */
while ( i > 0 )
i--;
AD_485TR = CN_RE_EN; /* the MAX487E receive enable. */
for(d_counter=0;d_counter<=CN_DELAYTIMER;d_counter++)
{
Delay_Set(CN_2ms);
if(RI==1)
{ RI = 0; break; }
if(d_counter==CN_DELAYTIMER) /* timer overflow. */
{ d_datatrok = FALSE; }
}
if(d_datatrok==TRUE)
{
d_charreg0 = SBUF; /* get the sub machine back information. */
if(d_charreg0==CN_COMMOK) /* data head frame transfer OK. */
{
Delay_Set(CN_2ms);
break;
}
else
{ d_datatrok = FALSE;}
}
}
}
}
Reset_Watchdog();
if(d_datatrok==TRUE) /* the data head frame transfer OK, go into the next step. */
{
Byte_Calculate(); /* calculate the transfer byte and point the start address. */
i = 0;
g_generalbit = TRUE;
for(g_uintreg0=0;g_uintreg0<g_sum_of_byte;)
{
i++;
if(i>=CN_COMMFLASH) /* flash to show the communication is on going. */
{
i = 0;
if(g_generalbit==TRUE)
{
g_generalbit = FALSE;
for(d_charreg=0;d_charreg<7;d_charreg++)
{
g_numshow[d_charreg] = CN_NWHITE;
}
}
else
{
g_generalbit = TRUE;
for(d_charreg=0;d_charreg<7;d_charreg++)
{
g_numshow[d_charreg] = CN_BUSYF;
}
}
Number_Show(g_itemcnter);
}
for(d_charreg0=0;d_charreg0<3;d_charreg0++)
{
Reset_Watchdog();
d_datatrok = TRUE;
g_sum_of_value = 0; /* clear the byte sum register. */
Delay_Set(CN_2ms);
AD_485TR = CN_TR_EN; /* the MAX487E transmit enable. */
Delay_Set(CN_2ms);
for(d_counter=0;d_counter<CN_FRAME+1;d_counter++) /* frame data transfer. */
{
TB8 = 0; /* transfer data but not address. */
if(d_counter==CN_FRAME)
{
SBUF = g_sum_of_value;
while( TI!=1 );
TI = 0;
d_charreg = CN_TIMER1; /* delay 1 ms . */
while ( d_charreg > 0 )
d_charreg--;
AD_485TR = CN_RE_EN; /* the MAX487E receive enable. */
}
else
{
if(g_uintreg0>=g_sum_of_byte)
{ d_charreg=CN_COMMOK; }
else
{ d_charreg = *g_char_ptr; }
SBUF = d_charreg; /* transmit a data. */
while( TI!=1 );
TI = 0;
g_sum_of_value = g_sum_of_value+d_charreg; /* frame summary. */
g_uintreg0++;
g_char_ptr++; /* point to next byte. */
}
}
for(d_charreg=0;d_charreg<=CN_DELAYTIMER;d_charreg++)
{
Delay_Set(CN_2ms);
if(RI==1)
{ RI = 0; break; }
if(d_charreg==CN_DELAYTIMER) /* timer overflow. */
{ d_datatrok = FALSE; }
}
if(d_datatrok==TRUE)
{
d_charreg = SBUF; /* get the sub machine back information. */
if(d_charreg==CN_COMMOK) /* data frame transfer OK. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -