Download presentation
Presentation is loading. Please wait.
1
MCU Peripherals - Introduction
Parallel Port (GPIO) Analog-to-Digital converter ADC Digital-to-Analog converter DAC Universal Asynchronous Receiver/Transmitter UART (serial port)
2
Pin assignments LPC213x
3
LPC2131 peripherals
4
General purpose Input Output (GPIO)
CEG2400 Ch8 Peripherals-1 V3b
5
1) General purpose Input Output (GPIO) http://www. nxp
LPC2131/x has two 32-bit General Purpose I/O ports. P0[31:0] except P0[31] is output only pin. Pin P0[24]is not available. P1[31:16] are IOs, P1[1:15] are not available. 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
6
Exercise 1 What are peripheral modules are available in LPC2131.
Ans: ?__________________ How many parallel input pins and output pins are available in LPC2131? (i) : Inputs?________________ (ii) : outputs:?__________________ What are the states of the parallel input/output pins after reset: ? ________
7
The experiment hardware
video switch Arm board green led red led --We will show how to blink the red-led
8
Our testing board connector p03(pin26) is input, p0. 8(pin33),p0
Our testing board connector p03(pin26) is input, p0.8(pin33),p0.9(pin34) are outputs CEG2400 Ch8 Peripherals-1 V3b
9
For 3.3V driving LEDs from a 3.3V system
10
Remind you that ARM has Registers (R0-R15) ..etc
32-bit memory addresses total 4-Gbytes (0x to 0xFFFF FFFF) : ; GPIO Port 0 Register address IO0DIR EQU 0xE ; IO direction IO0SET EQU 0xE ; turn on the bits IO0CLR EQU 0xE002800C;turn off the bits IO0PIN EQU 0xE ; pin assignment Registers (R0-R15) ..etc
11
Send data to GPIO registers
; GPIO Port 0 Register address ; IO0DIR EQU 0xE ; IO direction IO0SET EQU 0xE ; turn on the bits IO0CLR EQU 0xE002800C;turn off the bits IO0PIN EQU 0xE ; pin assignment
12
Explanation2 of GPIO.c (pure polling program) line 1-6
1) #include <lpc21xx.h> //define IO0PIN ,IO0DIR.. etc 2) #define RED_LED 0x //set p0.8 as RED LED 3) #define SW1 0x //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 PINSEL=0; 6)IO0DIR = RED_LED; // set p0.8 as output //so IO0DIR= p0.8=output p0.3=input // p0.8 is output output, all pins are inputs (include p0.3), CEG2400 Ch8 Peripherals-1 V3b
13
Explanation3 of GPIO.c (pure polling program) line 7-13
2) #define RED_LED 0x //set p0.8 as RED LED 3) #define SW1 0x //set p0.3 as SW1 : 7) while(1) 8) { tmp =IO0PIN & SW1;//read SW1(p0.3)depressed=0 9) if(tmp==0) ; 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) } Tmp=0x if SW1 is depressed because p0.3 is 0 Tmp=0x if SW1 is not depressed P0.3 of LPC213x CEG2400 Ch8 Peripherals-1 V3b
14
Exercise 2: A simple C program GPIO
Exercise 2: A simple C program GPIO.c When SW1 is depressed, RED-LED is on 1) #include <lpc21xx.h> //define IO0PIN ,IO0DIR.. etc 2) #define RED_LED 0x //set p0.8 as RED LED 3) #define SW1 0x //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)depressed=0 9) if(tmp==0) ; 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) } Question (a): What happens “if (tmp!=0)” is used in line 9? Ans: ?____________________________________________ Question (b): If the RED-LED is connected to p0.10, how do the program? Ans: ?______________________________________________ Question (c): If the switch SW1 is connected to p0.4, how do the program? Ans: ?_______________________________________________________
15
Applications Outputs Inputs Drive LED Drive motor Read on/off switches
Scan keyboard
16
The experiment software in
Assembly or C
17
(i) To drive the GPIO (General Purpose Input Output) using assembly
Step0: After power, by default, IO pins are set in GPIO mode Step1: set GPIO bit direction Loop Step2: Read IO pins, check Sw1 status If sw1 is depressed , goto step4 Step3: Turn off LED if sw1 is not depressed, goto loop Step4: Turn on LED if SW1 is depressed, goto loop
18
A simple assembly program GPIO.s When SW1 is depressed, RED-LED is on
Define registers Main steps ; GPIO Port 0 Register address ; IO0DIR EQU 0xE IO0SET EQU 0xE IO0CLR EQU 0xE002800C IO0PIN EQU 0xE RED_LED EQU 0x ; p0.22=RED LED SW1 EQU 0x ; p0.20 as SW1 ; ; User Initial Stack & Heap AREA |.text|, CODE, READONLY EXPORT __main __main 1) LDR R1, =RED_LED; p0.22 as output 2) LDR R0, =IO0DIR 3) STR R1,[R0]; 4)loop LDR R3, =IO0PIN; read SW1 (p0.22) 5) LDR R3, [R3] 6) TST R3, #SW1;if SW1 depressed R3=0 7) BEQ onled ;if SW1 depressed LEDon 8); 9) LDR R1, =RED_LED;otherwise LEDoff 10) LDR R0, =IO0CLR 11) STR R1,[R0] 12) B loop 13)onled;------ON LED 14) LDR R1, =RED_LED; on the LED 15) LDR R0, =IO0SET 16) STR R1,[R0] 17) B loop 18) END
19
CENG2400 Ch7: driving parallel loads v.3c
Define registers ; GPIO Port 0 Register address IO0DIR EQU 0xE ; IO direction IO0SET EQU 0xE ; turn on the bits IO0CLR EQU 0xE002800C;turn off the bits IO0PIN EQU 0xE ; address to read pin status ; ;RED_LED EQU 0x ; p0.22=RED LED (out) ; in binary = b, so p..22=out ; by default (reset value) others pins are inputs, so p.20=input SW1 EQU 0x ; p0.20 as SW1(input) by default reset value CENG2400 Ch7: driving parallel loads v.3c
20
Main steps 1) LDR R1, RED_LED; p0.22 as output 2) LDR R0, IO0DIR
Step1: set direction of GPIO pins p.0.22=output to drive LED Other pins are inputs Including P0.20=input from sw1 1) LDR R1, RED_LED; p0.22 as output 2) LDR R0, IO0DIR 3) STR R1,[R0]; 4)Loop LDR R3, IO0PIN; read SW1 (p0.20) 5) LDR R3, [R3] 6) TST R3, #SW1 ;if SW1 depressed (low) 7) BEQ onled ; if SW1 depressed LEDon 8); 9) LDR R1, RED_LED;otherwise LEDoff 10) LDR R0, IO0CLR 11) STR R1,[R0] 12) B loop 13)Onled; 14) LDR R1, RED_LED; on the LED 15) LDR R0, IO0SET 16) STR R1,[R0] 17) B loop 18) END Step2: Read IO pins and check Sw1 status Parallel port (GPIO) --Driving Parallel Loads Step3: Turn off LED if sw1 is not depressed Step4: Turn on LED if SW1 is depressed
21
Step1: set GPIO (General Purpose Input Output) bit direction
1) LDR R1, RED_LED; p0.22 as output 2) LDR R0, IO0DIR 3) STR R1,[R0]; Step1: set direction of GPIO pins p.0.22=output to drive LED Other pins are inputs Including P0.20=input from sw1 ;RED_LED EQU 0x ; p0.22=RED LED (out) ; binary = b, assigns bit 22 as out ; others as inputs CENG2400 Ch7: driving parallel loads v.3c
22
Step2a: Read IO pins, check Sw1 status
Memory location IOPIN holds the status of Input/output Pins P0.x I.e. P0.0 is bit 0 of IOPIN P0.1 is bit 1 of IOPIN : P0.20 is bit 20 of IOPIN Open=1 Closed=0 22
23
Recall: TST and BEQ BEQ TST
Same as AND (logical AND) except result of operation is not stored. Only the condition code bits (cc) {N,Z,C,V} in CPSR are changed. updates the N and Z flags according to the result Does not affect the C or V flags. BEQ Branch if result equal to zero (branch if Z=1) 23
24
Step2b: Read IO pins, check Sw1 status
4)Loop LDR R3, =IO0PIN; read SW1(p0.20=bit20 of IOPIN) 5) LDR R3, [R3] 6) TST R3, #SW1 ;if SW1 depressed (IOPIN=0) ;(R3=0) and (#SW1) result is 0, Z flag =1 7) BEQ onled ;if SW1 pressed LEDon,(branch if Z=1) ;If bit 20 of R3 is zero (key depressed) then the zero flag will set by “TST R3,#SW1” and this causes the BEQ to branch to ledon. See Read all 32 GPIO p0.0-P0.31 into R3 Since SW1 EQU 0x ; p0.20 (bit20 of IO0PIN)as SW1(input) Test using TST R3, #SW1 ;meaning“R3 and 0x ”status, check only p0.20 If it is on branch to onled 24 CENG2400 Ch7: driving parallel loads v.3c
25
12) B loop 9) LDR R1, =RED_LED ; otherwise LEDoff 10) LDR R0, =IO0CLR
Step3:Turn off LED if sw1 is not depressed Defined earlier IO0CLR EQU 0xE002800C RED_LED EQU 0x ; p0.22=RED LED 9) LDR R1, =RED_LED ; otherwise LEDoff 10) LDR R0, =IO0CLR 11) STR R1,[R0]; writes 0x into IO0CLR ; bit 22(p0.22 of IOPIN cleared 12) B loop
26
Step4:Turn on LED if SW1 is depressed Defined earlier IO0SET. EQU
Step4:Turn on LED if SW1 is depressed Defined earlier IO0SET EQU 0xE RED_LED EQU 0x ; p0.22=RED LED 13)Onled; 14) LDR R1,=RED_LED; on the LED 15) LDR R0, =IO0SET 16) STR R1,[R0]; writes 0x into IO0SET ; bit 22(p0.22) of IOPIN set 17) B loop 18) END 26
27
RED_LED EQU 0x00400000; p0.22=RED LED SW1 EQU 0x00100000; p0.20 as SW1
IO0DIR EQU 0xE IO0SET EQU 0xE IO0CLR EQU 0xE002800C IO0PIN EQU 0xE RED_LED EQU 0x ; p0.22=RED LED SW1 EQU 0x ; p0.20 as SW1 Exercise 7.3a: Key is not depressed IOPIN=0x Show R0,R1, CPSR( N,Z,V,C ) Current Program Status Register =CPSR, N=negative, Z=zero, V=overflow, C-carry ;_________________________________ R0 , R1 , R3 , NZVC 4)Loop LDR R3, =IO0PIN; read SW1 (p0.3) ; ______________________________ 5) LDR R3, [R3] ;______________________________ 6) TST R3, #SW1;if SW1 depressed ;______________________________ 7) BEQ onled ;if SW1 depressed LEDon ;______________________________ 8); 9) LDR R1, =RED_LED;otherwise LEDoff ; ____________________________ 10) LDR R0, =IO0CLR ; _____________________________ 11) STR R1,[R0] ; _____________________________ 12) B loop ; _____________________________ 13) onled;------ON LED 14) LDR R1, =RED_LED; on the LED ; _____________________________ 15) LDR R0, =IO0SET ; _____________________________ 16) STR R1,[R0] ; _____________________________ 17) B loop ; _____________________________ 18) END
28
RED_LED EQU 0x00400000; p0.22=RED LED SW1 EQU 0x00100000; p0.20 as SW1
IO0DIR EQU 0xE IO0SET EQU 0xE IO0CLR EQU 0xE002800C IO0PIN EQU 0xE RED_LED EQU 0x ; p0.22=RED LED SW1 EQU 0x ; p0.20 as SW1 Exercise 7.3b: Key is depressed IOPIN=0x Show R0,R1, CPSR( N,Z,V,C ) Current Program Status Register =CPSR, N=negative, Z=zero, V=overflow, C-carry ;_________________________________ R0 , R1 , R3 , NZVC 4)Loop LDR R3, =IO0PIN; read SW1 (p0.3) ; ______________________________ 5) LDR R3, [R3] ;______________________________ 6) TST R3, #SW1;if SW1 depressed ;______________________________ 7) BEQ onled ;if SW1 depressed LEDon ;______________________________ 8); 9) LDR R1, =RED_LED;otherwise LEDoff ; ____________________________ 10) LDR R0, =IO0CLR ; _____________________________ 11) STR R1,[R0] ; _____________________________ 12) B loop ; _____________________________ 13) onled;------ON LED 14) LDR R1, =RED_LED; on the LED ; _____________________________ 15) LDR R0, =IO0SET ; _____________________________ 16) STR R1,[R0] ; _____________________________ 17) B loop ; _____________________________ 18) END
29
Exercise 7.4 : Modify the previous program to initial the IO pins for the following circuit (Green LED is not used here) P0.6 P0.5
30
(i) To drive the GPIO (General Purpose Input Output) Using C
A much simpler solution 30
31
A simple C program GPIO.c When SW1 is depressed, RED-LED is on
#include <lpc21xx.h> //define IO0PIN ,IO0DIR.. Etc // see #define RED_LED 0x //set p0.22 as RED LED #define SW1 0x //set p0.20 as SW1 int main(void) { long tmp; // variable for temp storage of port 0 status IO0DIR = RED_LED; // set p0.22 as output while(1) { tmp =IO0PIN & SW1;//read SW1(p0.20)depressed=0 If(tmp==0) ; //What happens “if (tmp!=0)” is used? IO0SET = RED_LED; //if SW1 pressed LED is on else IO0CLR = RED_LED; // otherwise off the LED }
32
Summary Learned how to interface parallel GPIO (General Purpose Input Output) loads Learned how to drive LEDs using the ARM MCU
33
2) Analog-to-Digital converter (ADC)
CEG2400 Ch8 Peripherals-1 V3b
34
2) Analog-to-Digital converter ADC
Analog voltages ADC 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 (in robot car) , temperature sensor, force sensor. Video demo:
35
Exercise 3 Code for Analog-to-Digital ADC converter pin assignment for sensor pins
#include <lpc21xx.h> #define ADCR (*((volatile unsigned long *) 0xE )) #define ADDR (*((volatile unsigned long *) 0xE )) int main(void) { .... //From line 92 of ARMdemo06.c // We must initialize the IO pin //before using ADC channel 94) PINSEL1 = 0x ; // set p0.27 to ad0.0 95) PINSEL1 |= 0x ; // set p0.28 to ad0.1 96) PINSEL1 |= 0x ; // set p0.29 to ad0.2 97) PINSEL1 |= 0x ; // set p0.30 to ad0.3 98) PINSEL1 |= 0x ; // set p0.25 to ad0.4 Question (a) : Write one instruction to replace of all instructions from line 94 to 98 . Answer: ? ______________________________________________ Question (b) :How to set the system to use ad0.5 Answer:? ________________________________________________
36
PINSEL1 Pin assignment for AD0. 1 94) PINSEL1 = 0x00400000; // set p0
PINSEL1 Pin assignment for AD0.1 94) PINSEL1 = 0x ; // set p0.27 to ad0.0 PINSEL1 = xxxx xxx xxxx xxxx xxxx xxxx bit Bit23:22 Bit23:22=01 Bit for ADC Other bits are For other purposes
37
Exercise 4 What is purpose of PINSEL1 register?
ANS: ?____________________________ Where is the location of PINSEL1 register? What is the pin number of P0.28 How to set P0.28 pin to be the function of ADC0.1?
38
PINSEL1 Pin assignment for AD0.1 94) PINSEL1 |= 0x ; // set p0.28 to ad0.1 PINSEL1 = xxxx xxxx bit Bit25:24 Bit25:24=01 Ref: Volume 1: LPC213x User Manual UM10120
39
Exercise 5: Fill in the blanks in the flow diagram of this program Code for Analog-to-Digital ADC converter In C:\Keil\ARM\INC\Philips\lpc21xx.h, ADCR=0xE ADCR=0xE , ADDR=0xE Conversion not done From line 71 of ARMdemo06.c //(1) ADC interface 71) int read_sensor(int channel) 72) { 73) int temp; 74) 75) ADCR=0x1 << ________;//__select channel___ 76) ADCR|=_____________; // set up the control bits_ 77) 78) while(((temp=ADDR)& ___________)==0); //MSB =1 meaning ADC is done 79) temp>>=6; //?________________________________(bit6->15: 10-bit conversion result) 80) temp&=0x3ff;//TEMP=output IS 0-3V PRESICION IS (10 bits: 2^10=1024 levels) 81) 82) return (temp*33); //?? Why temp*33__________________________ 83) } ..... Loop until ADC is done Explanation ?_______________ Conversion Done Return temp*scale
40
ADCR -- one (AD0) for lpc2131 line 75) ADCR=0x1<<_______(fill in the blank); line 76) ADCR|=_______(fill in the blank);//operational,start convert ADCR= xxxx xxxx bit Point to which channel Bit 15:8=10b=2 CLKDIV=2+1=3 Freq=13.824MHz/3=4.608MHz Ref:
41
ADCR -- one (AD0) for lpc2131 line 75) ADCR=0x1<<________(fill in the blank); line 76) ADCR|=_________(fill in the blank);//operational,start convert ADCR= xxxx xxxx bit Bit21=1 operational Bit24=1 Ref:
42
Polling for the completion of Analog-to-digital conversion 78) while(((temp=ADDR)&_________(fill in the blank))==0); ADDR= xxxx xxxx xx //MSB =1 meaning ADC is done //if bit 31 of ADDR is 1, it is done //bit 15:6 contain the result ADC result CEG2400 Ch8 Peripherals-1 V3b
43
78)while(((temp=ADDR)&0x80000000)==0);
Find the Analog-to-digital converted result ADC result xx xxxx xxxx ADDR= xxxx xxxx xx result 78)while(((temp=ADDR)&0x )==0); 79) temp>>=6; temp>>6; = xx xxxx xxxx 80) temp&=0x3ff;//TEMP=output IS 0-3V PRESICION is 1024 (10bit ADC precision) temp&=0x3ff;= xx xxxx xxxx 82) return (temp*33);// make it a full integer. polling result CEG2400 Ch8 Peripherals-1 V3b
44
3) Digital-to-Analog converter (DAC)
CEG2400 Ch8 Peripherals-1 V3b
45
3)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 Digital-to-Analog converter DAC -- Convert digital codes into an analog signal Analog value
46
DAC reg. 10-bit
47
Universal Asynchronous Receiver / Transmitter UART (serial port)
CEG2400 Ch8 Peripherals-1 V3b
48
4) Universal Asynchronous Receiver / Transmitter UART (serial port)
//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 , part1 36) U0DLM = 0x00; //Baud rate setting , part 2 37) U0LCR = 0x03; //DLAB=0 to complete the setting } 0x0a=New line , 0x0d=carriage return in ARM06Demo.c, and
49
line34) U0LCR = 0x83; //8 bit length ,DLAB=1 //U0LCR = 1000 0011b
CEG2400 Ch8 Peripherals-1 V3b
50
UART0(baudrate)=PCLK/(16*(16*0+15))
Exercise 6: Baud rate setting 35) U0DLL = 0x0F;//=15 36) U0DLM = 0x00;//=0 Because , PCLK=13.824MHz UART0(baudrate)=PCLK/(16*(16*0+15)) =13.824MHz/3840=57600 is the baud rate
51
Getchar() in “C” polling method (not interrupt)
40) char getchar(void) { 41) volatile char ch = '0'; 42) 43) while ((U0LSR & 0x1)==0)//wait until a byte is received 44) ; 45) ch = U0RBR;// receive character //(U0RBR - 0xE000 C000, 46) 47) return ch; 48) } Yes Is bit1 of U0LSR==0? (receive buffer empty?) polling No, receive character
52
Sendchar() in “C” polling method (not interrupt)
Is bit6 of U0TH==0? (Transmitter contains valid data, previous data not sent yet?) Yes No, Transmit Next character 49)//////////////////////////////////////////////////////////// 50)void sendchar(char ch) { 51) while( (U0LSR & 0x40)==0 ); 52) 53) U0THR = ch;// Transmit next character 54) } // at 0xE000 C000 bit7:0 polling Bit of U0LSR at 0xE000 C014
53
Print(), 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;
54
Ascii table from http://enteos2. area. trieste
55
Exercise 7: putint( 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); //%=modulus 66) sendchar('0' + (count/100) % 10); 67) sendchar('0' + (count/10) % 10); 68) sendchar('0' + count % 10); 69)} Question: If “count” is “2597”, what hex numbers have been sent to the serial port? Answer: ?_________________________ Print an ASCII character representing that digit at one time,
56
UART main print example
int main(void) { ...... // Initialize IO pin before using TXD0 and RXD0 PINSEL0 = 0x ; // 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 (ver1.3) *"); NEWLINE; print("**"); NEWLINE; print("* I2C (Master Receiver) Test Program (ver1.3)*"); NEWLINE;
57
11-Sep-18 Chapter 6 : Serial Interface -- UART (universal asynchronous receiver transmitter ) 57
58
11-Sep-18 Overview Introduction to the universal asynchronous receiver transmitter : UART. UART Hardware Interface UART Software Interface Initialize the UART Hardware Sending data using handshaking 58
59
RS232 standard and application, e.g.
11-Sep-18 1) Introduction: Serial interface (universal asynchronous receiver transmitter : UART) RS232 standard and application, e.g. RS232 standard 3 wires +10 V=‘0’=SPACE -10V=‘1’=MARK Pin3 Pin2 pin5 Pin2 Pin3 pin5 RS232 port (UART) RS232 port (UART) 59
60
Serial data transmission means sending data bits one by one.
11-Sep-18 U Universal A Asynchronous R Receiver T Transmitter UART for data communication Serial data transmission means sending data bits one by one. Asynchronous transmission means a data (including one start bit , 8-bit data, and stop bits) can be sent at any time. 60
61
Universal asynchronous receiver transmitter : UART
11-Sep-18 Universal asynchronous receiver transmitter : UART RS232 is a serial communication standard Since it is asynchronous, no external clock is needed, only 3 wires are required for the simplest RS232 connection (GND, tx, rx) Bit 0 to 7 (least sig. bit first ) +10V=Logic 0=space -10V= Logic 1=mark Start stop Exercise: Sketch Bit Patterns for character A and B 61
62
ASCII table http://www.cs.utk.edu/~pham/ascii_table.jpg
11-Sep-18 ASCII table 62
63
EXAMPLES Sending ASCII ‘5’=0x35=0011 0101b Oscilloscope Probe
11-Sep-18 EXAMPLES Sending ASCII ‘5’=0x35= b Oscilloscope Probe The ASCII character has 10 bits (1 start+8 data + 1 stop bit) Start bit=0 Stop bit=1 What is this code ? (stop bit=1) Least Sign. Bit first T=bit period =1/baud rate 63
64
Each bit lasts for (1/Baud rate) seconds
11-Sep-18 If the baud rate is bits per second Define ‘bit period’ :the amount of time to transmit a logic 1 or 0. (bit period= 1/57600 seconds) Each bit lasts for (1/Baud rate) seconds bit period = 1/baud rate =(1 / 57600) seconds=17.4 us Exercise: What is the “bit period” if baud rate is 2400? 64
65
11-Sep-18 Hello world example Manual ( Application Note astartup.s ahello.s (appendix1)
66
11-Sep-18 2) UART Hardware Interface
67
UART Hardware Interface
11-Sep-18 UART Hardware Interface ARM7 output signals are 0->3.3V. A MAX232 chip is needed to translate the 0->3.3V levels to the +/-10V RS232 levels (VCC=5V)
68
ARM system support for UART: Memory Map
Ref: page 9 of ARM7-LPC213x-user manual (Hardware Guide, UM10120) 11-Sep-18 ARM system support for UART: Memory Map Software interface involves appropriately programming registers in the memory map to manipulate the hardware i.e. from 0xE000 C000 68
69
Like office addresses for mail delivery
11-Sep-18 Like office addresses for mail delivery UART Office addresses from floor 0xE000 C000 to 0xE000 C030 Memory address space 69
70
Reminded that ARM7 has these registers and memory addresses
11-Sep-18 UART Office addresses from floor 0xE000 C000 to 0xE000 C030 Reminded that ARM7 has these registers and memory addresses Registers (R0-R15) ..etc 32-bit memory addresses (0x to 0xFFFF FFFF)
71
Revision Name the usages of these memory locations in an ARM (LPC21xx) processor 0000 7FFFh ?_______ ROM, code 4000 FFFFh ?_______ RAM, data,Stack E000 C000 E000 C030 ?_______ (UART)
72
11-Sep-18 UART Register address map : 0xE000 C000 to 0xE000 C030 U0RBR (read only ) and U0THR (write only) share the same address but have no conflict (because one is for “write” another is for “read”). Ref: table 96 of ARM7-LPC213x-user manual (Hardware Guide, UM10120) DLAB=0 DLAB=1 72
73
A general description of the software Interface ahello.s
11-Sep-18 3) UART A general description of the software Interface ahello.s 73 CEG2400 Ch6. Serial Interface -- UART V3a
74
Part3: subroutine to initialize uart0
11-Sep-18 Part3: subroutine to initialize uart0 File name : ahello.s ; UART0 registers U0RBR EQU 0xE000C000 U0THR EQU 0xE000C000 U0IER EQU 0xE000C004 U0IIR EQU 0xE000C008 U0FCR EQU 0xE000C008 U0LCR EQU 0xE000C00C U0LSR EQU 0xE000C014 U0SCR EQU 0xE000C01C U0DLL EQU 0xE000C000 U0DLM EQU 0xE000C004 PINSEL0 EQU 0xE002C000 ; User Initial Stack & Heap AREA |.text|, CODE, READONLY EXPORT __main __main BL iuart0 loop MOV R0, #'H' BL writec MOV R0, #'e' MOV R0, #'l' MOV R0, #'o' MOV R0, #' ' MOV R0, #'w' MOV R0, #'r' MOV R0, #'d' MOV R0, #'\n' B loop iuart0 MOV R1, #0x5 ; PINSEL0 = 0x5 LDR R0, =PINSEL0 STR R1, [R0] MOV R1, #0x7 ; U0FCR = 0x7 LDR R0, =U0FCR STRB R1,[R0] MOV R1, #0x83 ; U0LCR = 0x83 LDR R0, =U0LCR MOV R1, #0x0f ; U0DLL = 0x0f LDR R0, =U0DLL MOV R1, #0x00 ; U0DLM = 0x0 LDR R0, =U0DLM MOV R1, #0x03 ; U0LCR = 0x03 BX R14 ; return from subroutine writec LDR R1, =U0LSR LDRB R1, [R1] TST R1, #0x40 BEQ writec LDR R1, =U0THR STRB R0, [R1] END Part1: define registers Part2: main program Part4: subroutine to write data to serial port 74
75
Part 1 of ahello.s ;send hello to the serial line
11-Sep-18 Part 1 of ahello.s ;send hello to the serial line Define the address locations of the UART0(Universal asynchronous receiver transmitter) registers ; UART0 registers U0RBR EQU 0xE000C000 U0THR EQU 0xE000C000 U0IER EQU 0xE000C004 U0IIR EQU 0xE000C008 U0FCR EQU 0xE000C008 U0LCR EQU 0xE000C00C U0LSR EQU 0xE000C014 U0SCR EQU 0xE000C01C U0DLL EQU 0xE000C000 U0DLM EQU 0xE000C004 PINSEL0 EQU 0xE002C000 RS232 standard 3 wires +10 V=‘0’=SPACE -10V=‘1’=MARK Pin3 Pin2 pin5 Pin2 Pin3 pin5 RS232 port (UART) RS232 port (UART) 75
76
11-Sep-18 Define Uart Registers in assembly (in lpc21xx.h (ARM7) generated by uvision3 of the Keil assembler tool) ; UART0 registers U0RBR EQU 0xE000C000 U0THR EQU 0xE000C000 U0IER EQU 0xE000C004 U0IIR EQU 0xE000C008 U0FCR EQU 0xE000C008 U0LCR EQU 0xE000C00C U0LSR EQU 0xE000C014 U0SCR EQU 0xE000C01C U0DLL EQU 0xE000C000 U0DLM EQU 0xE000C004 PINSEL0 EQU 0xE002C000 EQU Pseudo instruction Define a constant X EQU 2 This is an assembler directive that is used to give a value to a label name. In this example it assigns X the value 2. Thus when X is used elsewhere in the code, the value 2 will be substituted (similar to using a #define to set up a constant in the C language). 76
77
Part 2 of ahello.s ;send hello to the serial line
11-Sep-18 ; User Initial Stack & Heap AREA |.text|, CODE, READONLY EXPORT __main __main BL iuart0 loop MOV R0, #'H' BL writec MOV R0, #'e' MOV R0, #'l' MOV R0, #'o' MOV R0, #' ' MOV R0, #'w' MOV R0, #'r' MOV R0, #'d' MOV R0, #'\n' B loop Part 2 of ahello.s ;send hello to the serial line In this program, it writes “hello world” to the serial line. Print hello world
78
Part 3 of ahello.s, (subroutine iuart0: initialize uart0 registers)
11-Sep-18 Part 3 of ahello.s, (subroutine iuart0: initialize uart0 registers) iuart0 MOV R1, #0x5 ; PINSEL0 = 0x5 LDR R0, =PINSEL0 STR R1, [R0] MOV R1, #0x7 ; U0FCR = 0x7 LDR R0, =U0FCR STRB R1,[R0] MOV R1, #0x83 ; U0LCR = 0x83 LDR R0, =U0LCR MOV R1, #0x0f ; U0DLL = 0x0f LDR R0, =U0DLL MOV R1, #0x00 ; U0DLM = 0x0 LDR R0, =U0DLM MOV R1, #0x03 ; U0LCR = 0x03 BX R14 ; return from subroutine 78
79
11-Sep-18 Part 4 of ahello.s (subroutine writec: write one character to the serial line) writec LDR R1, =U0LSR LDRB R1, [R1] TST R1, #0x40 BEQ writec LDR R1, =U0THR STRB R0, [R1] BX R14 ; return from ;subroutine END 79
80
4) Detailed description of subroutines “iuart0” in ahello.s
11-Sep-18 4) Detailed description of subroutines “iuart0” in ahello.s Initialize the UART Hardware Setup data into UART registers UART registers 0xE000 C000 to 0xE000 C030 80 CEG2400 Ch6. Serial Interface -- UART V3a
81
11-Sep-18 Subroutine iuart0: Software to initialize the UART in the LPC21xx (ARM7) chipset inside iuart0 Step 1: setup pins configure pin function select reg. i.e. pin 19=TXD Step 2 configure FIFO control reg. Step 3 configure line control reg. (start, stop bits) Step 4 Configure baud rate 81
82
Part 3 of ahello.s Initialize uart0 (iuart0) in ahello.s (Appendix1)
11-Sep-18 Part 3 of ahello.s Initialize uart0 (iuart0) in ahello.s (Appendix1) step4 Step1: setup pins MOV R1, #0x0f LDR R0, =U0DLL STRB R1,[R0] MOV R1, #0x00; U0DLM = 0x0 LDR R0, =U0DLM STRB R1,[R0] iuart0 MOV R1, #0x5 LDR R0, =PINSEL0 STR R1, [R0] MOV R1, #0x7 LDR R0, =U0FCR STRB R1,[R0] MOV R1, #0x83 LDR R0, =U0LCR step2 step3 82
83
Step1 of iuart0: pin configuration=0x05
11-Sep-18 Step1 of iuart0: pin configuration=0x05 Configure the pin for serial interfaces Arm7 pins are multi functions, depending on initialization. E.g. configure pin-symbol “p0.0” (pin19 or p0.0) to be the serial transmission pin “TXD” You must set Bit 1:0 of address 0xE002 C000=“01”. 83
84
11-Sep-18 Exercise 6.1 : which pin (what Px,y) is RXD? PINSEL0--pin function select reg. [0XE002 C000]=0x05 bit0, bit 2are 1 Somewhere inside there Setup Pin19 or P0.0 to have the function of TXD 84
85
11-Sep-18 Exercise 6.1 : which pin (what Px,y) is RXD? Answer: P0.1or pin21 PINSEL0--pin function select reg. [0XE002 C000]=0x05 bit0, bit 2are 1 Setup Pin19 or P0.0 to have the function of TXD Answer: Setup Pin21 or P0.1 to have the function of RXD 85
86
Step2 of iuart0:—setup FIFO control
11-Sep-18 Step2 of iuart0:—setup FIFO control Step4 Set Baud= 57600 Step1: setup pins iuart0 MOV R1, #0x5 LDR R0, =PINSEL0 STR R1, [R0] MOV R1, #0x7 LDR R0, =U0FCR STRB R1,[R0] MOV R1, #0x83 LDR R0, =U0LCR MOV R1, #0x0f LDR R0, =U0DLL STRB R1,[R0] MOV R1, #0x00; U0DLM = 0x0 LDR R0, =U0DLM STRB R1,[R0] Step2: setup First IN First Out ( FIFO) control step3 What do all of these values do? 86
87
Step2:U0FCR--FIFO control set reg=0x07 [U0FCR]=0x07
11-Sep-18 Step2:U0FCR--FIFO control set reg=0x07 [U0FCR]=0x07 Set all bit0,1,2=“111” for FIFO control 87
88
11-Sep-18 Step3 of iuart0: Exercise 6.2, Circle which bits are set,discuss their functions. U0LCR—line control set reg=0x83 [U0LCR]=0x83 88
89
11-Sep-18 Exercise 6.2a, which bits are set, and their functions. Answer:8-bit character length, 1 stop bit, no parity, enable division latch Step3:U0LCR—line control set reg=0x83, set reg [U0LCR]= =0x83= B [U0LCR]= 0x83= B Exercise2b: Repeat exercise2a but 7bit data, 1 stop bit , no parity Answer: U0LCR= b=0x82 89
90
11-Sep-18 Step2 of iuart0:U0DLL—set baud rate=0x0F [U0DLL]=0x0F, set Baudrate=576000 U0DLL = Fpclk / (16 * Baudrate) If we want baud (Fpclk=13.824MHz) then U0DLL=15=0x0f (U0DLM(for msc)=00;U0DLL(for lsb)=15) Check datasheet of how it should be set. Exercise4a: How to set the baud rate to be 9600? Answer4: U0DLL9600 = Fpclk / (16 * Baudrate) =13.824MHz / (16 * 9600)=90 Also U0DLM9600 =00 Exercise4b: How to set Baud rate = 2400? Useful tool: /(16(16M+L))=2400 /(16*2400)=360=16M+L M=?_____, L=?_____ 90
91
11-Sep-18 5) Detailed description of subroutines “writec” : Sending data using handshaking 91
92
11-Sep-18 Subroutine Writec: Basic concept of sending a character using the serial port The sending procedure is slower (e.g. only bits per second) than your program, so it needs to ask permission to send Writec() Sending rate (Baud rate) is slower than your program can gen. data True even for usb2 receiver Send-buffer Is transmitter Buffer empty ? no yes Send data
93
Read buffer status U0LSR(0xE000 C014)
11-Sep-18 Read buffer status U0LSR(0xE000 C014)
94
11-Sep-18 U0LSR—line status at U0LSR(0xE000 C014) (if bit6=TEMT (transmitter empty)=1 you can send next data Bit Bit0
95
Recall: TST and BEQ BEQ TST
11-Sep-18 Recall: TST and BEQ TST Same as AND (logical AND) except result of operation is not stored. Only the condition code bits (cc) {N,Z,C,V} in CPSR are changed. updates the N and Z flags according to the result Does not affect the C or V flags. BEQ Branch if result equal to zero (branch if Z=1) 95
96
1)Writec ; subroutine to send data in R0 to serial out
11-Sep-18 Write character in R0 subroutine when TEMT=1 TEMT=0 meaning transmitter is not empty, not ready to send TEMT=1 meaning transmitter is empty, ready to send 1)Writec ; subroutine to send data in R0 to serial out 2) LDR R1, =U0LSR; (line status reg) 3) LDRB R1, [R1]; get line status TST R1, #0x40 ;TEMT(bit 6 of ;U0LSR=1=buffer_empty)=0Z=1 5) BEQ writec ;if Z=1(buffer_not_empty), ;loop back writec to wait 6) LDR R1, =U0THR;U0THR=transmit reg 7) STRB R0, [R1]; send data out 8) BX R14 ; return from subroutine TST : same as “AND”, but result is not saved , affect Z bit in cpsr
97
11-Sep-18 Flow diagram of Writec of ahello.s TEMT=0 meaning transmitter is not empty, not ready to send TEMT=1 meaning transmitter is empty, ready to send Reason: The sending speed (Bit per second) is slower than your program, need to ask permission to send Writec( ); write data in R0 4)TST R1, #0x40 ;TEMT=0 (buffer_not _empty: transmitter not ready) Z=1 ;TEMT=1 (buffer_empty: transmitter is ready) Z=0 Brach if Z=1 5) BEQ writec If Z=1 Is the transmitter buffer is not empty, loop back. Because TEMT=0 Z=1, BEQ writec ;if Z=1,loop back to wait Otherwise send data If Z=0 6) LDR R1, =U0THR 7) STRB R0, [R1]; send data out
98
Summary Studied serial interface in ARM
11-Sep-18 Summary Studied serial interface in ARM Studied handshaking in interfacing
99
11-Sep-18 End 99
100
Summary Studied peripherals of the LPC213x ARM processor.
101
Appendix
102
Appendix (1) Watchdog timer register setting
If the system doesn’t give me any signals for a period of time (say 2 seconds), that means it hangs, so I will Press the reset bottom
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.