LPC 2368 Uart printf scanf 实现

本文档介绍了如何在IAR EWARM环境下为LPC2368微控制器实现printf和scanf功能。关键在于自定义的MyLowLevelPutchar和MyLowLevelGetchar函数,前者用于发送字符,后者用于接收字符。通过这两个函数,可以在UART接口上进行输入输出操作。示例代码包括了初始化UART、输入输出测试以及中断处理函数的详细实现。
摘要由CSDN通过智能技术生成
 

在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值