Смысл программы - настроить и отдать в порт 2 байта. Пока что ничего на выходе не наблюдаю.
Код
#include <LPC13xx.h>
#define SystemCoreClock 12000000
// Если не настраивать источник тактовой частоты, то по умолчанию работает внутренний генератор 12МГц
#define RS485_ENABLED 0
#define TX_INTERRUPT 0 /* 0 if TX uses polling, 1 interrupt driven. */
#define MODEM_TEST 0
#define IER_RBR 0x01
#define IER_THRE 0x02
#define IER_RLS 0x04
#define IIR_PEND 0x01
#define IIR_RLS 0x03
#define IIR_RDA 0x02
#define IIR_CTI 0x06
#define IIR_THRE 0x01
#define LSR_RDR 0x01
#define LSR_OE 0x02
#define LSR_PE 0x04
#define LSR_FE 0x08
#define LSR_BI 0x10
#define LSR_THRE 0x20
#define LSR_TEMT 0x40
#define LSR_RXFE 0x80
#define BUFSIZE 0x40
/* RS485 mode definition. */
//#define RS485_NMMEN (0x1<<0)
//#define RS485_RXDIS (0x1<<1)
//#define RS485_AADEN (0x1<<2)
//#define RS485_SEL (0x1<<3)
//#define RS485_DCTRL (0x1<<4)
//#define RS485_OINV (0x1<<5)
volatile uint32_t UARTStatus;
volatile uint8_t UARTTxEmpty = 1;
volatile uint8_t UARTBuffer[BUFSIZE];
volatile uint32_t UARTCount = 0;
uint32_t i;
uint8_t lock[2];
void LED_OFF(void)
{
LPC_GPIO2->DATA=0x200;
}
void LED_ON(void)
{
LPC_GPIO2->DATA=0x000;
}
void init_timer32(void)
{
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9); // Подача на таймер частоты для счетчика 0
LPC_TMR32B0->PR = 0; // Установка делителя
LPC_TMR32B0->MCR = 0; // Автосброс, остановка таймера
LPC_TMR32B0->TCR = 2; // Сброс таймера
LPC_TMR32B0->TCR = 1; // Старт
}
void UARTInit(uint32_t baudrate)
{
uint32_t Fdiv;
uint32_t regVal;
NVIC_DisableIRQ(UART_IRQn); // откл. прерывание
LPC_IOCON->PIO1_6 &= ~0x07; /* UART I/O config */
LPC_IOCON->PIO1_6 |= 0x01; /* UART RXD */
LPC_IOCON->PIO1_7 &= ~0x07;
LPC_IOCON->PIO1_7 |= 0x01; /* UART TXD */
/* Enable UART clock */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
LPC_SYSCON->UARTCLKDIV = 0x1; /* divided by 1 */
LPC_UART->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
regVal = LPC_SYSCON->UARTCLKDIV;
Fdiv = (((12000000/LPC_SYSCON->SYSAHBCLKDIV)/regVal)/16)/baudrate; /*baud rate */
LPC_UART->DLM = Fdiv / 256;
LPC_UART->DLL = Fdiv % 256;
LPC_UART->LCR = 0x03; /* Отключаем доступ к делителю */
LPC_UART->FCR = 0x07; /* Enable and reset TX and RX FIFO. */
/* Read to clear the line status. */
regVal = LPC_UART->LSR; // Если 1, то в буфере есть данные
NVIC_EnableIRQ(UART_IRQn);
#if TX_INTERRUPT
LPC_UART->IER = IER_RBR | IER_THRE | IER_RLS; /* Enable UART interrupt */
#else
LPC_UART->IER = IER_RBR | IER_RLS; /* Enable UART interrupt */
#endif
return;
}
void UARTSend(uint8_t *BufferPtr, uint32_t Length)
{
while ( Length != 0 )
{
/* THRE status, contain valid data */
#if !TX_INTERRUPT
while ( !(LPC_UART->LSR & LSR_THRE) );
LPC_UART->THR = *BufferPtr;
#else
/* Below flag is set inside the interrupt handler when THRE occurs. */
while ( !(UARTTxEmpty & 0x01) );
LPC_UART->THR = *BufferPtr;
UARTTxEmpty = 0; /* not empty in the THR until it shifts out */
#endif
BufferPtr++;
Length--;
}
return;
}
void UART_IRQHandler(void)
{
uint8_t IIRValue, LSRValue;
uint8_t Dummy = Dummy;
IIRValue = LPC_UART->IIR;
IIRValue >>= 1; /* skip pending bit in IIR */
IIRValue &= 0x07; /* check bit 1~3, interrupt identification */
if (IIRValue == IIR_RLS) /* Receive Line Status */
{
LSRValue = LPC_UART->LSR;
/* 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 */
UARTStatus = LSRValue;
Dummy = LPC_UART->RBR; /* Dummy read on RX to clear
interrupt, then bail out */
return;
}
if (LSRValue & LSR_RDR) /* Receive Data Ready */
{
/* If no error on RLS, normal ready, save into the data buffer. */
/* Note: read RBR will clear the interrupt */
UARTBuffer[UARTCount++] = LPC_UART->RBR;
if (UARTCount == BUFSIZE)
{
UARTCount = 0; /* buffer overflow */
}
}
}
else if (IIRValue == IIR_RDA) /* Receive Data Available */
{
/* Receive Data Available */
UARTBuffer[UARTCount++] = LPC_UART->RBR;
if (UARTCount == BUFSIZE)
{
UARTCount = 0; /* buffer overflow */
}
}
else if (IIRValue == IIR_CTI) /* Character timeout indicator */
{
/* Character Time-out indicator */
UARTStatus |= 0x100; /* Bit 9 as the CTI error */
}
else if (IIRValue == IIR_THRE) /* THRE, transmit holding register empty */
{
/* THRE interrupt */
LSRValue = LPC_UART->LSR; /* Check status in the LSR to see if
valid data in U0THR or not */
if (LSRValue & LSR_THRE)
{
UARTTxEmpty = 1;
}
else
{
UARTTxEmpty = 0;
}
}
return;
}
int main(void)
{
// LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);
LPC_SYSCON->SYSAHBCLKDIV = 0x1;
LPC_GPIO2->DIR = 0x200;
LED_ON();
LPC_TMR32B0->IR = 0; // Отключение прерываний
LPC_TMR32B0->PR = 0;
LPC_TMR32B0->MCR = 0;
LPC_TMR32B0->TCR = 1; // включение таймера
LED_ON();
init_timer32();
lock[0] = 0x55;
lock[1] = 0xAA;
UARTInit(9600);
while(1)
{
LPC_TMR32B0->TCR = 2;
LPC_TMR32B0->TCR = 1;
while(LPC_TMR32B0->TC < 12000000) {}; // Задержка светодиода 1сек.
LED_OFF();
LPC_TMR32B0->TCR = 2;
LPC_TMR32B0->TCR = 1;
while(LPC_TMR32B0->TC < 12000000) {};
LED_ON();
LPC_UART->THR = 0xFA;
lock[0]++;
lock[1]++;
// if(LPC_GPIO3->DATA & 0x1)
// {
// LED_OFF();
// }
// else
// {
// LED_ON();
// }
}
}
#define SystemCoreClock 12000000
// Если не настраивать источник тактовой частоты, то по умолчанию работает внутренний генератор 12МГц
#define RS485_ENABLED 0
#define TX_INTERRUPT 0 /* 0 if TX uses polling, 1 interrupt driven. */
#define MODEM_TEST 0
#define IER_RBR 0x01
#define IER_THRE 0x02
#define IER_RLS 0x04
#define IIR_PEND 0x01
#define IIR_RLS 0x03
#define IIR_RDA 0x02
#define IIR_CTI 0x06
#define IIR_THRE 0x01
#define LSR_RDR 0x01
#define LSR_OE 0x02
#define LSR_PE 0x04
#define LSR_FE 0x08
#define LSR_BI 0x10
#define LSR_THRE 0x20
#define LSR_TEMT 0x40
#define LSR_RXFE 0x80
#define BUFSIZE 0x40
/* RS485 mode definition. */
//#define RS485_NMMEN (0x1<<0)
//#define RS485_RXDIS (0x1<<1)
//#define RS485_AADEN (0x1<<2)
//#define RS485_SEL (0x1<<3)
//#define RS485_DCTRL (0x1<<4)
//#define RS485_OINV (0x1<<5)
volatile uint32_t UARTStatus;
volatile uint8_t UARTTxEmpty = 1;
volatile uint8_t UARTBuffer[BUFSIZE];
volatile uint32_t UARTCount = 0;
uint32_t i;
uint8_t lock[2];
void LED_OFF(void)
{
LPC_GPIO2->DATA=0x200;
}
void LED_ON(void)
{
LPC_GPIO2->DATA=0x000;
}
void init_timer32(void)
{
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<9); // Подача на таймер частоты для счетчика 0
LPC_TMR32B0->PR = 0; // Установка делителя
LPC_TMR32B0->MCR = 0; // Автосброс, остановка таймера
LPC_TMR32B0->TCR = 2; // Сброс таймера
LPC_TMR32B0->TCR = 1; // Старт
}
void UARTInit(uint32_t baudrate)
{
uint32_t Fdiv;
uint32_t regVal;
NVIC_DisableIRQ(UART_IRQn); // откл. прерывание
LPC_IOCON->PIO1_6 &= ~0x07; /* UART I/O config */
LPC_IOCON->PIO1_6 |= 0x01; /* UART RXD */
LPC_IOCON->PIO1_7 &= ~0x07;
LPC_IOCON->PIO1_7 |= 0x01; /* UART TXD */
/* Enable UART clock */
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
LPC_SYSCON->UARTCLKDIV = 0x1; /* divided by 1 */
LPC_UART->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
regVal = LPC_SYSCON->UARTCLKDIV;
Fdiv = (((12000000/LPC_SYSCON->SYSAHBCLKDIV)/regVal)/16)/baudrate; /*baud rate */
LPC_UART->DLM = Fdiv / 256;
LPC_UART->DLL = Fdiv % 256;
LPC_UART->LCR = 0x03; /* Отключаем доступ к делителю */
LPC_UART->FCR = 0x07; /* Enable and reset TX and RX FIFO. */
/* Read to clear the line status. */
regVal = LPC_UART->LSR; // Если 1, то в буфере есть данные
NVIC_EnableIRQ(UART_IRQn);
#if TX_INTERRUPT
LPC_UART->IER = IER_RBR | IER_THRE | IER_RLS; /* Enable UART interrupt */
#else
LPC_UART->IER = IER_RBR | IER_RLS; /* Enable UART interrupt */
#endif
return;
}
void UARTSend(uint8_t *BufferPtr, uint32_t Length)
{
while ( Length != 0 )
{
/* THRE status, contain valid data */
#if !TX_INTERRUPT
while ( !(LPC_UART->LSR & LSR_THRE) );
LPC_UART->THR = *BufferPtr;
#else
/* Below flag is set inside the interrupt handler when THRE occurs. */
while ( !(UARTTxEmpty & 0x01) );
LPC_UART->THR = *BufferPtr;
UARTTxEmpty = 0; /* not empty in the THR until it shifts out */
#endif
BufferPtr++;
Length--;
}
return;
}
void UART_IRQHandler(void)
{
uint8_t IIRValue, LSRValue;
uint8_t Dummy = Dummy;
IIRValue = LPC_UART->IIR;
IIRValue >>= 1; /* skip pending bit in IIR */
IIRValue &= 0x07; /* check bit 1~3, interrupt identification */
if (IIRValue == IIR_RLS) /* Receive Line Status */
{
LSRValue = LPC_UART->LSR;
/* 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 */
UARTStatus = LSRValue;
Dummy = LPC_UART->RBR; /* Dummy read on RX to clear
interrupt, then bail out */
return;
}
if (LSRValue & LSR_RDR) /* Receive Data Ready */
{
/* If no error on RLS, normal ready, save into the data buffer. */
/* Note: read RBR will clear the interrupt */
UARTBuffer[UARTCount++] = LPC_UART->RBR;
if (UARTCount == BUFSIZE)
{
UARTCount = 0; /* buffer overflow */
}
}
}
else if (IIRValue == IIR_RDA) /* Receive Data Available */
{
/* Receive Data Available */
UARTBuffer[UARTCount++] = LPC_UART->RBR;
if (UARTCount == BUFSIZE)
{
UARTCount = 0; /* buffer overflow */
}
}
else if (IIRValue == IIR_CTI) /* Character timeout indicator */
{
/* Character Time-out indicator */
UARTStatus |= 0x100; /* Bit 9 as the CTI error */
}
else if (IIRValue == IIR_THRE) /* THRE, transmit holding register empty */
{
/* THRE interrupt */
LSRValue = LPC_UART->LSR; /* Check status in the LSR to see if
valid data in U0THR or not */
if (LSRValue & LSR_THRE)
{
UARTTxEmpty = 1;
}
else
{
UARTTxEmpty = 0;
}
}
return;
}
int main(void)
{
// LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);
LPC_SYSCON->SYSAHBCLKDIV = 0x1;
LPC_GPIO2->DIR = 0x200;
LED_ON();
LPC_TMR32B0->IR = 0; // Отключение прерываний
LPC_TMR32B0->PR = 0;
LPC_TMR32B0->MCR = 0;
LPC_TMR32B0->TCR = 1; // включение таймера
LED_ON();
init_timer32();
lock[0] = 0x55;
lock[1] = 0xAA;
UARTInit(9600);
while(1)
{
LPC_TMR32B0->TCR = 2;
LPC_TMR32B0->TCR = 1;
while(LPC_TMR32B0->TC < 12000000) {}; // Задержка светодиода 1сек.
LED_OFF();
LPC_TMR32B0->TCR = 2;
LPC_TMR32B0->TCR = 1;
while(LPC_TMR32B0->TC < 12000000) {};
LED_ON();
LPC_UART->THR = 0xFA;
lock[0]++;
lock[1]++;
// if(LPC_GPIO3->DATA & 0x1)
// {
// LED_OFF();
// }
// else
// {
// LED_ON();
// }
}
}
Пока что внятного описания по настройке UARTа не нашел. Направьте в правильном направлении.
Сначала пользовался фунцией
UARTSend(lock, 2);
Тот же результат.