//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define BAUDRATE 19200 // Baud rate of UART in bps
// SYSCLK = System clock frequency in Hz
#define SYSCLK 20000000L
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
unsigned char Uart; // Global variable -- when '0', UART0
// is used for stdio; when '1', UART1
// is used for stdio
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function initializes the system clock to use an external 22.1184MHz
// crystal.
//
//-----------------------------------------------------------------------------
void OSCILLATOR_Init (void)
{
int i; // Software timer
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // Set SFR page
OSCICN = 0x80; // Set internal oscillator to run
// at its slowest frequency
CLKSEL = 0x00; // Select the internal osc. as
// the SYSCLK source
// Initialize external crystal oscillator to use 22.1184 MHz crystal
OSCXCN = 0x67; // Enable external crystal osc.
for (i=0; i < 256; i++); // Wait at least 1ms
while (!(OSCXCN & 0x80)); // Wait for crystal osc to settle
//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// putchar
//-----------------------------------------------------------------------------
//
/* Copyright KEIL ELEKTRONIK GmbH 1990 - 2002 */
/*
* putchar (basic version): expands '\n' into CR LF
*/
// Modified by BW
//
// This routine overloads the standard putchar() library function to support
// either UART0 or UART1, depending on the state of the global variable
// <Uart>.
//
char putchar (char c)
{
char SFRPAGE_SAVE = SFRPAGE;
if (Uart == 0)
{
SFRPAGE = UART0_PAGE;
}
else if (Uart == 1)
{
SFRPAGE = UART1_PAGE;
}
if (c == '\n') // If carriage return
{
while (!TI0);
TI0 = 0;
SBUF0 = 0x0d; // Output CR
}
while (!TI0); // Wait for transmit complete
TI0 = 0;
SBUF0 = c; // Send character
SFRPAGE = SFRPAGE_SAVE;
return c;
}
//-----------------------------------------------------------------------------
// _getkey
//-----------------------------------------------------------------------------
//
/* Copyright KEIL ELEKTRONIK GmbH 1990 - 2002 */
// Modified by BW
//
// This routine overloads the standard _getkey() library function to support
// either UART0 or UART1, depending on the state of the global variable
// <Uart>.
//
char _getkey ()
{
char c;
char SFRPAGE_SAVE = SFRPAGE;
if (Uart == 0)
{
SFRPAGE = UART0_PAGE;
}
else if (Uart == 1)
{
SFRPAGE = UART1_PAGE;
}
while (!RI0); // Wait for byte to be received
c = SBUF0; // Read the byte