Content originally posted in LPCWare by handthepig on Fri Jun 14 04:52:50 MST 2013
Hi all,
I'm trying to use a LPC1769 to communicate with another using UART.
I'm using UART2 (TXD2,RXD2)P0.10,P0.11 respectively. I just connected directly from the LPC1 (RXD2) to the LPC2 (TXD2) and LPC1(TXD2) to the LPC2 (RXD2).
I connected a push button to P2.12 of LPC1 and tried to send a data via UART to LPC2 to on a LED on P2.12. However it does not seem to work. Can someone help me? Sorry I'm new to LPC1769.
LPC 1 :
#include <stdio.h>
#include "lpc_types.h"
#include "lpc17xx_clkpwr.h"
#include "lpc17xx_gpio.h"
#include "lpc17xx_uart.h"
#include "lpc17xx_libcfg_default.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_nvic.h"
#include<string.h>
void Init_GPIO(void)
{
PINSEL_CFG_Type PinCfg2A;
PinCfg2A.Funcnum = 0;
PinCfg2A.OpenDrain=0;
PinCfg2A.Pinmode=0;
PinCfg2A.Portnum = 2;
PinCfg2A.Pinnum = 12;
PINSEL_ConfigPin (&PinCfg2A); //pushbutton
}
/*------------------------------*/
/*---------Pinsel UART2---------*/
void pinsel_uart2(void)
{
PINSEL_CFG_Type PinCfg;
PinCfg.Funcnum = 1;
PinCfg.OpenDrain=0;
PinCfg.Portnum = 0;
PinCfg.Pinnum = 10;
PINSEL_ConfigPin (&PinCfg); //transmitter
PinCfg.Pinnum = 11;
PINSEL_ConfigPin (&PinCfg); //receiver
}
/*---------Pinsel UART2---------*/
/*------------------------------*/
/*------------------------------------------*/
/*---------Initialise UART---------*/
void Init_UART(void)
{
UART_CFG_Type uartCfg;
uartCfg.Baud_rate = 115200;
uartCfg.Databits = UART_DATABIT_8;
uartCfg.Parity = UART_PARITY_NONE;
uartCfg.Stopbits = UART_STOPBIT_1;
//Pin select for uart2
pinsel_uart2();
//Supply power & setup working par.s for uart2
UART_Init(LPC_UART2, &uartCfg);
}
/*-----------------*/
/*------------------------------------------*/
int main(void)
{
SystemInit();
Init_GPIO();
Init_UART();//Initialise UART
int btn=1;
uint8_t Buffer[] = "1";
pinsel_uart2();
GPIO_SetDir(2, 1<<12, 0);//Configure Pin 2.12 as input pin
GPIO_SetDir(0, 1<<10, 1);//Output from UART P0.10
GPIO_SetDir(0, 1<<11, 0);//Input from UART P0.11
while(1)
{
btn = ((GPIO_ReadValue(2) >> 12) & 0x01);//btn to ground
if(btn==0)
{
UART_Send(LPC_UART2,(uint8_t *)Buffer,strlen(Buffer),NONE_BLOCKING );
}
}
}
LPC 2:
#include <stdio.h>
#include "lpc_types.h"
#include "lpc17xx_clkpwr.h"
#include "lpc17xx_gpio.h"
#include "lpc17xx_uart.h"
#include "lpc17xx_libcfg_default.h"
#include "lpc17xx_pinsel.h"
#include "lpc17xx_nvic.h"
#include<string.h>
void Init_GPIO(void)
{
PINSEL_CFG_Type PinCfg2A;
PinCfg2A.Funcnum = 0;
PinCfg2A.OpenDrain=0;
PinCfg2A.Pinmode=0;
PinCfg2A.Portnum = 2;
PinCfg2A.Pinnum = 12;
PINSEL_ConfigPin (&PinCfg2A); //pushbutton
}
/*------------------------------*/
/*---------Pinsel UART2---------*/
void pinsel_uart2(void)
{
PINSEL_CFG_Type PinCfg;
PinCfg.Funcnum = 1;
PinCfg.OpenDrain=0;
PinCfg.Portnum = 0;
PinCfg.Pinnum = 10;
PINSEL_ConfigPin (&PinCfg); //transmitter
PinCfg.Pinnum = 11;
PINSEL_ConfigPin (&PinCfg); //receiver
}
/*---------Pinsel UART2---------*/
/*------------------------------*/
/*------------------------------------------*/
/*---------Initialise UART---------*/
void Init_UART(void)
{
UART_CFG_Type uartCfg;
uartCfg.Baud_rate = 115200;
uartCfg.Databits = UART_DATABIT_8;
uartCfg.Parity = UART_PARITY_NONE;
uartCfg.Stopbits = UART_STOPBIT_1;
//Pin select for uart2
pinsel_uart2();
//Supply power & setup working par.s for uart2
UART_Init(LPC_UART2, &uartCfg);
}
/*-----------------*/
/*------------------------------------------*/
int main(void)
{
SystemInit();
Init_GPIO();
Init_UART();//Initialise UART
uint8_t Buffer;
pinsel_uart2();
GPIO_SetDir(2, 1<<12, 0);//Configure Pin 2.12 as input pin
GPIO_SetDir(0, 1<<10, 1);//Output from UART P0.10
GPIO_SetDir(0, 1<<11, 0);//Input from UART P0.11
while(1)
{
GPIO_ClearValue(2, 1<<12);
UART_Receive(LPC_UART2, &Buffer, 1, NONE_BLOCKING);//Check first char
if (Buffer=='1')
{
GPIO_SetValue(2, 1<<12);
}
}
}
Can someone help me? I'm currently doing it for a project and i dont have much time left.
Thank you!!!