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

📄 f34x_uart_multiuart.c

📁 芯科原厂所有c8051fxx程序的例子。
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// F34x_UART_MultiUART.c
//-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program configures UART0 and UART1 to operate in polled mode, suitable
// for use with the stdio.h functions printf() and scanf().
//
// Test code is included for printf() and getchar(), and shows an example
// of how the putchar() and _getkey() library routines can be overridden to
// allow for multiple UARTs to be selected by the functions (in this example, 
// the global variable UART is used to select between UART0 and UART1).
//
// The example sets system clock to maximum frequency of 48 MHz.
//
// The system clock frequency is stored in a global constant SYSCLK.  The
// target UART0 baud rate is stored in a global constant BAUDRATE0, and the
// target UART1 baud rate is stored in a global constant BAUDRATE1.
//
//
// How To Test:
//
// 1) Download code to a 'F34x device that is connected to a UART transceiver
// 2) Connect serial cable from the transceiver to a PC
// 3) On the PC, open HyperTerminal (or any other terminal program) and connect
//    to the COM port at <BAUDRATE0>/<BAUDRATE1> and 8-N-1
// 4) To test UART 0, place jumpers connecting P0.4 to TX and P0.5 to RX on 
//    C8051F340 Target Board header J12.  To test UART 1, run wires from
//    P0.0 on J2 to the TX pin of J12, and P0.1 on J2 to the RX pin of J12.
//
//
// FID:          34X000081  
// Target:       C8051F34x
// Tool chain:   KEIL C51 7.20 / KEIL EVAL C51
//               Silicon Laboratories IDE version 2.72
//
// Release 1.0
//    -Initial Revision (PD)
//    -17 JUL 2006
//    -Initial Release
//
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <C8051F340.h>                 // SFR declarations
#include <stdio.h>

//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F34x
//-----------------------------------------------------------------------------

sfr16 SBRL1 = 0xB4;

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define SYSCLK       48000000          // SYSCLK frequency in Hz
#define BAUDRATE0     115200           // Baud rate of UART0 in bps
#define BAUDRATE1     115200           // Baud rate of UART1 in bps

sbit LED = P2^2;                       // LED = 1 means ON

//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void SYSTEMCLOCK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
void UART1_Init (void);
void Delay (void);

//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------

bit UART = 0;                          // '0 is UART0; '1' is UART1


//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main (void) {
   char input_char;

   PCA0MD &= ~0x40;                    // Disable Watchdog timer

   SYSTEMCLOCK_Init ();                // initialize oscillator
   PORT_Init ();                       // initialize crossbar and GPIO
   UART0_Init ();                      // initialize UART0
   UART1_Init ();                      // initialize UART1

   // transmit example UART0
   UART = 0;   // select UART0
   printf ("Hello, from UART0!\n");


   // transmit example UART1
   UART = 1;                           // select UART1
   printf ("Hello, from UART1!\n");


   // receive example: a '1' turns LED on; a '0' turns LED off.
   // select which UART to receive on by changing the next line of code.

   UART = 1;                           // select UART: 0 = UART0, 1 = UART1

   while (1) {
      input_char = getchar();
      printf (" '%c', 0x%02x\n", (unsigned char) input_char, 
                                 (unsigned) input_char);

      switch (input_char) {
         case '0':
            LED = 0;
            break;
         case '1':
            LED = 1;
            break;
         default:
            break;
      }

   }

}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// SYSTEMCLOCK_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None

// This routine initializes the system clock to use the internal system clock
// routed through the clock multiplier as its clock source.
//
//-----------------------------------------------------------------------------

void SYSTEMCLOCK_Init (void)
{
   OSCICN |= 0x03;                     // Configure internal oscillator for
                                       // its maximum frequency and enable
                                       // missing clock detector

   CLKMUL  = 0x00;                     // Select internal oscillator as
                                       // input to clock multiplier

   CLKMUL |= 0x80;                     // Enable clock multiplier
   Delay();                            // Delay for clock multiplier to begin
   CLKMUL |= 0xC0;                     // Initialize the clock multiplier
   Delay();                            // Delay for clock multiplier to begin

   while(!(CLKMUL & 0x20));            // Wait for multiplier to lock
   CLKSEL  = 0x03;                     // Select system clock
}

//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   : None

// Configure the Crossbar and GPIO ports
//
// P0.0     TX1 (UART 1)
// P0.1     RX1 (UART 1)
// P0.2     
// P0.3
// P0.4     TX0 (UART 0)
// P0.5     RX0 (UART 0)
// P0.6
// P0.7

// P1       not used in this example

// P2.0
// P2.1
// P2.2     LED
// P2.3    
// P2.4
// P2.5
// P2.6
// P2.7
//-----------------------------------------------------------------------------

void PORT_Init (void)
{  
   XBR0 = 0x01;                        // route UART 0 to crossbar
   XBR2 = 0x01;                        // route UART 1 to crossbar
   XBR1 = 0x40;                        // enable crossbar
   P0MDOUT |= 0x11;                    // set P0.4 to push-pull output

⌨️ 快捷键说明

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