---
# System prepended metadata

title: Embedded Systems Lab 04

---

<h2 style='border: none'><center>Embedded Systems Lab 04</center></h2>

### <center>Clock Configurations & UART</center>
###### Author: Mohammed Nafiz ALMadhoun
---

<p style='text-align: justify;'>
In this lab, we are going to talk about UART peripheral and how to initialize it in the LPC111x, but first, we learn how to setup the microcontroller clock, so please return to your textbook to understand the concepts about clock and UART.</p>

### **Configuring Clock in LPC111x**
<p style='text-align: justify;'>
Knowing your clock frequency is a very important step if you want to do anything that needs precise time, as you already know from your digital design course, without a clock, we could not know what is past and what is present (That is why we call it clock XD).
</p>
<p style='text-align: justify;'>
The clock in any microcontroller is very flexible, as you might need the microcontroller for different applications (an elevator controller needs a slow clock, a streaming camera should have a fast clock).
Even if your microcontroller supports a very high frequency, you should not use it (Use just what you need), as it is a critical part in power consumption.
</p>

<center>

![Figure 1: Clock Configuration Diagram](https://i.imgur.com/jdYu4VD.png)
Figure 1: Clock Configuration Digram [^1]
</center>

[^1]: [LPC111x/LPC11Cxx User manual](https://www.keil.com/dd/docs/datashts/nxp/lpc11xx/lpc111x_lpc11cxx_um.pdf)

<p style='text-align: justify;'>
In this figure, we can see all the options we have to drive the main clock and other clocks, starting from <b>SYSPLLCLKSEL</b> you can select the main PPL clock (which you will configure to output PPLCLK), then using <b>MAINCLKSEL</b> you can select the main clock for the device, as you can see you have four options.
</p>


### **Monitoring Clocks**
<p style='text-align: justify;'>
In this section, we will talk about monitoring various clocks, in this lab, we are interested in the main clock as it is the clock that drives our UART Peripheral.
As you may notice from the previous diagram, there is a <b>CLKOUT</b> pin, we can use this pin to output various clocks from the microcontroller to the outer world, this will make it way easier for us to debug any timing or clocks issues.
Now using the User Manual, you can search about <b>CLKOUT</b> to find different useful registers which are:
<ul>
    <li>CLKOUT clock source select register</li>
    <li>CLKOUT clock source update enable register</li>
    <li>CLKOUT clock divider register</li>
</ul>

Please refer to the [User Manual](https://www.keil.com/dd/docs/datashts/nxp/lpc11xx/lpc111x_lpc11cxx_um.pdf) to understand more about the registers.
</p>

After we setup the CLKOUT block, we should configure the assigned pin to output the CLKOUT.
<center>

![Figure 2: LPC11114FBD PINS](https://i.imgur.com/IQDHzPJ.png)
Figure 2: LPC11114FBD PINS
</center>
<p style='text-align: justify;'>
As you might notice, that PIN 1 of PORT 0 could be used as CLKOUT, to that you should change the value of <b>IOCON_PIO0_1</b> to select the CLKOUT function (0x1).</p>

Example code:
```c=
#define CLKOUTCLKSEL (*((volatile unsigned long *)0x400480E0))
#define CLKOUTCLKUEN (*((volatile unsigned long *)0x400480E4))
#define CLKOUTCLKDIV (*((volatile unsigned long *)0x400480E8))

#define IOCON_PIO0_1 (*((volatile unsigned long *)0x40044010))

int main (void) {
    CLKOUTCLKDIV = 0x1;
    CLKOUTCLKSEL = 0x3;
    CLKOUTCLKUEN = 0x0;
    CLKOUTCLKUEN = 0x1;
    IOCON_PIO0_1 = 0x1;
    while(1);
}
```
The previous code will connect CLKOUT to the main clock.

### **Configuring UART Peripheral**
As you might notice, the UART peripheral clock source is the main clock, so please remember and be sure about your main clock frequency for the next section.

Starting by looking at Chapter 13 in the User Manual, you will find all the needed information to setup and start the UART peripheral, and here it is again!

1. You should configure PIO1_6 and PIO1_7 to RXD and TXD functionality (Same as configuring PIO0_1 in the previous section).
2. You should enable the peripheral clock using **SYSAHBCLKCTRL** register by setting bit 12.
3. Configure the peripheral clock divider using the following formula to output the desired baud rate.
<center>

![Figure 3: Baudrate formula](https://i.imgur.com/C16mRR8.png)
Figure 3: Baudrate formula. [^1]
</center>

Please return to **Table 183** in the User Manual.

To calculate the values of the registers you could get some help from **Table 200**

In the next example, I will setup the baud rate to 115200 baud while my main clock is 12 MHz.

```c=
// Do NOT forget your definitions here!

int main (void) {
    IOCON_PIO1_6 |= 0x1;
    IOCON_PIO1_7 |= 0x1;
    SYSAHBCLKCTRL |= 1 << 12;
    // You should set bit 7 to access U0DLM, U0DLL
    U0LCR = (1 << 7) | 3;
    // U0DL = 0x0004
    U0DLM = 0;
    U0DLL = 4;
    U0FDR = 5 | (8 << 4);
    UARTCLKDIV = 1;
    // You should clear bit 7 to access U0THR
    U0LCR = 3;
    
    // This code is not correct you should check U0LSR (Line status register)
    U0THR = 'M';
    U0THR = 'o';
    U0THR = 'h';
    U0THR = '9';
    U0THR = '7';
    
    while(1);
}
```

In Proteus, you can use the Virtual Terminal instrument, or COMPIM to communicate with the chip.


<center>

### END OF LAB 4</center>

<div style="display:none">

# Embedded Systems Lab 04

</div>
