Arm7 Examples
Arm7 Examples
PROGRAM -1
This program blinks the LEDs continuously with a small delay. The LEDs are connected to the
Port1 pins P1.24 to P1.31 and the these pins are configured as General Purpose output pins.
#include<lpc2148.H>
void delay()
{
for(int i=0x00;i<=0xff;i++)
for(int j=0x00;j<=0xFf;j++) ;
}
void main()
{
PINSEL2 = 0X00000000;
IO1DIR = 0XFF000000;
while(1)
{
IO1SET=0XFF000000;
delay();
IO1CLR=0XFF000000;
delay() ;
}
}
//LPC2148 Header
// Delay program
PROGRAM 2
This program glows LEDs alternately by sending 55H and AAH through the port1 Pins.
# include <LPC214X.H>
void delay(void)
{
unsigned int i;
i=0xffffff;
while(i--);
}
int main(void)
{
PINSEL2=0x0000;
IODIR1 = 0XFF <<24 ;
2
//LPC2148 HEADER
// Delay Program
// Port 1 is I/O
// Port Pins P1.24 to P1.31 as Output Pins
while(1)
// Infinite loop
IOSET1=0X55<<25
delay()
IOSET1=0XAA<<24
delay ()
IOCLR1=0XAA<<24
}
}
below shows the interfacing of the Relay to ARM controller. When the input is
energized, the relay turns on and the '+' output is connected to +12v. When the relay is off, the '+'
output is connected to Ground. The '-' output is permanently wired to Ground.
The relay is interfaced to P0.30 Pin through an Opto-isolator. This opto-isolator protects the port
pin from damage due to any high currents .The opto-isolator consists of a pair of an LED and a
Photo transistor as shown in the diagram. The power transistor is used at the input. So, when the
input is high , the output of the transistor is LOW and the relay is in OFF state .Similarly when
we apply a low to the transistor ,the out put is high and the relay is ON.
Interfacing Circuit.
PROGRAM
The following program configures the P0.30 pin as an out port. When a low signal is sent
through this pin to the relay the relay is switched ON and when a high signal is sent the relay is
switched OFF.A constant delay is created between the two events and hence the relay switches
ON and OFF in regular intervals of time.
# include <LPC214X.H>
//LPC2148 HEADER
void DELAY(void)
{
unsigned int i;
// Delay function
i=0xffffff;
while(i--) ;
}
int main(void)
{
IODIR0=1<<30
// Main program
;
while(1)
//INFINITE LOOP
{
IOSET0=1<<30
DELAY()
//CALL DELAY
IOCLR0=1<<30
DELAY()
; // CALL DELAY
}
}
// SWITCH ON RELAY
// REPEAT LOOP
PROGRAM
This program first configures the ARM Port1 as a GPIO and also as an out port. The sequence
code is sent to the driver IC using these port pins. A suitable delay is incorporated between each
step rotation. By applying the code in the reverse order, the stepper motor can be rotated in the
anticlockwise direction.
# include <LPC214X.H>
// LPC2148 HEADER
void delay_ms()
; // Delay function
void main()
{
PINSEL2 = 0X00000000;
6
IO1DIR=0x000000F0 ;
while(1)
// Infinite Loop
{
IO1PIN = 0X00000090;
delay_ms() ;
// Call Delay
IO0PIN = 0X00000050 ;
delay_ms()
IO1PIN = 0X00000060 ;
delay_ms()
// Call Delay
// Send the code 3 for phase 3
// Call Delay
IO1PIN = 0X000000A0 ;
delay_ms()
}
}
void delay_ms()
{
int i,j
;
// Call Delay
for(i=0;i<0x0a;i++)
for (j=0;j<750;j++) ;
}
4. INTERFACING OF DAC-ARM LPC2148
A digital-to-analog converter is a device for converting a digital signal into to an analog signal
(current or voltage ). Digital-to-Analog Converters are the interface between the abstract digital
world and the analog real world. Simple switches, a network of resistors, current sources or
capacitors may be used to implement this conversion. A DAC inputs a binary number and
outputs an analog voltage or current signal.
The Microchip Technology Inc. MCP4921 is 2.7 5.5V, low-power, 12-Bit Digital-to-Analog
Converter (DAC) with SPI interface. The MCP4921 DACt provides high accuracy and low
noise performance for industrial applications where calibration or compensation of signals is
required.
With an SPI connection there is always one master device (usually a microcontroller) which
controls the peripheral devices. Typically there are three lines common to all the devices,
Master In Slave Out (MISO) - The Slave line for sending data to the master,
Master Out Slave In (MOSI) - The Master line for sending data to the peripherals,
Serial Clock (SCK) -
master, and
Slave Select pin - the pin on each device that the master can use to enable and disable specific
devices.
When a device's Slave Select pin is low, it communicates with the master. When it's high, it
ignores the master. In SPI, the clock signal is controlled by the master device LPC2148 . All
data is clocked in and out using this pin. These lines need to be connected to the relevant pins on
the LPC21xx processor. Any unused GIO pin can be used for CS, instead pull this pin high.
Conversion speed is the time it takes for the DAC to provide an analog output when the digital
input word is changed.
The MCP4291 DAC - SPI connections with LPC21xx have four I/O
lines (P0.4 P0.7) required. The analog output is generated by using these four lines.
PROGRAM
#include <LPC2148.H>
#include "SPIsw.h"
// 2148 Header
// Infinite Loop
PROGRAM
#include "lpc214x.h"
int main(void)
{
// Initialise ADC 0, Channel 3
adcInit0_3() ;
// Constantly read the results of ADC0.3
int results = 0;
while (1)
{
results = adcRead0_3();
10
}
}
void adcInit0_3(void)
{
SCB_PCONP |= SCB_PCONP_PCAD0;
AD0_CR = AD_CR_CLKS10
| AD_CR_PDN
| ((3 - 1) << AD_CR_CLKDIVSHIFT)
| AD_CR_SEL3;
}
int adcRead0_3(void)
// 10-bit precision
// Exit power-down mode
// 4.0MHz Clock (12.0MHz / 3)
// Use channel 3
// Select channel 3
lines(P1.16 to P1.22) and MSB module is interfaced to Port0 GPIO lines(Port0.16 to Port0.22) as
shown in the circuit diagram.
PROGRAM
#include<lpc21xx.h>
unsigned char seg[16] ={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,
0x03,0x46,0x21,0x06,0x0e};
unsigned char seg_val,seg_val1;
unsigned char count,count1;
unsigned long int var,var1;
void main(void)
12
PINSEL1=0X00000000;
IODIR0 = 0X00FF0000;
IODIR1 = 0X00FF0000;
for (count=0;count<=15;count++)
{
IOCLR1 = var;
seg_val = seg[count];
var = seg_val;
var = var<<16;
IOSET1 = var;
for(count1=0;count1<=15;count1++)
{
IOCLR0=var1;
seg_val1=seg[count1];
var1=seg_val1;
var1=var1<<16;
IOSET0=var1;
for(k=0;k<50000;k++);
}
13
// End main.
PROGRAM
#include <LPC21xx.H>
14
temp2=temp2<<16;
cmd_wrt();
delay(800);
temp1 = 0x28;
lcd_com();
delay(800);
temp1 = 0x0c;
lcd_com();
delay(800);
temp1 = 0x06;
lcd_com();
delay(500);
temp1 = 0x80;
lcd_com();
delay(800);
}
void delay(unsigned int j)
// Delay subroutine
{
unsigned int k;
for(k=0;k<j;k++);
}
void clr_disp(void)
// To clear LCD display
{
temp1 = 0x01;
lcd_com();
delay(320);
}
void ret_home(void)
// To return home
{
temp1 = 0x02;
lcd_com();
delay(320);
}
void lcd_com(void)
// Byte to nibble conversion of LCD command
{
temp2= temp1 & 0x00f0;
temp2=temp2<<16;
cmd_wrt();
temp2 = temp1 & 0x000f;
temp2 = temp2 << 20;
cmd_wrt();
}
-------------------xxxxxxx------------Acknowledgment: I thank all the people without whose contribution ,this class notes would
have not been possible ,especially Pantech Solutions website .
16