//-----------------------------------------------------------------------------
// F500_UART0_Interrupt.c
//-----------------------------------------------------------------------------
// Copyright 2008 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This program demonstrates how to configure the C8051F500 to write to and
// read from the UART interface. The program reads a word using the UART
// interrupts and outputs that word to the screen, with all characters in
// uppercase.
//
// How To Test:
//
// 1) Ensure that jumpers are placed on J17 of the C8051F500 target board
// that connect the P0.4 pin to the TX signal, and P0.5 to the RX signal
// 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 on an 'F500 target board
// 6) Type up to 64 characters into the Terminal and press Enter. The MCU
// will then print back the characters that were typed
//
// 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);
INTERRUPT_PROTO (UART0_ISR, INTERRUPT_UART0);
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
#define UART_BUFFERSIZE 64
U8 UART_Buffer[UART_BUFFERSIZE];
U8 UART_Buffer_Size = 0;
U8 UART_Input_First = 0;
U8 UART_Output_First = 0;
U8 TX_Ready = 1;
static U8 Byte;
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
SFRPAGE = ACTIVE_PAGE; // Change for PCA0MD
PCA0MD &= ~0x40; // Disable watchdog timer
PORT_Init (); // Initialize Port I/O
SYSCLK_Init (); // Initialize Oscillator
UART0_Init (); // Initialize UART
EA = 1; // Enable global interrutpts
while (1)
{
// If the complete word has been entered via the terminal followed by
// carriage return
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
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// 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
ES0 = 1; // Enable UART0 interrupts
TX_Ready = 1; // Flag showing that UART can transmit
SFRPAGE = SFRPAGE_save;
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// UART0_Interrupt
//-----------------------------------------------------------------------------
//
// This routine is invoked whenever a character is entered or displayed on the
// Hyperterminal.
//
//-----------------------------------------------------------------------------
INTERRUPT(UART0_ISR, INTERRUPT_UART0)
{
if (RI0 == 1)
{
if( UART_Buffer_Size == 0) // If new word is entered
{
UART_Input_First = 0;
}
RI0 = 0; // Clear interrupt flag
Byte = SBUF0; // Read a character from UART
if (UART_Buffer_Size < UART_BUFFERSIZE)
{
UA
UART.rar_C8051F UART
版权申诉
55 浏览量
2022-09-24
04:00:41
上传
评论
收藏 5KB RAR 举报
JaniceLu
- 粉丝: 79
- 资源: 1万+
最新资源
- 基于matlab实现 powell算法 用matlab实现,使用方法内附详细说明.rar
- 基于matlab的手写字体识别程序,并对结果进行保存.rar
- 基于c语言指纹识别demo代码 包括了指纹图像方向图计算、频率计算、gabor滤波器增强,细化,特征点提取,特征点匹配.rar
- 基于c++NSGA-2思想的多目标优化程序,采用进化算法处理多目标实值优化问题.rar
- Linux系统中常用权限管理命令
- Coello Coello等人提出了MOPSO 该程序基于matlab实现针对测试函数matlab程序
- Linux系统中常用权限管理命令
- AIR-AP1815-K9-ME-8-5-182-0.tar For Cisco AP1815
- 实验七.zip
- ESP8266刷固件软件flash-download-tools-v3.6.5,AT固件,机智云固件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈