Tuesday, 13 June 2017

DATA reception using UART in LPC2148

Receiving data via UART is same as sending in some ways. So registers used here are same as we used in our last tutorial, "data transmission using UART in LPC2148". Go HERE to read it. I will just skip to main part.

While transmitting data, we wait for THRE bit of U0LSR register to go high(1),which indicates that U0THR is empty. Than we load data to U0THR register.
During reception of data, we will again wait for  RDR bit of U0LSR register to high, which indicates that U0RBR is full and than we will fetch the data from it.

For now I will not use any interrupt, and receive the data in a while loop. I will display the data received on LCD, which we already did, check it HERE. As I mentioned before, I will be using header files for PLL and LCD to reduce the code length here. You can download the full working code at the end of this tutorial.

HOW TO-

1. Initialize pll, lcd and uart.

2. In the while loop continuously check for incoming data
    while (1)
      {
         while (!(U0LSR&(1<<0)));      // wait until RDR (bit 0) of U0LDR becomes high
          char data = U0RBR;                // get the data from U0RBR register
          decode_data (data);                 // display the data on LCD
      }

That is all for receiving the data.

CODE-
#include <lpc214x.h>
#include "pll.h"
#include "lcd.h"


void Uart_init ()
{
  PINSEL0 |= (1<<0) | (1<<2);   //Configure P0.0/P0.1 as RX0 and TX0
  U0FCR = (1<<0) | (1<<1) | (1<<2);  // Enable FIFO and reset Rx/Tx FIFO buffers  
  U0LCR = 3 | (1<<7);   // 8bit data(0x03), 1Stop bit(bit 4), No parity(bit 5) and enable DLAB(bit 7)
U0DLL = 0x61;   // (15MHz)/(16*9600)
U0DLM = 0;
U0LCR &= ~(1<<7);   // clear the DLAB bit
}

int main ()
{
char data;
PINSEL2 = 0;    // set GPIO for lcd pins
IODIR1 = 0x00ff0000;    // set pin 16 to 23 as output
pll_init ();
lcd_init ();
Uart_init ();
while (1)
{
while (!(U0LSR&(1<<0)));     // wait untill RDR bit goes 1
data = U0RBR;     // load data from U0RBR register
decode_data (data);    // display data on LCD
}
}

In the Proteus I am using virtual terminal to send data to the controller. Tx of terminal should be connected to Rx of controller and vice versa.

That's it guys. Subscribe for more tutorials.
IMP- I have created a youtube channel for the tutorials. To watch the working of a real microcontroller, subscribe to controllers tech.
For full working code with simulation, click here.

Servo motor with STM32

I have already covered a tutorial about Pulse Width Modulation in STM32  HERE  and in this tutorial i am going to cover one of its applicat...