LPC 2368 Uart IAR EWARM printf scanf 实现2

在前面的实现里是使用中断来收发数据的,感觉代码较长,而且不好理解,效率也比较低,根据printf,scanf来阻塞的特点,改用查看标志位来收发数据,简洁,速度快,可以供大家借鉴。

下面是我写的关健代码。

/*****************************************************************************
*   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 "uart.h"

#include <intrinsics.h>

 

/*****************************************************************************
** Function name:   UART0Handler
**
** Descriptions:    UART0 interrupt handler
**
** parameters:      None
** Returned value:    None
**
*****************************************************************************/

/*****************************************************************************
** Function name:   UARTInit
**
** Descriptions:    Initialize UART0 port, setup pin select,
**            clock, parity, stop bits, FIFO, etc.
**
** parameters:      portNum(0 or 1) and UART baudrate
** Returned value:    true or false, return false only if the
**            interrupt handler can't be installed to the
**            VIC table
**
*****************************************************************************/
DWORD UART0Init(  DWORD baudrate )
{
   DWORD Fdiv;
 
 
    PINSEL0 = 0x00000050;       /* RxD0 and TxD0 */
   
    U0LCR = 0x83;   /* 8 bits, no Parity, 1 Stop bit */
    Fdiv = ( Fpclk / 16 ) / baudrate ;  /*baud rate */
    U0DLM = Fdiv / 256;            
    U0DLL = Fdiv % 256;
    U0LCR = 0x03;   /* DLAB = 0 */
    U0FCR = 0x07;   /* Enable and reset TX and RX FIFO. */
   

   
    U0IER = 0; /* disable all UART0 interrupt */
    return (TRUE);
 
}

void UART0SendChar(char c)
{
  U0FCR = 0x04;   /* Enable and reset TX  FIFO. */
  U0THR=c;
//  U0TER=0x80;
  int temp=0;
  do
  {
    temp=U0LSR;

   
  }while((temp&0x20)==0);
//  U0TER=0x00;
}


int UART0ReceiveChar()
{
  U0FCR = 0x02;   /* Enable and reset RX  FIFO. */
 

  int temp=0;
  do
  {
    temp=U0LSR;
  
  }while((temp&0x01)==0);
  temp=U0RBR;
  return temp;

}

/******************************************************************************
**                            End Of File
******************************************************************************/

write.c

/*******************
 *
 * Copyright 1998-2003 IAR Systems.  All rights reserved.
 *
 * $Revision: 3162 $
 *
 * This is a template implementation of the "__write" function used by
 * the standard library.  Replace it with a system-specific
 * implementation.
 *
 * The "__write" function should output "size" number of bytes from
 * "buffer" in some application-specific way.  It should return the
 * number of characters written, or _LLIO_ERROR on failure.
 *
 * If "buffer" is zero then __write should perform flushing of
 * internal buffers, if any.  In this case "handle" can be -1 to
 * indicate that all handles should be flushed.
 *
 * The template implementation below assumes that the application
 * provides the function "MyLowLevelPutchar".  It should return the
 * character written, or -1 on failure.
 *
 ********************/
#include <yfuns.h>
#include "type.h"
#include "uart.h"

_STD_BEGIN

#pragma module_name = "?__write"

int MyLowLevelPutchar(int x)
{
 
  UART0SendChar(x);
  return 1;
}

/*
 * If the __write implementation uses internal buffering, uncomment
 * the following line to ensure that we are called with "buffer" as 0
 * (i.e. flush) when the application terminates.
 */

size_t __write(int handle, const unsigned char * buffer, size_t size)
{
  /* Remove the #if #endif pair to enable the implementation */


  size_t nChars = 0;

  if (buffer == 0)
  {
    /*
     * This means that we should flush internal buffers.  Since we
     * don't we just return.  (Remember, "handle" == -1 means that all
     * handles should be flushed.)
     */
    return 0;
  }

  /* This template only writes to "standard out" and "standard err",
   * for all other file handles it returns failure. */
  if (handle != _LLIO_STDOUT && handle != _LLIO_STDERR)
  {
    return _LLIO_ERROR;
  }

  for (/* Empty */; size != 0; --size)
  {
    if (MyLowLevelPutchar(*buffer++) < 0)
    {
      return _LLIO_ERROR;
    }

    ++nChars;
  }

  return nChars;

 

  /* Always return error code when implementation is disabled. */
  return _LLIO_ERROR;

 

}

_STD_END

 

 

read.c

 

/*******************
 *
 * Copyright 1998-2003 IAR Systems.  All rights reserved.
 *
 * $Revision: 3161 $
 *
 * This is a template implementation of the "__read" function used by
 * the standard library.  Replace it with a system-specific
 * implementation.
 *
 * The "__read" function reads a number of bytes, at most "size" into
 * the memory area pointed to by "buffer".  It returns the number of
 * bytes read, 0 at the end of the file, or _LLIO_ERROR if failure
 * occurs.
 *
 * The template implementation below assumes that the application
 * provides the function "MyLowLevelGetchar".  It should return a
 * character value, or -1 on failure.
 *
 ********************/

#include <yfuns.h>
#include "type.h"
#include "uart.h"

 

 


_STD_BEGIN

#pragma module_name = "?__read"

 


int MyLowLevelGetchar()
{

  return  UART0ReceiveChar();
 
}
 
 

 


size_t __read(int handle, unsigned char * buffer, size_t size)
{
  /* Remove the #if #endif pair to enable the implementation */
  

  int nChars = 0;

 

 
  /* This template only reads from "standard in", for all other file
   * handles it returns failure. */
  if (handle != _LLIO_STDIN)
  {
    return _LLIO_ERROR;
  }

  for (/* Empty */; size > 0; --size)
  {
    int c = MyLowLevelGetchar();
    if (c < 0)
      break;

    *buffer++ = c;
    ++nChars;
  }

  return nChars;

 

  /* Always return error code when implementation is disabled. */
  return _LLIO_ERROR;


}

_STD_END

 

/*****************************************************************************
 *   irq.c: Interrupt handler C file for NXP LPC230x Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.07.13  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "LPC230x.h"   /* LPC23XX Peripheral Registers */
#include "type.h"
#include "irq.h"

/* Initialize the interrupt controller */
/******************************************************************************
** Function name:  init_VIC
**
** Descriptions:  Initialize VIC interrupt controller.
** parameters:   None
** Returned value:  None
**
******************************************************************************/
void init_VIC(void)
{
    DWORD i = 0;
    DWORD *vect_addr, *vect_cntl;
    
    /* initialize VIC*/
    VICIntEnClr = 0xffffffff;
    VICVectAddr = 0;
    VICIntSelect = 0;

    /* set all the vector and vector control register to 0 */
    for ( i = 0; i < VIC_SIZE; i++ )
    {
  vect_addr = (DWORD *)(VIC_BASE_ADDR + VECT_ADDR_INDEX + i*4);
  vect_cntl = (DWORD *)(VIC_BASE_ADDR + VECT_CNTL_INDEX + i*4);
  *vect_addr = 0x0; 
  *vect_cntl = 0xF;
    }
    return;
}

/******************************************************************************
** Function name:  install_irq
**
** Descriptions:  Install interrupt handler
** parameters:   Interrupt number, interrupt handler address,
**      interrupt priority
** Returned value:  true or false, return false if IntNum is out of range
**
******************************************************************************/
DWORD install_irq( DWORD IntNumber, void *HandlerAddr, DWORD Priority )
{
    DWORD *vect_addr;
    DWORD *vect_cntl;
     
    VICIntEnClr = 1 << IntNumber; /* Disable Interrupt */
    if ( IntNumber >= VIC_SIZE )
    {
  return ( FALSE );
    }
    else
    {
  /* find first un-assigned VIC address for the handler */
  vect_addr = (DWORD *)(VIC_BASE_ADDR + VECT_ADDR_INDEX + IntNumber*4);
  vect_cntl = (DWORD *)(VIC_BASE_ADDR + VECT_CNTL_INDEX + IntNumber*4);
  *vect_addr = (DWORD)HandlerAddr; /* set interrupt vector */
  *vect_cntl = Priority;
  VICIntEnable = 1 << IntNumber; /* Enable Interrupt */
  return( TRUE );
    }
}

/******************************************************************************
**                            End Of File
******************************************************************************/

 

/*****************************************************************************
 *   uarttest.c:  main C entry file for NXP LPC23xx Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.07.13  ver 1.00    Prelimnary version, first Release
 *
 * Note:
 *  After power-up the controller get clock from internal RC oscillator that
 * is unstable and may fail with J-Link auto detect, therefore adaptive clocking
 * should always be used. The adaptive clock can be select from menu:
 *  Project->Options..., section Debugger->J-Link/J-Trace  JTAG Speed - Adaptive.
 *
 ******************************************************************************/
#include "LPC230x.h"                        /* LPC21xx definitions */
#include "type.h"

#include "target.h"
#include "uart.h"

#include    <stdio.h>

 

/*****************************************************************************
**   Main Function  main()
*****************************************************************************/
int main (void)
{
  TargetResetInit();

  UART0Init( 115200); /* baud rate setting */
 

 
  printf("Hello,World! This demo is modified by hammergo!/n");
  printf("Welcome to arm programming world!/n");

 
  int test=0;
  while(1)
  {
    printf("Please input test value:");
    scanf("%d",&test);
    if(test>0)
    {
      printf("test>0/n");
    }else
    {
      printf("test<=0/n");
    }
  }
 

  return 0;
}

/*****************************************************************************
**                            End Of File
*****************************************************************************/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值