ARM Embedded Controller Manual
ARM Embedded Controller Manual
ARM Embedded Controller Manual
VI Semester
STUDY PROGRAMS:
1. Program to blink all LED and see the result in simulation.
2. Program to Blink only two LED and see the result in simulation.
3. Program to generate square wave using internal DAC and see the result in simulation.
4. Program to generate Triangular wave using internal DAC and see the result in simulation.
PART-A: Conduct the following Study experiments to learn ALP using ARM
Cortex M3 Registers using an Evaluation board and the required software tool.
1. ALP to multiply two 16 bit binary numbers.
2. ALP to find the sum of first 10 integer numbers.
#include <lpc17xx.h>
void delay(void);
int main(void)
{
LPC_GPIO0->FIODIR = 0xFFFFFFFF; // Configure all pins on Port 0 as Output
while(1)
{
LPC_GPIO0->FIOSET = 0xFFFFFFFF; // Turn on LEDs
delay();
LPC_GPIO0->FIOCLR = 0XFFFFFFFF ; // Turn them off
delay();
}
// return 0; // normally this wont execute
}
#include <lpc17xx.h>
void delay(void);
int main(void)
while(1)
delay();
delay();
for(count=0; count < 3000/*000*/; count++) // You can edit this as per your needs
i++; // something needs to be here else compiler will remove the for loop!
}
Program to generate square wave using internal DAC
#include <lpc17xx.h>
void initTimer0(void);
int count = 0;
int main(void)
{
unsigned int value=0; //Binary value used for Digital To Analog Conversion
initTimer0();
while(1)
{
}
}
void initTimer0(void)
{
#include <lpc17xx.h>
void initTimer0(void);
int main(void)
{
initTimer0();
unsigned int value=0; //Binary value used for Digital To Analog Conversion
while(1)
{
}
for(value=1023;value>0;value--)
{
LPC_DAC->DACR = (value<<6);
}
}
void initTimer0(void)
{
PART-A: Conduct the following Study experiments to learn ALP using ARM Cortex M3
Registers using an Evaluation board and the required software tool.
MULTIPLY
LDR R0,=0X706F
LDR R1,=0X7161
MUL R2,R1,R0
MOV R3,R2;
LDR R3,= globvar ; load the addr of var into R0
STR R2,[R3]
NOP
NOP
BX LR ; jumps back to C code
END
ALP to find the sum of first 10 integer numbers.
THUMB
AREA sum,CODE,READONLY
EXPORT DVALUE
EXPORT sumten
IMPORT globvar
ENTRY
sumten
MOV R1,#10 ; load 10 to regester
MOV R2,#0 ; empty the register to store result
loop
ADD R2,R2,R1 ; add the content of R1 with result at R2
SUBS R1,#0x01 ; Decreament R1 by 1
BNE loop ; repeat till r1 goes 0
AREA DATA1,DATA,READWRITE
DVALUE
DCD 0x00000000;
ALIGN
END
PART-B: Conduct the following experiments on an ARM CORTEX M3
evaluation board using evaluation version of Embedded 'C' & Keil uVision-4
tool/compiler.
#include<LPC17xx.h>
void delay(unsigned int r1);
void UART0_Init(void);
unsigned int i ;
unsigned char *ptr, arr[] = "Hello world\r";
#define THR_EMPTY 0x20 //U0THR is Empty
int main(void)
{
UART0_Init();
while(1)
{
ptr = arr;
while ( *ptr != '\0')
{
while ((LPC_UART0->LSR & THR_EMPTY) != THR_EMPTY) ;
//Is U0THR is EMPTY??
LPC_UART0->THR = *ptr++;
}
for(i=0;i<=60000;i++);
}
}
void UART0_Init(void)
{
LPC_SC->PCONP = 0x00000008; //UART0 peripheral enable
LPC_PINCON->PINSEL0 = 0x00000050;
LPC_UART0->LCR = 0x00000083; //enable divisor latch, parity disable, 1 stop bit, 8bit
word length
LPC_UART0->DLM = 0X00;
LPC_UART0->DLL = 0x1A; //select baud rate 9600 bps for 4Mhz
LPC_UART0->LCR = 0X00000003;//Disable divisor latch
LPC_UART0->FCR = 0x07;
//FIFO enable,RX FIFO reset,TX FIFO reset
}
Result: Thus the program to display “hello word” message using internal UART is executed
and the output I obtained.
Ex.No:2 Interface and Control a DC Motor.
#include <LPC17xx.H>
void Clock_Wise(void);
void AClock_Wise(void);
unsigned long i;
int main(void)
{
LPC_PINCON->PINSEL1 = 0x00000000; //P0.26 GPIO, P0.26 controls dir
LPC_PINCON->PINSEL3 = 0x00000000; //P1.24 GPIO
LPC_GPIO0->FIODIR |= 0x04000000; //P0.26 output
LPC_GPIO1->FIODIR |= 0x01000000; //P1.24 output
while(1)
{
Clock_Wise();
for(i=0;i<300000;i++);
AClock_Wise();
for(i=0;i<300000;i++);
} //end while(1)
} //end main
void Clock_Wise(void)
{
LPC_GPIO1->FIOCLR = 0x01000000; //P1.24 Kept low to off DCM
for(i=0;i<10000;i++); //delay to componsate
inertia
LPC_GPIO0->FIOSET = 0x04000000; //coil is on
LPC_GPIO1->FIOSET = 0x01000000; //motor in on
} //end void Clock_Wise(void)
void AClock_Wise(void)
{
LPC_GPIO1->FIOCLR = 0x01000000; //P1.24 Kept low to off DCM
for(i=0;i<10000;i++); //delay to componsate inertia
LPC_GPIO0->FIOCLR = 0x04000000; //coil is off
LPC_GPIO1->FIOSET = 0x01000000; //Motor is on
} //end void AClock_Wise(void)
Result: Thus the program to rotate DC motor in both the direction is executed.
B. Program to control speed of DC Motor in using PWM.
#include <lpc17xx.h>
void pwm_init(void);
void PWM1_IRQHandler(void);
unsigned long int i;
unsigned char flag,flag1;
int main(void)
{
pwm_init();
while(1);
} //end of main
void pwm_init(void)
{
LPC_SC->PCONP = (1<<6); //PWM1 is powered
LPC_PINCON->PINSEL3 = 0x00020000;
//pwm1.5 is selected for the pin P1.24
LPC_PWM1->PR = 0x00000000; //Count frequency : Fpclk
LPC_PWM1->PCR = 0x0002000;
//select PWM5 single edge and PWM5 output is enabled
LPC_PWM1->MCR = 0x00000003;
//Reset and interrupt on PWMMR0
LPC_PWM1->MR0 = 60000; //setup match register 0 count
LPC_PWM1->MR5 = 0x00000100; //setup match register MR5
LPC_PWM1->LER = 0x000000FF; //enable shadow copy register
LPC_PWM1->TCR = 0x00000002; //RESET COUNTER AND PRESCALER
LPC_PWM1->TCR = 0x00000009; //enable PWM and counter
NVIC_EnableIRQ(PWM1_IRQn);
}
void PWM1_IRQHandler(void)
{
LPC_PWM1->IR = 0xff; //clear the interrupts
if(flag == 0x00)
{
LPC_PWM1->MR5 += 100;
LPC_PWM1->LER = 0x000000FF;
if(LPC_PWM1->MR5 >= 57000)
//for 10% of duty cycle MR5=3000,for 50% MR5=15000 ,for 90% MR5= 27000
{
flag1 = 0xff;
flag = 0xff;
LPC_PWM1->LER = 0x000000fF;
}
for(i=0;i<8000;i++);
}
else if(flag1 == 0xff)
{
LPC_PWM1->MR5 -= 100;
LPC_PWM1->LER = 0x000000fF;
if(LPC_PWM1->MR5 <= 0x300)
//1% of duty cycle MR5=300
{
flag = 0x00;
flag1 = 0x00;
LPC_PWM1->LER = 0X000000fF;
}
for(i=0;i<8000;i++);
}
}
for(k=0;k<65000;k++);
//Delay to show anti_clock Rotation
for(j=0;j<50;j++)
//50 times in Anti Clock wise Rotation
anti_clock_wise();
for(k=0;k<65000;k++);
//Delay to show clock Rotation
}
//End of while(1)
}
//End of main
void clock_wise(void)
{
var1 = 0x00000001; //For Clockwise
for(i=0;i<=3;i++) //for A B C D Stepping
{
LPC_GPIO2->FIOCLR = 0X0000000F;
LPC_GPIO2->FIOSET = var1;
var1 = var1<<1; //For Clockwise
for(k=0;k<3000;k++); //for step speed variation
}
}
void anti_clock_wise(void)
{
var1 = 0x0000008; //For Anticlockwise
for(i=0;i<=3;i++) //for A B C D Stepping
{
LPC_GPIO2->FIOCLR = 0X0000000F;
LPC_GPIO2->FIOSET = var1;
var1 = var1>>1; //For Anticlockwise
for(k=0;k<3000;k++); //for step speed variation
}
}
Result: Thus the program to interface and control Stepper motor in both the direction is
executed
Ex.No:4 Interface a DAC and generate Triangular and Square
waveforms.
/*********************************************************************
Description : This example explains about how Sqaure Wave is generated.P0.4 to P0.11 are used to
get the Digital values.
**************************************************************************/
#include <LPC17xx.H>
void delay(void);
int main ()
{
LPC_PINCON->PINSEL0 &= 0xFF0000FF ;
// Configure P0.4 to P0.11 as GPIO
LPC_GPIO0->FIODIR |= 0x00000FF0 ;
LPC_GPIO0->FIOMASK = 0XFFFFF00F;
while(1)
{
LPC_GPIO0->FIOPIN = 0x00000FF0 ;
delay();
LPC_GPIO0->FIOCLR = 0x00000FF0 ;
delay();
}
}
void delay(void)
{
unsigned int i=0;
for(i=0;i<=9500;i++);
}
To generate Triangular wave
Description : This example explains about how Triangular Wave is generated.P0.4 to P0.11 are used
to get the Digital values.
*********************************************************************/
#include <LPC17xx.H>
int main ()
{
unsigned long int temp=0x00000000;
unsigned int i=0;
LPC_PINCON->PINSEL0 &= 0xFF0000FF ;
// Configure P0.4 to P0.11 as GPIO
LPC_GPIO0->FIODIR |= 0x00000FF0 ;
LPC_GPIO0->FIOMASK = 0XFFFFF00F;
while(1)
{
//output 0 to FE
for(i=0;i!=0xFF;i++)
{
temp=i;
temp = temp << 4;
LPC_GPIO0->FIOPIN = temp;
} // output FF to 1
for(i=0xFF; i!=0;i--)
{
temp=i;
temp = temp << 4;
LPC_GPIO0->FIOPIN = temp;
}
}//End of while(1)
}//End of main()
Result : Thus the program to generater square wave and triangle waveform is executed and the output
waveform is obtained.
#include <LPC17xx.h>
#include "lcd.h"
void scan(void);
int main(void)
{
LPC_PINCON->PINSEL3 = 0x00000000; //P1.20 to P1.23 MADE
GPIO
LPC_PINCON->PINSEL0 = 0x00000000; //P0.15 as GPIO
LPC_PINCON->PINSEL1 = 0x00000000; //P0.16 t0 P0.18 made GPIO
LPC_GPIO0->FIODIR &= ~0x00078000; //made INput P0.15 to P0.18
(cols)
LPC_GPIO1->FIODIR |= 0x00F00000; //made output P1.20
to P1.23 (rows)
LPC_GPIO1->FIOSET = 0x00F00000;
lcd_init();
temp1 = 0x80;
//point to first line of LCD
lcd_com();
delay_lcd(800);
lcd_puts(&Msg1[0]);
//display the messsage
temp1 = 0xC0;
//point to first line of LCD
lcd_com();
delay_lcd(800);
lcd_puts(&Msg2[0]);
//display the messsage
while(1)
{
while(1)
{
for(row=1;row<5;row++)
{
if(row == 1)
var1 = 0x00100000;
else if(row == 2)
var1 = 0x00200000;
else if(row == 3)
var1 = 0x00400000;
else if(row == 4)
var1 = 0x00800000;
temp = var1;
LPC_GPIO1->FIOSET = 0x00F00000;
LPC_GPIO1->FIOCLR = var1;
flag = 0;
scan();
if(flag == 1)
break;
}
//end for(row=1;row<5;row++)
if(flag == 1)
break;
}
//2nd while(1)
for(i=0;i<16;i++)
{
if(key == SCAN_CODE[i])
{
key = ASCII_CODE[i];
break;
}
//end if(key == SCAN_CODE[i])
}
//end for(i=0;i<16;i++)
temp1 = 0xCC;
lcd_com();
delay_lcd(800);
lcd_puts(&key);
}
//end while 1
}
//end main
void scan(void)
{
unsigned long temp3;
temp3 = LPC_GPIO0->FIOPIN;
temp3 &= 0x00078000;
if(temp3 != 0x00078000)
{
for(i=0;i<500;i++);
temp3 = LPC_GPIO0->FIOPIN;
temp3 &= 0x00078000;
if(temp3 != 0x00078000)
{
flag = 1;
temp3 >>= 15;
//Shifted to come at LN of byte
temp >>= 16;
//shifted to come at HN of byte
key = temp3|temp;
}
//2nd if(temp3 != 0x00000000)
}
//1st if(temp3 != 0x00000000)
}
//end scan
Result: Thus the program to interface a 4x4 keyboard and display the key code on an LCD is
executed and the outputs are verified.
Ex.No:6 Using the Internal PWM module of ARM controller
generate PWM and vary its duty cycle.
#include <LPC17xx.H>
void pwm_init(void);
void PWM1_IRQHandler(void);
unsigned long int i;
unsigned char flag,flag1;
int main(void)
{
pwm_init();
while(1);
}
//end of main
void pwm_init(void)
{
LPC_SC->PCONP = (1<<6); //PWM1 is powered
LPC_PINCON->PINSEL7 = 0x000C0000; //pwm1.2 is selected for the pin P3.25
NVIC_EnableIRQ(PWM1_IRQn);
}
void PWM1_IRQHandler(void)
{
LPC_PWM1->IR = 0xff; //clear the
interrupts
if(flag == 0x00)
{
LPC_PWM1->MR2 += 100;
LPC_PWM1->LER = 0x000000FF;
}
else if(flag1 == 0xff)
{
LPC_PWM1->MR2 -= 100;
LPC_PWM1->LER = 0x000000fF;
Result: Thus the program using the Internal PWM module of ARM controller is used to
generate PWM by varing its duty cycle.
Ex.No:7 Demonstrate the use of an external interrupt to toggle an
LED On/Off.
#include<LPC17xx.h>
void EINT3_IRQHandler(void);
unsigned char int3_flag=0;
int main(void)
{
LPC_PINCON->PINSEL4 = 0x04000000; //P2.13 as EINT3
LPC_GPIO2->FIODIR = 0x00001000; //P2.12 is assigned output
LPC_GPIO2->FIOSET = 0x00001000; //Initiall LED is kept off
LPC_SC->EXTINT = 0x00000008;
//writing 1 cleares the interrupt, get set if there is interrupt
LPC_SC->EXTMODE = 0x00000008;
//EINT3 is initiated as edge senitive, 0 for level sensitive
LPC_SC->EXTPOLAR = 0x00000000;
//EINT3 is falling edge sensitive, 1 for rising edge
//above registers, bit0-EINT0, bit1-EINT1, bit2-EINT2,bit3-EINT3
NVIC_EnableIRQ(EINT3_IRQn);
//core_cm3.h
while(1) ;
}
void EINT3_IRQHandler(void)
{
LPC_SC->EXTINT = 0x00000008; //cleares the interrupt
Result: Thus the program to demonstrate the use of an external interrupt to toggle an LED
On/Off.
Ex.No:8 Program to display 0-F on seven segments display
#include <LPC17xx.h>
while(1)
{
LPC_GPIO0->FIOSET |= ALLDISP;
LPC_GPIO0->FIOCLR = 0x00000ff0; // clear the data lines to 7-
segment displays
LPC_GPIO0->FIOSET = Disp[Switchcount]; // get the 7-segment display
value from the array
for(j=0;j<3;j++)
for(delay=0;delay<30000;delay++); // 1s delay
Switchcount++;
if(Switchcount == 0x10) // 0 to F has been displayed ? go
back to 0
{
Switchcount = 0;
LPC_GPIO0->FIOCLR = 0x00180ff0;
}
}
}
Result: Thus the program to display 0-F is executed and the outputs are obsevered in the on
seven segments display.
Ex.No:9 Interface a simple Switch and display its status through
Relay, Buzzer and LED.
#include <LPC17xx.H>
int main(void)
{
LPC_PINCON->PINSEL1 = 0x00000000;
//P0.24,P0.25 GPIO
LPC_GPIO0->FIODIR = 0x03000000;
//P0.24 configured output for buzzer,P0.25 configured output for
Relay/Led
while(1)
{
if(!(LPC_GPIO2->FIOPIN & 0x00000800)) //Is
GP_SW(SW4) is pressed??
{
LPC_GPIO0->FIOSET = 0x03000000;
//relay on
}
else
{
LPC_GPIO0->FIOCLR = 0x03000000;
//relay off
}
}
}
//end int main(void)
Result: Thus the program to interface a simple Switch and display its status through Relay,
Buzzer and LED is executed.
#include <LPC17xx.h>
#include "SPI.h"
unsigned char spi_flag = 0, temp=0;
void SPI_Init(void)
{
LPC_PINCON->PINSEL0 |= 0xC0000000; //P0.15 as SCK
LPC_PINCON->PINSEL1 = 0x0000003C; //select MISO-P0.17,MOSI-P0.18
LPC_SPI->SPCCR = 0x1E; // SPI CLOCK SELECTED AS 100KHZ
LPC_SPI->SPCR = 0xA0; //8 bit data, actve high clk, master SPI mode,SPI Int enable
// Master mode and SCK line is active high
LPC_SPI->SPINT = 0x01; //clear the interrupt flag
NVIC_EnableIRQ(SPI_IRQn);
}
void SPI_IRQHandler(void)
{
spi_flag = 1;
temp = LPC_SPI->SPSR; // To clear SPIF bit we have to read status register.
temp = LPC_SPI->SPDR; // Then read the data register(optional)
LPC_SPI->SPINT = 0x01; // To clear the SPI interrupt
}