Title: CEG2400 Chapter 9 Peripherals
1CEG2400Chapter 9Peripherals
- of embedded systems
- using the example of ARMdemo06.c
- References
- http//www.nxp.com/acrobat_download/usermanuals/UM
10120_1.pdf - Trevor Martins , The insider's guide to the
Philips ARM7 based microcontrollers,
www.hitex.co.uk
2Introduction
- Timer
- Watchdog
- Parallel Port (GPIO)
- Analog-to-Digital converter ADC
- Digital-to-Analog converter DAC
- Pulse Width Modulation PWM unit
- Real time clock
- Universal Asynchronous Receiver/Transmitter UART
(serial port)
3Pin assignments LPC213x
4LPC2131 peripherals
51) Timerhttp//www.keilsoftware.com/dd/vtr/3735/8
064.htm
- Including these Features
- A 32-bit Timer/Counter with a programmable 32-bit
Prescaler. - Counter or Timer operation
- Four 32-bit match registers that allow
- Up to four external outputs corresponding to
match registers, with the following capabilities
Set low on match, Set high on match, Toggle on
match, Do nothing on match. - Applications
- Interval Timer for counting internal events.
- Pulse Width Demodulator via Capture inputs.
- Free running timer.
6Part 1 of void init_timer_Eint() of EINT.c
(interrupt rate 1KHz)( for init timer , use
VICVectAddr0
------------------------------RECALL--------------
-----------------------
- / Setup the Timer Counter 0 Interrupt /
- void init_timer_Eint (void)
- T0PR 0
- // set prescaler to 0
- T0MR0 13824 // set interrupt interval
to 1mS - // since pclk/1KHz (11059200 x 5)/(4 x
1000)13824 - T0MCR 3 //
Interrupt and Reset on MR0 - T0TCR 1 //
Timer0 Enable - VICVectAddr0 (unsigned long)IRQ_Exception
- // set interrupt vector in 0 (This becomes the
highest priory interrupt) - VICVectCntl0 0x20 4 // use it
for Timer 0 Interrupt - VICIntEnable 0x00000010 // Enable Timer0
Interrupt
cclkMFosc, M5 pclkcclk/4 Pclk110592005/4
7Summary of ClocksOne oscillator generates two
outputs CCLK, PCLK
ARM-LPC213x
FOSCx5CCLK for MCU 55.296MHz
FOSC 11.0592MHz
CCLK/4 PCLK for peripherals 13.824MHz
PCLK13.824MHz
8Concept of the timer Operation
- PCLK /freq_out(11059200 x 5/4)/freq_out
- 13.824MHz /freq_out
- When timer counter (TC)match reg0 (T0MR0), an
pulse is generated, the the timer counter is
reset
Match reg0 T0MR0 13824
Timer Counter TC
When TCT0MR0 a pulse is sent The frequency
generated PCLK/T0MR0
PCLK 13.824MHz
reset
9Example of a 1KHzfreq_out interrupt generator
- PCLK /freq_out PCLK/1K(11059200 x 5)/(4
)13.824 MHz/1K13824 - When timer counter (TC)match reg0 (T0MR0), an
interrupt is generated
Match reg0 T0MR0 13824
Timer Counter TC
Divided by (pre-scale1) Since pre-scale T0PR
0 So divided by 1
PCLK Or an input pin CAPx.y (See pin assignment
of lpc2131)
Freq_out PCLK/T0MR0 Interrupt request or
output pin (MATx.y) (1KHz, every 1ms)
102) Watchdog timer
- For implementing fail safe systems
If the system doesnt give me any signals for a
period of time (say 2 seconds), that means it
hangs, so I will Press the reset bottom
11Example, solar power wireless telephone(register
setting , see appendix)
- At remote area, maintenance is difficult
- If the software does not operate properly (hangs)
- I.e. does not send regular signals to the watch
dog sensor, - Then
- the watch-dog resets the system
If the system doesnt give me any signals for a
period of time (say 2 seconds), that means it
hangs, so I will Press the reset bottom
12Software
If the system doesnt give me any signals for a
period of time (say 2 seconds), that means it
hangs, so I will Press the reset bottom
- Main
-
- While(1)
- Do_the _neccessary()
- Send_a_pulse_to_watch_dog()
-
-
- If the software hangs, it will not
Send_a_pulse_to_watch_dog() - so the system is reset by the watch_dog_hardware
13Applications of watchdog timers
Paid Telephone box www.viewimages.com
Space robot www.links999.net
Solar power wireless emergency
telephone http//www.homepower.ca/
Industrial machine http//www.maxengineering.us/im
g/machine1.jpg
143) General purpose IO (GPIO)http//www.nxp.com/ac
robat_download/usermanuals/UM10120_1.pdf
- LPC2131/2/4/6/8 has two 32-bit General Purpose
I/O ports. Total of 30 input/output and a single
output only pin out of 32 pins are available on
PORT0. PORT1 has up to 16 pins available for GPIO
functions - Features
- Direction control of individual bits
- Separate control of output set and clear
- All I/O default to inputs after reset
- Applications
- General purpose I/O
- Driving LEDs, or other indicators
- Controlling off-chip devices
- Sensing digital inputs
- An example in 2400_04.ppt
15The experiment hardware
video
switch
Arm board
green led
red led
--We will show how to blink the red-led
16Our testing board connectorp03(pin26) is input,
p0.8(pin33),p0.9(pin34) are outputs
17For 3.3V driving LEDs from a 3.3V system
18Remind you thatARM has
- 32-bit memory addresses total 4-Gbytes
- (0x0000 0000 to 0xFFFF FFFF)
-
- GPIO Port 0 Register address
- IO0DIR EQU 0xE0028008 IO direction
- IO0SET EQU 0xE0028004 turn on the bits
- IO0CLR EQU 0xE002800Cturn off the bits
- IO0PIN EQU 0xE0028000 pin assignment
19Send data to GPIO registers
GPIO Port 0 Register address IO0DIR EQU 0xE00
28008 IO direction IO0SET EQU 0xE0028004 turn
on the bits IO0CLR EQU 0xE002800Cturn off the
bits IO0PIN EQU 0xE0028000 pin assignment
20A simple C program GPIO.cWhen SW1 is depressed,
RED-LED is on
- 1) include ltlpc21xx.hgt //define IO0PIN ,IO0DIR..
etc - 2) define RED_LED 0x00000100 //set p0.8 as RED
LED - 3) define SW1 0x00000008 //set p0.3 as SW1
- 4) int main(void)
- 5) long tmp // variable for temp storage of
port 0 status - 6) IO0DIR RED_LED // set p0.8 as output
- 7) while(1)
- 8) tmp IO0PIN SW1//read
SW1(p0.3)depressed0 - 9) if(tmp0) What happens if (tmp!0)
is used? - 10) IO0SET RED_LED //if SW1 pressed
LED is on - 11) else IO0CLR RED_LED // otherwise off
the LED - 12)
- 13)
21Explanation1 of GPIO.c (not an interrupt program,
pure polling program)line 1-6
- 1) include ltlpc21xx.hgt //define IO0PIN ,IO0DIR..
etc - 2) define RED_LED 0x00000100 //set p0.8 as RED
LED - 3) define SW1 0x00000008 //set p0.3 as SW1
- 4) int main(void)
- 5) long tmp // variable for temp storage of
port 0 status - //after power up by default all pins are GPIOs,
same as PINSEL0 - 6)IO0DIR RED_LED // set p0.8 as output
- //so IO0DOR0000 0000 0000 0000 0000 0001 0000
0000 -
p0.8output p0.3input - // p0.8 is output output, all pins are inputs
(include p0.3),
22Explanation2 of GPIO.c(not an interrupt program,
pure polling program)line 7-13
- 2) define RED_LED 0x00000100 //set p0.8 as RED
LED - 3) define SW1 0x00000008 //set p0.3 as SW1
-
- 7) while(1)
- 8) tmp IO0PIN SW1//read
SW1(p0.3)depressed0 - 9) if(tmp0) What happens if (tmp!0)
is used? - 10) IO0SET RED_LED //if SW1 pressed
LED is on - 11) else IO0CLR RED_LED // otherwise off
the LED - 12)
- 13)
- Tmp0x0000 0000 if SW1 is depressed because p0.3
is 0 - Tmp0x0000 0008 if SW1 is not depressed
P0.3 of LPC213x
234) Analog-to-Digital converter ADC
- Example in our robot car to
- measure reflected light intensity
Analog voltages
Light sensor0(IRS0) Light sensor1(IRS1) Light
sensor2(IRS2) Light sensor3(IRS3) Light
sensor4(IRS4)
Ad0.0 Ad0.1 Ad0.2 Ad0.3 Ad0.4
Program Use read_sensor (int channel) To read
the data
Applications Light sensor output or Temperature
sensor or Force sensor
ADC
24Code for Analog-to-Digital ADC converter pin
assignment for sensor pins
- From line 71 of ARMdemo06.c
- // ADC interface
- 71) int read_sensor(int channel)
- 72)
- 73) int temp
- 74)
- 75) ADCR0x1ltltchannel
- 76) ADCR0x01200200
- 77)
- 78) while(((tempADDR)0x80000000)0) //MSB 1
meaning ADC is done - 79) tempgtgt6
- 80) temp0x3ff//TEMPoutput IS 0-3V PRESICION
IS 1024 - 81)
- 82) return (temp33)
- 83) .
- .....
- int main(void)
- ....
25Code for Analog-to-Digital ADC converterpin
assignment for sensor pins
- include ltlpc21xx.hgt
- define ADCR (((volatile unsigned long
) 0xE0034000)) - define ADDR (((volatile unsigned long
) 0xE0034004)) - int main(void)
- ....
- //From line 92 of ARMdemo06.c
- // We must initialize the IO pin //before using
ADC channel - 94) PINSEL1 0x00400000 // set p0.27 to ad0.0
- 95) PINSEL1 0x01000000 // set p0.28 to ad0.1
- 96) PINSEL1 0x04000000 // set p0.29 to ad0.2
- 97) PINSEL1 0x10000000 // set p0.30 to ad0.3
- 98) PINSEL1 0x00040000 // set p0.25 to ad0.4
26PINSEL1 Pin assignment for AD0.194) PINSEL1
0x00400000 // set p0.27 to ad0.0 PINSEL1
0000 0000 0100 0000 0000 0010 xxxx xxxxbit
31 27 23 19
15 11 7 3
Bit2322
Bit232201
27PINSEL1 Pin assignment for AD0.194) PINSEL1
0x010000000 // set p0.28 to ad0.1 PINSEL1
0000 0001 0000 0000 0000 0000 xxxx xxxxbit
31 27 23 19 15
11 7 3
Bit2524
Bit252401
28Code for Analog-to-Digital ADC converterIn
C\Keil\ARM\INC\Philips\lpc21xx.h, ADCR0xE0003
4000 ADCR0xE0003 4000, ADDR0xE0003 4004
- From line 71 of ARMdemo06.c
- //(1) ADC interface
- 71) int read_sensor(int channel)
- 72)
- 73) int temp
- 74)
- 75) ADCR0x1ltltchannel
- 76) ADCR0x01200200
- 77)
- 78) while(((tempADDR)0x80000000)0) //MSB 1
meaning ADC is done - 79) tempgtgt6
- 80) temp0x3ff//TEMPoutput IS 0-3V PRESICION
IS 1024 - 81)
- 82) return (temp33)
- 83)
- .....
Loop until ADC is done
29ADCR -- one (AD0) for lpc2131line 75)
ADCR0x1ltltchannelline 76) ADCR0x01200200//op
erational,start convertADCR 0000 0001 0010 0000
0000 0010 xxxx xxxxbit 31 27
23 19 15 11 7
3
Point to which channel
Bit15810b2 CLKDIV213 Freq13.824MHz/34.608M
Hz
30ADCR -- one (AD0) for lpc2131line 75)
ADCR0x1ltltchannelline 76) ADCR0x01200200//op
erational,start convertADCR 0000 0001 0010 0000
0000 0010 xxxx xxxxbit 31 27
23 19 15 11 7
3
Bit211 operational
Bit241
31Polling for the completion of Analog-to-digital
conversion 78) while(((tempADDR)0x80000000)0
)ADDR 1000 0000 0000 0000 xxxx xxxx xx00 0000
//MSB 1 meaning ADC is done//if bit 31 of ADDR
is 1, it is done//bit 156 contain the result
result
32Find the Analog-to-digital converted result ADC
result xx xxxx xxxx ADDR 1000 0000 0000
0000 xxxx xxxx xx00 0000
result
- 78)while(((tempADDR)0x80000000)0)
- 79) tempgtgt6tempgtgt6 0000 0010 0000 0000
00xx xxxx xxxx - 80) temp0x3ff//TEMPoutput IS 0-3V PRESICION
is 1024 (10bit ADC precision) - temp0x3ff 0000 0000 0000 0000 00xx xxxx xxxx
- 82) return (temp33)// make it a full integer.
polling
335)Digital-to-Analog converter DAC
- Applications
- Sound generation, e.g. MP3 player
- Motor speed control use analog methods
- Usage Similar to Analog-to-Digital converter ADC
ADC Convert digital codes into an analog signal
Analog value
34DAC reg. 10-bit
356) Pulse Width Modulation PWM unitUse on-off
time to control energy delivery
- The DC motor speed is determined by the on/off
time of the motor enable signal MLE
On/off (MEL)
DC Motor
Battery
36Timing diagrams of pulse width modulation
- Comparing two pulse modulated signals S1,S2
37PWM
PWM5 Right-motor
Pin1PWM5 Pin31PWM2
PWM2 Left-motor
38Code for Pulse Width Modulation PWMARM06demo.c
(refer to line numbers)
- 17) define PWM_FREQ 276480 //set PWM frequency
to 50 Hz, since timer is 13824KHz - //FREQ_of_PWM13824000/27648050
- 85) int main(void)
- ..
- 89)long leftPWM,rightPWM
- .....
- // Initialize IO pin for PWM
- 97) PINSEL1 0x00000400 // set p0.21 to
PWM5-right motor - 98) PINSEL0 0x00008000 // set p0.7 to
PWM2-left motor - .....
- 122) PWMPCR0x2000 // enable pwm5
- 123) PWMPCR0x0400 // enable pwm2
- 124) PWMMCR0x0002
- 125) PWMMR0 PWM_FREQ //set PWM frequency
to 50 Hz
PINSEL10xE002 C004
PINSEL0 0xE002 C000
39Code for Pulse Width Modulation PWMARM06demo.c
(refer to line numbers)
- 17) define PWM_FREQ 276480 //set PWM frequency
to 50 Hz, -
- 122) PWMPCR0x0000 2000 // enable pwm5
- 123) PWMPCR0x0000 0400// enable pwm2
- 124) PWMMCR0x0000 0002
- 125) PWMMR0 PWM_FREQ //set PWM frequency to 50
Hz
40Code for Pulse Width Modulation PWMARM06demo.c
(refer to line numbers)
- 17) define PWM_FREQ 276480 //set PWM frequency
to 50 Hz, -
- 122) PWMPCR0x0000 2000 // enable pwm5
- 123) PWMPCR0x0000 0400// enable pwm2
- 124) PWMMCR0x0000 0002
- 125) PWMMR0 PWM_FREQ //set PWM frequency to 50
Hz
41Code for Pulse Width Modulation PWMARM06demo.c
(refer to line numbers)
- 17) define PWM_FREQ 276480 //set PWM frequency
to 50 Hz, -
- 122) PWMPCR0x0000 2000 // enable pwm5
- 123) PWMPCR0x0000 0400// enable pwm2
- 124) PWMMCR0x0000 0002
- 125) PWMMR0 PWM_FREQ //set PWM frequency to 50
Hz
PCLK 13.824MHz (see previous slide) The
formula PCLK/PWM_FREQ 13.824MHz/ 27648050Hz
42Code for Pulse Width Modulation PWM
-
- 17) define PWM_FREQ 276480
-
- 127) //set robot to full speed
- 128) leftPWMPWM_FREQ//set a value you prefer
- 129) rightPWMPWM_FREQ //a value you prefer
- 130) PWMMR2 leftPWM// left motor PWM width to
full speed - 131) PWMMR5 rightPWM//right motor PWM width
to full - 132) PWMLER 0x25 //enable match 0,2,5 latch
to effective - 133) PWMTCR0x09
leftPWM
rightPWM
43 133) PWMTCR0x09//0000 1001B, enable
counter,PWM
44Application driving a robot
Left-motor forward P0.16 L_DIR
1 P0.17L_DIRinv0 Left motor backward P0.16
L_DIR0 P0.17 L_DIRinv1 Left-motor
speed PWMMR2
Right-motor forward P0.18 R_DIR 1 P0.19
R_DIRinv0 Left motor backward P0.18
R_DIR0 P0.19 R_DIRinv1 Right-motor
speed PWMMR5
L293 see next slide
45Use L293 H bright circuit
- A chip for generating enough current to drive 2
motors controlled by 4 signals
2 (1A) 1Y(3) 1(EN1/2) 7(2A)
(2Y)6 10(3A)
(3Y)11 9(EN3/4) 15(4A)
(4Y)14
46Setting drive direction pins
- 18) define L_DIR 0x00010000 //set p0.16 left
motor dir. - 19) define L_DIRinv 0x00020000 //set p0.17
inverted left motor dir. - 20) define R_DIR 0x00040000 //set p0.18 right
motor dir. - 21) define R_DIRinv 0x00080000 //p0.19 inverted
right motor dir. - 22) define TEST_PIN 0x00010000 //set p1.16 as
Test pin -
- 135) //set p0.16-p0.19 as output
- 136) IO0DIRL_DIR //p0.16
- 137) IO0DIRL_DIRinv //p0.17
- 138) IO0DIRR_DIR //p0.18
- 139) IO0DIRR_DIRinv //p0.19
- 140) IO1DIRTEST_PIN// p1.16 as Outputs
Set p0.16-19 as output pins
47Forward 100 steps and stopFour line (170-173) to
start the robot move forward
- 170) IO0SETL_DIR
- 171) IO0CLRL_DIRinv
- 172) IO0SETR_DIRinv
- 173) IO0CLRR_DIR
48sensors
49Left Wheel sensor LWheelsen (same for Right
wheel sensor RWheelsen)
Our motor and speed encoder Each wheel
rotation 88 on/off changes
IR receiver
Darkened part blocks light
LWSensor
IR light source
RWSensor
50Setup for LWheelsen p0.6 (LPC213-pin30),
Rwheelsen p0.3(LPC213x-pin26)
- // set p0.0 to TXD0, p0.1 to RXD0 and the rest to
GPIO - //After power up (reset value) , all GPIOs are
inputs - //So by default p0.6 (LWheelsen), p0.3(Rwheelsen)
are inputs - 91)PINSEL0 0x00000005
-
- 23) define LWheelSen 0x00000040 //p0.6 as left
wheel sensor input - 24) define RWheelSen 0x00000008 //p0.3 as right
wheel sensor input
51Sensor connection
LWsensor
RWSensor
52It uses a timer interrupt service routine
programs
- void init_timer (void)
- Setup 1000 tmer interrupt for _IRQ exception()
- _IRQ exception()
- Capture the rotation count, (each rotation 88
counts.) - Result saved at lcount, rcount
53Read wheel count (lcount, rcount) using
interrupts
IR receiver Speed Encoder sensor
1000 interrupts per second
interrupts
time
Main( ) Setup( )
_IRQ exception() //1000Hz read wheel
speed Update rcount Update lcount
54Read wheel count, result at lcount, rcount
23) define LWheelSen 0x00000040 24) define
RWheelSen 0x00000008
- 265) void __irq IRQ_Exception()
- 266)
- 267) timeval
- 268) //generate square wave at test pin
- 269) if((timeval2)0) IO1SETTEST_PIN
- 270) else IO1CLRTEST_PIN
- 271) //
- 272)
- 273) //get the current wheel sensor values
- 274) lcurIO0PIN LWheelSen
- 275) rcurIO0PIN RWheelSen
- 276)
- 277) //count the number of switching
- 278) if(lcur!lold)
- 279) lcount
- 280) loldlcur
- 281)
- 282) if(rcur!rold)
- 283) rcount
P0.6 (left wheel) , or P0.3 (right wheel)
CEG2400 Ch9 Peripherals V93b
54
55Explanation1 , line265-271
For testing purpose You can observe a waveform at
this pin
- 265) void __irq IRQ_Exception()
- 266)
- 267) timeval// increases at 1000 per
second - 268) //generate square wave at test pin
- 269) if((timeval2)0) IO1SETTEST_PIN
- 270) else IO1CLRTEST_PIN
- 271) //
-
-
-
-
56Explanation2, line 273-275
- 23) define LWheelSen 0x00000040 //bit 6 is1,
others 0, p0.6 as left wheel sensor input -
- 273) //get the current wheel sensor values
- 274) lcurIO0PIN LWheelSen // read left sensor
- 275) rcurIO0PIN RWheelSen // read right
sensor - If LWSesnor is 1
- lcurIO0PIN LWheelSen IO0PIN 0x0000 0040
0x0000 0040 - If LWSesnor is 0
- lcurIO0PIN LWheelSen IO0PIN 0x0000 0040
0x0000 0000
LWSensor Bit6 of IO0PIN P0.6 of LPC213x
57Explanation3, line273-289
- 273) //get the current wheel sensor values
- 274) lcurIO0PIN LWheelSen // read left sensor
- 275) rcurIO0PIN RWheelSen // read right
sensor - 276)
- 277) //count the number of switching
- 278) if(lcur!lold)
- 279) lcount
- 280) loldlcur
- 281)
- 282) if(rcur!rold)
- 283) rcount
- 284) roldrcur
- 285)
- 286)
- 287) T0IR 1 // Clear interrupt flag
- 288) VICVectAddr 0 // Acknowledge Interrupt
- 289)
If there is change increment lcount
CEG2400 Ch9 Peripherals V93b
57
58Explanation4, line174-183 In main()f command
Forward 100 steps and stopwhen lcountgt100, stop
left motorwhen rcountgt100, stop right motor
Interrupt service routine Running at
1000Hz Update lcount and rocunt As the wheels
rotate 265) void __irq IRQ_Exception() 266)
274)lcurIO0PIN LWheelSen 275)rcurIO0PIN
RWheelSen 278)if(lcur!lold) 279)
lcount 280) loldlcur 281) 282)if(rcur!ro
ld) 283) rcount 284)
roldrcur 285) 289)
- Main()
-
- 174) if(cin'f')
- 175) lcount0 //reset left step count
- 176) rcount0 //reset right step count
- 177) //stop when stepcount reach 100 steps
- 178) while((lcountlt100)(rcountlt100))
- 179) if(lcountgt100)
- 180) IO0CLRL_DIRinv//stop left motor
- 181) IO0CLRL_DIR
- 182) lcount0xff
- 183)
- 184) if(rcountgt100) stop right motor
59Timer interrupt at 1KHz,interrupt service routine
is at IRQ_ExceptionRefer to the notes on how to
set timer interrupt
- 291) / Setup the Timer Counter 0 Interrupt /
//1KHz - 292) void init_timer (void)
- 293) T0PR 0 // set prescaler to 0
- 294) T0MR0 13800 // set interrupt
interval to 1mS - 295) T0MCR 3 // Interrupt and
Reset on MR0 - 296) T0TCR 1 // Timer0 Enable
- 297) VICVectAddr0 (unsigned
long)IRQ_Exception//interrupt vector in 0 - 298) VICVectCntl0 0x20 4 // use it
for Timer 0 Interrupt - 299) VICIntEnable 0x00000010 // Enable
Timer0 Interrupt - 300)
607) Real time clock
618) Universal Asynchronous Receiver/Transmitter
UART (serial port)in ARM06Demo.c, and
www.nxp.com/acrobat_download/applicationnotes/AN10
369_1.pdf
- //init UART0 setting
- ......
- 26) define NEWLINE sendchar(0x0a)
sendchar(0x0d) - 33) void Init_Serial_A(void)
- 34) U0LCR 0x83 //8 bit length ,DLAB must be
1 to access - 35) U0DLL 0x0F //Baud rate setting ,
part136) U0DLM 0x00 //Baud rate setting ,
part 2 - 37) U0LCR 0x03 //DLAB0 to complete the
setting -
0x0aNew line , 0x0dcarriage return
- //Pclk/(16baudrate)(11059200 x 5)/(4 x 16 x
57600)
6234) U0LCR 0x83 //8 bit length ,DLAB1
//U0LCR 1000 0011b
63Baud rate setting35) U0DLL 0x0F//15 36)
U0DLM 0x00//0
- PCLK13.824MHz
- UART0(baudrate)PCLK/(16(16015))
- 13.824MHz/384057600
Exercise Find U0Dll and U0DLM if the required
baud rate is 9600.
64Getchar() polling method (not interrupt)
Polling method
Yes
- 40) char getchar(void)
- 41) volatile char ch '0'
- 42)
- 43) while ((U0LSR 0x1)0)//wait until
receive a byte - 44)
- 45) ch U0RBR// receive character
- //(U0RBR - 0xE000
C000, - 46)
- 47) return ch
- 48)
Is bit1 of U0LSR0? (receive buffer empty?)
polling
No,
receive character
65Sendchar() polling method (not interrupt)
Polling method
- 49)///////////////////////////////////////////////
///////////// - 50)void sendchar(char ch)
- 51) while( (U0LSR 0x40)0 )
- 52)
- 53) U0THR ch// Transmit next character
- 54) // at 0xE000 C000
bit70
polling
Bit of U0LSR at 0xE000 C014
66Print(), Print a string of characters on screen
- 56) int print(char p)
- 57) while(p!'\0') //\0 is end of text,
0x03 - 58) sendchar(p) // if not end of text
send characters of the string - 59)
- 60) return(0)
- 61)
- ......
- Example
- print(---Hello world---")NEWLINE
67Ascii table fromhttp//enteos2.area.trieste.it/ru
sso/IntroInfo2001-2002/CorsoRetiGomezel/ASCII-EBCI
DC_table.htm
68putint( int count) print an integer on screen
0 is 0x30 (ASCII for number zero)
- 63) void putint(int count)
- 64) sendchar('0' count/10000)
- 65) sendchar('0' (count/1000) 10)
- 66) sendchar('0' (count/100) 10)
- 67) sendchar('0' (count/10) 10)
- 68) sendchar('0' count 10)
- 69)
- ......
Print an ASCII character representing that digit
at one time,
69UART main print example
- int main(void)
- ......
- // Initialize IO pin before using TXD0 and RXD0
- PINSEL0 0x00000005 // set p0.0 to TXD0, p0.1
to RXD0 and the rest to - GPIO
- .....
- Init_Serial_A() // Init COM port
- ......
- NEWLINE
- print("
") NEWLINE - print("")
NEWLINE - print(" CUHK Computer Science and
Engineering Department") NEWLINE - print(" LPC2131 ARM Board
(2006) ")
NEWLINE - print("")
NEWLINE - print(" I2C (Master Receiver) Test
Program (2/2008)") NEWLINE - print("
") NEWLINE - NEWLINE
70Summary
- Studied peripherals of the LPC213x ARM processor.
71Appendix
72Our robot Circuits of this chapter are from this
design
732) Watchdog timer register setting
If the system doesnt give me any signals for a
period of time (say 2 seconds), that means it
hangs, so I will Press the reset bottom
74Examplehttp//www.keil.com/download/docs/317.asp
- void feed_watchdog (void) / Reload the
watchdog timer / - WDFEED 0xAA
- WDFEED 0x55
-
- void sendhex (int hex) /
Write Hex Digit to Serial Port / - if (hex gt 9) sendchar('A' (hex - 10))
- else sendchar('0' hex)
-
- void sendstr (char p) /
Write string / - while (p)
- sendchar (p)
-
-
- / just waste time here for demonstration /
- void do_job (void)
- int i
- for (i 0 i lt 10000 i)
-
75Main and use of feed
- int main (void)
- unsigned int i
- init_serial() /
Initialize Serial Interface / - if( WDMOD 0x04 ) / Check for watchdog
time out / - sendstr("Watchdog Reset Occurred\n")
- WDMOD 0x04 / Clear time out flag
/ -
- WDTC 0x2000 / Set watchdog time out
value / - WDMOD 0x03 / Enable watchdog
timer and reset / - for(i 0 i lt 50 i)
- do_job () / the
actual job of the CPU / - feed_watchdog() /restart watchdog timer,
for_loop will run until complete / -
- while (1) /
Loop forever / - do_job () / the
actual job of the CPU / - / no watchdog restart, watchdog reset will
occur! / -
-
76Watchdog Registers
77Watch dog mode reg. WMOD
78void feed_watchdog (void) / Reload the
watchdog timer / WDFEED 0xAA WDFEED
0x55
Watchdog Block diagram