您的位置:首页 > 其它

LPC 2368 Uart IAR EWARM printf scanf 实现2

2007-10-26 19:54 681 查看
在前面的实现里是使用中断来收发数据的,感觉代码较长,而且不好理解,效率也比较低,根据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
*****************************************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: