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

📄 dos.c

📁 windwos系统下控制DOS系统自动开关机(DOS系统部分)
💻 C
字号:
#include <stdio.h>
#include <string.h>
#include <io.h>
#include <fcntl.h>
#include <dos.h>
#include <conio.h>

#define PORT1				0x3F8 /* Port Address Goes Here */
#define INTVECT				0x0C /* Com Port's IRQ here (Must also change PIC setting) */
#define PORTBUFFERSIZE		1024
#define FILEBUFFERSIZE		512

/* Defines Serial Ports Base Address */
/* COM1 0x3F8 */
/* COM2 0x2F8 */
/* COM3 0x3E8 */
/* COM4 0x2E8 */

int		g_Is24HourMode		= 1;
int		g_IsBCDMode			= 1;

int		g_iBufferIn			= 0;
int		g_iMsg2IsOK			= 0;
char	g_chReadBuf[ PORTBUFFERSIZE ];


/*--- Define Old Receive ISR ---*/
void interrupt (*OldPort1ISR)();

/*--- Port1 Receive ISR ---*/
void interrupt PORT1INT() /* Interrupt Service Routine (ISR) for PORT1 */
{
	int iRecChk = 0;

	do
	{
		iRecChk = inportb( PORT1 + 5 );
		if( iRecChk & 1 )
		{
			g_chReadBuf[ g_iBufferIn ] = inportb( PORT1 );

			if( g_chReadBuf[ g_iBufferIn ] == 'y' ) /*--- Replay Test Side Message 2 ---*/
			{
				g_iMsg2IsOK= 1;					
			}

			if( g_chReadBuf[ 0 ] == 'Y' )  /*--- Replay Test Side Message 1 ---*/
			{
				g_iBufferIn++;					
			}
			
			if( g_iBufferIn == (PORTBUFFERSIZE - 1) )
			{
				g_iBufferIn = 0;		
			}			
			
		}
		
	} while( iRecChk & 1 );
	
	outportb(0x20, 0x20);
}


/*--- Initialize Port1 ---*/
void InitPort()
{
	outportb( PORT1 + 1, 0 ); /* Turn off interrupts - Port1: The LCR Bit 7 must be 0 */
	
	OldPort1ISR = getvect( INTVECT ); /* Save old Interrupt Vector of later recovery */
	
	setvect( INTVECT, PORT1INT ); /* Set Interrupt Vector Entry */
	/* COM1 - 0x0C */
	/* COM2 - 0x0B */
	/* COM3 - 0x0C */
	/* COM4 - 0x0B */
	
	/* PORT 1 - Communication Settings */								 
	outportb(PORT1 + 3, 0x80);	/* SET DLAB ON */
	outportb(PORT1 + 0, 0x0C);	/* Set Baud rate - Divisor Latch Low Byte */
	outportb(PORT1 + 1, 0x00);  /* Set Baud rate - Divisor Latch High Byte */
	/* Default 0x03 = 38,400 BPS */
	/* 0x01 = 115,200 BPS */
	/* 0x02 = 57,600 BPS */
	/* 0x06 = 19,200 BPS */
	/* 0x0C = 9,600 BPS */
	/* 0x18 = 4,800 BPS */
	/* 0x30 = 2,400 BPS */	
	
	outportb(PORT1 + 3, 0x03); /* 8 Bits, No Parity, 1 Stop Bit */
	outportb(PORT1 + 2, 0xC7); /* FIFO Control Register */
	outportb(PORT1 + 4, 0x0B); /* Turn on DTR, RTS, and OUT2 */
	
	outportb(0x21, (inportb(0x21) &0xEF));	/* Set Programmable Interrupt Controller */
	/* COM1 (IRQ4) - 0xEF */
	/* COM2 (IRQ3) - 0xF7 */
	/* COM3 (IRQ4) - 0xEF */
	/* COM4 (IRQ3) - 0xF7 */
	outportb(PORT1 + 1, 0x01); /* Interrupt when data received */
}


/*--- Release Port1 resources that InitPort() created ---*/
void ReleasePort()
{
	outportb(PORT1 + 1, 0); /* Turn off interrupts - Port1 */
	
	outportb(0x21, (inportb(0x21) | 0x10)); /* MASK IRQ using PIC */
	/* COM1 (IRQ4) - 0x10 */
	/* COM2 (IRQ3) - 0x08 */
	/* COM3 (IRQ4) - 0x10 */
	/* COM4 (IRQ3) - 0x08 */
	
	setvect(INTVECT, OldPort1ISR); /* Restore old interrupt vector */
}


/*--- Waiting until the RTC time register value could be read ---*/
void ChkReady()
{
	unsigned char ucRead = 0;

	outportb( 0x70, 0x0A );
	while(1)
	{
		ucRead = inportb( 0x71 );
		if( ucRead & 0x80 )
		{
			break;
		}
	}
	
	while(1)
	{
		ucRead = inportb( 0x71 );
		if( !(ucRead & 0x80) )
		{
			break;
		}
	}

}


unsigned char BinaryToBCD( unsigned char ucBinaryData )
{
	ucBinaryData = ((ucBinaryData / 10) << 4) + (ucBinaryData % 10);
	return ucBinaryData;
	
}

unsigned char BCDToBinary( unsigned char ucBCDData )
{
	ucBCDData = (ucBCDData >> 4) * 10 + (ucBCDData & 0x0F);
	return ucBCDData;
	
}


void GetModes()
{
	/*---
	Control register ( 0x0B ):	
	Bit7: Disable updates for clock setting (0: auto update, 1: user will set new date)
	Bit6: Periodic interrupt enable (0: disable, 1: enable)
	Bit5: Alarm interrupt enable (0: disable, 1: enable)
	Bit4: Update-finished interrupt enable (0: disable, 1: enable)
	Bit3: Enable square-wave output (0: disable, 1: enable)
	Bit2: All time/date values are BCD if clear (0: BCD, 1: Binary)
	Bit1: 24 hour mode, else hours bit 7 means PM (0: AM/PM, 1: 24 hour)
	Bit0: Auto switch DST - works f. USA only (always as "0")
	---*/

	unsigned char ucData = 0;
	
	ChkReady();

	outportb( 0x70, 0x0B );
	ucData = inportb( 0x71 );

	if( 0 == (ucData & 0x02) )
	{
		g_Is24HourMode = 0;
	}

	if( 1 ==  (ucData & 0x04) )
	{
		g_IsBCDMode = 0;
	}
	
}


void GetRTCDate( unsigned char* pucGetHour, unsigned char* pucGetMinute, unsigned char* pucGetSecond )
{
	ChkReady();

	outportb( 0x70, 0x00 );
	*pucGetSecond = inportb( 0x71 );
	
	outportb( 0x70, 0x02 );
	*pucGetMinute = inportb( 0x71 );
	
	outportb( 0x70, 0x04 );
	*pucGetHour = inportb( 0x71 );
}


void GetCurData( unsigned char* pucGetBuf, 
	unsigned char ucCurHour, 
	unsigned char ucCurMinute, 
	unsigned char ucCurSecond )
{
	unsigned char ucLasHour			= 0;
	unsigned char ucLasMinute		= 0;
	unsigned char ucLasSecond		= 0;	
	
	if( g_Is24HourMode ) /*--- if( m_IsDate24HourMode ) ---> if begin ---*/
	{
		if( g_IsBCDMode )
		{
			ucLasSecond = BCDToBinary( ucCurSecond );
			ucLasMinute = BCDToBinary( ucCurMinute );
			ucLasHour = BCDToBinary( ucCurHour );			
		}		
		else
		{
			ucLasSecond = ucCurSecond;
			ucLasMinute = ucCurMinute;
			ucLasHour= ucCurHour;				
		}
		
	} /*--- if( m_IsDate24HourMode ) ---> if end ---*/
	
	else /*--- if( m_IsDate24HourMode )	---> else begin ---*/
	{
		if( g_IsBCDMode )
		{
			ucLasSecond = BCDToBinary( ucCurSecond );
			ucLasMinute = BCDToBinary( ucCurMinute );
			if( ucCurHour & 0x80 )
			{
				ucCurHour &= 0x7F;
				ucLasHour = BCDToBinary( ucCurHour );
				ucLasHour += 12;
			}
			else
			{
				ucLasHour = BCDToBinary( ucCurHour );
			}
			
		}		
		else
		{
			ucLasSecond = ucCurSecond;
			ucLasMinute = ucCurMinute;
			if( ucCurHour & 0x80 )
			{
				ucCurHour &= 0x7F;
				ucLasHour = ucCurHour + 12;		
			}
			else
			{
				ucLasHour = ucCurHour;
			}				
		}
		
	} /*--- if( m_IsDate24HourMode ) ---> else begin ---*/
	
	sprintf( pucGetBuf, 
		"Current Time:  %d Hour:%d Minute:%d Second.\n",		
		ucLasHour,
		ucLasMinute,
		ucLasSecond );
}


void GetStartDate( unsigned char* pucGetBuf, 
	unsigned char ucCurHour, 
	unsigned char ucCurMinute, 
	unsigned char ucCurSecond )
{
	unsigned char ucLasHour			= 0;
	unsigned char ucLasMinute		= 0;
	unsigned char ucLasSecond		= 0;

	struct date   startDate; 

	getdate( & startDate );
	
	
	if( g_Is24HourMode ) /*--- if( m_IsDate24HourMode ) ---> if begin ---*/
	{
		if( g_IsBCDMode )
		{
			ucLasSecond = BCDToBinary( ucCurSecond );
			ucLasMinute = BCDToBinary( ucCurMinute );
			ucLasHour = BCDToBinary( ucCurHour );			
		}		
		else
		{
			ucLasSecond = ucCurSecond;
			ucLasMinute = ucCurMinute;
			ucLasHour= ucCurHour;				
		}

	} /*--- if( m_IsDate24HourMode ) ---> if end ---*/

	else /*--- if( m_IsDate24HourMode )	---> else begin ---*/
	{
		if( g_IsBCDMode )
		{
			ucLasSecond = BCDToBinary( ucCurSecond );
			ucLasMinute = BCDToBinary( ucCurMinute );
			if( ucCurHour & 0x80 )
			{
				ucCurHour &= 0x7F;
				ucLasHour = BCDToBinary( ucCurHour );
				ucLasHour += 12;
			}
			else
			{
				ucLasHour = BCDToBinary( ucCurHour );
			}
						
		}		
		else
		{
			ucLasSecond = ucCurSecond;
			ucLasMinute = ucCurMinute;
			if( ucCurHour & 0x80 )
			{
				ucCurHour &= 0x7F;
				ucLasHour = ucCurHour + 12;		
			}
			else
			{
				ucLasHour = ucCurHour;
			}				
		}

	} /*--- if( m_IsDate24HourMode ) ---> else begin ---*/

	sprintf( pucGetBuf, 
		"Start Time:  %d Year-%d Month-%d Day, %d Hour:%d Minute:%d Second.\n",
		startDate.da_year,
		startDate.da_mon,
		startDate.da_day,
		ucLasHour,
		ucLasMinute,
		ucLasSecond );
	
}


void TranRTCDate( unsigned char* pucLasHour,
	unsigned char* pucLasMinute, 
	unsigned char* pucLasSecond,
	unsigned char ucCurHour,
	unsigned char ucCurMinute,
	unsigned char ucCurSecond,
	unsigned char ucInputHour,
	unsigned char ucInputMinute,
	unsigned char ucInputSecond )
{
	if( g_Is24HourMode ) /*--- if( m_IsDate24HourMode ) ---> if begin ---*/
	{
		if( g_IsBCDMode )
		{
			ucCurSecond = BCDToBinary( ucCurSecond );
			*pucLasSecond = ucCurSecond + ucInputSecond;
			if( *pucLasSecond >= 60 )
			{
				*pucLasSecond -= 60;
				ucInputMinute += 1;				
			}
			*pucLasSecond = BinaryToBCD( *pucLasSecond );
			
			ucCurMinute = BCDToBinary( ucCurMinute );
			*pucLasMinute = ucCurMinute + ucInputMinute;
			if( *pucLasMinute >= 60 )
			{
				*pucLasMinute -= 60;
				ucInputHour += 1;				
			}
			*pucLasMinute = BinaryToBCD( *pucLasMinute );

			ucCurHour = BCDToBinary( ucCurHour );
			*pucLasHour = ucCurHour + ucInputHour;
			if( *pucLasHour >= 24 )
			{
				*pucLasHour -= 24;						
			}
			*pucLasHour = BinaryToBCD( *pucLasHour );				
		}
		
		else
		{
			*pucLasSecond = ucCurSecond + ucInputSecond;
			if( *pucLasSecond >= 60 )
			{
				*pucLasSecond -= 60;
				ucInputMinute += 1;				
			}			
			
			*pucLasMinute = ucCurMinute + ucInputMinute;
			if( *pucLasMinute >= 60 )
			{
				*pucLasMinute -= 60;
				ucInputHour += 1;				
			}		
			
			*pucLasHour = ucCurHour + ucInputHour;
			if( *pucLasHour >= 24 )
			{
				*pucLasHour -= 24;						
			}			
		}

	} /*--- if( m_IsDate24HourMode ) ---> if end ---*/

	else /*--- if( m_IsDate24HourMode )	---> else begin ---*/
	{
		if( g_IsBCDMode )
		{
			/*--- Translate Second ---*/
			ucCurSecond = BCDToBinary( ucCurSecond );
			*pucLasSecond = ucCurSecond + ucInputSecond;
			if( *pucLasSecond >= 60 )
			{
				*pucLasSecond -= 60;
				ucInputMinute += 1;				
			}
			*pucLasSecond = BinaryToBCD( *pucLasSecond );
		
			/*--- Translate Minute ---*/
			ucCurMinute = BCDToBinary( ucCurMinute );
			*pucLasMinute = ucCurMinute + ucInputMinute;
			if( *pucLasMinute >= 60 )
			{
				*pucLasMinute -= 60;
				ucInputHour += 1;				
			}
			*pucLasMinute = BinaryToBCD( *pucLasMinute );

			/*--- Translate Hour ---*/
			if( ucCurHour & 0x80 )
			{
				ucCurHour &= 0x7F;
				ucCurHour = BCDToBinary( ucCurHour );
				ucCurHour += 12;				
			}
			else
			{
				ucCurHour = BCDToBinary( ucCurHour );
			}

			*pucLasHour = ucCurHour + ucInputHour;
			if( *pucLasHour >= 24 )
			{
				*pucLasHour -= 24;
				*pucLasHour = BinaryToBCD( *pucLasHour );
			}
			else if( *pucLasHour >= 12 )
			{
				*pucLasHour -= 12;
				*pucLasHour = BinaryToBCD( *pucLasHour );
				*pucLasHour |= 0x80;
			}
			else
			{
				*pucLasHour = 0x7F & BinaryToBCD( *pucLasHour );
			}			
		}

		else
		{
			/*--- Translate Second ---*/		
			*pucLasSecond = ucCurSecond + ucInputSecond;
			if( *pucLasSecond >= 60 )
			{
				*pucLasSecond -= 60;
				ucInputMinute += 1;				
			}
			
			/*--- Translate Minute ---*/	
			*pucLasMinute = ucCurMinute + ucInputMinute;
			if( *pucLasMinute >= 60 )
			{
				*pucLasMinute -= 60;
				ucInputHour += 1;				
			}		
			
			/*--- Translate Hour ---*/
			if( ucCurHour & 0x80 )
			{
				ucCurHour &= 0x7F;
				ucCurHour += 12;				
			}
			
			*pucLasHour = ucCurHour + ucInputHour;
			if( *pucLasHour >= 24 )
			{
				*pucLasHour -= 24;	
			}
			else if( *pucLasHour >= 12 )
			{
				*pucLasHour -= 12;	
				*pucLasHour |= 0x80;
			}
			else
			{
				*pucLasHour &= 0x7F;
			}
		}

	} /*--- if( m_IsDate24HourMode ) ---> else begin ---*/
}


void main()
{	
	unsigned char	ucInputHour		= 0;
	unsigned char	ucInputMinute	= 0;
	unsigned char	ucInputSecond	= 0;
	unsigned char	ucInputDelay	= 0;

	unsigned char	ucCurHour		= 0;
	unsigned char	ucCurMinute		= 0;
	unsigned char	ucCurSecond		= 0;

	unsigned char	ucLasHour		= 0;
	unsigned char	ucLasMinute		= 0;
	unsigned char	ucLasSecond		= 0;

	unsigned char	ucRead			= 0;
	int				iRead			= 0;
	int				iBaseAdr		= 0;

	int				fHandle			= -1;
	int				iISFirst		= 1;

	char			chFileBuf[ FILEBUFFERSIZE ];
	char			chCurBuf[ FILEBUFFERSIZE ];
	void*			pVoidPoint		= &chFileBuf[ 0 ];
	int				iRWLen			= 0;
	char			cOutChar;

	/*--- Step1: Show Time ---*/

	GetModes();

	fHandle = open( "Testside.dat", O_RDONLY );
	if( fHandle == -1 )
	{
		fHandle = open( "Testside.dat", O_RDWR | O_CREAT );
		if( fHandle == -1 )
		{
			printf( "Create file fail.\n" );
			return;
		}
	}
	
	lseek( fHandle, 0, SEEK_SET );
	iRWLen = read( fHandle, pVoidPoint, FILEBUFFERSIZE );	
	if( -1 == iRWLen )
	{
		close( fHandle );
		printf( "Read file fail.\n" );
		return;
	}	
	if( iRWLen == 0 )
	{
		iISFirst = 1;
	}
	else
	{
		iISFirst = 0;
	}

	GetRTCDate( &ucCurHour, &ucCurMinute, &ucCurSecond );		

	lseek( fHandle, 0, SEEK_SET );

	if( iISFirst == 1 )
	{
		GetStartDate( chFileBuf, ucCurHour, ucCurMinute, ucCurSecond );
		iRWLen = write( fHandle, pVoidPoint, FILEBUFFERSIZE );
		if( iRWLen ==-1 )
		{
			close( fHandle );
			printf( "Write file fail.\n" );
			return;
		}
	}
	else
	{
		iRWLen = read( fHandle, pVoidPoint, FILEBUFFERSIZE );		
		if( -1 == iRWLen )
		{
			close( fHandle );
			printf( "Read file fail.\n" );
			return;
		}
	}

	close( fHandle );

	printf( "%s", chFileBuf );

	GetCurData( chCurBuf, ucCurHour, ucCurMinute, ucCurSecond );
	printf( "%s", chCurBuf );
	

	/*--- Step2: Set COM1 ---*/

	InitPort();

	printf( "Send Msg1 to monitor side to get settings information.\n" );
	cOutChar = 'A';
	outportb( PORT1, cOutChar );	
	while( g_iBufferIn < 2 )
	{	
	}
	ucInputHour		= g_chReadBuf[ 1 ];
	ucInputMinute	= g_chReadBuf[ 2 ];
	ucInputSecond	= g_chReadBuf[ 3 ] + g_chReadBuf[ 4 ];
	ucInputDelay	= g_chReadBuf[ 4 ];
	printf( "Receive Msg1-Respond from monitor side.\n" );

	printf( "Send Msg2 to monitor side that test side have gotten settings information.\n" );
	cOutChar = 'B';
	outportb( PORT1, cOutChar );
	while( !g_iMsg2IsOK )
	{
	}
	printf( "Receive Msg2-Respond from monitor side.\n" );

	printf( "Send Msg3 to monitor side that Msg2-Respond have gotten.\n\n" );
	cOutChar = 'C';
	outportb( PORT1, cOutChar );

	TranRTCDate( &ucLasHour,
		&ucLasMinute, 
		&ucLasSecond,
		ucCurHour,
		ucCurMinute,
		ucCurSecond,
		ucInputHour,
		ucInputMinute,
		ucInputSecond );

	/*--- Step1 ---*/
	ChkReady();

	/*--- Step2 ---*/
	outportb( 0x70, 0x0B );
	ucRead = inportb( 0x71 );
	ucRead |= 0x80; /*--- 1000 0000 ---*/
	outportb( 0x71, ucRead );

	outportb( 0x70, 0x05 );
	outportb( 0x71, ucLasHour );

	outportb( 0x70, 0x03 );
	outportb( 0x71, ucLasMinute );

	outportb( 0x70, 0x01 );
	outportb( 0x71, ucLasSecond );

	/*--- Step3 ---*/	
	iBaseAdr = 0x400;
	iBaseAdr += 2;

	iRead = inport( iBaseAdr );
	iRead |= 0x500; /*--- Bit10: RTC_EN ---*/
	outport( iBaseAdr, iRead );

	/*--- Step4 ---*/
	outportb( 0x70, 0x0B );
	ucRead = inportb( 0x71 );	
	ucRead = ((ucRead | 0x20) & 0x7F);
	outportb( 0x71, ucRead );


	if( ucInputDelay != 0 )
	{
		printf( "Delay  %d  seconds to power off the system ...\n", ucInputDelay );
		sleep( ucInputDelay );
	}
	
	/*--- Step5 ---*/
	iBaseAdr += 4;
	outport( iBaseAdr, 0x0000 );
	iBaseAdr -= 2;
	outport( iBaseAdr, 0x3C00 );
	

	ReleasePort();

}

⌨️ 快捷键说明

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