⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 基于51的单片机的PPP协议实现
💻 C
字号:
#include <iogp20.h>
#include "CommDrv.h"
#include "ModemDrv.h"
#include "ppp.h"
#include "UDP.h"
#include "IP.h"
#include "SLIP.h"

//#define USE_SLIP			// Uncomment this line if SLIP is to be used

BYTE RemoteServer [4] = {200, 168, 3, 11};	// Remote Server to send notifications		

const char * ModemCommand [] = {	// Array of modem initialization commands
	"ATZ\r",			// Reset Command
	"ATE0\r",			// Disable Echo 
	"AT&C1\r",			// Track presence of data carrier
	"AT&D3\r"			// Reset modem when an on-to-off transition of DTR ocurres	
};




/***********************************************************************
Function : 	ModemHandler

Parameters : 	Code - Numeric response code from a Modem dial command

Date : 		January 2001

Desc : 		This function handles the numeric responses from a dial command
		issued to the modem

***********************************************************************/
void ModemHandler (BYTE Code) {

	switch (Code) {
		case '0':		// OK
		break;

		case '1':		// CONNECT
#ifdef USE_SLIP
			CommEventProc (ProcSLIPReceive);	// Install SLIP service routine
#else

			ModemBuffFlush ();			// Flush contents of Modem Buffer 
			if (ModemGetch () != 0x7F) {		// Test for PPP packets
				Waitfor (":", 100);		// Wait for "Username:" of ISP script
				PPPSendVoidLCP ();		// Force PPP transactions instead of scripts
		            }
			CommEventProc (ProcPPPReceive);		// Install PPP service routine
#endif
		break;

		case '2':		// RING
		break;

		case '3':		// NO CARRIER
		break;

		case '4':		// ERROR
		break;

		case '6':		// NO DIAL TONE
		break;

		case '7':		// BUSY
		break;

		case '8':		// NO ANSWER
		break;

		case '9':		// CONNECT 2400
		break;

		default:		// TIME OUT, NO RESPONSE FROM MODEM RECEIVED!
        	break;
	}
}



/***********************************************************************
Function : 	UDPReceive

Parameters : 	Data of UDP packet, 
		size - size of data in bytes
		RemoteIP - sender IP address
		port - UDP port number

Date : 		January 2001

Desc : 		This function is executed each time a UDP packet is received 
		and validated.

***********************************************************************/
void UDPReceive (BYTE *data, BYTE size, DWORD RemoteIP, WORD port) {
	
	switch (port) {						// Select the port number of the UDP packet
		case 1080:					// If port number equals 1080 then reply with ADC channel 0
			ADSCR &= 0x00;				// Get an A/D lecture
			while (!(0x80 & ADSCR));		
			udp_out->Payload [0] = ADR;		// Format UDP payload
			UDPSendData ((BYTE *)&RemoteIP, 11222, 0, 1);	// Send UDP reply
		break;

		case 1081:					// Port = 1081, reply with ADC ch1	
			ADSCR &= 0x01;
			while (!(0x80 & ADSCR));
			udp_out->Payload [0] = ADR;
			UDPSendData ((BYTE *)&RemoteIP, 11222, 0, 1);
		break;

		case 1082:					// Do something here
		break;

		case 1083:					
		break;
	}
}


void LinkTask (void) {
	if ((PPPStatus & LinkOn) && (!ModemOnLine())) {		// PPP Link ON while Phone is on-hook!
		PPPStatus &= ~LinkOn;				// Clear PPP link flag
		PORTC = 0x00;	
		CommEventProc (ProcModemReceive);		// Install Modem handler
	}
}

void ApplicationTask (void) {			
	ADSCR &= 0x02;						// Test A/D channel 2
	while (!(0x80 & ADSCR));				// Wait for A/D conversion
	if (ADR > 0x35) {					// If sample is above 0x35 
								// Send a potification
		if (!ModemOnLine ()) {					// Test if Modem on-line
			NoOperation;					// Modem Not on-line, we can re-dial here
		}
		UDPSendData ((BYTE *)&RemoteServer, 8010, "Warning from HC08!" , 18);
	}
}


/////////////////////////////////////////////////////////////////////////////
// M A I N 
/////////////////////////////////////////////////////////////////////////////
void main(void) { 

  InitPLL ();					// Init PLL to 4.9152MHz

  CONFIG1 = (BYTE)0x0B;			// LVI operates in 5-V mode,
					// STOP instruction enabled
					// COP Module Dissabled

  CONFIG2 = (BYTE)0x03;			// Oscillator enabled to operate during stop mode
					// Internal data bus clock used as clock source for SCI
					

  PORTC = 0; 
  DDRC = 0xFF;
  
	IPInit ();				// Initialize IP

#ifdef USE_SLIP
	SLIPInit ();				// Initialize SLIP implementation
	IPBindAdapter (SLIP);			// Send IP packets using SLIP format
#else
	PPPInit ();				// Initialize PPP interface
	IPBindAdapter (PPP);			// Send IP packets using PPP format
#endif	
	UDPSetCALLBACK (UDPReceive);		// Set Callback function for incoming UDP data

	ModemInit ();				// Modem Init
	ModemBindBuff (PPPGetInputBuffer());	// Set Modem Buffer for command reception
	CommEventProc (ProcModemReceive);	// re-direct incoming SCI characters to the Modem interface
	OpenComm (BAUDS_2400, 			// Open the serial port
		ENABLE_RX | 			// Enable SCI Rx and Tx modules
		ENABLE_TX | 			
		ENABLE_RX_EVENTS);		// Enable Rx IRQs

	{
		BYTE Res = 0;			// Create two temp vars in the stack
		BYTE index;
	
			for (index = 0; index <= 3; index++) {		// Loop through Modem initiazation commands
				transmit (ModemCommand [index]);	// Transmit modem command
				Res = Waitfor ("OK", 30);		// Wait for OK 

				if (!Res) {				// Invalid response received
					// Do something here

					ModemReset ();			// Reset modem 
					index = 0;			// Loop again	
				}	
			}
			Res = ModemDial ("6842626");			// Dial ISP
			ModemHandler (Res); 				// Handle Modem response
	}

	EnableInterrupts;						
	for (;;) {							// Application Loop
#ifdef USE_SLIP
		SLIPEntry();						// Poll SLIP packets
#else
		LinkTask ();						// Synchronize PPP link with Modem 
		PPPEntry ();						// Poll for PPP packets
#endif
		ApplicationTask ();					// Call application task
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -