Module 4
Module 4
Module 4
Introduction to ARM
Programming
• LPC2148 have 2 PORTS, P0 and P1. Each pin of these ports are
named as P0.0, P0.1, P0.2, P1.0, P1.2 etc.
#include<lpc214x.h>
void delay();
void main()
{
PINSEL0=0X00000000;
IO0DIR |=0XFFFFFFFF; //Port 0 is now
acting as a output pin
while(1)
{
IOSET0 |=0X00000008; //Port 0.3 pin
high now (LED is glowing)
delay();
IOCLR0 |=0X00000008; //Port 0.3 pin
low now (LED is OFF)
delay();
}
}
void delay()
{
unsigned int i;
for(i=0;i<30000;i++);
}
Department of EECE-19ECS431-EMBEDDED SYSTEMS 14
14
Simple C programs for application with LED
#include<lpc214x.h>
void delay();
void main()
{
PINSEL0=0X00000000;
IO0DIR |=0XfffffFFF; //Port 0 is now
acting as a output pin
while(1) {
IOSET0 |=0XfffffFFF; //Port 0's all pins
are high now (LED is glowing)
delay();
IOCLR0 |=0XFFFfffff; //Port 0's all pins
are low now (LED is OFF)
delay();
}
}
void delay()
{
unsigned int i;
for(i=0;i<30000;i++);
}
Department of EECE-19ECS431-EMBEDDED SYSTEMS 15
15
Simple C programs for application with LED
#include<lpc214x.h>
void delay();
void main()
{
PINSEL0=0X00000000;
IO0DIR |=0XFFFFFFFF;
while(1)
{
IOSET0 |=0X55555555;
delay();
IOCLR0 |=0XAAAAAAAA;
delay();
}
}
void delay()
{
unsigned int i;
for(i=0;i<30000;i++);
}
Department of EECE-19ECS431-EMBEDDED SYSTEMS 16
16
Simple C programs for application with LED
for(;x>0;x--)
for(j=0; j<0x1FFF; j++); Department of EECE-19ECS431-EMBEDDED SYSTEMS 17
} 17
Timers
The LPC2148 has two functionally identical general-purpose
timers: Timer0 and Timer1.
#include<lpc214x.h>
void pll();
int main(void)
IO0DIR=0xffffffff;
pll(); //Fosc=12Mhz,CCLK=60Mhz,PCLK=60MHz
while(1) {
IO0SET=0xffffffff;
IO0CLR=0xffffffff;
PLL0CON=0x01;
PLL0CFG=0x24;
PLL0FEED=0xaa;
PLL0FEED=0x55;
while(!(PLL0STAT&(1<<10)));
PLL0CON=0x03;
PLL0FEED=0xaa;
PLL0FEED=0x55;
VPBDIV=0x01;
T0TCR=0x01; //Timer ON
while(T0TC<z);
• The PLL0 is used to generate the CCLK clock (system clock) while the PLL1 has to
supply the clock for the USB at the fixed rate of 48 MHz.
• Structurally these two PLLs are identical with exception of the PLL interrupt
capabilities reserved only for the PLL0.
• The PLL0 and PLL1 accept an input clock frequency in the range of 10 MHz to 25
MHz only.
• The input frequency is multiplied up the range of 10 MHz to 60 MHz for the CCLK
and 48 MHz for the USB clock using a Current Controlled Oscillators (CCO).
The multiplier can be an integer value from 1 to 32 (in practice, the multiplier value
cannot be higher than 6 on the LPC2141/2/4/6/8 due to the upper frequency limit of the
CPU).
The CCO operates in the range of 156 MHz to 320 MHz, so there is an additional
divider in the loop to keep the CCO within its frequency range while the PLL is
providing the desired output frequency.
The output divider may be set to divide by 2, 4, 8, or 16 to produce the output clock.
Since the minimum output divider value is 2, it is insured that the PLL output has a 50%
duty cycle.
PLL activation is controlled via the PLLCON register. The PLL multiplier and divider
values are controlled by the PLLCFG register. These two registers are protected in order
to prevent accidental alteration of PLL parameters or deactivation of the PLL.
1)Timer Control Register (TCR):Timer Control register used to control the timer
control functions. We’ll enable, disable and reset Timer Counter (TC) through this
register
The 32-bit Timer Counter is incremented when the Prescale Counter reaches its terminal
count. Unless it is reset before reaching its upper limit, the TC will count up through the
value 0xFFFF FFFF and then wrap back to the value 0x0000 0000.
The 32-bit Prescale Register specifies the maximum value for the Prescale Counter.
The Prescale Counter is incremented on every PCLK. When it reaches the value stored in the
Prescale Register, the Timer Counter is incremented and the Prescale Counter is reset on the
next PCLK.
• A 16×2 LCD means it can display 16 characters per line and there are
2 such lines. In this LCD each character is displayed in a 5×7 pixel
matrix.
• LCD 16x2 is 16 pin device which has 8 data pins (D0-D7) and 3
control pins (RS, RW, EN). The remaining 5 pins are for supply and
backlight for the LCD.
The data register stores the data to be displayed on the LCD. The data
is the ASCII value of the character to be displayed on the LCD.
LCD 16x2 can be used in 4-bit mode or 8-bit mode depending on the
requirement of the application
•#include <lpc214x.h>
•#include <stdint.h>
•#include "LCD-16x2-8bit.h"
•#include <stdio.h>
•#include <string.h>
•int main(void)
•{
•uint32_t result;
float voltage;
•char volt[18];
•LCD_Init();
•PINSEL1 = 0x01000000; /* P0.28 as AD0.1 */
•AD0CR = 0x00200402; /* ADC operational, 10-bits, 11 clocks for conversion */
•
•while(1)
• {
• AD0CR = AD0CR | (1<<24); /* Start Conversion */
• while ( (AD0DR1 & 0x80000000) ==0); /* Wait till DONE */
• result = AD0DR1;
• result = (result>>6);
• result = (result & 0x000003FF);
• voltage = ( (result/1023.0) * 3.3 ); /* Convert ADC value to equivalent voltage */
• LCD_Command(0x80);
• sprintf(volt, "Voltage=%.2f V ", voltage);
• LCD_String(volt);
• memset(volt, 0, 18);
• }
Department of EECE-19ECS431-EMBEDDED SYSTEMS 64
•}
ADC (Analog to Digital Converter)
• Analog to Digital Converter(ADC) is used to convert analog signal into
digital form. LPC2148 has two inbuilt 10-bit ADC i.e. ADC0 & ADC1.
• Hence, we can connect 6 distinct types of input analog signals to ADC0 and
8 distinct types of input analog signals to ADC1.
Monitor the DONE bit (bit number 31) of the corresponding ADxDRy (ADC
Data Register) till it changes from 0 to 1. This signals completion of
conversion. We can also monitor DONE bit of ADGSR or the DONE bit
corresponding to the ADC channel in the ADCxSTAT register.
Read the ADC result from the corresponding ADC Data Register.
ADxDRy. E.g. AD0DR1 contains ADC result of channel 1 of ADC0.