📄 sl.c
字号:
else if((*RECEIVEBUFFERPOINT==0xFF)&&(*(RECEIVEBUFFERPOINT+2)==0xC0)&&(*(RECEIVEBUFFERPOINT+3)==0x21)&&(*(RECEIVEBUFFERPOINT+4)==0x5))
{
Reset_Flag=1;
}
else if((*RECEIVEBUFFERPOINT==0x21)&&(*(RECEIVEBUFFERPOINT+1)==0x45)&&(*(RECEIVEBUFFERPOINT+10)==0x11))
{
if((*(RECEIVEBUFFERPOINT+13)==IP_Head[12])&&(*(RECEIVEBUFFERPOINT+14)==IP_Head[13])&&(*(RECEIVEBUFFERPOINT+15)==IP_Head[14])&&(*(RECEIVEBUFFERPOINT+16)==IP_Head[15]))
{
WAVE_Data_Receive = 1;
// WAVE_Rec_Ack(); //注意中心的IP地址
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------
//PPP的FCS校验子程序
UINT pppfcs16(UINT fcs,unsigned char *cp,int len)
{
while (len--)
fcs = (fcs >> 8) ^ fcstab[(fcs ^ *cp++) & 0xff];
return (fcs);
}
//----------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------
//PPP的FCS校验子程序
void Fcs16(unsigned char *cp,int len)
{
UINT trialfcs;
cp++;
trialfcs = pppfcs16( PPPINITFCS16, cp, len-4 );
trialfcs ^= 0xffff; // complement
cp[len-4] = (trialfcs & 0x00ff); // least significant byte first
cp[len-3] = ((trialfcs >> 8) & 0x00ff);
}
/********************************************************************
// PPP连接时交互语句
********************************************************************/
// 发送PPP连接建立的第1条交互语句子程序
void EXCH_SEND1()
{
UINT xdata i,len=22;
Fcs16(EXCHANGE1,len);
for(i=0;i<len;i++)
{
TI0 = 0;
SBUF0 = EXCHANGE1[i];
while(TI0==0); TI0 = 0;
}
}
//-----------------------------------------------------------------------------------------------------------
// 发送PPP连接建立的第2条交互语句子程序
void EXCH_SEND2()
{
UINT xdata i,len=30;
Fcs16(EXCHANGE2,len);
for(i=0;i<len;i++)
{
TI0 = 0;
SBUF0 = EXCHANGE2[i];
while(TI0==0); TI0 = 0;
}
}
//-----------------------------------------------------------------------------------------------------------
// 发送PPP连接建立的第3条交互语句子程序
void EXCH_SEND3()
{
UINT xdata i,len=21;
Fcs16(EXCHANGE3,len);
for(i=0;i<len;i++)
{
TI0 = 0;
SBUF0 = EXCHANGE3[i];
while(TI0==0); TI0 = 0;
}
}
//-----------------------------------------------------------------------------------------------------------
// 发送PPP连接建立的第4条交互语句子程序
void EXCH_SEND4()
{
UINT xdata i,len=21;
Fcs16(EXCHANGE4,len);
for(i=0;i<len;i++)
{
TI0 = 0;
SBUF0 = EXCHANGE4[i];
while(TI0==0); TI0 = 0;
}
}
//-----------------------------------------------------------------------------------------------------------
// 发送PPP连接建立的第5条交互语句子程序
void EXCH_SEND5()
{
UINT xdata i,len=16;
Fcs16(EXCHANGE5,len);
for(i=0;i<len;i++)
{
TI0 = 0;
SBUF0 = EXCHANGE5[i];
while(TI0==0); TI0 = 0;
}
}
//-----------------------------------------------------------------------------------------------------------
// 发送PPP连接建立的第6条交互语句子程序
void EXCH_SEND6()
{
UINT xdata i,len=16;
Fcs16(EXCHANGE6,len);
for(i=0;i<len;i++)
{
TI0 = 0;
SBUF0 = EXCHANGE6[i];
while(TI0==0); TI0 = 0;
}
}
//-----------------------------------------------------------------------------------------------------------
// 发送PPP连接建立的第7条交互语句子程序
void EXCH_SEND7()
{
UINT xdata i,len=16;
Fcs16(EXCHANGE7,len);
for(i=0;i<len;i++)
{
TI0 = 0;
SBUF0 = EXCHANGE7[i];
while(TI0==0); TI0 = 0;
}
}
//--------------------------------------------------------------------
// Interrupt Programe
//--------------------------------------------------------------------
// Timer3 interrupt programe
void Timer3_Interrupt(void) interrupt 14
{
unsigned char i;
TMR3CN &=0x7f; // Clear TF3
if(Timer3_Counter<300){ //Wait 1s
Timer3_Counter++;
}
else{
Timer3_Counter = 0;
counter_timer3++;
if(Net_Ok_Flag==1){
if(CAIJI_Timer != 0) // 采集板重发数据定时处理
{
CAIJI_Timer--;
if(CAIJI_Timer == 0)
{
CAIJI_Timer_End = 1;
}
}
if(WAVE_Timer != 0) // 监控中心重发数据定时处理
{
WAVE_Timer--;
if(WAVE_Timer == 0)
{
WAVE_Timer_End = 1;
}
}
}
else{
if(Timer3_Flag==1){
if(counter_timer3>20){
counter_timer3=0;
TMR3CN &=0xFB; //Closing Timer3
Reset_Flag=1; //Setting reset Flag
}
else{
//EXCH_SEND4();
EXCH_SEND4_Flag=1;
}
}
else{
if(counter_timer3>10){ //sending AT+CSQ more than 10 ,reset
WDTCN = 0xa5;
for(i=0;i<100;i++)Delay10ms();
}
}
}
}
}
//-----------------------------------------------------------------------------
// PCA 中断服务子程序
void PCA_ISR(void) interrupt 9
{
static char SUTXST = 0; // SW_UART TX state variable
static char SURXST = 0; // SW_UART RX state variable
static unsigned char RXSHIFT; // SW_UART RX Shift Register
unsigned int PCA_TEMP; // Temporary storage variable for
// manipulating PCA module high & low bytes.
// Check receive interrupt flag first; service if CCF0 is set.
if (CCF0){
CCF0 = 0; // Clear interrupt flag.
switch (SURXST){
// State 0: START bit received.
// In this state, a negative edge on SW_TX has caused the interrupt,
// meaning a START has been detected and the PCA0CP0 registers have
// captured the value of PCA0.
// - Check for receive enable and good START bit
// - Switch PCA module 0 to software timer mode
// - Add 3/2 bit time to module 0 capture registers to sample LSB.
// - Increment RX state variable.
case 0:
if (SREN & ~SW_RX){ // Check for receive enable and a good
// START bit.
PCA_TEMP = (PCA0CPH0 << 8); // Read module 0 contents into
PCA_TEMP |= PCA0CPL0; // PCA_TEMP.
PCA_TEMP += TH_TIME_COUNT; // Add 3/2 bit times to PCA_TEMP
PCA0CPL0 = PCA_TEMP; // Restore PCA0CPL0 and PCA0CPH0
PCA0CPH0 = (PCA_TEMP >> 8); // with the updated value
PCA0CPM0 = 0x49; // Change module 0 to software
// timer mode, interrupts enabled.
SURXST++; // Update RX state variable.
}
break;
// States 1-8: Bit Received
// - Sample SW_RX pin
// - Shift new bit into RXSHIFT
// - Add 1 bit time to module 0 capture registers
// - Increment RX state variable
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
RXSHIFT = RXSHIFT >> 1; // Shift right 1 bit
if (SW_RX) // If SW_RX=1,
RXSHIFT |= 0x80; // shift '1' into RXSHIFT msb
PCA_TEMP = (PCA0CPH0 << 8); // Read module 0 contents into
PCA_TEMP |= PCA0CPL0; // PCA_TEMP.
PCA_TEMP += TIME_COUNT; // Add 1 bit time to PCA_TEMP
PCA0CPL0 = PCA_TEMP; // Restore PCA0CPL0 and PCA0CPH0
PCA0CPH0 = (PCA_TEMP >> 8); // with the updated value
SURXST++; // Update RX state variable.
break;
// State 9: 8-bits received, Capture STOP bit.
// - Move RXSHIFT into RDR.
// - Set SRI (indicate receive complete).
// - Prepare module 0 for next transfer.
// - Reset RX state variable.
// - Trigger IE7 if user-level interrupt support is enabled.
case 9:
RDR = RXSHIFT; // Move received data to receive register.
SRI = 1; // Set receive complete indicator.
PCA0CPM0 = 0x11; // Switch module 0 to negative capture
// mode; interrupt enabled for START
// detection.
SURXST = 0; // Reset RX state variable.
if (SES){ // If user-level interrupt support enabled
EIE2 |= 0x20; // Enable IE7.
PRT1IF |= 0x80; // Trigger IE7.
}
break;
}
}
// Check Transmit interrupt; service if CCF1 is set.
else if (CCF1){
CCF1 = 0; // Clear interrupt flag
switch (SUTXST){
// State 0: Transmit Initiated.
// Here, the user has loaded a byte to transmit into TDR, and set the
// module 1 interrupt to initiate the transfer.
// - Transmit START bit (drop SW_TX)
// - Read PCA0, add one bit time, & store in module 1 capture registers
// for first bit.
// - Increment TX state variable.
case 0:
SW_TX = 0; // Drop TX pin as START bit.
PCA_TEMP = PCA0L; // Read PCA counter value into
PCA_TEMP |= (PCA0H << 8); // PCA_TEMP.
PCA_TEMP += TIME_COUNT; // Add 1 bit time.
PCA0CPL1 = PCA_TEMP; // Store updated match value into
PCA0CPH1 = (PCA_TEMP >> 8); // module1 capture/compare registers.
PCA0CPM1 |= 0x48; // Enable module 1 software timer.
SUTXST++; // Update TX state variable.
break;
// States 1-9: Transmit Bit.
// - Output LSB of TDR onto TX
// - Shift TDR 1 bit right.
// - Shift a '1' into MSB of TDR for STOP bit in State 9.
// - Add 1 bit time to module 1 capture register
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
SW_TX = (TDR & 0x01); // Output LSB of TDR onto SW_TX pin.
TDR >>= 1; // Shift TDR right 1 bit.
TDR |= 0x80; // Shift '1' into MSB of TDR for
// STOP bit in State 9.
PCA_TEMP = (PCA0CPH1 << 8); // Read module 0 contents into
PCA_TEMP |= PCA0CPL1; // PCA_TEMP.
PCA_TEMP += TIME_COUNT; // Add 1 bit time to PCA_TEMP
PCA0CPL1 = PCA_TEMP; // Restore PCA0CPL1 and PCA0CPH1
PCA0CPH1 = (PCA_TEMP >> 8); // with the updated value
SUTXST++; // Update TX state variable.
break;
// State 10: Last bit has been transmitted. Transmit STOP bit
// and end transfer.
// - Transmit STOP bit
// - Set TX Complete indicator, clear Busy flag
// - Reset TX state
// - Prepare module 1 for next transfer.
// - Trigger IE7 interrupt if user-level interrupts enabled.
case 10:
STI = 1; // Indicate TX complete.
SUTXST = 0; // Reset TX state.
SW_TX = 1; // SW_TX should remain high.
PCA0CPM1 = 0x01; // Disable module 1 software timer; leave
// interrupt enabled for next transmit.
if (SES){ // If user-level interrupt support enabled:
EIE2 |= 0x20; // Enable IE7.
PRT1IF |= 0x80; // Trigger IE7.
}
STXBSY = 0; // SW_UART TX free.
break;
}
}
}
//----------------------------------------------------------------------------------------
// USER_ISR: User SW_UART Interrupt Service Routine (IE7 ISR)
// If interrupt-mode test code is enabled, this ISR
// transmits 15 characters and receives 15 characters. This routine is triggered each
// time a SW_UART transmit or receive is completed.
// - Checks receive complete indicator, and services.
// - Checks transmit complete indicator, and services.
// - Checks for transmits or receives that completed during the ISR; if so, triggers the
// interrupt again.
// 软串口中断服务程序(外部中断7服务程序)
void USER_ISR(void) interrupt 19 // IE7 Interrupt Service Routine
{
unsigned char Rec_Data;
PRT1IF &= ~(0x80); // Clear IE7 interrupt flag
if (SRI) // If Receive Complete:
{
SRI = 0; // Clear receive flag.
Rec_Data=RDR;
if (Receive_Ini_Flag==0)
{
if(Rec_Data==0xa5)
{
Receive_Ini_Flag=1; //Has received the first byte
}
}
else if ( Receive_Ini_Flag==1)
{
if(Rec_Data==0xa5)
{
Receive_Ini_Flag=2;
Ini_Data_BufferPoint=Ini_Data_Buffer;
Ini_Data_Num_Reci=0;
}
else
{
Receive_Ini_Flag=0;
}
}
else if ( Receive_Ini_Flag==2)
{
*(Ini_Data_BufferPoint+Ini_Data_Num_Reci)=Rec_Data;
Ini_Data_Num_Reci++;
if( Ini_Data_Num_Reci==23)
{
Receive_Ini_Flag=0;
Ini_Data_Num_Reci=0;
//待处理程序
Ini_Data_Flag=1;
}
}
else
{
Receive_Ini_Flag=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -