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aHR0cHM6Ly9pbmZvLnlh
aG9vLmNvbS9sZWdhbC91cy95YWhvby91dG9zL3Rlcm1zLwoK

An Engineer's Guide to the LPC2100 Series

use another timer connected to an interrupt to time the dropping of the txfr rs485 enable line. this way you can time it to the micro second. remember to calculate for baud rate and start, stop and parity bits.
Sent from Yahoo Mail on Android
From:"m...@yahoo.com [lpc2000]"
Date:Mon, 4 May, 2015 at 1:45
Subject:[lpc2000] LPC2119 uart 485 delay
 
I need help. My problem is marked in the photo . I donot know why the delay is so important. The delay () function is in "void UART0Send(void )".
#include "LPC214x.H" /* LPC21xx definitions */
#include "type.h"
#include "target.h"
#include "irq.h"
#include "uart.h"
DWORD Dummy;
BYTE UART0TxEmpty = 0;
volatile BYTE UART0Buffer[BUFSIZE];
volatile DWORD UART0Count = 0;
volatile BYTE* BufferPtr = NULL;
void DelayNS (unsigned int dly)
{
unsigned int i;
for ( ; dly>0; dly--)
for (i=0; i<20000; i++);
}
void ReadBit (void)
{
//DelayNS (300);//why not here???????????????????????????????
int i = 0;
while (!(U1LSR & 0x01)); //等待U0RBR contains valid data.
for (i=8; i > 0; i--)
{
UART0Buffer[UART0Count] = U1RBR;
UART0Count++;
}
}
void RLS (void)
{
Dummy = U1LSR; //读取UOLSR以清除中断//The interrupt is cleared upon an U0LSR read.
Dummy = U1RBR; //读取UORBR以清除中断
}
//A THRE interrupt is set immediately if the UART0 THR FIFO has held two or more characters at one time and
//currently, the U0THR is empty.
void THRE (void)
{
Dummy = U1IIR;
if ( U1LSR & 0x20 )
UART0TxEmpty = 1; //发送空间为空,可写
else
UART0TxEmpty = 0; //发送空间非空,不可写
}
void __irq UART0Handler (void)
{
BYTE IIRValue;
IIRValue = U1IIR; //The U0IIR must be read in order to clear the interrupt prior to exiting the Interrupt Service Routine
while ( !(IIRValue % 2) ) //检查中断是否处理完
{
IIRValue >>= 1;
switch (IIRValue & 0x7)
{
case 0x03:
RLS ();
break;
case 0x02:
ReadBit ();
break;
case 0x06: //The CTI interrupt is intended to flush the UART0 RBR after a message has been received that is not a multiple of the trigger level size.
ReadBit ();
break;
case 0x01:
THRE ();
break;
default:
break;
}

}
VICVectAddr = 0;
return;
}
void VICSetup (void)
{
VICIntSelect = 0x00000000; //设置所有的中断为IRQ中断
VICVectCntl0 = 0x20 | 0x07; //开启0号中断槽功能,注册0中断槽为UART0中断
VICVectAddr0 = (unsigned long) UART0Handler; //注册0号中断对应的中断处理函数地址
VICIntEnable = 1 << 0x07; //使能中断
}
void PLLInt (void)
{
PLLCON = 0x01; // 使能PLL
PLLCFG = 0x24; // 设置M 为5,P 为2
PLLFEED = 0xAA; // 发送PLL馈送序列
PLLFEED = 0x55;
while((PLLSTAT & (1 << 10)) == 0); // 等待PLL锁定
PLLCON = 0x03; // PLL使能和连接
PLLFEED = 0xAA; // 发送PLL馈送序列
PLLFEED = 0x55;
VPBDIV = 0x00;//设置片上外设的频率为四分之一的时钟频率
}
void UART0Init( DWORD baudrate )
{
DWORD Fdiv;
PINSEL0 = 0x50005; //使能UART0和UART1的引脚功能
IO0DIR = 1 << 10;
U1LCR = 0x83; //DLAB = 1,使能波特率设置功能
Fdiv = ( Fpclk / 16 ) / baudrate ; //设置波特率
U1DLM = Fdiv / 256;
U1DLL = Fdiv % 256;
U1LCR = 0x03; // DLAB = 0 ,关闭波特率设置功能,使能uart,
U1FCR = 0x87; // 使能UART0的输入和输出FIFOs,初始化(reset)FIFOs,设置中断触发条件为1个character
U1IER = IER_RBR | IER_THRE | IER_RLS; //使能中断功能
return;
}
/******************************************************
**函数功能:一次性读完输出FIFO里的所有字符,
*****************************************************/
void UART0Send(void )
{
//DelayNS (100);
IO0CLR = 1 << 10;
BufferPtr = UART0Buffer; //新的一次写进输出FIFO时要初始化读取指针
while ( UART0Count > 0 ) //一次性写出完所有的数据进输出FIFO
{
//while ( ! UART0TxEmpty ); //等待输出FIFO为空
while ( (U1LSR & 0x20) == 0 ); //等待,直到U0THR and the U0TSR 为空
if (*BufferPtr == 0)
{
Dummy = *BufferPtr;
}
else
{
U1THR = *BufferPtr;
}
while ( (U1LSR & 0x40) == 0 ); //代替了DelayNS (1);的功能
UART0TxEmpty = 0;
BufferPtr++;
UART0Count--;
}
IO0SET = 1 << 10;
DelayNS (50); //MMMMMMark

return;
}