在IAR 的EWARM中提供了printf和scanf,但是需要自己提供hardware low level 层的getchar和putchar,才能实现,本例子在EWARM 5.10和LPC 2368调试通过,其code是根据EWARM 5.10中自带的例子程序更改得来的LPC23xx/KEIL-MCB2300/simple/UART,
最主要的是实现
int MyLowLevelPutchar(int x)
{
*ptr=(BYTE)x;
UARTSend( 0, ptr, 1 );
return 1;
}
和
int MyLowLevelGetchar()
{
while(ReceivedByte==-1); //not received
temp=ReceivedByte;
ReceivedByte=-1;
return temp;
}
网上没找到相关资料,自己想了很长时间对将scanf的getchar实现想出来(putchar 较简单)
下面是几个重要的code
/*****************************************************************************
uarttest.c
/*****************************************************************************
#include "LPC230x.h" /* LPC21xx definitions */
#include "type.h"
#include "irq.h"
#include "target.h"
#include "uart.h"
#include <stdio.h>
extern volatile DWORD UART0Count;
extern volatile BYTE UART0Buffer[BUFSIZE];
extern volatile DWORD UART1Count;
extern volatile BYTE UART1Buffer[BUFSIZE];
/*****************************************************************************
** Main Function main()
*****************************************************************************/
int main (void)
{
TargetResetInit();
UARTInit(0, 115200); /* baud rate setting */
printf("Hello,World! This demo is modified by hammergo!/n");
printf("Welcome to arm programming world!/n");
int test=0;
printf("Please input test value:");
scanf("%d",&test);
if(test>0)
{
printf("test>0");
}else
{
printf("test<=0");
}
while (1)
{ /* Loop forever */
}
return 0;
}
/*****************************************************************************
** End Of File
*****************************************************************************/
/*****************************************************************************
* uart.c: UART API file for NXP LPC23xx/24xx Family Microprocessors
*
* Copyright(C) 2006, NXP Semiconductor
* All rights reserved.
*
* History
* 2006.07.12 ver 1.00 Prelimnary version, first Release
*
******************************************************************************/
#include "LPC230x.h" /* LPC23xx/24xx definitions */
#include "type.h"
#include "target.h"
#include "irq.h"
#include "uart.h"
#include <intrinsics.h>
volatile DWORD UART0Status;
volatile BYTE UART0TxEmpty = 1;
volatile BYTE UART0Buffer[BUFSIZE];
volatile DWORD UART0Count = 0;
extern volatile int ReceivedByte;
/*****************************************************************************
** Function name: UART0Handler
**
** Descriptions: UART0 interrupt handler
**
** parameters: None
** Returned value: None
**
*****************************************************************************/
__irq __nested __arm void UART0Handler (void)
{
BYTE IIRValue, LSRValue;
volatile BYTE Dummy;
__enable_interrupt(); /* handles nested interrupt */
IIRValue = U0IIR;
IIRValue >>= 1; /* skip pending bit in IIR */
IIRValue &= 0x07; /* check bit 1~3, interrupt identification */
if ( IIRValue == IIR_RLS ) /* Receive Line Status */
{
LSRValue = U0LSR;
/* Receive Line Status */
if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
{
/* There are errors or break interrupt */
/* Read LSR will clear the interrupt