Skip to content

Instantly share code, notes, and snippets.

@inkwisit
Created June 14, 2016 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inkwisit/d34eb11d57dba71bcde00d14894c504d to your computer and use it in GitHub Desktop.
Save inkwisit/d34eb11d57dba71bcde00d14894c504d to your computer and use it in GitHub Desktop.
UART2_ARM_CORTEX_M3_LPC17xx
#include "LPC17xx.h"
#include <stdint.h>
#include "uart2.h"
#include <stdlib.h>
int main()
{
SystemInit();
char data;
char temp[15];
UART2_setup(9600);
UART2_send_string("Subha Sarkar\n");
while(1)
{
data = UART2_read();
UART2_write(data);
utoa(SystemCoreClock,temp,10);
UART2_send_string(temp);
UART2_write('\n');
}
return 0;
}
#include <stdint.h>
#include "LPC17xx.h"
#include "system_LPC17xx.h"
//Step0: Power on the PCONP Register (LPC_SC) uart2 bit 24 set
//Step1: Configure the GPIO pin for UART0 function using PINSEL register.
//Step2: Configure the FCR for enabling the FIFO and Reste both the Rx/Tx FIFO.
//Step3: Configure LCR for 8-data bits, 1 Stop bit, Disable Parity and Enable DLAB.
//Step4: Get the PCLK from PCLKSELx register 7-6 bits.
//Step5: Calculate the DLM,DLL values for required baudrate from PCLK.
//Step6: Updtae the DLM,DLL with the calculated values.
//Step6: Finally clear DLAB to disable the access to DLM,DLL.
//Step7: Monitor the LSR register to send and recieve byte.
#define PCUART2 24
#define PCLK_UART2_17 17
#define PCLK_UART2_16 16
#define STOP_BIT_1 2
#define DISABLE_PARITY 3
#define _8_BIT_MODE ((uint32_t)(1<<1)|(1<<0))
#define FIFO_ENABLE 0
#define DLAB_DLL_DLM 7
#define RDR 0
#define THRE 5 //transmitter holding register empty
#define FIFO_RX_RESET 1
#define FIFO_TX_RESET 2
int UART2_setup(uint32_t baudrate)
{
uint32_t temp = 0;
LPC_SC->PCONP |= (1<<PCUART2);
//powering on the UART2
LPC_SC->PCLKSEL1 |= (1<<PCLK_UART2_16);
LPC_SC->PCLKSEL1 &= ~(uint32_t)(1<<PCLK_UART2_17);
//selecting the clock source (CCLK) (actual CPU frequency)
//this makes SystemcoreClock
LPC_UART2->LCR |= (1<<DLAB_DLL_DLM);
//enable access to baud rate registers DLL , DLM called Divider latch access bit
//it should be declared here only otherwise program does not work
LPC_PINCON->PINSEL0 |= (uint32_t)((1<<20)|(1<<22));
LPC_PINCON->PINSEL0 &= ~(uint32_t)((1<<21)|(1<<23));
//selecting the RXD2 and TXD2 mode from 4 multiplexed functions
//selecting the P0.10(TXD2) and P0.11(RXD2)
LPC_UART2->LCR |= _8_BIT_MODE;
//8 bit mode
LPC_UART2->LCR &= ~(uint32_t)((1<<STOP_BIT_1)|(1<<DISABLE_PARITY));
//stop bit 1, disable parity generation
LPC_UART2->FCR |= (1<<FIFO_ENABLE)|(1<<FIFO_RX_RESET)|(1<<FIFO_TX_RESET);
//enabling the FIFO
temp = ((uint32_t)SystemCoreClock/((uint32_t)(16*baudrate)));
//calculating the baud rate
LPC_UART2->DLL = temp & 0xFF;
LPC_UART2->DLM = (temp>>8)&(0xFF);
//setting the baud rate
LPC_UART2->LCR &= ~(uint32_t)(1<<DLAB_DLL_DLM);
//disabling the access to the DLL and DMM register
return 0;
}
char UART2_read()
{
char data;
while(!(LPC_UART2->LSR & (1<<RDR)));
data = LPC_UART2->RBR;
return data;
}
int UART2_write(char data)
{
while(!(LPC_UART2->LSR & (1<<THRE)));
LPC_UART2->THR = data;
return 0;
}
int UART2_send_string(char *ptr)
{
while(*ptr)
{
UART2_write(*ptr);
ptr++;
}
return 0;
}
#include<stdint.h>
int UART2_setup(uint32_t);
char UART2_read();
int UART2_write(char);
int UART2_send_string(char *);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment