C8051F040的UART程序1
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// Save Current SFR page
SFRPAGE = CONFIG_PAGE;
// Set SFR page
XBR0 XBR1 XBR2
= 0x04; = 0x00; = 0x40;
//-----------------------------------------------------------------------------
// F04x_UART0_INT.c
//-----------------------------------------------------------------------------
//
// 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;
//
// How To Test:
//
// 1) Download code to a 'F04x device that is connected to a UART transceiver
// 2) Verify jumpers J6 and J9 are populated on the 'F04x TB.
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
// Pinout:
//
// P0.0 digital push-pull UART TX
// P0.1 digital open-drain UART RX
sfr16 RCAP2 sfr16 TMR2
= 0xCA; = 0xCC;
// Timer2 capture/reload // Timer2
//----------------------------------------------------------------------------// Global Constants //-----------------------------------------------------------------------------
// Copyright 2006 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program demonstrates how to configure the C8051F040 to write to and read
if((TX_Ready == 1) && (UART_Buffer_Size != 0) && (Byte == 13))
{
TX_Ready = 0;
// Set the flag to zero
TI0 = 1;
// Set transmit flag to 1
}
}
}
//----------------------------------------------------------------------------// Initialization Subroutines //-----------------------------------------------------------------------------
void OSCILLATOR_Init (void); void PORT_Init (void); void UART0_Init (void);
//----------------------------------------------------------------------------// Global Variables //-----------------------------------------------------------------------------
SFRPAGE = SFRPAGE_SAVE; }
// Restore SFRPAGE
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
// 5) Type up to 64 characters into the Terminal and press Enter. The MCU
// will then print back the characters that were typed
//
//
// Target:
C8051F04x
//----------------------------------------------------------------------------// main() Routine //-----------------------------------------------------------------------------
#include <C8051F040.h> #include <stdio.h>
// SFR declarations
//----------------------------------------------------------------------------// 16-bit SFR Definitions for 'F04x //-----------------------------------------------------------------------------
#define BAUDRATE 115200
// Baud rate of UART in bps
// SYSTEMCLOCK = System clock frequency in Hz
#define SYSTEMCLOCK
22118400L
//----------------------------------------------------------------------------// Function Prototypes //-----------------------------------------------------------------------------
UART0_Init ();
// Initialize UART0
EA = 1;
SFRPAGE = UART0_PAGE;
while (1)
{
// If the complete word has been entered via the terminal followed
// by carriage return
// Set internal oscillator to run
// at its slowest frequency
CLKSEL = 0x00;
// Select the internal osc. as // the SYSTEMCLOCK source
// Initialize external crystal oscillator to use 22.1184 MHz crystal
OSCXCN = 0x67; for (i=0; i < 256; i++);
// Enable external crystal osc. // Wait at least 1ms
while (!(OSCXCN & 0x80));
// Wait for crystal osc to settle
CLKSEL = 0x01; // Select external crystal as SYSTEMCLOCK source
#define UART_BUFFERSIZE 64 unsigned char UART_Buffer[UART_BUFFERSIZE]; unsigned char UART_Buffer_Size = 0; unsigned char UART_Input_First = 0; unsigned char UART_Output_First = 0; unsigned char TX_Ready =1; static char Byte;
//-----------------------------------------------------------------------------
// OSCILLATOR_Init
ห้องสมุดไป่ตู้
//-----------------------------------------------------------------------------
void main (void) {
SFRPAGE = CONFIG_PAGE;
WDTCN = 0xDE; WDTCN = 0xAD;
// Disable watchdog timer
OSCILLATOR_Init ();
// Initialize oscillator
PORT_Init ();
// Initialize crossbar and GPIO
// from the UART interface. The program reads a word using the UART0 interrupts
// and outputs that word to the screen, with all characters in uppercase
// P1.6 digital push-pull LED
//-----------------------------------------------------------------------------
void PORT_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE;
// Tool chain: Keil C51 7.50 / Keil EVAL C51
// Command Line: None
//
// Release 1.0
// -Initial Revision (SM)
// -10 JULY 2007
//
//----------------------------------------------------------------------------// Includes //-----------------------------------------------------------------------------
// 3) Connect serial cable from the transceiver to a PC
// 4) On the PC, open HyperTerminal (or any other terminal program) and connect
// to the COM port at <BAUDRATE> and 8-N-1