#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,