Professional Documents
Culture Documents
ΔΙΠΛΩΜΑΤΙΚΗ ΜΕ ΜΙΚΡΟΕΛΕΓΚΤΗ LPC2148
ΔΙΠΛΩΜΑΤΙΚΗ ΜΕ ΜΙΚΡΟΕΛΕΓΚΤΗ LPC2148
: .
...
, 2008
: .
...
28 2008.
............................
............................
............................
.
..
...
...
, 2008
...................................
.
...
Copyright , 2008
. All rights reserved.
, ,
, . ,
, ,
.
.
.
.
.
.
.
.
.
I2C.
.
Matlab.
,
.
.
, , ,
.
, , , ARM,
LPC2148, MMA7260.
Abstract
The purpose of this thesis is the study, design and implemenation of an
integrated system for measuring the accelerations produced by the foot
during the gait. With proper measurement processing we can identify the gait
events during a gait cycle and the number of footsteps.
The system consists of two printed circuit boards. The first one is the pcb with
the microcontroller and the power supply unit while in the second one the
accelerometer and the analog to digital converter take place. The
accelerometer has three outputs, whose voltages are proportional to the
acceleration based on the three orthogonal axes. The analog to digital
converter converts the signals from the accelerometer into digital values and
sends them to the microcontroller through the I2C bus. The microcontroller
stores these values in order to send them later to the main computer through
the serial port. The computer analyses the data and produces the results.
Matlab is used for processing the data. A function created for processing the
initial signals returns new ones,from which useful information can be easily
derived.
The system is small in size , easy to use, reliable, low power and very useful
for three axes acceleration measurements , not only on foot but on many
places of the body as well.
Keywords
Accelerometer,
MMA7260.
Gait,
Gait
Analysis,
ARM
Microcontroller,
LPC2148,
,
,
.
, ,
.
, ,
.
10
.........5
........9
.....11
1.
...................................................................15
2.
2.1
2.2
2.3
.....................17
..................................................................................17
................................................17
2.2.1 .........................................................17
2.2.2 ......20
2.2.3 .......... .......22
2.2.4 .................25
2.2.5 .....................................29
........................31
3.
...............33
3.1 ......................................................33
3.2 .................................33
3.2.1 ...............................................33
3.2.2 ...............................................35
3.2.3 .....................................38
3.2.4 ................44
4.
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
4.10
4.11
4.12
4.13
4.14
4.15
4.16
4.17
LPC2148.......................................55
........................................................................................55
(Pipeline)...........................................................59
(Registers)..........................................................59
....................................................60
...........................................................61
ARM7........................................................63
............................................................66
MAC..........................................................................67
Thumb..............................................................67
(Bus structure)...............................69
(Memory Map)..............................................71
..............................................72
(MAM)........................................73
(PLL)............................................75
.....................78
..................................................79
LPC2000........................................81
4.17.1 ........................81
4.17.2 ................................81
4.17.3 ..............................................................82
11
4.18
4.19
4.20
4.21
4.22
4.23
4.24
4.25
4.26
4.17.4 (FIQ)................................................83
4.17.5 (Vectored IRQ)........................84
4.17.6 ............................................86
/ .................................................87
................................................87
.......................................................91
UART................................93
I2C .........................................................................99
SPI.......................................................................108
...........................................110
......................112
..........................................................112
5.
5.1
5.2
......................................113
.............................................113
..............................................................115
6.
6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
.................................................................................121
..................................121
C.............................................123
...............................124
...........................................126
..........................................................129
.....................................................131
6.7.1 .............................................................................131
6.7.2 PLL.................................................................131
6.7.3 MAM...........................................................133
6.7.4 ..........................................................133
...........................................................134
7.
7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.8
7.9
7.10
7.11
...........................................................121
.............................................137
......................................................................................137
main.c...................................................................138
LPC214x.h............................................................142
armlibdefs.h..........................................................142
armlibtypes.h........................................................144
board.h.................................................................146
delays.c................................................................146
uart.h uart.c..................................................147
i2c.c i2c.h.....................................................150
max1237.h max1237.c.................................156
includes.h.............................................................157
8.
...........................................................159
12
9.
9.1
9.2
9.3
...............................................................161
.................................................................161
...................................164
Matlab....................................167
9.3.1 ..................................167
9.3.2 E gait1.m...........170
10.
E
.........................................................173
10.1
10.2
..............................................................173
................................................176
10.2.1 ...................................................176
10.2.2 ...................................................176
10.2.3 ................................................177
10.3 ..............................................................179
10.4 ..................................................................179
10.5 .............................................................180
10.6 .....................................................................189
10.7 ..........................................190
10.8 .......................191
10.8.1 .............................192
10.8.2 ..................................192
10.8.3 .................192
10.8.4 ............................192
10.8.5 ...........................192
10.8.6 .192
10.9 ...............................................................193
10.9.1 ...............................193
10.9.2 ...............................193
10.9.3 ..................194
10.10 ................................................................................203
11.
11.1
11.2
11.3
11.4
...............................................................205
.................................................................................205
..................................................................205
...........................................207
.....................................................207
12.
12.1
12.2
..209
.............................................................209
................................................209
...........................................................................................213
: .........................................215
: ...........................................219
13
14
,
, , , , ,
.
,
, , ,
.
,
.
:
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11) ( )
12)
:
1)
2)
,
, .
15
o
.
.
.
16
2.1
.
,
.
.
.
.
2.2
2.2.1
.
.
.
, (, , ,
, )
,
, .
.
.
, .
17
.
,
,
.
.
2-1:
,
.
2-2:
.
18
2-3:
.
2-4:
.
19
2-5: .
2.2.2
(.
) .
,
.
.
.
.
.
65%.
.
20
2-6: .
2-7:
.
2-8: .
21
2-9:
.
2.2.3
.
,
.
.
.
.
.
22
2-10:
.
2-11:
.
23
2-12:
.
2-13: .
24
2-14: .
2.2.4
.
(strain gauges).
.
.
.
.
, ,
.
, ,
.
.
.
,
,
.
.
.
25
2-15:
.
2-16:
.
26
2-17:
.
2-18:
.
27
2-19:
.
2-20:
.
.
.
.
28
2-21:
.
2.2.5
.
.
.
.
29
2-22:
.
2-23: .
30
2-24:
.
2.3
.
.
.
.
, .
.
.
.
,
.
31
.
.
.
.
.
, ..
. ,
,
, ,
.
.
32
3.1
. ( 3-1)
LPC2148 . ( 36) 7260
(ADC) 1237
I2C.
ADC 10 bit, ADC MAX1237
12 bit
,
.
.
3.2
3.2.1
ADC . LEDs
.
.
3-1.
33
P1
SDA0
4
3
2
1
SCL0
3.3V
Hi2c
GND
P4
16
12
8
4
48
44
40
36
P1.16
P1.17
P1.18
P1.19
P1.20
P1.21
P1.22
P1.23
32
28
24
64
60
56
52
20
3.3V
P0.8/TXD1
P0.9/RXD1
P0.10
P0.11
P0.12
P0.13
P0.14
P0.15
RTCX1
RTCX2
49
P0.16
P0.17/SCK1
P0.18/MISO1
P0.19/MOSI1
P0.20/SSEL1
P0.21
P0.22
P0.23
VBAT
23
43
51
VDD3-1
VDD3-2
VDD3-3
6
18
25
42
50
VSS1
VSS2
VSS3
VSS4
VSS5
GND
3.3V
P0.0/TXD0
P0.1/RXD0
P0.2/SCL0
P0.3/SDA0
P0.4/SCK0
P0.5/MISO0
P0.6/MOSI0
P0.7/SSEL0
P1.24
P1.25
P1.26
P1.27
P1.28
P1.29
P1.30
P1.31
3
5
GND
GND
U2
63
P0.25 (AOUT)
USBD+
USBDP0.28
P0.29
P0.30
P0.31
VREF
X1
X2
VDD3(A)
59
RST
VSSA
19
21
22
26
27
29
30
31
1
2
3
4
3.3V
TXD0
RXD0
SCL0
SDA0
33
34
35
37
38
39
41
45
Hserial
P8
txd1
rxd1
xbeesleep
3.3V
1
2
3
4
5
3.3V
1
2
Hxbee
bsl
GND
J3
1
6
5
4
Slide Switch
46
47
53
54
55
1
2
58
Rled1
D1
GND
3.3V
1K
LED2
9
10
11
13
14
15
17
Rled2
J5
3.3V
1K
1 2
switchRs
D2
LED2
button
GND
3.3V
10K
62
61
57
Yxtal
12MHz
LPC-2148
D3
GND
Cxtal1
39p
RST
Diode 1N4148
Rrst
3.3V
Cxtal2
39p
GND
10K
3.3V
J6
1 2
3.3V
RESET
Cvdd3a
100nF
VDD
Cvdd33
100nF
GND
Cvdd32
100nF
U4
MCP130T
GND
Cvdd31
100nF
button
GND
GND
3-1: .
LPC2148
3.3V decoupling
capacitors 100nF. 3.3V VBAT
real time clock (RTC)
.
LPC2148.
UART0,
34
.
LPC2148 reset pin41-P0.14 0Volt.
(bootloader)
Flash. .hex
UART0 LPC2000 Flash Utility.
UART0
(slide switch)
3.3V 0V P0.14 .
2 leds .
. button
switch 0V pull-up
, 3.3V.
leds
.
12MHz. LPC2148
PLL
60MHz. datasheet
LPC2148 .
reset
MCP130T reset
3V.
RAM Flash .
reset
. J6
reset D3
.
UART0, UART1 2C
.
3.2.2
3-2 .
1724 Step-Up DC-DC
Converter 3.3V
.
(Li+ cell), NiMH .
led
.
35
L1
Vin
Inductor
10mH
U6
1
C11
10uF
GND
- +
Vin
BATT
3.3V
SHDN
OUT
MAX-1724
1
2
1
GND
P3
P6
Header 5
LX
Vin
Header 2
C10
22uF
DS
LED2
J4
1
Vin
GND
RledS
1K
Slide Switch
GND
GND
GND
GND
3-2: .
1724 MAXIM ( 3-3) Step-Up DC-DC ,
, SOT23-5.
1.5 3.3V 0.9V.
.
3-3: 1724.
:
- 90%
- FET
-1.5
-1%
- 150mA
-0.8V 5.5V
-0.91V
-
-SOT23-5 (1.1mm )
36
pins :
Pin1 : BATT,
Pin2 : GND ,
Pin3 : SHDN, ,
,
Pin4 : OUT, , bootstrap
Pin5 : LX, P channel Mosfets.
MAX1724
3-4 3-5.
3-4: MAX1724.
37
3-5: MAX1724.
3.2.3
MMA7260 ADC MAX1237. To MMA7260
.
ADC . ADC
,
.
.
3-6.
38
Cmma7260
100nF
Rx 1K
3
2
3.3Vb
DIP
Switch4
3.3Vb
Rg1 10K
Xadc
Cx
100nF
U3
GNDb
MMA7260Q
2
1
NC
NC
NC
NC
NC
Xout
sleep
Yout
Zout
GNDb
16
Ry 1K
Yadc
15
Cy
100nF
14
13
GNDb
Rz 1K
12
9
10
11
NC
NC
NC
5
6
7
8
VDD
VSS
GNDb
gSel2
gSel1
Rg2 10K
Zadc
Cz
100nF
3.3Vb
GNDb
3.3Vb
U5
3.3Vb
Xadc
Yadc
Zadc
3
4
AIN0
VDD
AIN1
GND
AIN2
SDA
AIN3/REF
SCL
Crefmax
100n
Cmax1237
100nF
Rsda Rscl
1.8K 1.8K
7
6
5
Cmax1237b 3.3Vb
4.7uF
GNDb
SDA0b
SCL0b
MAX-1237
GNDb
P7
4
3
2
1
SCL0b
SDA0b
3.3Vb
Header 4
GNDb
3-6: .
MMA7260 Freescale
. QFN 16
3-7.
39
3-7: MMA7260.
:
- (1.5g/2g/4g/6g)
- (500)
- (sleep mode 3)
- 2.2V-3.6V
- 6mm x 6mm x 1.45mm QFN
- (800 mV/g @ 1.5g)
-
-
- ,
-
-
pins :
Pin 1: g-Select1,
Pin 2: g-Select2,
Pin 3: VDD,
Pin 4: VSS,
Pin 5-7:N/C,
Pin 8-11: /C,
Pin 12: Sleep Mode,
Pin 13: Zout,
Pin 14: Yout, Y
Pin 15: Xout,
Pin 16: N/C,
MMA7260 (g-cell)
.
.
40
.
( 3-8).
3-8: .
,
.
.
3-8. ,
C=A/D,
, D
.
.
.
g-Select1 gSelect2 1.5g, 2g,
4g 6g ( 3-a).
.
. g-Select1 g-Select2
1.5g
MMA7260 pull-down
.
41
3-a: MMA7260.
Sleep Mode ,
3.
.
3-9
3-10.
3-9: MMA7260.
42
3-10: MMA7260.
3-10 datasheet .
100nF
MMA7260, ADC ,
(ground plane) ,
RC
switched capacitor
ADC 11kHz,
,
aliasing. dip
.
3-11.
3-11: MMA7260.
43
(1g). 3-12
.
3-12: MMA7260.
3.2.4
MAX1237 MAXIM
(ADC). 12 bit , , 4 ,
I2C.
:
- I2C
400kHz Fast mode
1.7MHz High-Speed Mode
- 2.7V 3.6V
- : 2.048V
- : 1V VDD
-
-4 2
- FIFO Channel-Scan mode
-
670 94.4ksps
230 40ksps
60 10ksps
6 1ksps
44
3-13: MAX1237.
:
Pin 1: AIN0, 0
Pin 2: AIN1, 1
Pin 3: AIN2, 2
Pin 4: AIN3/REF, 3,
Pin 5: SCL, (I2C)
Pin 6: SDA, / (I2C)
Pin 7: GND,
Pin 8: VDD,
MAX1237 successive-approximation
track/hold
12 bit. ADC
(I2C) 1.7MHz.
MAX1237
, track-and-hold (T/H), / ,
(DAC).
0V VDD.
0V-0,3V VDD+0.3V
.
50mV VDD
0V .
45
3-14: MAX1237.
46
SCAN
0
CS3-CS0 .
2 CS1CS0. CS1-CS0 0-2,
2. /REF
AIN3.
CS3-CS0.
AIN0 CS3CS0. /REF ,
3.
3-c: .
47
SGL/DIF=1
SGL/DIF=0
3-d: .
(Single-ended/differential input).
SGL/DIF bit configuration byte
. SGL/DIF=1
CS[3:0] bit . SGL/DIF=0
+ -
CS[3:0] bit. ( 3-d).
(Unipolar/Bipolar).
, BIP/UNI bit Setup byte
.
0V
VREF ( 3-23).
.
VREF/2 +VREF/2 ( 3-24).
. ,
1237
BIP/UNI.
0V VREF.
(I2C).
MAX1237 I2C .
SDA master
slave 1.7MHz. To
48
49
3-18: Bit -.
master slave
START
slave . 1237
START .
MAX1237 .
MAX1237
( 3-e).
2C
MAX1237EUA
110100
MAX1237KEUA
110000
MAX1237LEUA
110010
MAX1237MEUA
110110
50
MAX1237
(Fast mode-F/S),
22.2ksps. To MAX1237
(High Speed mode-H/S)
94.4ksps.
MAX1237 H/S mode master byte 00001 ( =0
1). 1237 bit H/S . , master
START (Sr)
slave H/S
. An o master STOP , 1237
F/S mode. 3-20.
51
1237 .
, MAX1237 SCL
.
. 40ksps
94.4ksps .
40ksps
. SCL
60s .
SCAN0 SCAN1 configuration byte
. AIN/REF pin
(SEL=1), .
. bytes
. 3-21
3-22 .
3-21: .
3-22: .
52
3-23: .
3-24: .
53
1237
3-25. pull-up
1k. 100nF 4.7F VDD
100nF /REF .
MAX1237 .
.
3-25: MAX1237
.
54
LPC2148
4.1
LPC2148 NXP(Philips) 32/16 bit
ARM7TDMI-S CPU real-time emulation embedded trace support,
flash
512kB. 128-bit
32-bit
.
,
16-bit Thumb mode 30%
.
.
USB 2.0 , UART, SPI, SSP
I2C. SRAM 32kB (
), 32-bit ,
10-bit ADCs, 10-bit DAC, PWM 45
- (GPIO) 9 (interrupts)
. To block LPC2148 4-1.
:
32-bit 4 (capture)
4 (compare) ,
PWM (pulse-width modulation) 6 watchdog.
(RTC)
32kHz .
2 UART, 2 I2C
(400kbit/s), SPI, SSP .
(vectored interrupt controller)
.
45 - 5V.
9 , .
60MHz CPU
PLL 100s.
1MHz 30MHz
50Hz.
.
.
, USB, RTC(real time clock) BOD(Brown-out
detect).
(POR Power-on reset) BOD.
3.3V10% 5V / /.
56
4-1: LPC2148.
LPC2148
memory mapped.
.
57
4-2: LPC2148.
58
4.2 (Pipeline)
LPC2000
(pipeline).
. ARM7
. ,
.
.
ARM
.
4.3 (Registers)
ARM7
.
.
4-3: .
16 R0-R15.
32-bit. R0-R12
, R13-R15
CPU. O R13
-stack pointer (SP), R15 -program
counter (PC)
R14
-link register (LR). ,
.
.
(R13).
59
4-4: LPC2148.
4.4
(Current Program Status Register) CPSR.
bit-
CPU.
4-5: CPSR.
bit 31-28 CPSR N,Z,C,V. bit
, ,
. bit 7 8 F bits.
60
. bit 5 bit
ARM 32-bit
THUMB 16-bit .
4.5
ARM7 .
(user mode)
R0-R15 CPSR.
,
.
R0-R12 R15 R13(LR)
R14(SP) ,
.
.
(FIQ)
R7-R12.
. .
, SPSR (Saved Program Status Register).
CPSR
SPSR.
, CPSR
SPSR
.
4-6: ARM7
.
61
, CPU
(PC)
.
0 (reset vector)
bytes. 0x00000014
. ARM7
ARM. LPC2000
byte .
4-7: ARM7 .
4-8.
4-8: .
, (IRQ),
:
(PC+4) (LR).
CPSR SPSR
62
(. SPSR_irq). O PC
(
0x00000018).
IRQ R13
R14 IRQ R13 R14.
IRQ, I bit CPSR 1,
IRQ. (nested interrupts),
IRQ
LR
.
, ISR.
IRQ
R0-R12
.
.
,
. ARM
PC .
4.6 ARM7
7
C,
(assembly language)
. ARM7 . ARM
32-bit THUMB
16-bit.
ARM7 big-endian little-endian
. little-endian bit (MSB)
bit big-endian
. LPC2000 ARM7 NXP littleendian. compiler
little-endian
.
63
4-10: bits .
bit CPSR. ,
,
. assesmbly MOV ADD
16
.
64
4-11: .
:
EQMOV
R1, #0x00800000
0x00800000 R1
Z bit- CPSR.
(branch) (jump)
.
ARM :
(branching), (data processing),
(data transfer), (block transfer),
(multiply) (software interrupt).
CPSR SPSR
. ARM
. MSR MRS
CPSR SPSR
. IRQ
, CPSR
, I bit
CPSR .
65
4.7
(SWI)
,
(supervisor mode) PC
0x00000008. ARM, SWI
bits .
. .
bit.
,
bit .
SWI
,
.
4-13: SWI.
66
4.8 MAC
O ARM7 (Multiply
Accumulate Unit) MAC.
(integer) (long interger) .
, 32-bit
32-bit (modulo32).
.
32-bit
32-bit .
.
4.9 Thumb
ARM7 32-bit ,
16-bit THUMB
ARM .
16-bit , 32-bit ARM
. THUMB
ARM, .
.
THUMB
30% ARM, 40%
.
4-14: THUMB.
THUMB
. (
)
. THUMB
.
R0-R7.
67
4-15: .
R8-R12 : MOV,ADD
CMP. CPSR SPSR
ARM .
BX BLX. (reset)
ARM .
4-16: ARM .
68
69
60MHz, o VPB
AHB. VPB
AHB
14
CPU.
Flash RAM CPU.
4-18: ARM7 .
70
4-19: LPC2000.
Flash 0x00000000
RAM 0x40000000 .
LPC2000
Flash Bootloader RM real time monitor .
0x7FFFFFFF
0x80000000. 0x80000000 0xE0000000
( LPC22xx ).
71
4-20: .
VPB,
0xE0000000 - 0xE0200000
16K.
0xFFFFF000.
, CPU
(abort exception).
4.12
(Special function register) SFR
. Set
bits, Clear bits
Status SFR.
72
4-21: .
4.13 (MAM)
( Memory acceleration module) MAM
LPC2100.
Flash ARM7 CPU.
4-22:
Flash.
73
ARM7 CPU
Flash. H ARM7 CPU
80MHz Flash 50ns.
, Flash
CPU 20MHz.
.
RAM .
RAM Flash
.
RAM Flash
. cashe
ARM7
ARM7. MAM
Flash
.
4-23: MAM.
cashe, MAM ARM
. Flash
128 bits
. Flash
4 ARM 8 THUMB.
Flash,
,
Flash MAM 128 bits
.
128 bit
. ARM
74
. MAM
.
MAM
, .
MAM.
Flash. bits
MAM Flash. Flash
20MHz 60MHz,
Flash .
MAM
. MAM.
4-24: MAM.
,
Flash.
,
Flash.
Flash
.
4.14 (PLL)
(Phased Locked Loop) PLL
10MHz 25MHz
60MHz
.
ARM7.
LPC2000
75
(EMC). PLL
. PLL
,
.
P=2.
76
4-26: PLL.
SFR
PLL
PLL.
PLLCON PLLCFG 0x000000AA
0x00000055 PLLFEED .
.
PLLFEED
. PLL
P PLLCFG .
PLLCON PLL . PLL
.
LOCK bit PLLSTATUS
. LOCK bit 1, PLL
. PLLC bit
PLLCON .
77
4-27: PLL.
PLLCFG P-1
M-1.
power-down, PLL .
PLL
.
4.15
PLL Cclk
AHB .
, VPB.
78
4-28:
.
VPB Pclk Cclk VPB
. VPB
Cclk 1, 2 4. VPB
.
4, Pclk
Cclk. LPC2000
60MHz
.
4.16
.
LPC2000 .
LPC2000 .
PLL VPB
. LPC2000
. ARM7
(power down modes)
bits PCON .
(idle mode)
.
.
ARM7 (power
down mode)
.
pins
.
SRAM
,
79
/ .
PLL .
LPC2000
. ,
.
, 4096
Flash
.
.
, bit (power
control bit) PCONP .
: (watchdog timer),
/ (GPIO), pin connect block system control
block.
4-29: LPC2148
80
4.17 LPC2000
4.17.1
pins / LPC2000
(pin select block).
pin /
. pins
/ / GPIO.
PINSEL .
4-30: .
4.17.2
. EXTMODE
(level
sensitive) (edge sensitive).
, EXPOL
.
.
, EXTWAKE
.
81
4-31:
.
4.17.3
ARM7 ,
(Fast interrupt request) FIQ
(General purpose interrupt request) IRQ.
,
,
.
IRQ .
OR-.
,
.
. ARM7
(Vector Interrupt Controller) VIC.
82
4-32: VIC.
O VIC
.
VIC .
. VIC
FIQ ,
IRQ (vectored interrupt) IRQ (non
vectored interrupt).
. H FIQ ,
.
4.17.4 (FIQ)
FIQ.
VIC (VIC interrupt select register)
VICIntSelect bit . 1 bit
FIQ .
. bits VICIntSelect,
FIQ .
FIQ (VIC FIQ Status register)
VICFIQStatus
. FIQ
,
. FIQ ,
(VIC Interrupt Enable register).
.
FIQ ,
FIQ FIQ
0x0000001C.
.
83
,
bit (interrupt status flag)
. 1
.
4-33: .
:
bit . ,
VIC
bit .
,
.
84
C
.
4-34: .
PC
.
.
VIC
.
4-35: .
85
4.17.6
VIC 16 IRQ
FIQ . 17
.
(ISR).
(default vector address
register). FIQ
IRQ .
,
.
IRQ (IRQ Status register)
.
VIC.
4-36:
.
VIC
(User mode). VIC,
. IRQ,
FIQ SWI .
86
4.18 /
/ .
/ . IODIR
bit
(0) (1).
IOSET IOCLR
. 1 bit 1 0
. /
IOPIN .
4-37: / .
4.19
LPC2148 .
32-bit (timer counters) 32-bit
(prescalers).
VLSI Pclk.
87
4-38: .
.
Pclk
.
.
(Timer control register) bits
/
().
4-39: .
,
(capture channels).
.
pin connect block. O (Capture Control
register) ,
, .
,
, .
88
(match channels).
(match register)
32-bit.
.
, .
(, ),
1, 0 .
4-40: .
,
.
(match control register).
bits
:
(interrupt) ,
.
.
pin
connect block.
4-41: .
89
4-42: .
90
4.20
LPC2xxx (Real Time
Clock) RTC
2099. RTC
Pclk .
.
, RTC (alarm registers)
,
.
4-43: .
RTC 32.7kHz.
Pclk
Pclk.
PREINT PREFRAC
.
91
4-44: .
.
. : (DOW), ,
,
, , , (DOM)
.
4-45: RTC.
RTC (interrupt)
.
.
.
- .
( Alarm mask
register),
.
2099
.
92
RTC ,
bit-
Interrupt Location .
.
4-46: .
4.21 UART
LPC2xxx UARTs.
UART1
modem. (baud rate)
FIFOs 16 byte.
93
4-47: UART.
UART Rx
Tx GPIO. UART (UART Line
Control register) UxLCR
UART stop-bit, no-parity, 8bits
.
4-48: .
Pckl
(Divisor Latch registers) UxDLL UxDLM.
bit7 DLAB
UxLCR 1.
(Fractional Divider register) UxFDR
94
DIVADDVAL
MULVAL. :
4-49: UART.
UART,
(Transmit Holding register) UxTHR
byte FIFO .
(Receive Buffer register) UxRBR
byte FIFO . DLAB bit UxLCR
0 .
UxRBR
UxTHR
UxLSR
UxRBR UxTHR
.
95
4-50: .
UART VIC
.
.
UxLSR
.
FIFO .
4-51: byte
RBR.
1,
4, 8 14 bytes. bytes
.
.
.
96
4-52:
.
97
4-53: UART0.
98
4.22 I2C
I2C
Philips
, , .. I2C
(master) (slave)
400bps. SDA SCL I2C
I2C
pin select block.
4-54: I2C.
99
4-55: I2C.
To LPC2148 I2C 2C0 I2C1
. H I2C .
I2CxCONSET
1 1 bit
I2CxCONCLR 1 0
bit .
I2CxSCLH I2CxSCLL Pclk :
I2CxSTAT
.
I2CxDAT byte
byte .
slave
(I2C slave address register) I2CxADR.
o I2C
.
(interrupt).
VIC
I2C.
I2C GPIO SDA SCL pin
select block.
I2CSCLH I2CSCLL.
100
I2C ,
I2C .
.
4-56: .
master
bit (enable) bit
(acknowledge) 0 .
I2C master
slave. master
. I2C
.
101
102
103
bytes slave,
, I2C,
.
.
105
4-63: .
106
4-64: I2C .
107
4.23 SPI
H SPI bytes
SPI .
.
4-65: SPI.
SPI .
(SCK), master in / slave out (MISO), master out / slave
in (MOSI) (CS). H
. SPI master
CS
/ GPIO.
0 , o slave
. SPI slave
CS 0 . MISO MOSI
master slave.
H SPI
. (Clock Counter register)
S0SPCCR Pclk
.
. SPI
S0SPCR. bit enable
CPHA CPOL bits
. SPI. To
MSTR bit master slave. To LSBF bit
MSB LSB byte. SPIE
SPI . o BITS
bits .
108
SPI
SPI,
byte
S0SPDR. SPIF bit
S0SPSR
byte
. SPI
.
4-66: SPI.
4-67: byte .
109
4-68: SPI .
4.24
ADC LPC2000 10bit,
,
2.44sec 410ksps.
6 8 (ADC0 ADC1).
110
4-69: ADC.
O ADCR ADC
. ,
Pclk 4.5z
.
4-70: ADCR.
Pclk CLKDIV . PDN bit
ADC ADC
. ADC GPIO
ADC.
H CLKS
10bits 3bits. H
. 10bit 11
3bit 4 .
ADC. (hardware)
ADC.
.
ADDRx.
4-71: ADDR.
DONE bit
.
111
RESULT
(V/Vdda). To
OVERUN bit
.
(software). ,
SEL bits
0x01 START. ADC
ADDR
.
DONE bit ADDR.
.
4.25
LPC2148 (Digital to
Analog Converter) DAC 10bits. H
DACR. DACR VALUE 10bits
BIAS bit.
4-72: DACR.
P0.24 AOUT
DAC.
VALUE
DACR. bit BIAS. A
1 2.5s
700. 0
1s 350A.
.
AOUT OUT= (VALUE/1024)xVREF. To VREF
.
VDDA VSSA .
4.26
LPC2148
USB
, PWM (Pulse Width Modulator)
led
(watchdog timer)
.
112
5.1
.
.
5-1:
.
113
5-2:
.
5-3:
.
114
5.2
.
.
.
5-4:
.
5-5: .
115
5-6: .
5-7: .
116
5-8:
.
5-9:
.
117
5-10: .
5-11: .
118
5-12: MAX3232.
5-13: MAX3232.
119
5-14: MAX3232.
5-15:
.
120
6.1
C
(assembly)
(startup
code). ,
,
(memory map)
Flash SRAM
.
6.2
.
1. (Basic startup assembler)
/ ex1 . s /
/
Exception vect ors
/
. text
. arm
. global s t a r t
start:
/ Vectors (8 t o t a l ) /
b r e s e t / r e s e t /
b loop / undef ined i n s t ruc t ion /
b loop / sof tware int e r r upt /
b loop / p r e f e tch abort /
b loop / data abor t /
nop / reserved for the bootloader checksum /
b loop / IRQ /
b loop / FIQ /
/
121
Test code
/
reset:
l d r r0 , IODIR1
l d r r1 , IODIR1 VALUE
s t r r1 , [ r0 ]
l d r r0 , IOCLR1
s t r r1 , [ r0 ]
l d r r0 , IOSET1
l d r r1 , IOSET1 VALUE
s t r r1 , [ r0 ]
loop : b loop
/
Constants
/
/ LED cont r o l r e g i s t e r s /
IOSET1 : . word 0xE0028014
IODIR1 : . word 0xE0028018
IOCLR1: . word 0xE002801C
IODIR1 VALUE: . word 0x00FF0000
IOSET1 VALUE: . word 0x00550000
. end
leds,
.
_start. (exception
vectors) bytes : (reset),
(undefined instruction),
(software interrupt), (prefetch abort),
(data abort),
bootloader, (IRQ) (FIQ).
. ,
reset.
reset (b reset).
(loop: b loop).
reset r0
IODIR1 r1
r0 r1.
.
IOCLR1 IOSET1.
, ( IOSET1)
.
C (. )
122
. LPC2148
LPC214X.h.
.hex
.
6.3 C
2. C.
/ e x2 s t a r t . s /
. text
. arm
. global main
. global _start
start :
/ Vectors (8 t o t a l ) /
ldr pc , main_addr / r e s e t /
ldr pc , loop_addr / undefined ins t ruc t ion /
ldr pc , loop_addr / sof tware int e r r upt /
ldr pc , loop_addr / p r e f e tch abort /
ldr pc , loop_addr / data abor t /
nop / res erved for the boot ldr checksum /
ldr pc , loop_addr / IRQ /
ldr pc , loop_addr / FIQ /
loop addr : .word loop
main addr : . word main
loop : b loop
. end
/ ex2 main . c /
#define IOSET1 ( ( ( volatile unsigned long ) 0xE0028014) )
#define IODIR1 ( ( ( volatile unsigned long ) 0xE0028018) )
#define IOCLR1 ( ( ( volatile unsigned long ) 0xE002801C) )
int main (void)
{
/ Define the LED pins P1 . [ 1 6 . . 2 3 ] as output /
IODIR1 = 0x00FF0000 ;
/ Clear a l l pins /
IOCLR1 = 0x00FF0000 ;
/ LED[ 7 : 0 ] ; even on ( high ) , odd o f f ( low ) /
IOSET1 = 0x00550000 ;
while ( 1 ) ;
return 0 ;
}
assembly
reset
C .
123
1
,
PC .
C
(. IOSET1). main() P1[16..23]
IODIR1. ,
high low
IOSET1 IOCLR1. while(1)
.
6.4
,
.
.
3. C .
/ e x3 s t a r t . s /
. text
. arm
. global main
. global s t a r t
start:
/ Vectors (8 t o t a l ) /
b r e s e t / r e s e t /
b loop / undef ined i n s t ruc t ion /
b loop / sof tware int e r r upt /
b loop / p r e f e tch abort /
b loop / data abor t /
nop / reserved for the bootloader checksum /
b loop / IRQ /
b loop / FIQ /
/ Setup the stack pointer and then jump to main /
reset:
l d r sp , stack addr
bl main
/ Catch return from main /
loop : b loop
/ Constants /
/ LPC SRAM s t a r t s at 0x40000000 , and ther e i s 32Kb = 8000h /
stack addr : . word 0x40008000
. end
/ ex3a main . c /
#include led .h
int main (void)
{ledinit();
led (0 x55 ) ;
124
while ( 1 ) ;
return 0 ;
}
/ l e d . h /
#ifndef LED H
#define LED H
/ I n i t i a l i z e the LEDs /
void l ed i n i t ( ) ;
/ Control the LEDs /
void led ( unsigned long val ) ;
/ Set LEDs /
void l e d s e t ( unsigned long s e t ) ;
/ Clear LEDs /
void l e d c l r (unsigned long c l r ) ;
#endif
/ l e d . c /
#include led .h
#define IOSET1 ( ( ( volatile unsigned long ) 0xE0028014) )
#define IODIR1 ( ( ( volatile unsigned long ) 0xE0028018) )
#define IOCLR1 ( ( ( volatile unsigned long ) 0xE002801C) )
void l ed i n i t ()
{ / Define the LED pins P1 . [ 1 6 . . 2 3 ] as output /
IODIR1 = 0x00FF0000 ;
/ Clear a l l pins /
IOCLR1 = 0x00FF0000 ;
}
void led ( unsigned long val )
{ / LEDs o f f /
IOCLR1 = ( va l & 0xFF) << 16;
/ LEDs on /
IOSET1 = ( va l & 0xFF) << 16;
}
void l e d s e t ( unsigned long s e t )
{ IOSET1 = ( s e t & 0xFF) << 16;
}
void l e d c l r (unsigned long c l r )
{ IOCLR1 = ( c l r & 0xFF) << 16;
}
125
6.5
O LPC2148 512kB Flash 32kB SRAM.
Flash ,
SRAM. .text
(read only variables)
.rodata .
.data
.bss. .data
SRAM, Flash
SRAM. O
(startup code) Flash
SRAM .data
.
SRAM .bss .
.
,
Flash SRAM,
.data .bss .
.
126
127
128
(.text) (.rodata)
Flash. (.data)
SRAM _data _edata,
Flash .text .rodata _etext.
.
6.6
.
Flash SRAM. H
SRAM. To .bss .
.bss
(_bss)
(_ebss) .
SRAM _bss
_ebss. O
:
/ e x4 s t a r t . s /
. global main
. global s t a r t
/ Symbols defined by the l ink e r s c r i p t /
. global etext
. global data
. global edata
. global bss
. global ebss
. text
. arm
start:
/ Vectors (8 t o t a l ) /
b r e s e t / r e s e t /
129
leds :
/ ex4b main . c /
#include led .h
static int l e d value [ 2 ] = {0x55 , 0xAA} ;
int main (void)
{
130
int i ;
ledinit();
while (1) { led ( led value [ 0 ] ) ;
for ( i = 0; i < 0x50000 ; i++);
led ( led value [ 1 ] ) ;
for ( i = 0; i < 0x50000 ; i++);
}
return 0 ;
}
6.7
6.7.1
LPC21xx
:
1)
2)
3)
4)
PLL
MAM
5) .data SRAM
6) .bss
7) main()
.
PLL MAM .
6.7.2 PLL
PLL ,
.
C
main(). C PLL :
#define PLLCON (*(volatile unsigned int *)0xE01FC080)
#define PLLCFG (*(volatile unsigned int *)0xE01FC084)
#define PLLSTAT (*(volatile unsigned int *)0xE01FC088)
#define PLLFEED (*(volatile unsigned int *)0xE01FC08C)
#define PLLCON_PLLE (1 << 0)
#define PLLCON_PLLC (1 << 1)
#define PLLSTAT_PLOCK (1 << 10)
#define PLLFEED1 0xAA
#define PLLFEED2 0x55
#define PLLCFG_VALUE 0x24
void pll_init(void)
{
PLLCFG = PLLCFG_VALUE;
PLLCON = PLLCON_PLLE;
131
PLLFEED = PLLFEED1;
PLLFEED = PLLFEED2;
while ((PLLSTAT & PLLSTAT_PLOCK) == 0);
PLLCON = PLLCON_PLLC|PLLCON_PLLE;
PLLFEED = PLLFEED1;
PLLFEED = PLLFEED2;
}
PLL :
/* Constants (and storage, used in ldr statements) */
PLLBASE: .word 0xE01FC080
/* Constants (used as immediate values) */
.equ PLLCON_OFFSET, 0x0
.equ PLLCFG_OFFSET, 0x4
.equ PLLSTAT_OFFSET, 0x8
.equ PLLFEED_OFFSET, 0xC
.equ PLLCON_PLLE, (1 << 0)
.equ PLLCON_PLLC, (1 << 1)
.equ PLLSTAT_PLOCK, (1 << 10)
.equ PLLFEED1, 0xAA
.equ PLLFEED2, 0x55
.equ PLLCFG_VALUE, 0x24
pll_init:
/* Use r0 for indirect addressing */
ldr r0, PLLBASE
/* PLLCFG = PLLCFG_VALUE */
mov r3, #PLLCFG_VALUE
str r3, [r0, #PLLCFG_OFFSET]
/* PLLCON = PLLCON_PLLE */
mov r3, #PLLCON_PLLE
str r3, [r0, #PLLCON_OFFSET]
/* PLLFEED = PLLFEED1, PLLFEED2 */
mov r1, #PLLFEED1
mov r2, #PLLFEED2
str r1, [r0, #PLLFEED_OFFSET]
str r2, [r0, #PLLFEED_OFFSET]
/* while ((PLLSTAT & PLLSTAT_PLOCK) == 0); */
pll_loop:
ldr r3, [r0, #PLLSTAT_OFFSET]
tst r3, #PLLSTAT_PLOCK
beq pll_loop
/* PLLCON = PLLCON_PLLC|PLLCON_PLLE */
mov r3, #PLLCON_PLLC|PLLCON_PLLE
str r3, [r0, #PLLCON_OFFSET]
/* PLLFEED = PLLFEED1, PLLFEED2 */
str r1, [r0, #PLLFEED_OFFSET]
str r2, [r0, #PLLFEED_OFFSET]
132
6.7.3 MAM
MAM :
/* Constants (and storage, used in ldr statements) */
MAMBASE: .word 0xE01FC000
/* Constants (used as immediate values) */
.equ MAMCR_OFFSET, 0x0
.equ MAMTIM_OFFSET, 0x4
.equ MAMCR_VALUE, 0x2 /* fully enabled */
.equ MAMTIM_VALUE, 0x4 /* fetch cycles */
mam_init:
/* Use r0 for indirect addressing */
ldr r0, MAMBASE
/* MAMCR = MAMCR_VALUE */
mov r1, #MAMCR_VALUE
str r1, [r0, #MAMCR_OFFSET]
/* MAMTIM = MAMTIM_VALUE */
mov r1, #MAMTIM_VALUE
str r1, [r0, #MAMTIM_OFFSET]
6.7.4
ARM
. /, , IRQ, FIQ,
. ARM
.
CPSR
.
. , LR
SPSR ,
.
ARM
,
.
, , IRQ FIQ
.
:
/* Constants (and storage, used in ldr statements) */
STACK_START: .word 0x40008000
/* Constants (used as immediate values) */
/* Processor modes (see pA2-11 ARM-ARM) */
.equ FIQ_MODE, 0x11
.equ IRQ_MODE, 0x12
.equ SVC_MODE, 0x13 /* reset mode */
.equ ABT_MODE, 0x17
.equ UND_MODE, 0x1B
133
.
(SWI)
.
LR .
6.8
ARM
. :
1) (FIQ)
2) (IRQ)
3) ,
4)
134
5) (SWI),
SP (R13)
LR (R14). FIQ
(R8-R12)
.
LR
. SPSR CPSR
.
:
sub lr, lr, #4
stmfd sp!, {<other_registers>, lr}
... interrupt handler ...
ldmfd sp!, {<other_registers>, pc}^
(exception handler)
, C
.
GCC non-ANSI
C .
IRQ :
void irq_handler(void) __attribute__ ((interrupt("IRQ")));
IRQ,
VicVectAddr
VIC.
VIC. FIQ
FIQ (fiq_handler) FIQ
.
135
136
7.1
Programmers Notepad
WinARM.
:
1) main.c
2) lpc214x.h
3) armlibdefs.h
4) armlibtypes.h
5) board.h
6) delays.c
7) delays.h
8) i2c.c
9) i2c.h
10) max1237.c
11) max1237.h
12) uart.c
13) uart.h
14) rprintf.c
15) rprintf.h
16) includes.h
17) boot.s
18) Makefile
19) lpc2138-rom.ld
main.c .
.
. To armlibdefs.h
bits. armlibtypes.h
. board.h
leds
. delays.h delays.c
. i2c.h i2c.c
i2c . max1237.h
max1237.c max1237.
uart.h uart.c rprintf
137
.
includes.h header (.h)
. boot.s
lpc2138-rom.ld
. Makefile
.
7.2 main.c
main.c .
boot.s main.c.
:
//#################################################################################
//############################ ARM ACCELEROMETER ##############################
//#################################################################################
/* includes */
#include <stdlib.h>
#include "includes.h"
#define NSAMPLES 5000
#define T100NSEC 50
//T=T100NSEC*100nsec
u08 adc_data[10];
u16 acc_data[3];
u16 data[3][NSAMPLES];
volatile u32 timer1_counter;
/* General purpose timers */
volatile u32
timer_1sec;
volatile u32
timer_10ms;
// Configures the processor
static void SetupHardware( void );
// Timer 1 functions
void irq_timer1_handler(void) __attribute__ ((interrupt("IRQ")));
void init_timer1(void);
void stop_timer1(void);
void start_timer1(void);
//#################################################################################
//################################# MAIN ##########################################
//#################################################################################
int main( void )
{
/* Setup the hardware */
SetupHardware();
init_UART0(9600);
rprintf_devopen( sendc_UART0 );
rprintf("\nrprintf: %s", "rprintf init ok.\n");
IODIR0 = LED_1 | LED_2 ;
timer1_counter =0;
init_timer1();
stop_timer1();
asm volatile ("msr CPSR_c,#0x13 | 0x40");
//enable IRQ's
i2cInit();
138
max1237_init();
LEDS_OFF;
while(1)
{
while(!BUTTON_1_PRESSED);
delay(20000);
while(BUTTON_1_PRESSED);
delay(10000000);
LED_1_ON;
rprintf("Button pressed\n");
for(u32 i=0;i<NSAMPLES;i++)
//pairnei ta samples
{
if(max1237_read(adc_data)==I2C_OK)
{
acc_data[0]= (0x0f&adc_data[0])*256 + adc_data[1];
acc_data[1]= (0x0f&adc_data[2])*256 + adc_data[3];
acc_data[2]= (0x0f&adc_data[4])*256 + adc_data[5];
data[0][i] = acc_data[0];
data[1][i] = acc_data[1];
data[2][i] = acc_data[2];
}
timer1_counter = 0;
start_timer1();
do{
;
}while(timer1_counter!= T100NSEC);
stop_timer1();
}
rprintf("samples collected\n");
LED_2_ON;
while(!BUTTON_1_PRESSED);
delay(20000);
while(BUTTON_1_PRESSED);
delay(10000000);
LED_1_OFF;
rprintf("button pressed again\n");
for(u32 j=0;j<NSAMPLES;j++)
//stelnei ta samples
{
rprintf("%d %d %d %d\n",j,data[0][j],data[1][j],data[2][j]);
delay(50000);
if(j%1000 == 0)
delay(500000);
}
LED_2_OFF;
//svinei kai to LED_2
rprintf("data sent\n");
} //end while(1)
while(1);
return 0;
}
//#################################################################################
static void SetupHardware( void )
{
/* Configure the RS2332 pins. All other pins remain at their default of 0. */
PINSEL0 |= 0x0001;
PINSEL0 |= 0x0004;
/* Setup the PLL to multiply the XTAL input by 4. */
PLLCFG = 4 | (0<<6) | (1<<5);
139
/* Activate the PLL by turning it on then feeding the correct sequence of bytes. */
PLLCON = 0x0001;
PLLFEED = 0xaa;
PLLFEED = 0x55 ;
/* Wait for the PLL to lock... */
while( !( PLLSTAT & 0x0400 ) );
/* ...before connecting it using the feed sequence again. */
PLLCON = 0x0003;
PLLFEED = 0xaa;
PLLFEED = 0x55 ;
/* Setup and turn on the MAM. Three cycle access is used due to the fast
PLL used. It is possible faster overall performance could be obtained by
tuning the MAM and PLL settings. */
MAMTIM = 0x03;
MAMCR = 0x02;
/* Setup the peripheral bus to be the same as the PLL output. */
VPBDIV = 0x01;
}
//#################################################################################
void init_timer1(void)
{
T1TCR = BIT(1);
//counter disabled and reset
T1CTCR = 0x00;
//timer mode : every PCLK edge
T1PR = 0;
//counter counts every PCLK edge
T1PC = 0;
T1MR0 = 6000;
// 100nsec
T1MCR = BIT(0) | BIT(1) ; //interrupt and TC reset when match
T1TCR = BIT(0); //counter1 enable
VICIntSelect &= ~BIT(5) ; //timer1 Vectored IRQ
VICVectCntl1 = 0x20 | 5 ;
VICVectAddr1 = (DWORD)irq_timer1_handler;
VICIntEnable = BIT(5) ;
}
//#################################################################################
void irq_timer1_handler (void)
{
timer1_counter++;
T1IR = BIT(0);//T1IR;
// Clear interrupt flag
VICVectAddr = 0;
// Acknowledge Interrupt
}
//#################################################################################
void stop_timer1(void)
{
VICIntEnClr = BIT(5); //disable timer1 interrupt
T1TCR = BIT(1);
// disable timer1 counting
}
//#################################################################################
void start_timer1(void)
{
VICIntEnable = BIT(5) ;
T1TCR = BIT(0) | BIT(1) ; //timer1 reset and enabled
T1TCR = BIT(0);
}
main.c
.
.
140
main.c.
main() SetupHardware().
. PLL
12MHz
60MHz.
MAM Flash.
(VPBDIV=1).
SetupHardware() UART0.
init_UART0() .
9600
9600kbps. H rprintf_devopen()
rprintf() .
sendc_UART0 rprintf()
rprintf. H IODIR0 = LED_1 | LED_2 ;
LEDS .
timer1
IRQ. H
init_timer1().
60MHz.
6000 ( 100nsec)
timer1. H
. irq_timer1_handler()
timer1_counter .
100nsec.
asm volatile ("msr CPSR_c,#0x13 | 0x40");
CPSR C.
H i2cInit() I2C
.
I2C max1237_init()
max1237
. leds
.
, led1
rprintf(). NSAMPLES
max1237_read(). 5msec
(100NSEC*100nsec)
200Hz .
141
, led2
. led1
.
, led2 .
7.3 LPC214x.h
LPC214x.h
LPC2148
.
, .
:
/* Pin Connect Block */
#define PINSEL_BASE_ADDR
0xE002C000
#define PINSEL0
(*(volatile unsigned long *)(PINSEL_BASE_ADDR + 0x00))
#define PINSEL1
(*(volatile unsigned long *)(PINSEL_BASE_ADDR + 0x04))
#define PINSEL2
(*(volatile unsigned long *)(PINSEL_BASE_ADDR + 0x14))
/* General Purpose Input/Output (GPIO) */
#define GPIO_BASE_ADDR
0xE0028000
#define IOPIN0
(*(volatile unsigned long *)(GPIO_BASE_ADDR + 0x00))
#define IOSET0
(*(volatile unsigned long *)(GPIO_BASE_ADDR + 0x04))
#define IODIR0
(*(volatile unsigned long *)(GPIO_BASE_ADDR + 0x08))
#define IOCLR0
(*(volatile unsigned long *)(GPIO_BASE_ADDR + 0x0C))
#define IOPIN1
(*(volatile unsigned long *)(GPIO_BASE_ADDR + 0x10))
#define IOSET1
(*(volatile unsigned long *)(GPIO_BASE_ADDR + 0x14))
#define IODIR1
(*(volatile unsigned long *)(GPIO_BASE_ADDR + 0x18))
#define IOCLR1
(*(volatile unsigned long *)(GPIO_BASE_ADDR + 0x1C))
Pin Select Block / GPIO.
.
datasheet .
(. IODIR0)
(. 0xE0028008). ,
volatile,
.
7.4 armlibdefs.h
armlibdefs.h
.
142
#ifndef ARMLIBDEFS_H
#define ARMLIBDEFS_H
// MIN/MAX/ABS macros
#define MIN_VAL(a,b)
#define MAX_VAL(a,b)
#define ABS(x)
// constants
#define PI
(((a)<(b))?(a):(b))
(((a)>(b))?(a):(b))
(((x)>0)?(x):(-x))
3.14159265359
(1 << (n))
(1 << (n))
(num|=(1<<bitloc))
(num&=~(1<<bitloc))
((num&=(1<<bitloc))>>bitloc)
#define P24
#define P25
#define P26
#define P27
#define P28
#define P29
#define P30
#define P31
BIT(24)
BIT(25)
BIT(26)
BIT(27)
BIT(28)
BIT(29)
BIT(30)
BIT(31)
// P0.24
// P0.25
// P0.26
// P0.27
// P0.28
// P0.29
// P0.30
// P0.31
#endif
MIN_VAL(), MAX_VAL() ABS()
.
BIT(n)
n .
bits .
bits 3 7 reg
reg | = BIT(3) | BIT(7). bit 4
reg reg &= ~BIT(4). bits
sbi(num,bitloc),
cbi(num,bitloc). P00 P31
.
7.5 armlibtypes.h
armlibtypes.h
. :
#ifndef ARMLIBTYPES_H
#define ARMLIBTYPES_H
#define FALSE
#define TRUE
#define OK
#define ERROR
0
-1
0
1
u08;
s08;
u16;
s16;
u32;
s32;
u64;
s64;
144
BOOL;
BYTE;
WORD;
DWORD;
UCHAR;
UINT;
USHORT;
ULONG;
typedef char
typedef int
typedef long
CHAR;
INT;
LONG;
s08 (signed). bits
unsigned long
.
7.6 board.h
board.h (hardware)
.
#ifndef BOARD_H
#define BOARD_H
#define BUTTON_1
#define LED_1
#define LED_2
P30
P21
P22
#define BUTTON_1_PRESSED
#define LED_1_ON
#define LED_2_ON
#define LEDS_ON
#define LED_1_OFF
#define LED_2_OFF
#define LEDS_OFF
#endif
/ 1
leds 1 2.
leds.
7.7 delays.c
delays.c .
#include "includes.h"
void delay(u32 d)
{
for(; d; --d)
{
asm volatile ("nop");
}
}
delay() nop
. volatile nop
.
146
0x0D
U0LCR
U0DLM
U0DLL
U0LCR
U0FCR
= (1<<7);
= Fdiv / 256;
= Fdiv % 256;
= 0x03;
= 0x07;
wr_index_uart0 = 0;
counter_uart0++;
if(counter_uart0 == BUFSIZE_UART0)
{
counter_uart0 = 0;
buffer_overflow_uart0 = 1;
};
VICVectAddr = 0;
// Acknowledge Interrupt
}
// Write character to Serial Port 0 with \n -> \r\n
char sendchar_UART0 (char ch)
{
if (ch == '\n') {
while (!(U0LSR & 0x20));
//wait until U0THR is empty
U0THR = CR;
// output CR Carriage Return
}
while (!(U0LSR & 0x20));
return (U0THR = ch);
}
// Write character to Serial Port 0 without \n -> \r\n
int sendc_UART0 (u08 ch)
{
while (!(U0LSR & 0x20));
return (U0THR = ch);
}
void sendstring_UART0 (const char *string)
{
char ch;
while ((ch = *string)) {
sendchar_UART0(ch);
string++;
}
}
// Read character from Serial Port
char getc_UART0 (void)
{
if (U0LSR & 0x01) {
return (U0RBR);
}
else {
return 0;
}
}
148
UART0.
. uart
U0LCR 8bits,
stop bit .
UART
. UART0Buffer[]
.
buffer
UART0_getchar().
buffer ,
.
.
.
sendc_UART0()
byte , sendstring_UART0()
send_data_UART0()
bytes .
(polling).
UART1. rprintf()
rprintf.h rprintf.c.
0x08
0x10
0x18
0x20
0x28
0x30
0x38
0x38
0x40
150
#define TW_MR_SLA_NACK
#define TW_MR_DATA_ACK
#define TW_MR_DATA_NACK
/* Slave Transmitter */
#define TW_ST_SLA_ACK
#define TW_ST_ARB_LOST_SLA_ACK
#define TW_ST_DATA_ACK
#define TW_ST_DATA_NACK
#define TW_ST_LAST_DATA
/* Slave Receiver */
#define TW_SR_SLA_ACK
#define TW_SR_ARB_LOST_SLA_ACK
#define TW_SR_GCALL_ACK
#define TW_SR_ARB_LOST_GCALL_ACK
#define TW_SR_DATA_ACK
#define TW_SR_DATA_NACK
#define TW_SR_GCALL_DATA_ACK
#define TW_SR_GCALL_DATA_NACK
#define TW_SR_STOP
/* Misc */
#define TW_NO_INFO
#define TW_BUS_ERROR
// bit defines
#define I2CON_AA
#define I2CON_SI
#define I2CON_STO
#define I2CON_STA
#define I2CON_I2EN
2
3
4
5
6
0x0F
0xF8
// return values
#define I2C_OK
#define I2C_ERROR_NODEV
0x00
0x01
0x48
0x50
0x58
0xA8
0xB0
0xB8
0xC0
0xC8
0x60
0x68
0x70
0x78
0x80
0x88
0x90
0x98
0xA0
0xF8
0x00
i2c.c
i2c . :
void i2cInit(void)
{
// setup SCL pin P02
PINSEL0 &= ~(3<<4);
PINSEL0 |= 1<<4;
// setup SDA pin P03
PINSEL0 &= ~(3<<6);
151
PINSEL0 |= 1<<6;
// set bitrate
i2cSetBitrate(75); // 60MHz - 400kHz
// disable and reset interface
I20CONCLR = 0xFF;
delay(10);
// enable interface
I20CONSET = BIT(I2CON_I2EN);
}
i2cInit() .
i2c SDA SCL
i2c.
i2cSetBitrate() 400kHz
i2c .
void i2cSetBitrate(u16 bitrateDiv)
{
// @60MHz and VPB=1, set to 75 for 400KHz
// set equal high and low periods
I20SCLL = bitrateDiv;
I20SCLH = bitrateDiv;
}
i2cSetBitrate()
i2c I20SCLL
I20SCLH. Fi2c=Pclk/(I20SCLL+
I20SCLH) Fi2c=60000000/150=400kHz.
void i2cSendStart(void)
{
I20CONSET = BIT(I2CON_STA);
I20CONCLR = BIT(I2CON_SI);
}
void i2cSendStop(void)
{
I20CONSET = BIT(I2CON_STO);
I20CONCLR = BIT(I2CON_SI);
}
i2cSendStart() START
i2c i2cSendStop() STOP .
bits
i2c .
152
void i2cWaitForComplete(void)
{
// wait for a valid status code
while(I20STAT == TW_NO_INFO);
}
i2cWaitForComplete()
i2c i2c
i2c.h.
,
.
void i2cSendByte(u08 data)
{
// save data into data register
I20DAT = data;
// clear SI bit to begin transfer
I20CONCLR = BIT(I2CON_SI);
}
i2cSendByte() byte
i2c.
u08 i2cGetReceivedByte(void)
{
return I20DAT;
}
i2cGetReceivedByte() byte
.
void i2cReceiveByte(u08 ackFlag)
{
// begin receive over i2c
if( ackFlag )
{
// ackFlag = TRUE: ACK the recevied data
I20CONSET = BIT(I2CON_AA);
}
else
{
// ackFlag = FALSE: NACK the recevied data
I20CONCLR = BIT(I2CON_AA);
}
// clear SI bit to begin transfer
I20CONCLR = BIT(I2CON_SI);
}
153
154
155
156
i2c.c i2c
i2cInit(). H max1237_init()
MAX1237_ADDRESS Setup Configuration bytes
max1237.h.
max1237 .
max1237
,
max1237_read()
(. )
bytes ( max1237 10bits
).
7.11 includes.h
includes.h
.
#ifndef includes_h_
#define includes_h_
#include "LPC214x.h"
#include "armlibtypes.h"
#include "armlibdefs.h"
#include "board.h"
#include "uart.h"
#include "i2c.h"
#include "rprintf.h"
#include "delays.h"
#include "max1237.h"
#endif
157
158
LPC2148
GCC (GNU Compiler Collection) ARM .
WinARM
Programmers Notepad.
C
Programmers Notepad.
159
8-2:
.
.
LPC2000 Flash Utility.
160
9.1
.
(ADC)
.
I2C .
.
.
led
.
.
.
200z ( =5msec) 5000 .
5000*5msec=25sec.
led.
9.1: , .
161
9-2: .
9-3: .
162
9-4: .
9-5: .
163
9-6: .
9.2
.
12V 3.3V
3.3V 12V
.
.
MAX3232 3.3V
.
164
9-7: MAX3232
RS232 .
Brays Terminal.
,
9600bps, 8 ,
. connect Start log
.
165
9-9:
Brays Terminal.
.
Stop log .
.
, x
, y z .
166
9.3 Matlab
9.3.1
Matlab.
Brays Terminal
Matlab, ,
,
. Matlab File -> Import
Data Brays Terminal
. Matlab 5000 4
, . Next Finish.
Workspace .
plot ,
.
.
. array2=array1,
smooth, . array2(:,i) = smooth(array1(:,i),20); i
i=24 20
.
167
9-12: Matlab
.
9-13: 4 5000
Matlab .
168
9-14: x,y z
.
9-15: x,y z
.
169
9.3.2 E gait1.m
Matlab gait1.m
g, dc
,
,
, n
.
gait1(array2,100). To
.
O 4 5000 .
, ,
x , y
z .
function [en, pr, sm, varen] = gait1(data , n)
Limit=[3700:3950];
=((data(:,2))*0.80566/800);
y=((data(:,3))*0.80566/800)-2;
z=((data(:,4))*0.80566/800)-2.2;
y2 = y.^2;
z2 = z.^2;
en = y2 + z2;
pr = y.*z;
sm = y + z;
L = length(data(:,1));
i = 1;
while i <= L-n
varen(i) = var(en(i:i+n));
i = i+1;
end
figure
plot(Limit, y(Limit),'k');
title('Y acceleration')
figure
plot(Limit, z(Limit),'k');
title('Z acceleration');
figure
plot(Limit,en(Limit)/2666,'k');
title('Energy');
figure
plot(Limit,pr(Limit),'k');
title('Product')
170
figure
plot(Limit,sm(Limit),'k');
title('Sum')
figure
plot(Limit,varen(Limit),'k');
title(['Variance ',num2str(n)])
Limit = [3700:3950]
.
.
, g.
.
:
ADC 12bit 3.3V,
, 0.80566mV
[ (3.3/212)=3.3/4096 = 0.80566mV ].
800mV/g
. g.
y z
dc offset
.
y2 z2 y z
. (en),
(pr) (sm)
.
n
var. plot
.
, ,
. 10.
171
172
10
10.1
.
.
10-1 .
,
10-2.
10-1: .
173
10-2: .
, ,
.
,
.
,
.
,
.
.
.
.
.
174
10-3: Y .
.
10-4: .
.
175
10.2
.
.
, .
10.2.1
Z.
Energy = Y 2 + Z 2
,
.
. ,
, .
.
.
.
,
.
10.2.2
.
,
.
Pr oduct = Y Z
,
,
.
176
,
.
.
10.2.3
Y
.
Sum = Y + Z
.
.
.
,
.
10-5: .
177
10-6: .
10-7: .
178
10.3
.
.
.
.
,
.
.
.
.
10.4
.
1g2
.
, 10g2.
,
.
1g2. ,
,
. ,
.
.
179
10.5
(variance) sn2, n :
Sn j
2
1 i= j+n
= ( xi x j )
n i= j
x j n .
1 i= j +n
x j = xi
n i= j
,
.
,
.
.
.
.
.
,
.
.
.
.
.
,
n
.
70 120
180
.
,
, 100 ,
.
,
,
.
.
. Y Z
.
.
10-8: Z
.
181
10-9:
.
10-10:
.
182
10-11:
.
10-12:
.
183
10-13:
.
0.6.
.
10-14: 10
.
184
10-15: 50
,
10-16: 60
,
185
10-17: 70
.
10-18: 80
.
186
10-19: 90
.
10-20: 100
.
187
10-21: 120
.
10-22: 200
.
188
10-23: 500
.
10.6
. :
.
.
. 10-25
.
.
t=4,25s
t=4,3s. ,
.
,
.
.
,
.
189
10-24: .
10.7
,
,
.
, .
,
.
( )
,
. ,
.
,
.
,
.
.
,
.
,
.
190
50
60
65
85
95
10-a: o.
10.8
, ,
,
. 10-a
.
, . ,
.
.
,
.
.
n=100.
.
.
10-20.
191
10.8.1
.
10.8.2
.
10.8.3
.
.
10.8.4
.
.
10.8.5
.
.
10.8.6
.
.
.
.
192
10.9
23 24 .
.
.
1.90m
75kg
1.82m
118kg
1.67m
65kg
10-b:
.
10.9.1
.
.
. ,
,
.
,
. .
.
.
.
.
.
.
.
10.9.2
.
.
193
.
, .
.
,
.
. ,
.
.
.
, .
. , ,
.
10.9.3
.
.
.
.
.
,
.
.
.
.
.
.
194
10-25: Y .
10-26: Y .
195
10-27: Y .
10-28: .
196
10-29: .
10-30: .
197
10-31: .
10-32: .
198
10-33: .
10-34: .
199
10-35: .
10-36: .
200
10-37: .
10-38: .
201
10-39: .
202
10.10
.
,
,
.
,
,
.
,
.
203
204
11
11.1
.
11.2
.
.
,
( ).
.
.
.
.
.
. , ,
.
, ,
,
,
( ) .
205
.
.
.
.
.
.
.
.
. :
, , , ,
.
.
(.
).
.
.
.
. .
( ,
)
.
.
.
.
,
, ,
.
206
11.3
.
, , , ,
.
.
,
.
.
.
.
.
.
.
11.4
.
.
.
,
, ,
207
.
.
. ,
, ,
.
208
12
12.1
.
.
,
.
.
,
.
.
12.2
,
.
.
( ),
.
,
, ,
.
209
12-2: .
12-3: .
210
12-4: .
12-5:
.
,
,
.
:
, ..
211
.
, ,
.
o 7260
sleep .
212
[1] .., , 2003.
[2] LPC214x User Manual UM10139, Philips Semiconductors
[3] ARM7TDMI datasheet ARM
[4] ARM System on chip architecture Steve Furber
[5] ARM System developers guide Andrew N. Sloss,Domonic Symes.
[6] GCC The complete reference Arthur Griffith
[7] Real-time processing with the Philips LPC ARM microcontroller; using
GCC and the MicroC/OS-II RTOS. Philips 05: Project Number AR1803, 2005.
[8] J. J. Labrosse. Embedded Systems Building Blocks.
[9] B. Kernighan, D. Ritchie, C.
[10]P. Aitken, B.L. Jones. Teach Yourself C in 21 Days, Fourth edition.
[11] Sedra, Smith. Microelectronics Circuits, Third edition.
[12] MMA7260QT Datasheet, Freescale Semiconductors
[13] MAX1237 Datasheet, Maxim
[14] MAX1724 Datasheet, Maxim
[15] MAX3232 Datasheet, Maxim
[16] XBee Datasheet, Digi
[17] MCP130T Datasheet, Microchip
213
214
215
P4
16
12
8
4
48
44
40
36
P1.16
P1.17
P1.18
P1.19
P1.20
P1.21
P1.22
P1.23
32
28
24
64
60
56
52
20
VDD3-1
VDD3-2
VDD3-3
6
18
25
42
50
VSS1
VSS2
VSS3
VSS4
VSS5
GND
3.3V
63
7
GND
J5
switchRs
LED2
button
10K
Yxtal
Cxtal1
39p
Cxtal2
39p
RST
GND
3.3V
J6
1 2
GND
Cvdd3a
100nF
GND
U4
MCP130T
RESET
Cvdd33
100nF
VDD
button
GND
Cvdd32
100nF
GND
3.3V
Cvdd31
100nF
3.3V
1K
1 2
10K
3.3V
D2
Rled2
Diode 1N4148
Rrst
3.3V
GND
GND
L1
Vin
1
C11
10uF
GND
- +
Vin
P3
P6
Header 5
Inductor
10mH
U6
BATT
Vin
Header 2
LX
GND
3.3V
SHDN
OUT
MAX-1724
1
2
1
GND
3.3V
1K
12MHz
SDA0
Hi2c
D1
Rled1
57
3.3V
J3
Slide Switch
D3
SCL0
62
61
GND
4
3
2
1
Hxbee
bsl
LPC-2148
P1
LED2
RST
VSSA
3.3V
1
2
3
4
5
3.3V
9
10
11
13
14
15
17
X1
X2
VDD3(A)
59
txd1
rxd1
xbeesleep
46
47
53
54
55
1
2
58
P0.25 (AOUT)
USBD+
USBDP0.28
P0.29
P0.30
P0.31
VREF
Hserial
P8
P0.16
P0.17/SCK1
P0.18/MISO1
P0.19/MOSI1
P0.20/SSEL1
P0.21
P0.22
P0.23
VBAT
23
43
51
1
2
3
4
3.3V
TXD0
RXD0
SCL0
SDA0
33
34
35
37
38
39
41
45
P0.8/TXD1
P0.9/RXD1
P0.10
P0.11
P0.12
P0.13
P0.14
P0.15
RTCX1
RTCX2
49
3.3V
19
21
22
26
27
29
30
31
P0.0/TXD0
P0.1/RXD0
P0.2/SCL0
P0.3/SDA0
P0.4/SCK0
P0.5/MISO0
P0.6/MOSI0
P0.7/SSEL0
P1.24
P1.25
P1.26
P1.27
P1.28
P1.29
P1.30
P1.31
3
5
GND
GND
U2
2
3
C10
22uF
DS
LED2
J4
1
Vin
5
4
GND
RledS
1K
Slide Switch
GND
GND
GND
GND
216
6
5
4
Cmma7260
100nF
4
3
2
1
DIP
Switch4
SCL0b
SDA0b
3.3Vb
Header 4
GNDb
Rx 1K
3.3Vb
P7
3.3Vb
Rg1 10K
Cx
100nF
VDD
gSel2
gSel1
GNDb
VSS
NC
NC
NC
NC
NC
Xout
Zout
16
Ry 1K
15
Yadc
Cy
100nF
14
13
GNDb
Rz 1K
12
9
10
11
NC
NC
NC
Yout
sleep
5
6
7
8
U3
GNDb
MMA7260Q
2
1
Rg2 10K
GNDb
Xadc
Zadc
Cz
100nF
3.3Vb
GNDb
3.3Vb
U5
3.3Vb
Xadc
Yadc
Zadc
3
4
Crefmax
100n
AIN0
VDD
AIN1
GND
AIN2
SDA
AIN3/REF
SCL
Cmax1237
100nF
Rsda Rscl
1.8K 1.8K
7
6
Cmax1237b 3.3Vb
4.7uF
GNDb
SDA0b
SCL0b
MAX-1237
GNDb
217
3.3Volt
U1
C1
100nF
C2
2
3
4
C4
100nF
C5
100nF
15
GND
Cmax3232
14
T1OUT
C1-
R1IN
C2+
P2
12
R1OUT
C2-
GND2
13
11
T1IN
V-
1
2
3
4
3.3Volt
100nF
GND2
7
8
P9
GND2
3
2
1
J2
V+
100nF
GND2
16
VCC
C1+
T2OUT
10
T2IN
GND2
11
10
D Connector 9
GND2
R2OUT
R2IN
Header 4
1
6
2
7
3
8
4
9
5
MAX-3232
3.3Volt
4
3
2
1
Header 3
GND2
P10
Header 4
RS232.
218
219
220
() ()
RS232.
221