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

📄 modemdrv.c

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

#define MODEM_BUFFER_SIZE	32		/* Size of Modem Buffer */

#define DTR_ON		PORTD &= 0xFE;
#define DTR_OFF		PORTD |= 0x01;

#define DTR_PIN (PORTD & 0x01)			/* DTR Pin = Pin 0 of PORT D */

						/* Byte pointers of the ring buffer (FIFO) */
volatile BYTE	mDataSlot = 0;			/* Points to the next available character */
volatile BYTE 	mEmptySlot = 0;			/* Points to next available slot of the FIFO */

static BYTE *ModemBuffer;			/* Pointer to Modem buffer */


/***********************************************************************
Function : 	ModemInit

Parameters : 	None

Date : 		December 2000

Desc : 		Initializes the ring buffer & clears the DTR pin

***********************************************************************/
void ModemInit (void) {
	mDataSlot = 0;
	mEmptySlot = 0;

	DDRD |= 0x01;
	DTR_OFF;
}


/***********************************************************************
Function : 	ModemBuffFlush

Parameters : 	None

Date : 		January 2001

Desc : 		Flushes the receiving FIFO (ring buffer)

***********************************************************************/
void ModemBuffFlush (void) {
	mDataSlot = mEmptySlot;
}


/***********************************************************************
Function : 	ModemDial

Parameters : 	A string containing the phone number to dial

Date : 		December 2000

Desc : 		It sets the modem response mode to numeric (instead of verbose), 
		then it dials a phone number & sets the DTR pin. This function 
		returns a numeric code describing a response from the modem or 
		a timeout. Applications should handle this reaponse code.
***********************************************************************/
BYTE ModemDial (char * Number) {
signed char delayCount = 80;
	transmit ("ATV0\r");				// Force a numeric response from modem
   	if (!Waitfor ("0", 30)) {			// Wait for an OK response
		return -1;
	}
  
	DTR_ON;						// Set DTR to ON
	transmit ("ATDT");				// Dial the ISP number
	transmit (Number);
	transmit ("\r");
	ModemBuffFlush ();  				//  Flush contents of buffer
							// Wait for a reply
 	while ((!ModemBuffNotEmpty()) && (--delayCount > 0)) {
		Delay (250);
   	}
	if (delayCount) {
		return ModemGetch ();			// Return the numeric response to caller
   	}
	return -1;					// No response received from modem
}

/***********************************************************************
Function : 	ModemHangUp

Parameters : 	None

Date : 		December 2000

Desc : 		This function clears DTR to force the modem to hang up if
		it was on line and/or make the modem to go to command mode.
***********************************************************************/
void ModemHangUp (void) {
	DTR_ON;						// Make a DTR transition to hang-up 
	Delay (40);					// Wait a couple of miliSeconds
	DTR_OFF;					// Finish the DTR transition
}


/***********************************************************************
Function : 	ModemOnLine

Parameters : 	None

Date : 		January 2001

Desc : 		Returns the status of the CD (carrier detect) signal.
***********************************************************************/
BYTE ModemOnLine (void) {
	return (PORTD & 0x02) ^ 0x02;			// Return the status of the CD line
}



/***********************************************************************
Function : 	ModemBindBuff

Parameters : 	A pointer to a buffer in RAM 

Date : 		January 2001

Desc : 		Binds the FIFO capabilities of this module to a buffer 
		in RAM.
***********************************************************************/
void ModemBindBuff (BYTE *lpInBuffer) {
	ModemBuffer = lpInBuffer;			
	ModemBuffer [0] = 0;
}

/***********************************************************************
Function : 	ModemReset

Parameters : 	None

Date : 		January 2001

Desc : 		Resets the Modem
***********************************************************************/
void ModemReset (void) {
	ModemInit ();
}

/***********************************************************************
Function : 	ModemBuffNotEmpty

Parameters : 	None

Date : 		January 2001

Desc : 		Returns True if modem buffer NOT empty, false otherwise.
***********************************************************************/
BYTE ModemBuffNotEmpty (void) {
	return !(mDataSlot == mEmptySlot);

}

/***********************************************************************
Function : 	ModemInBufferCount

Parameters : 	None

Date : 		January 2001

Desc : 		Returns the number of characters available in the Modem 
		Queue.
***********************************************************************/
BYTE ModemInBufferCount (void) {
	if ((mEmptySlot - mDataSlot) >= 0)
	   return (BYTE)(mEmptySlot - mDataSlot);
   else {
		return (BYTE)((mEmptySlot + MODEM_BUFFER_SIZE) - mDataSlot);
	}
}



/***********************************************************************
Function : 	Waitfor

Parameters : 	A string to wait for
		A Time out value 

Date : 		January 2001

Desc : 		Returns True if Modem response matches the String argument,
		False otherwise. Time is the number of times the Delay funtion
		will be called from within the waiting loop.
***********************************************************************/

BYTE Waitfor (char *String, BYTE Time) {
BYTE c = 0;
BYTE Offset = 0;

   while (Time-- > 0) {
		Delay (100);					// Wait =~ 150 mSec
		while (ModemBuffNotEmpty()) {			// Wait for characters
			c = ModemGetch ();			// Extract a character from FIFO
			if (c == String [Offset]) {		// Is C a part of the string? 
				Offset++;			// Compare with next character
				if (String [Offset] == 0) {	// is this the end of string?
					return True;		// match = True
				}
		   	}
			else					// c does not belong to String
         	Offset = 0;					// Reset String pointer
		}
	}
	return False;
}





/***********************************************************************
Function : 	ProcModemReceive

Parameters : 	A character received from the SCI

Date : 		November 2000

Desc : 		Stores incoming characters in the Modem Queue
***********************************************************************/
void ProcModemReceive (BYTE c) {
	ModemBuffer [mEmptySlot++] = c;
	if (mEmptySlot > MODEM_BUFFER_SIZE) {
		mEmptySlot = 0;
   }
}


/***********************************************************************
Function : 	ModemGetch

Parameters : 	None 

Date : 		November 2000

Desc : 		Dequeue a previously stored character in the Modem Queue.
		Returns a null character if the Queue is empty  
***********************************************************************/
BYTE ModemGetch (void) {
BYTE c = 0;
    if (mDataSlot != mEmptySlot) {
         c = ModemBuffer [mDataSlot];
         mDataSlot++;
         if (mDataSlot > MODEM_BUFFER_SIZE) mDataSlot = 0;
			return(c);
    }
    else {
       return (BYTE)0x00;
	}
}


/***********************************************************************
Function : 	transmit

Parameters : 	A string to transmit to the Modem

Date : 		November 2000

Desc : 		Any data passed to this function will be sended to the Modem.
		Applications can build complex scripts by calling transmit and
		Waifor functions however, its up to the application to control
		the appropriate flow of data when the Modem is on command mode
		and on-line mode.
***********************************************************************/
void transmit (char *data) {
	Delay (250);
	while (*data) {
		WriteComm (*data++);
	}
}

⌨️ 快捷键说明

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