📄 f500_uart0_stdio.c
字号:
//-----------------------------------------------------------------------------
// F500_UART0_STDIO.c
//-----------------------------------------------------------------------------
// Copyright 2008 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program demonstrates how to configure the C8051F500 to use routines
// in STDIO.h to write to and read from the UART interface. The program
// reads a character using the STDIO routine getkey(), outputs that character
// to the screen, and then outputs the ASCII hex value of that character.
//
//
// How To Test:
//
// 1) Ensure that jumpers are placed on J17 of the C8051F500 target board
// that connect P0.4 to the TX signal, and P0.5 to the RX field.
// 2) Connect USB cable from the development board to a PC
// 3) Specify the target baudrate in the constant <BAUDRATE>.
// 4) On the PC, open HyperTerminal (or any other terminal program) and connect
// to the USB port (virtual com port) at <BAUDRATE>, 8 data bits, no parity,
// 1 stop bit and no flow control.
// 5) Download and execute code to an 'F500 target board.
//
//
// Target: C8051F500 (Side A of a C8051F500-TB)
// Tool chain: Keil C51 8.0 / Keil EVAL C51
// Command Line: None
//
// Release 1.0 / 10 MAR 2008 (GP)
// -Initial Revision
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <compiler_defs.h>
#include <C8051F500_defs.h> // SFR declarations
#include <stdio.h>
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 24000000 // SYSCLK frequency in Hz
#define BAUDRATE 115200 // Baud rate of UART in bps
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init (void);
void UART0_Init (void);
void PORT_Init (void);
#ifdef SDCC
U8 getkey (void);
void putchar (char output);
#endif
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
U8 inputcharacter; // Used to store character from UART
SFRPAGE = ACTIVE_PAGE; // Change for PCA0MD and SBUF0
PCA0MD &= ~0x40; // Disable the watchdog timer
PORT_Init (); // Initialize Port I/O
SYSCLK_Init (); // Initialize Oscillator
UART0_Init ();
while (1)
{
printf ("\nEnter character: ");
inputcharacter = getkey ();
printf ("\nCharacter entered : %c",inputcharacter);
printf ("\n Value in Hex: %bx",inputcharacter);
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure the Crossbar and GPIO ports.
//
// P0.4 digital push-pull UART TX
// P0.5 digital open-drain UART RX
//
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
P0MDOUT |= 0x10; // Enable UTX as push-pull output
XBR0 = 0x01; // Enable UART on P0.4(TX) and P0.5(RX)
XBR2 = 0x40; // Enable crossbar and weak pull-ups
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This routine initializes the system clock to use the internal oscillator
// at its maximum frequency.
// Also enables the Missing Clock Detector.
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
OSCICN |= 0x87; // Configure internal oscillator for
// its maximum frequency
RSTSRC = 0x04; // Enable missing clock detector
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//-----------------------------------------------------------------------------
void UART0_Init (void)
{
U8 SFRPAGE_save = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
SCON0 = 0x10; // SCON0: 8-bit variable bit rate
// clear RI0 and TI0 bits
// Baud Rate = [BRG Clock / (65536 - (SBRLH0:SBRLL0))] x 1/2 x 1/Prescaler
#if ((SYSCLK / BAUDRATE / 2 / 0xFFFF) < 1)
SBRL0 = -(SYSCLK / BAUDRATE / 2);
SBCON0 |= 0x03; // Set prescaler to 1
#elif ((SYSCLK / BAUDRATE / 2 / 0xFFFF) < 4)
SBRL0 = -(SYSCLK / BAUDRATE / 2 / 4);
SBCON0 &= ~0x03;
SBCON0 |= 0x01; // Set prescaler to 4
#elif ((SYSCLK / BAUDRATE / 2 / 0xFFFF) < 12)
SBRL0 = -(SYSCLK / BAUDRATE / 2 / 12);
SBCON0 &= ~0x03; // Set prescaler to 12
#else
SBRL0 = -(SYSCLK / BAUDRATE / 2 / 48);
SBCON0 &= ~0x03;
SBCON0 |= 0x02; // Set prescaler to 48
#endif
SBCON0 |= 0x40; // Enable baud rate generator
TI0 = 1; // Indicate TX0 ready
SFRPAGE = SFRPAGE_save;
}
#ifdef SDCC
// SDCC does not include a definition for putchar(), which is used in printf()
// and so it is defined here. The prototype does not need to be explicitly
// defined because it is provided in stdio.h
//-----------------------------------------------------------------------------
// putchar
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : character to send to UART
//
// This function outputs a character to the UART.
//-----------------------------------------------------------------------------
void putchar (char output)
{
if (output == '\n')
{
while (!TI0);
TI0 = 0;
SBUF0 = 0x0D;
}
while (!TI0);
TI0 = 0;
SBUF0 = output;
}
// SDCC does not include a definition for getkey(), and so it is defined here
//-----------------------------------------------------------------------------
// getkey
//-----------------------------------------------------------------------------
//
// Return Value : character received from UART
// Parameters : None
//
// This function returns a character received from the UART
// Assumes SFR page has been set correctly by the application
//-----------------------------------------------------------------------------
U8 getkey (void)
{
U8 received;
while (!RI0); // Wait for character to be received
received = SBUF0; // Read it
RI0 = 0; // Clear received data indicator
return received;
}
#endif
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -