ARM Example Codes and Projects Based on LPC2138

ARM Logo

The best series of ARM Example Codes. Learn them, change them and experiment with them to accelerate your projects with these extra ordinary sample codes.

LED flasher example code for ARM LPC2138

this example code displays 1, 2, 4 and 8 numbers respectively on the port 0.0 to 0.3 and also, additional modes can be added to this single-mode code

Char-LCD example code in 8bit mode for ARM LPC2138

Note: This example code is written without checking the busy-flag.
This program displays the value of an incremental counter on the LCD and the port 1 of MCU have only 16 pins, these pins are pulled-up by default so they are displayed in red.

Arm microcontroller connected to LCD

External interrupt (EINT0) example code for ARM LPC2138

In this program, every time the push-button is pressed, an interrupt occurs and a variable in the interrupt program is quadrupled and its value is shown in binary on port 0. This interrupt is of type IRQ and we need to clear the interrupt-flag after exiting the interrupt function program.

ARM External Interrupt Example code schematic

UART example code for ARM LPC2138

In this program, by typing letters in Proteus Virtual Terminal, these letters are sent by Virtual Terminal to the ARM microcontroller and the microcontroller receives these letters and displays them on the LCD and ports P0.16 to P0.23.
Note: In order to prevent booting into the bootloader, the P1.26, P1.31, P1.20 and P0.14 should be pulled up/down. But in the simulation delete the resistor of P0.14.

ARM UART Example Code schematic

Char-LCD example code in 8bit mode for ARM LPC2138 (AVR similar functions)

This program allows you to connect LCD pins to any custom ARM pins and to change these pins, simply open the “lcd.h” file and change the pin defines and recompile the program.

Setting up the ADC and reading the temperature from LM35 sensor

This program reads the temperature from LM35 using the ADC module and displays the temperature on the LCD.

Playing sound using PWM and DAC

PWM / DAC Wave form

The PWM module is very suitable for playing 8-bit sounds because with the 8-bit PWM it is possible to create a PWM frequency equal to twice the Sample Rate frequency. But with 16-bit PWM accuracy, the input frequency needs to be increased to 3 GHz, which is impossible, therefore using DAC is the correct way of playing sounds. This program reads and plays a Wav file in the ARM memory. The specifications of this file are as follows:
Sample rate: 22050 Sample size: 8bit
The following picture is describing the. Wav file header.

Wav audio format
Wav file header and data format

Playing sound using MMC or SD card memory

Note: This program is not simulated with Proteus software.
This program reads the following Wav file from MMC memory and plays it in high quality.
Sample Rate: 22050 Sample size: 16bit
This program communicates with the MMC using SPI and reads the information from it.
According to the clock frequency (16 MHz) the maximum read speed of the MMC is read.
Now, if we calculate the sample rate, it takes about 45 microseconds, and every 45 microseconds two bytes of data should be fed into the 10-bit ARM microcontroller DAC, which is done using a zero-timer interrupt, and in this count increment. But when you reach 22222 (that is, about a second), the time displayed on the LCD also increases by one second.
The following are the basics of MMC types:

Using DS1307 chip to receive time and date using hardware accelerated I2C

This program reads the time and date from the DS1307 and displays it on the LCD and pressing key1 and key2 can change the time and Date.
By calling. The i2c_init (100,4.608) function, the I2C frequency equals 100 kHz and PCLK frequency is also a quarter of CPU frequency equal to 18,432 MHz, i.e. 4,608MHz. Specifies the frequency of the SCL.
SDA and SCL pins are enabled by setting the PINSEL0 register and the time is set by the “rtc_get_time” function and if the time is over 59 (i.e. the first time the power is connected to the DS1307) the time is set to 12:00 by default. Eventually date and time will be displayed on the LCD.

arm lpc connected to ds1307 ic schematic

Communicating with external eeprom storage

This program has an EEPROM memory attached to the ARM. ARM reads the codes of this memory and displays it on the LCD. Also, at the beginning of the program using the eeprom_write function two numbers 0x37 and 0x38 are stored at address 0 and 2 of this memory, respectively. This function takes three inputs. One EEPROM address equal to 0xA0, another memory home address and the other is the data to be written.
Also, the delay function is created using timer 0. In order to “delay function” functions correctly, we first call the function and give it the exact PCLK frequency value.
The eeprom_read function also retrieves the device address and memory address and reads and returns the code

Using the watchdog timer(wdt)

In this program, the WDT counter will first be set with 250,000. Then by timing the WDMOD register and then WDFEED this timer is activated and starts descending. In the middle of the program, we have to constantly adjust the WDFEED registry so that the timer is continuously filled with 250,000. If WDFEED is not set this timer will be set to zero and reset the ARM.
In this program, the PCLK frequency is set to 1 MHz, and the reason for the WDTC register to be set at 250000 is that the timer is set to zero at 250ms and must be executed before this time the WDFEED command.

Connecting to the Ethernet port using ENC28J60 module

The ENC28J60 module communicates with SPI and can run at maximum 10Mbps speed. This program is written with the help of “11 project library finder” functions for AVR and modifies these functions for the ARM microcontroller. In this program you can enable the computer using two keys (key1 and key2).

arm microcontroller connected to enc28j60 ethernet module example code schematic

In this connection, we need to set the IP address of the network card to and then enter the number in a web browser, and then see the character count on the browser LCD. Now if we type
We see “ARM.html” on the LCD character.
See below screenshots for more info:

for windows xp:

network connections setting
local area connection setting
tcp/ip properties
manual ip address
displaying tempreture in html page

for windows 7 and 8:

network and sharing center
change adapter setting
ethernet properties
tcp/ipv4 properties

In the main loop of code, the ethernet_process function is continuously called, received and processed if packets are present on the network. UART1 is also enabled in this program and send information to the serial port via the send_char function. So that all information can be viewed accurately. The UART baud rate is 115200. The oscillator attached to the ARM is 18.432MHz.

Playing sounds using VS1003 module

schematic of arm and VS1003 example code

This module can play Wav, MP3, WMA, MIDI and AAC formats with super high-quality.
Actually, it is a hardware codec for playing audio. The processing of each file (i.e. decoding the file) is very difficult and compresses according to the Huffman algorithm. The mp3 file is made of some Frames. Each frame looks like the following pictures.

mp3 file frame format

The header of each Frame is made of 32bits look like the following information.

In the above picture the header and side information displayed in two different colors.
Decoding and converting MP3 to Wav format has a complicated algorithm for playback. In addition, each MP3 file has different parameters such as bit rate, so without this module it is impossible to decode the MP3 file. This IC looks like a microcontroller that has great speed and program for decoding and playing back those files. This IC plays stereo sound and the volume is software adjustable. Even the ability to connect a microphone to this IC and read the sound from the microphone and record it is another feature of the IC. The hardware of this IC is as follows:

VS1003 IC Schematic Circuit

Schematic description

The input frequency of this IC should be between 12 and 13MHz and it is best to be 12.288MHz to reach 52MHz after processing PLL to process audio files. In the figure the P1 terminal is for microphone connection. The power supply of this IC should be 3.3 volts.
The program reads the data from an MMC card and sends it to the VS1003. Before sending the data, the Xdcs pin is zeroed by the microcontroller and when the data is sent, the Xdcs pin is 1. Then we wait for the VS1003 to make the Dreq pin 1, which means it has processed the information. We read the next byte from MMC and send it to VS1003. The vs1003_init function also adjusts the volume, bass and treble.

Ks0108 Graphical LCD code for ARM

KS0108 GLCD ARM Example code Schematic

This LCD with KS0108 driver is 128X64 pixels. It is divided into two halves which can be selected using CS1 and CS2 pins. To clear the GLCD, first select the first half and send the 0x3F command to it, then select the second half and send the 0x3F command to the LCD. Then, using the glcd_show_pic function, a picture is shown on the LCD and this picture is stored in the code1 array and created using the GLCD_Editor program and a BMP image.

After displaying the image on the LCD with the “glcd_show_character”, the numbers from 0 to 9 are displayed in coordinates 2 and 62 of the LCD, the second row and column 62, respectively.

Pin control method:

Two Latch ICs have been used to reduce ARM pins consumption. Originally, there were four main Latch ICs two of them were used to control 7-Segment and GLCD, and two were used to control the Nokia LCD. This is for reducing the usage of ARM pins to accommodate other features such as UART, SPI and ADC. Used in other projects.
The pins of this LCD are as follows:

KS0108 GLCD Pinout
GLCD picture
NOKIA N96 display and 7-segment Display and KS0108 GLCD Display

In the above, you can see the Nokia N96 LCD and the 7-Segment where the programs written for each section can run on this board. This board is attached to the Main board.

Connecting Nokia N96 LCD to ARM

ARM + Nokia N96 lcd Example Code schematic

This program is Mr. Ali Imanifar’s modified program written for AVR. This program has been modified to run on ARM. Find the full article on Google. The only difference between this program and Mr. Imanifar’s program is that sending information to LCD is only one-way and cannot be read back from LCD, so no transparent text can be displayed on LCD because Due to the limitations of the I/O pins in the designed board I couldn’t use enough ARM pins.

Nokia N96 Display

At the beginning of the “tftlcd_functions.h” file, all pins are defined. This program controls the pins bit by bit but if we control pins byte by byte, the LCD speed can increase up to 10x. But for the sake of simplicity, this has not been done.

http://httpsHS0038 IR sensor | receiving data from Sony TV remote control Example Code

Due to the presence of an AGC (automatic amplifier) inside the IC, it is so sensitive and can detect Sony TV control codes from faraway.

hs0038 sensor

Sony’s TV control sends 011 as one and 01 as zero and the time between each number is 600uS.
unfortunately, in this program the timer is not capable of producing accurate microsecond timings so if it’s not responding correctly, change the value of delay functions until you get the best results.

Note: This program works well with 18.432MHz crystal.

Debugging and Simulation

For more info see below.

proteus step button

Click on the step button in the bottom-left of proteus window.

debugging in proteus

Choose HS0038.c in the opened window.

debugging code in proteus

Now put 3 break points by double clicking on the line 878, 868 and 860. Then press F12 to start the program.

HS0038 schematic in simulation

After running the program as above, click on LogicState to become one and click again to become zero to create a falling-edge on the ARM pin (instead of Logic State, HS0038 is connected in reality but since Proteus does not have it, we use Logic State). After creating the falling-edge, the program stops on line 860. Because you put a breakpoint on this line. Now press F12 again (or Play button at the bottom of the screen). Again, the program stops on line 868. You should now see the time elapsed from line 860 to line 868 at the bottom of the page:

time ealapsed in proteus

This time should be around 300uS. Press the play key again to reach the 878 line, and again to the 878 line again (this line is in the loop, running 40 times). If so, you should see 600uS at the bottom of the page (as shown above).

If you change the frequency, it’s time you should see in the picture below

Change the number 260 in the “for” loop so that the delay becomes 600uS. You can also write this loop on line 860. I.e. clear line 860 and place this loop on this line.

The following are the Sony control keys, which are hex:

In the following picture you can see the encoding method of above codes:

debugging code in proteus

encoding method:

sony tv remote control codes

Suppose we press the “0” key of the control. The hex code for this key is 089 and its binary is 0000-1000-1001.

sony tv remote control code timings

“0” bit bits are sent as 01 and “one” bits are 011 and the time between each is about 600uS. For example, for binary number 0110, this number is sent as 0101101101, which has a transmission time of 6000uS. So the number 089 is sent as follows:

089 = 0000-1000-1001> send> 11110 01010101-011010101-0110101011

hex    binary     code      header   0 0 0 0- 1 0 0 0- 1 0 0 1

Then the header is sent for 3000uS, one (011) takes 1800uS and zero (01) takes 1200uS. By sending a (one-nine-bit-one-data-number) a 40KHz carrier frequency is sent, which the HS0038 sensor only gives us the push of that frequency. As shown below:

sony remote control tmings

ADXL330 accelerometer ARM Example Code

adxl330 sensor schematic

The sensor is capable of measuring acceleration and angle. Just connect the X, Y, and Z pins in the figure above to the same name in the attached Sim.DSN file. This file opens in Proteus program but is not simulated. This program only shows the three-axis numbers and no acceleration calculations have been done in this program.

adxl330 module

The image above shows the extension board for the ADXL330.

Connecting HMC1052 magnetometer to ARM

hmc1052 schematic

The sensor can measure the magnetic field (around a power cord or earth (compass) or a magnet). Due to the very poor analog outputs of this sensor, an amplifier should be used to connect it to a microcontroller. It should be all SMD components, and the 100nF capacitors connected to the ground and VCC_HMC should be located at the closest distance to the sensor IC and sensor to eliminate noise.

hmc1052 full noise reduction schematic

Changing the brightness of LEDs

If you want to increase the brightness of LEDs, you should replace the R13 and R12 with R21 and R22 respectively. The LEDs become dimmer and brighter by changing the magnetic polarity, which must be carefully adjusted to calibrate P2 and P3 multiturn potentiometers. Last but not least, isolate the VCC_HMC power supply from other circuits to reduce noise. When designing a PCB, avoid all of these high-power and high-frequency parts as far as possible because the sensor is very sensitive. You can even design a separate PCB for the sensor for better results.

Programming LPC1768 registers

The LPC1768 registers are very similar to the LPC2138 and the only difference is that they are structured. For example, we need to write like below examples to access “PORT0” control registers:

LPC_GPIO0 -> FIOSET = 0x0.. ;
LPC_GPIO0 -> FIOCLR= 0x0.. ;
LPC_GPIO0 -> FIODIR= 0x0.. ;


The LPC2138 microcontroller has IOCLR, IOSET and IODIR registers and also has FIOSET, FIOCLR and FIODIR registries. Because FIO registers are faster and are directly connected to the CPU, the IOCLR registers are eliminated in the LPC1768 model.

In this program, an LED with this microcontroller is switched on and off. The disadvantage of this chip is that there isn’t a simulated model in Proteus for it, which is why most of the applications here are with LPC2138. (Proteus does not properly simulate some parts of LPC2138, such as DAC converter or some ADC pins).
The startup file in this chip looks for the “systeminit” function in C language, which perform PLL and clock peripheral settings.

Programming ARM STM32F107 registers:

This microcontroller has cool features and cheap price.
This program is an example of LED blinking.

RCC -> APB2ENR |= 1 | (1 << 4); // enabling the GPIO C clock.
GPIOC -> BSRR = 1; // turn LED on
GPIOC -> BSRR = 16; // turn LED off

ARM Example Code for IAR compiler

The IAR compiler is so similar to the Keil compiler. The newer versions of this compiler do not require a Startup file and we need to customize the Startup Registers by ourselves. I had copied the program from keil to iar without any problems many times, because they are so similar to each other.

Displaying time on 7-segments using ARM

This example code won’t work well in the proteus simulator but it works well in reality.
This program receives time and date from DS1307 and displays it on the 7-segment display.

ARM seven-segment control Example code schematic

Don’t forget to take a look at our Ultimate AVR Project Pack!

Leave a Reply

Your email address will not be published. Required fields are marked *