JX-2148
LPC2148 ARM7-32 bit Microcontroller Education board
1. Kit Contents
In standard package of JX-2148 board include : 1. JX-2148 Education board 2. CX-232 serial port cable 3. AWG#22 wire jumper, 7cm. length 4. Documentation 5. CD-ROM 6. GLCD5110 graphic LCD module x1 x1 x 10 x1 x1 x1
To run this education board youll need: DC adaptor +6Vdc 500mA (maximum +9Vdc)
l Standard JTAG connector l USB 2.0 Full Speed Interface (USB connector type B). JX-2148 board provides a
USB interface connector that interfaces ot the on-chip USB peripheral of the LPC2148 device. You may configure the board as self-powered or USB powered device.
l Dual Serial Ports. JX-2148 provides standard DB9 connectors for both of the
LPC214xs serial ports. UART-0 for communication and support In-System Programming (ISP), UART-1 for serial communication and select to connect ESD-200 Bluetooth module (optional) by jumpers.
l SD/MMC socket. The JX-2148 provides one SPI module to interface SD/MMC
memory socket.
l A PS/2 jack for interface Keyboard or Mouse. l 2 of push-button switches with resistor pull-up.
l 2 of LED indicators l Analog Voltage Control for ADC Input. JX-2148 provides an adjustable analog
voltage source for testing the A/D converter feature of the LPC2148.
l A small buzzer for sound experiment l Mini-breadboard 170 points contact. l 32kHz crytal and +3V battery backup for real-time clock system within MCU. l +3.3V on-board regulator for MCU and +5V for PS/2 circuit. l Polarity voltage protection.
3. System requirements
To use the JX-2148 Education Board, the following item must be prepared :
Top view
"
&
CS
MOSI
RESET
+ +4.5-9V ON OFF
JTAG/DEBUG port
JX-2148
DEBUG EN.
12MHz
UART CH.1
ON USB
32.768kHz LPC2148
P1.19 P1.18 P1.17 P1.16
+3V BATT
LITIUM BATTERY 3V PIEZO 0.12-0.13 PS/2 0.22 CLK 0.16 DAT 0.15
G 0.02 0.03 +
TWI-0
PORT
SPI-0
USB
PORT
0.21
SWITCH
ISP
0.28
0.29
0.30 (A/D)
ESD-02
GND
GND
GND
GND
GND
GND
Vcc
5. JX-2148 operation
The operation of JX-2148 board has 3 main sections following : (1) LPC2148 microcontroller unit (2) Power supply (3) Input/Output circuit Microcontroller unit consist of NXPs LPC2148 and 2 of Clock oscillator circuit; 12MHz main clock oscillator and 32.768kHz for real-time clock. The full schematic can see in the figure 2. Power supply of JX-2148 has 2 regulator. One is +3.3V. It receives +6 to +16V from external DC adaptor. The 3.3V regulated circuit supplies to the microccontroller unit and many I/O devices. Another one is +5V for supply PS/2 circuit. Many I/O devices are installed on the JX-2148 board. Includes LED, Push-button switches, Variable resistor for A/D converter circuit, Two of RS-232 serial port interface, PS/ 2 jack, MMC/SD socket, USB port interface, JTAG interface for many debugger such as ULINK from Keil or Olimex JTAG adaptor or Wiggler from Macraigor, ESD-02 Bluetooth connector, Free I/O microcontroller port and a Mini-breadboard 180 points for construction the experimental circuit.
D1 1N5819 R2 22k
CLK DAT CP1
+3.3V
+5V
+3.3V
K1 USB
R3 22k
RES RES
+5VUSB USBDUSBD+
SD/MMC
R10 10k R11 10k
Vdd OUT
+V SHD
WP1 Vss1 Vss2 CS DI DO SCK
BD1 1A50V +3.3V C3 0.1 F R8 510 R5 27 +3.3V 16 Vdd V+ V10 T1I T1O IC4 MAX3232 9 R1O R1I 10 USBD+ USBD- P0.23 P0.22 P0.21 VBUS 17 P0.31/ UP_LED P0.20 P0.19 P0.18 P0.17 P0.16 P0.15 SSEL1 MOSI1 MISO1 SCK1 41 P0.13 39 P0.12 37 P0.11 R19 220
RTS UART
SW1 POWER +3.3V +3.3V Q1 R26 DTA114 1.5k R13 150 R9 510 SP1 PIEZO 54 53 47 46 45 C11 0.1 F 15 2 6 7 C12, C13 0.1 F x2 MOD1
IC1 78R33
PS2
R1 510
IN
OUT
LED1 ON
C2 1000 F 6.3V
16 35
CTS_UART
JP4 RTS1_SELECT
6 7 8 9
+3.3V
R16 10k *4
SPI-0
P0.07/SSEL0 P0.06/MOSI0 P0.05/MISO0 P0.04/SCK0 GND +3.3 V
IC3 LPC2148
C14 0.1 F
4 5
C2+ C2-
C1+ C1-
1 3
UART1 RS-232
C15 0.1 F
JP1 DBG_EN K5 C4 15pF RTCX1 5 RTCX2 C5 15pF XTAL1 32.768kHz 62 X1 C6 33pF +3.3V X2 C7 33pF XTAL2 12MHz 63 Vref 61 3
DIS
1 3 5 7 9 11 13 15 17 19 P1.24 12 P1.25 8 P1.26/RTCK 4 P1.27/TDO 48 P1.28/TDI 44 P1.29/TCK 40 P1.30/TMS 36 P1.31/TRST 31 P0.07/SSEL0 30 P0.06/MOSI0 29 P0.05/MISO0 27 P0.04/SCK0
2 4 6 8 10 12 14 16 18 20
EN
IC5 MAX3232
+3.3V 16
C16 0.1 F 15 1 C17 0.1 F +3.3V 26 P0.03/SDA0 22 P0.02/SCL0 C18 0.1 F P0.03/SDA0 P0.02/SCL0 GND P0.01/RxD0 P0.00/TxD0 P0.14/BSL +3.3V 21 19 41 SW2 RESET 57
R21 10k
JTAG
TWI (I2C)
R17 10k
R18 10k
2 6 R1I T1O 12 R2O R2I 7 8 13 C19, C20 0.1 F x2 1 2 3 4 5 SW3 R23 47 RST RST P0.25/AD0.4/AOUT 9 P0.28/DA GND P0.30 IC6 KIA7031 GND +3.3V K7 6 7 8 9
JP2 Vref GND 23 D2 1N5819 C8 C10 D3 1N5819 C8-C10 0.1 F x3 59 49 C9 43 Vdd3-2(I/O) 51 Vdd3-3(I/O) 7 Vdd3(A) VssA Vbat Vss1 6 Vdd3-1(I/O)
+3.3V
ISP
+V +3.3 V
+3.3V
BATT1 +3V
R25 10k
BLUETOOTH ESD-200
BLUETOOTH ESD-200
ANTENNA
ESD-200 STATUS CTS RTS TxD RxD UART CH.1 STATUS CTS RTS TxD RxD UART CH.1
Select jumpers to define UART1 module of LPC2148 connected with RS-232 serial port
Select jumpers to define UART1 module of LPC2148 connected with ESD-200 Bluetooth module
Figure 3 Shows the step of select UART1 to connect RS-232 port and ESD-200 Bluetooth module (ESD-200 is optional device)
P0.21 and P0.22 are connected with LED in active low. P0.25 is D/A output of D/A converter module in LPC2148. P0.28 and P0.29 are connected push-button switches with pull-up resistors. P0.30 is connected with 10kW variable resistor for testing A/D converter module. P0.31 is used to control USB port interfacing and connected with REDY connection indicator circuit. P1.16 to P1.23 are free port. P1.24 and P1.25 are connected MMC/SD socket to testing card insertion. P1.26 to P1.31 are assigned as JTAG interface. In-system flash programming of JX-2148 will work via UART0 module. SW3 is ISP mode switch. Must pess this switch to enter ISP mode. LPC2148 has each of SPI (Serial Peripheral Interface) and SSP (Synchronous Serial Port) module. THe SSP module can work in SPI mode.On JX-2148 board define SSP to connect with MMC/SD card interface. The SPI module (SPI0) will reserve to connect addition SPI peripheral.
In A/D converter demonstration, on JX-2148 board provides one POT or variable resistor is connected at P0.30 ready to test with programming. In testing simple I/O port, the JX-2148 board provides 2 of LED that connected with P0.21 and P0.22. About input device, provides 2 of push-button switch to connect with P0.28 and P0.29. Two port pins P0.12 and P0.13 are connected with buzzer to sound generator. PS/2 interface need +5V. LM2931-5.0 IC is regulator +5V IC. It receive input voltage from main DC adaptor. USB interface use USBD+ and USBD- pin. They are connected limited current protection resistor. Port 0.23/VBUS is connected with +5V from USB port connector. Interface controlling port is function of P0.31 port pin. In connection must control this pin to logic 0. Thus, user can control the USB connections via software. REF jumper : use to select the reference voltage of A/D converter module. Normally connected with +3.3V. If need to use external reference voltage, user can do very easy step. Remove jumper out and connect the external reference voltage with middle pin of REF jumper. DEBUG EN. jumper : Select to enable degugging via JTAG connector.
(2) The License Agreement window is appeared. Select option I accept the terms of License Agreement and click on the Next button. (3) Click OK or Next button to accept all step until to Choose Install Set window. Select type of installation as Typical. Click on the Next button.
(4) Choose Install Folder window is appeared. Select drive and folder as C:\Program Files\CodeSourcery\Sourcery G++ Lite. Click on the Next button.
(5) Add product to the PATH? window is appeared for changing installation path. Select Modify PATH for current user heder for adding installtion path for speific user or select Modify PATH for all users. header for adding path for everyone. Click on the Next button.
(6) The Choose Shortcut Folder is appeared next. Click on other header and Next button.
(7) Software shows installation summary. Click on the Next button until software is installed successfully. Click on Done button to finish.
(2) The License Agreement window is appeared. Select option I accept the terms of License Agreement and click on the Next button. (3) Folder Selection window is appeared. Selecte drive and folder for installtion. For example is C:\Keil . Click on the Next button.
(4) Enter the Customer Information window. Type your information and e-mail address. After that the Next button will be active. Click this button to next step.
(6) The Keil uVision3 Setup Completed will be appeared. Click the box following the picture below and Finish button. The installation is finished.
8. How to develop code with Keil Vision3 and Sourcery G++ Lite compiler
This sections introduce you to the Keil development tools, and take you through the process using them with the JX-2148 board. Youll learn how to create the C code, compile, download and run a program on this board. Developing programs for the JX-2148 board is easy. The process is: (1) Create code using the Vision IDE. Compile with Sourcery G++ Lite compiler. (2) Download the program to the on-chip Flash of the JX-2148 Board by using LPC2000 Flash Utility or Flash Magic.
(2.1) At Select ARM Development Tools , select Use c Compiler (2.2) Set the new compiler at GNU-Tool-Prefix box as arm-none-eabi(2.3) At Cygnus Folder box, select the code location. Normally it is in C:\Program Files\CodeSourcery\Sourcery G++ Lite\ folder. Click on the OK button.
This example is set name as Test in C:\NXP2148 folder. Click on the Save button. (2) Select target chip on the Select Device for Target Target 1 window
(3) At Data base box, select Microcontroller manufacturer as NXP (founded by Philips) and select device as LPC2148. Click on the OK button.
(4) Select menu Project Option for Target Target1 . The Option for Target Target1 is appeared. Select Target tab. At Xtal (MHz): box, set clock frequency as 12.0. Its mean 12MHz clock frequency.
(5) Next, select Output tab. Click to mark at Create HEX File box for creating the hex file afrer compiling the project file.
(6) Select User tab. At Run User Programs After Build/Rebuild box, select Run #1 and type this command below into the box.
arm-none-eabi-size *.hex
(7) Click CC tab. At Optimization box, select No Optimization for setting normal compilation.
(8) At Linker tab, click at Do not use Standard System Startup Files for removing the mark. Next , check box at Enable Garbage Collection. At Linker Script File box, type the filename ARM.ld following the picture below. Click on the OK button to back the main window of program.
(9) Copy the start up file Startup.s and ARM.ld from CD-ROM into same project folder. (in this example is C:\NXP2148) (10) Back to main window, double-click at Source Group 1 message in Project Workspace box following the picture below.
(11) The Add Files to Group Source Group 1 window is appeared. At Files of type box, select the file type as Asm Source file (*.s; *.src; *.a). It shows location of Startup.s file from step (9). Click on the Startup.s file following click on the Add button and Close button.
(12) Select menu File New . The blank window will be appeared. Name is Text1. Type this C code below (// is single line comment) //// // Program for test board JX-2148 // File : LED.C // Target MCU : NXP LPC2148 12.0MHz, // : CCLK = 60 MHz, PCLK = 30 MHz // IDE : Keil RVMDK V3.22a, // Compiler : Code Sourcery G++ Lite 2008q1V126 // : LED Blink on GPIO0.22 //// #include <LPC214x.H> // LPC2148 MPU Register //// // Delay Time Function //// void delay(unsigned long int count) { while(count > 0) {count;} } int main(void) { PINSEL1 &= 0xFFFCFFFF; IODIR0 = 0x00400000; IOSET0 = 0x00400000; while(1) { IOCLR0 = 0x00400000; delay(1500000); IOSET0 = 0x00400000; delay(1500000); } }
// Set GPIO0.22 = GPIO Function // Set GPIO0.22 = Output // Set GPIO0.22 Pin (OFF LED)
// // // //
Clear Pin P0.22 (ON LED) Delay Set Pin P0.22 (OFF LED) Delay
(13) Save file as LED.c. At the Project Workspace; it displays all files that included in this project. This project has 2 files; Startup.s and LEC.c
(14) Compile this project file by selecte menu Project Rebuild all target files If no any error, the Output window reports messages 0 Error(s), 0 Warning(s) and file size 0f the hex file.
Finally, the result of compilation is .hex file for downloading to LPC2148 microcontroller by using LPC2000 Flash Utility or Flash Magic software
(4) In the first time, developers must set some parameter before l At Device box set as : - Device select to LPC2148 set to 12000 (12MHz)
l At Communication box set as : - Connected To Port select the serial port is connected - Use Baud Rate set baudrate. 9,600 bps is default.
(5) Set the JX-2148 board to ISP mode by press ISP SWITCH (its shaft will down) following press RESET switch once.
2
RESET
+ +4.5-9V ON OFF
JTAG/DEBUG port
JX-2148
DEBUG EN.
12MHz
UART CH.1
ON USB
32.768kHz LPC2148
P1.19 P1.18 P1.17 P1.16
+3V BATT
LITIUM BATTERY 3V PIEZO 0.12-0.13 PS/2 0.22 CLK 0.16 DAT 0.15
G 0.02 0.03 +
TWI-0
PORT
SPI-0
USB
PORT
0.21
SWITCH
ISP
0.28 1
0.29
0.30 (A/D)
If connection fail, the warning window will appear following the figure 41. Click Read Device ID button again and see the result. May be back to do in step (5) and (6) again.
(7) If connection complete, click at Browse button in Filename of Flash Programming. Select led.hex in path C:\led\led.hex. Next, click Upload to Flash button to download. After downloading complete the message File Upload Successfully Completed will show at the status bar.
(8) Developers can test the program by press ISP switch again to RUN mode (The switchs shaft will release) and press RESET switch once. Observe the operation. LED at P0.22 blink continuous.
(2) At Advance Option window, select Hardware Config tab and click to remove the marl at Use DTR control RTS box following the picture below. Click on the OK button.
(3) Open the HEX file at Step 3 - Hex File. (4) Select Verify after programming in Step 4 - Option if require to verify. (5) Click on the Start button in Step 5 - Start! to start the downloading.
(6) After downloading is finished, it shows Finish message. You can test the program by press ISP switch again to RUN mode (The switchs shaft will release) and press RESET switch once. Observe the operation. LED at P0.22 blink continuous.
Procedure :
1.1 Build new project, in name led 1.2 Write the program Listing P1-1. Compile to led.hex and download to microcontroller. 1.3 Run program. Observe the operation of LED at P0.21 and P0.22 Both LED will blink every 0.5 second.
RESET
+ +4.5-9V ON OFF
JTAG/DEBUG port
JX-2148
DEBUG EN.
12MHz
UART CH.1
TWI-0
PORT
SPI-0
ON USB
32.768kHz LPC2148
P1.19 P1.18 P1.17 P1.16
+3V BATT
LITIUM BATTERY 3V PIEZO 0.12-0.13 PS/2 0.22 CLK 0.16 DAT 0.15
G 0.02 0.03 +
PORT
0.21
SWITCH
ISP
0.28
0.29
0.30 (A/D)
//// // Program : Example for display LED // Description : LED Blink toggle at P0.21 and P0.22 // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : led.c // C compiler : Sourcery G++ Lite Compiler //// #include lpc214x.h // Header file for Phillips LPC2148 controller #define LED1_ON FIO0CLR = 0x00200000 // Red led 0.21 on #define LED1_OFF FIO0SET = 0x00200000 // Red led 0.21 off #define LED2_ON FIO0CLR = 0x00400000 // Red led 0.22 on #define LED2_OFF FIO0SET = 0x00400000 // Red led 0.22 off // Function for void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; Initial system clock // // MSEL = 4,PSEL = 2 // Feed process
// Feed process
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked PLL0CON=0x3; // Connect the PLL as the clock source PLL0FEED=0xAA; // Feed process PLL0FEED=0x55; MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch // (4 clks in this case) MAMTIM=0x4; VPBDIV=0x02; // PCLK at 30 MHz } // Function delay // void delay_ms(long ms) // delay 1 ms per count { long i,j; for (i = 0; i < ms; i++ ) for (j = 0; j < 4000; j++ ); } // Main Program // int main() { init(); // Initialize the system SCS = 0x03; // select the fast version of the I/O ports FIO0DIR |= 0x00600000; // Config. pin P0.22 and P0.21 as output while (1) // Infinite loop { LED1_ON; // Led at P0.21 ON LED2_OFF; // Led at P0.22 OFF delay_ms(500); // Delay 500 ms LED2_ON; // Led at P0.22 ON LED1_OFF; // Led at P0.21 OFF delay_ms(500); // Delay 500 ms } }
Listing P1-1 : led.c file of led project for LPC2148 output port experiment (continue)
LPC2148
Procedure :
2.1 Build new project, in name switch 2.2 Write the program Listing P2-1. Compile to switch.hex and download to microcontroller. 2.3 Run the program. Try to press switch at P0.28 and P0.29. Watch the operation of LED at P0.21 and P0.22. P0.28 switch will control LED at P0.21 and P0.29 switch will control LED at P0.22
RESET
+ +4.5-9V ON OFF
JTAG/DEBUG port
JX-2148
DEBUG EN.
12MHz
UART CH.1
G 0.02 0.03 +
TWI-0
PORT
SPI-0
ON USB
32.768kHz LPC2148
+3V BATT
LITIUM BATTERY 3V PIEZO 0.12-0.13 PS/2 0.22 CLK 0.16 DAT 0.15
PORT
0.21
SWITCH
ISP
0.28
0.29
0.30 (A/D)
//// // Program : Example for test switch // Description : Test switch at P0.28 and P0.29 for toggle LED at P0.21 and P0.22 // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : switch.c // C compiler : CodeSourcery G++ Lite Compiler //// #include lpc214x.h // Header file for Phillips LPC2148 controller // Function for Initial system clock // void init() { PLL0CFG=0x24; // MSEL = 4,PSEL = 2 PLL0FEED=0xAA; // Feed process PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; // PLL0FEED=0x55; while(!(PLL0STAT & 0x400)) ; // PLL0CON=0x3; PLL0FEED=0xAA; // PLL0FEED=0x55; Feed process Wait until PLL Locked // Connect the PLL as the clock source Feed process
MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch // (4 cclks in this case) MAMTIM=0x4; VPBDIV=0x02; // PCLK at 30 MHz } // Function Read input P0 // char inp0(char _bit) { unsigned long c; c = 1<<_bit; // Calculate digit to configuration for input port FIO0DIR &= ~c; // Set input port from parameter _bit return((FIO0PIN & c)>>_bit); // Read and return data bit } // Main Program // void main() { init(); // Initialize the system SCS = 0x03; // select the fast version of the I/O ports FIO0DIR |= 0x00600000; // P0.21 and P0.22 for output port FIO0SET |= 0x00600000; // OFF led P0.21 and P0.22 for initial while (1) // Infinite loop { if(inp0(28)==0) // Check switch P0.28 push? { while(inp0(28)==0); // Wait until release switch P0.28 FIO0PIN ^= (1<<21); // Toggle led at P0.21 } if(inp0(29)==0) // Check switch P0.29 push? { while(inp0(29)==0); // Wait until release switch P0.29 FIO0PIN ^= (1<<22); // Toggle led at P0.22 } } }
Listing P2-1 : switch.c file of switch project for LPC2148 input port experiment (continue)
char x; x = inp0(28);
Listing P2-1 : switch.c file of switch project for LPC2148 input port experiment (final)
LPC2148
+3.3V
10k P0.07/EINT2 5 S1
LPC2148
+3.3V P0.21 19 LED 0.21
P0.13 P0.12 41 39 220
510
SP1 PIEZO
RESET
JTAG/DEBUG port
DEBUG EN.
12MHz
UART CH.1
LITIUM BATTERY 3V PIEZO 0.12-0.13 PS/2 0.22 CLK 0.16 DAT 0.15
G 0.02 0.03 +
TWI-0
PORT
0.25 (D/A) G
SPI-0
PORT
10k
0.21
SWITCH
ISP
0.28
0.29
0.30 (A/D)
Procedure :
3.1 Build new project, in name ext_int2 3.2 Write the program Listing P3-1. Compile to ext_int2.hex and download to microcontroller. 3.3 Run program. Try to press swtich at P0.07 or EINT2 port pin. Observe the operation of LED at P0.21 and Piezo. Switch P0.07 pressing is used to control LED at P0.21 and generate sound for informing the external interrupt is happen.
//// // Program : Example for EINT2(External interrupt2) // Description : Test interrupt from switch press display by LED at P0.21 // : toggle and sound beep // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : ext_int2.c // C compiler : CodeSourcery G++ Lite Compiler //// #include lpc214x.h // Header file for Phillips LPC2148 controller #include sound.h // Header file for Phillips LPC2148 controller // Function for void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)) ; PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; Initial system clock // // MSEL = 4,PSEL = 2 // Feed process
// Feed process // Wait until PLL Locked // Connect the PLL as the clock source // Feed process
MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch // (4 cclks in this case) MAMTIM=0x4; VPBDIV=0x02; // PCLK at 30 MHz } // Interrupt service routine for EINT2 // void __attribute__ ((interrupt(IRQ))) isr_int2(void) { beep(); // Sound beep 1 time FIO0PIN ^= (1<<21); // Toggle LED at P0.21 EXTINT |= 0x4; // Clear interrupt flag EINT2 VICVectAddr = 0; // Acknowledge Interrupt }
Listing P3-1 : ext_int2.c file of ext_int2 project for LPC2148 external interrupt port experiment (continue)
// Main Program // int main() { init(); // Initialize the system SCS = 0x03; // select the fast version of the I/O ports FIO0DIR |= (1<<21); // Config. output P0.21 connect LED FIO0SET |= (1<<21); // OFF LED EXTMODE |= 0x4; // EINT2 Edge sensitive detection(Falling edge in this program) PINSEL0 = 0xC000; // Enable EINT2 at P0.7 VICVectAddr0 = (unsigned)isr_int2; // Register Interrupt service routine name VICVectCntl0 = 0x20 | 16; // EINT2 Interrupt VICIntEnable |= 1 << 16; // Enable EINT2 Interrupt while(1); // Infinite loop }
void beep()
Listing P4-1 : ext_int2.c file of ext_int2 project for LPC2148 external interrupt port experiment (final)
LPC2148
Experiment -4 : UART
uart.h library
In this experiment must include an important library file ; uart.h. This library will set the UART module in LPC2148. The source program of this library file in shown in Listing P4-1 Function that added in uart.h library has detail as :
uart1_init
Set the baudrate for UART1 module.
syntax :
_baudrate
uart1_putc
Send a character to Transmitter buffer of UART1 module.
syntax :
void uart1_putc(char c)
parameter :
uart1_puts
Transmit string out to UART1 module
syntax :
uart0_init
Set the baudrate for UART0 module.
syntax :
_baudrate
uart0_putc
Send a character to Transmitter buffer of UART0 module.
syntax :
void uart0_putc(char c)
parameter :
uart0_puts
Transmit string out to UART0 module
syntax :
p Index the transmited character Note : For only UART1 module, developers may be execute the many support functions about data communication that added in stdio.h library such as printf, puts, getchar, scanf etc...
Procedure :
4.1.1 Build new project, in name uart_0_1 4.1.2 Write the program Listing P4-2. Compile to uart_0_1.hex and download to microcontroller. 4.1.3 Open the Terminal program such as Hyper terminal or RS-232 Terminal to test the operation. Set baudrate to 9,600 bit per second. 4.1.4 Connect serial port cable from computer RS-232 port to UART CH.0 connector on JX2148 board. If have more serial port, connect the serial cable to UART CH.1 connector. 4.1.5 Run the program. Watch the result on the terminal program operation. Message on the Terminal panel will show message : Test UART0 If connect between UART CH.0 with RS-232 serial port
Test UART1 If connect between UART CH.1 with RS-232 serial port
RESET BLUETOOTH ESD-02 SD/MMC STATUS 12MHz CTS RTS TxD RxD UART CH.1
+ +4.5-9V ON OFF
JTAG/DEBUG port
JX-2148
DEBUG EN.
ON USB
32.768kHz LPC2148
+3V BATT
LITIUM BATTERY 3V PIEZO 0.12-0.13 PS/2 0.22 CLK 0.16 DAT 0.15
G 0.02 0.03 +
TWI-0
PORT
SPI-0
USB
PORT
0.21
* In case connect the both serial port in same time, must connect different port and open terminal program separate But if connect not same time, must connect swap and open the terminal program only one window.
SWITCH
ISP
0.28
0.29
0.30 (A/D)
#ifndef _UART_H_ #define _UART_H_ //// // Program : Library for serial communication(UART) // Description : Library for serial communication UART0 and UART1 of LPC2148 // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : uart.h // C compiler : CodeSourcery G++ Lite Compiler //// #define _PCLK 30000000 // Define PCLK for configuration baudrate #define uart1_setbaud(x) uart1_init(x) // Define function name uart1_setbaud equal uart1_init #define uart0_setbaud(x) uart0_init(x) // Define function name uart0_setbaud equal // uart0_init // Function for Initial UART1 // void uart1_init(unsigned int _baudrate) { unsigned short u1dl; u1dl = _PCLK/(16*_baudrate); // Calculate for U1DL value PINSEL0 |= 0x00050000; // Enable rx,tx U1LCR = 0x00000083; // 8 bit data,1 stop bit,no parity bit U1DLL = u1dl & 0xFF; // U1DL for low byte U1DLM = (u1dl>>8); // U1DL for high byte U1LCR = 0x00000003; // DLAB =0 } // Function for send character 1 time via UART1// void uart1_putc(char c) { while(!(U1LSR & 0x20)); // Wait until UART1 ready to send character U1THR = c; // Send character } // Function for send string via UART1// void uart1_puts(char *p) { while(*p) // Point to character { uart1_putc(*p++); // Send character then point to next character } } // Function for Initial UART0 // void uart0_init(unsigned int _baudrate) { unsigned short u0dl; u0dl = _PCLK/(16*_baudrate); // Calculate for U0DL value PINSEL0 |= 0x00000005; // Enable rx,tx U0LCR = 0x00000083; // 8 bit data,1 stop bit,no parity bit U0DLL = u0dl & 0xFF; // U0DL for low byte U0DLM = (u0dl>>8); // U0DL for high byte U0LCR = 0x00000003; // DLAB =0 } // Function for send character 1 time via UART0// void uart0_putc(char c) { while(!(U0LSR & 0x20)); // Wait until UART0 ready to send character U0THR = c; // Send character } // Function for send string via UART1// void uart0_puts(char *p) { while(*p) // Point to character { uart0_putc(*p++); // Send character then point to next character } } #endif
//// // Program : UART example // Description : Example for test module UART0 and UART1 // Frequency : Crytal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : uart_0_1.c // C compiler : CodeSourcery G++ Lite Compiler //// #include lpc214x.h // Header file for Phillips LPC2148 controller #include uart.h // Library for use module UART0,UART1(from jx2148_include floder) // Initial system clock // void init() { PLL0CFG=0x24; // MSEL = 4,PSEL = 2 PLL0FEED=0xAA; // Feed process PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55;
// Feed process
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; // Connect the PLL as the clock source // Feed process
MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch // (4 cclks in this case) MAMTIM=0x4; VPBDIV=0x02; } // Main Program // int main() { init(); // Initialize the system SCS = 0x03; // select the fast version of the I/O ports uart0_init(9600); // Initial UART0 @ 9600 bps,8 bit data ,1 stop bit ,no parity bit uart1_init(9600); // Initial UART1 @ 9600 bps,8 bit data ,1 stop bit ,no parity bit while (1) { uart0_puts(Test UART0\r\n); // Send string to UART0 uart1_puts(Test UART1\r\n); // Send string to UART1 } } // PCLK at 30 MHz
Listing P4-2 : uart_0_1.c file of uart_0_1 project for LPC2148 UART experiment
Procedure :
4.2.1 Build new project, in name uart1_int. 4.2.2 Write the program Listing P4-3. Compile to uart1_int.hex and download to microcontroller. 4.2.3 Open the Terminal program such as Hyper terminal or RS-232 Terminal to test the operation. Set baudrate to 9,600 bit per second. 4.2.4 Connect serial port cable from computer RS-232 port to UARTCH.1 connector. 4.2.5 Run the program.Watch the result on the terminal program operation.
RESET BLUETOOTH ESD-02 SD/MMC STATUS 12MHz CTS RTS TxD RxD UART CH.1
+ +4.5-9V ON OFF
JTAG/DEBUG port
JX-2148
DEBUG EN.
ON USB
32.768kHz LPC2148
P1.19 P1.18 P1.17 P1.16
+3V BATT
LITIUM BATTERY 3V PIEZO 0.12-0.13 PS/2 0.22 CLK 0.16 DAT 0.15
G 0.02 0.03 +
TWI-0
PORT
SPI-0
USB
PORT
0.21
SWITCH
ISP
0.28
0.29
0.30 (A/D)
The termional window shows message below : Now test UART1 for echo character Press any key for test! Try to press any keyboard button for transmitting that character to LPC2148. This transmit enable echo function (return character back). Thus, transmitted character will send back to serial port and appear at the terminal program window.
//// // Program : UART example // Description : Example for test UART1 interrupt mode // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename: uart_int.c // C compiler : CodeSourcery G++ Lite Compiler //// #include lpc214x.h // Header file for Phillips LPC2148 controller #include uart.h // Library for use module UART0,UART1 // Function for Initial system clock // void init() { PLL0CFG=0x24; // MSEL = 4,PSEL = 2 PLL0FEED=0xAA; // Feed process PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55;
// Feed process
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; // Connect the PLL as the clock source // Feed process
MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch // (4 cclks in this case) MAMTIM=0x4; VPBDIV=0x02; } // Interrupt service routine for UART1 // void __attribute__ ((interrupt(IRQ))) isr_uart1(void) { char msg; if(((msg = U1IIR) & 0x01) == 0) // Check status flag communication { switch (msg & 0x0E) // Filter message { case 0x04: while(!(U1LSR & 0x20)); // Receive Data Available U1THR = U1RBR; // Echo character break; case 0x02: break; // THERE Interrupt default: break; // Other } } VICVectAddr = 0; // Acknowledge Interrupt } // Main Program // int main() // PCLK at 30 MHz
Listing P4-3 : uart1_int.c file of uart1_int project for LPC2148 UART experiment (continue)
{ init(); // Initialize the system SCS = 0x03; // select the fast version of the I/O ports uart1_init(9600); // Initial UART1 @ 9600 bps, 8 bit data, 1 stop bit, no parity bit U1IER = 3; // Enable rx/tx interrupt for UART1 PINSEL0 |= (1<<18); // Enable RXD1(from UART1) at P0.9 VICVectAddr0 = (unsigned)isr_uart1; // Register Interrupt service routine name VICVectCntl0 = 0x20 | 7; // UART1 Interrupt VICIntEnable = 1 << 7; // Enable UART1 Interrupt uart1_puts(Now test UART1 for echo character\r\n); // Display message for test echo character uart1_puts(Press any key for test!\r\n); while(1); // Infinite loop }
Listing P4-3 : uart1_int.c file of uart1_int project for LPC2148 UART experiment with interrupt (final)
Figure P4-3 Shows the UART1 module data communication function with interrupt
LPC2148
+ +4.5-9V ON OFF
JTAG/DEBUG port
JX-2148
DEBUG EN.
G 0.02 0.03 +
TWI-0
PORT
SPI-0
ON USB
32.768kHz LPC2148
+3V BATT
LITIUM BATTERY 3V PIEZO 0.12-0.13 PS/2 0.22 CLK 0.16 DAT 0.15
PORT
0.21
SWITCH
ISP
0.28
0.29
0.30 (A/D)
5.5 Run the program.Watch the result on the terminal program operation. The terminal window shows message below :
Analog(AD0.3): xxx
//// // Program : Example Analog to Digital converter // Description : Display Analog to Digital convert value // : on terminal program(used UART0 communication) // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : adc.c // C compiler : CodeSourcery G++ Lite Compiler //// #include lpc214x.h // Header file for Phillips LPC2148 controller #include stdio.h // Library for sprintf function #include uart.h // Library for UART // Function for void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; Initial system clock // // MSEL = 4,PSEL = 2 // Feed process
// Feed process
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; // Connect the PLL as the clock source // Feed process
MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch //(4 cclks in this case) MAMTIM=0x4; VPBDIV=0x02; } // Function delay // void delay_ms(long ms) // delay 1 ms per count { long i,j; for (i = 0; i < ms; i++ ) for (j = 0; j < 4000; j++ ); } // Main Program // int main() { int val=0; char s[30]; // Buffer for keep string from integer convert init(); // Initialize the system SCS = 0x03; // select the fast version of the I/O ports uart0_init(9600); // Initial UART0 PINSEL1 |= 0x10000000; // Enable AD0.3 for used AD0CR = 0x00210608; // Setup A/D: 10-bit AIN0 @ 3MHz while (1) { AD0CR |= 0x01000000; // Start A/D Conversion while ((AD0DR3 & 0x80000000) == 0); // Wait for the conversion to complete val = ((AD0DR3 >> 6) & 0x03FF); // Extract the A/D result sprintf(s,Analog(AD0.3): %d \r,val); // Convert string to display analog value uart0_puts(s); // Display to Terminal program delay_ms(200); // Delay for display } } // PCLK at 30 MHz
Listing P5-1 : adc.c file of adc project for LPC2148 A/D converter experiment (final)
Figure P5-1 Shows the result of A/D converter experiment at the Hyper terminal window.
LPC2148
Procedure :
6.1.1 Build new project, in name rtc_int. 6.1.2 Write the program Listing P6-1. Compile to rtc_int.hex and download to microcontroller. 6.1.3 Open the Terminal program such as Hyper terminal or RS-232 Terminal to test the operation. Set baudrate to 9,600 bit per second. 6.1.4 Connect serial port cable from computer RS-232 port to UARTCH.1 connector.
RESET BLUETOOTH ESD-02 SD/MMC STATUS 12MHz CTS RTS TxD RxD UART CH.1
+ +4.5-9V ON OFF
JTAG/DEBUG port
JX-2148
DEBUG EN.
ON USB
32.768kHz LPC2148
P1.19 P1.18 P1.17 P1.16
+3V BATT
LITIUM BATTERY 3V PIEZO 0.12-0.13 PS/2 0.22 CLK 0.16 DAT 0.15
G 0.02 0.03 +
TWI-0
PORT
SPI-0
USB
PORT
0.21
SWITCH
ISP
0.28
0.29
0.30 (A/D)
//// // Program : Example for Real time clock // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : rtc.c // C compiler : CodeSourcery G++ Lite Compiler //// #include lpc214x.h // Header file for Phillips LPC2148 controller #include sound.h // Header file for Phillips LPC2148 controller #include stdio.h // Library for use puts function(For UART1) #include uart.h // Library for use module UART0,UART1 #define LED1_ON FIO0CLR = 0x00400000 // Red led 0.22 on #define LED1_OFF FIO0SET = 0x00400000 // Red led 0.22 off char alarm = 0; // Variable for status sound alarm // Function for Initial system clock // void init() { PLL0CFG=0x24; // MSEL = 4,PSEL = 2 PLL0FEED=0xAA; // Feed process PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; // Feed process
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; // Connect the PLL as the clock source // Feed process
MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch // (4 cclks in this case) MAMTIM=0x4; VPBDIV=0x02; // PCLK at 30 MHz } // Function delay // void delay_ms(long ms) // delay 1 ms per count @ CCLK 60 MHz { long i,j; for (i = 0; i < ms; i++ ) for (j = 0; j < 4000; j++ ); } // Interrupt service routine for UART1 // void __attribute__ ((interrupt(IRQ))) isr_rtc(void) { if(ILR & 0x01) // Check Interrupt block generate { LED1_ON; // LED on delay_ms(100); // Delay for Blink LED LED1_OFF; // LED off ILR = 0x01; // Clear interrupt flag } if(ILR & 0x02) { alarm = 1; // Set flag alarm for generate sound beep ILR = 0x02; // Clear interrupt flag } VICVectAddr = 0; // Acknowledge Interrupt }
Listing P6-1 : rtc_int.c file of rtc_int project for LPC2148 RTC experiment (continuous)
// Main Program // int main() { char i=0; char str[40]; init(); // Initialize the system SCS = 0x03; // select the fast version of the I/O ports FIO0DIR |= 0x00400000; // Config. pin P0.22 as output uart1_init(9600); PREINT = 0x00000392; // Set RTC Prescaler for PCLK 30 MHz PREFRAC = 0x00004380; CIIR = 0x00000001; // Enable seconds counter interrupt ALSEC = 0x00000003; // Set alarm register for 3 seconds // (match when xx:xx:03) AMR = 0x000000FE; // Enable seconds alarm CCR = 0x00000001; // Start RTC VICVectAddr13 = (unsigned)isr_rtc; VICVectCntl13 = 0x20 | 13; VICIntEnable |= (1<<13); // Enable RTC Interrupt while (1) // Infinite loop { if(alarm==1) // Check seconds alarm match { beep(); // Sound beep 1 time i++; // Increment counter for sound if(i>10) // Over 10 time? { i=0; // Clear counter alarm = 0; // Clear alarm flag } } sprintf(str,TIME: %d:%d:%d \r ,HOUR,MIN,SEC); // Convert time format hh:mm:ss uart1_puts(str); // Display time delay_ms(100); // Delay for display } }
Listing P6-1 : rtc_int.c file of rtc_int project for LPC2148 RTC experiment (final)
6.1.5 Run program. Watch the result on the terminal program and LED at P0.22 operation. The terminal window shows message below :
TIME: hh:mm:ss
by hh is time in hour unit., mm is time in minute unit and ss is second. LED at P0.22 will blink in rate 1 second. This operation is defined from the interrupt first case. In every second at number 3, piezo will drive beep signal 10 times continuous. This operation is defined from the interrupt second case.
However the operation of this experiment RTC module cannot operate continuous without supply voltage. Because the program set to RTC clock use same souce clock of CPU (CLKSRC bit in CCR register is 0). RTC module will operate when apply supply voltage to CPU only. In nthe next experiment will shows the solution of this limitation.
Procedure :
6.2.1 Build new project, in name rtc_setup. 6.2.2 Write the program Listing P6-2. Compile to rtc_setup.hex and download to microcontroller. Close LPC2000 Flash Utility program.
//// // Program : Example for Real time clock // Description : Example for display via Terminal program time format hh:mm:ss // : and user can setup new time by press key * on keyboard // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : rtc_setup.c // C compiler : CodeSourcery G++ Lite Compiler //// #include lpc214x.h // Header file for Phillips LPC2148 controller #include stdio.h // Library for use puts function(For UART1) #include uart.h // Library for use module UART0,UART1 #include ctype.h // Library for isdigit function #include stdlib.h // Library for atoi function char key = 0; // Variable for status sound alarm // Function for Initial system clock // void init() { PLL0CFG=0x24; // MSEL = 4,PSEL = 2 PLL0FEED=0xAA; // Feed process PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55;
// Feed process
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; // Connect the PLL as the clock source // Feed process
MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch // (4 cclks in this case) MAMTIM=0x4; VPBDIV=0x02; // PCLK at 30 MHz }
Listing P6-2 : rtc_setup.c file of rtc_setup project for LPC2148 RTC experiment (continuous)
// Function delay // void delay_ms(long ms) // delay 1 ms per count @ CCLK 60 MHz { long i,j; for (i = 0; i < ms; i++ ) for (j = 0; j < 4000; j++ ); } // Interrupt service routine for UART1 // void __attribute__ ((interrupt(IRQ))) isr_uart1(void) { char msg; if(((msg = U1IIR) & 0x01) == 0) // Check status flag communication { switch (msg & 0x0E) // Filter message { case 0x04: while(!(U1LSR & 0x20)); // Receive Data Available key = U1RBR; break; case 0x02: break; // THRE Interrupt default: break; // Other } } VICVectAddr = 0; // Acknowledge Interrupt } // Function for setup date/time for Real time clock // void rtc_uart1_setup( char *s) { unsigned char tm; // Buffer for keep date/time setup value char i=0; // Variable for loop counter for(i=0;i<2;i++) // Loop for keep value 2 byte { while(!isdigit(key)); // Wait for key 0-9' only if(i==0) tm = 10*atoi(&key); // Keep 1 value if(i==1) tm = tm+atoi(&key); // Keep 2 value uart1_putc(key); // Display key on Terminal program key = 0; // Clear old key for next key } *s = tm; // Load setup new value } // Main Program // int main() { char str[40]; init(); // Initialize the system SCS = 0x03; // select the fast version of the I/O ports uart1_init(9600); // Initial UART1 @ 9600 bps,8 bit data ,1 stop bit ,no parity bit U1IER = 3; // Enable rx/tx interrupt for UART1 PINSEL0 |= (1<<18); // Enable RXD1(from UART1) at P0.9 VICVectAddr0 = (unsigned)isr_uart1; // Register Interrupt service routine name VICVectCntl0 = 0x20 | 7; // UART1 Interrupt VICIntEnable |= 1 << 7; // Enable UART1 Interrupt
Listing P6-2 : rtc_setup.c file of rtc_setup project for LPC2148 RTC experiment (continuous)
CCR = 0x00000011; // Start RTC used 32.768 kHz crystal for RTCX1/RTCX2 pin while (1) // Infinite loop { sprintf(str,TIME: %d:%d:%d \r ,HOUR,MIN,SEC); uart1_puts(str); delay_ms(100); // Delay for display if(key==*) // Check key for setup time? { key = 0; // Clear old key for next key uart1_puts(\nSet Time:); rtc_uart1_setup(&HOUR); // Wait until new value of HOUR uart1_putc(:); // Display : at terminal program rtc_uart1_setup(&MIN); // Wait until user insert new value of MIN uart1_putc(:); // Display : at terminal program rtc_uart1_setup(&SEC); // Wait until user insert new value of SEC sprintf(str,\r\nTIME: %d:%d:%d \r ,HOUR,MIN,SEC); uart1_puts(str); } } }
Listing P6-2 : rtc_setup.c file of rtc_setup project for LPC2148 RTC experiment (final)
6.2.3 Open the Terminal program such as Hyper terminal or RS-232 Terminal to test the operation. Set baudrate to 9,600 bit per second. 6.2. 4 Connect serial port cable from computer RS-232 port to UARTCH.1 connector. 6.2.5 Run program. Watch the result on the terminal program. The terminal window shows message below : TIME: hh:mm:ss by hh is time in hour unit., mm is time in minute unit and ss is second.
6.2.6 Press * key for setting time to RTC module. Watch the result on the terminal program. The new message will appear in the next line : Set Time: Wait for data 6 digits. They mean time value in hh,mm and ss. Input the value complete 6 digits. The time value will store in the time registers of RTC module. The terminal will shows the setting time and run continue. The operation of this experiment RTC module can operate continuous without supply voltage. Because RTC receive the clock from 32kHz clock oscillator separate CPU clokc. CLKSRC bit in CCR register set to 1. The time value can store with +3V supply from litium battery on-board.
LPC2148
kb_init
Initialize keyboard clokc pin. In this experiment is EINT0 or P0.16
syntax :
void kb_init()
kb_getchar
Get the pressed key character that this library support.
syntax :
char kb_getchar()
return : Keyboard code or Key value
Clock Data
START DATA0 DATA1 DATA2 DATA3 DATA4 DATA5 DATA6 DATA7 PARITY STOP
//// // Program : Library for PS/2 Keyboard // Description : Detect for input key from PS/2 keyboard // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : keybord.h // C compiler : Keil CARM Compiler //// #include stdio.h // Library for use puts function(For UART1) #include ctype.h // Library for use toupper function #define LOWER 0 // Macro for lower case keyboard #define UPPER 1 // Macro for upper case keyboard #define CAPS_LOCK_CODE 0x58 // Macro for Caps Lock scan code unsigned int _code=0; // Variable for keep scan code char char_case =LOWER; // Variable for status character case(lower or upper) const { 0 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , unsigned char _ascii_key[128] = 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 q ! 0 0 0 z s a w @ 0 0 c x d e $ # 0 0 // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // 0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F 0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1A 0x1B 0x1C 0x1D 0x1E 0x1F 0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 // Table for decode
, 32 , v , f , t , r , % , 0 , 0 , n , b , h , g , y , ^ , 0 , 0 , 0 , m , j , u , & , * , 0 , 0 , 44 , k , i , o , ) , ( , 0 , 0 , . , / , l , ; , p , - , 0 , 0 , 0 , 39 , 0 , 0 , = , 0 , 0 , 0 , 0 , 13 , ] , 0 , 92 , 0 , 0 , 0 , 0 , 0 , 0
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
0x29 0x2A 0x2B 0x2C 0x2D 0x2E 0x2F 0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3A 0x3B 0x3C 0x3D 0x3E 0x3F 0x40 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x48 0x49 0x4A 0x4B 0x4C 0x4D 0x4E 0x4F 0x50 0x51 0x52 0x53 0x54 0x55 0x56 0x57 0x58 0x59 0x5A 0x5B 0x5C 0x5D 0x5E 0x5F 0x60 0x61 0x62 0x63
, 0 , 0 , 8 , 0 , 0 , 1 , 0 , 4 , 7 , 0 , 0 , 0 , 0 , . , 2 , 5 , 6 , 8 , 27 , 0 , 0 , + , 3 , - , * , 9 , 0 , 0 };
// // // // // // // // // // // // // // // // // // // // // // // // // // // //
0x64 0x65 0x66 0x67 0x68 0x69 0x6A 0x6B 0x6C 0x6D 0x6E 0x6F 0x70 0x71 0x72 0x73 0x74 0x75 0x76 0x77 0x78 0x79 0x7A 0x7B 0x7C 0x7D 0x7E 0x7F
// Function Check input P0 // char _inp0(char _bit) { unsigned long c; c = 1<<_bit; // Calculate digit to configuration for input port return((FIO0PIN & c)>>_bit); // Read and return data bit } // Function get character from keyboard // char kb_getchar() { static unsigned char temp=0 ,bk=0; unsigned char result; while(_code == 0); // Wait for key code support only temp = _code; // Keep code detect if(bk==2) // Not keep code for 0xF0,0xE0,0xE1 sequent { bk =0; } if(bk==1) // Not keep code for 0xF0,0xE0,0xE1 sequent { bk =2; } if(_code==0xF0 || _code==0xE0) // Check break code and none key support { bk=1; // Start break code sequent } _code = 0; // Clear old code
if((temp !=0xF0 || temp !=0xE0 || temp !=0xE1) && bk==0) // Check code support? { if(temp == CAPS_LOCK_CODE) // Check Caps Lock mode key push? { char_case ^=1; // Toggle status Caps Lock mode } if(char_case==UPPER) // Check Upper case for character A-Z result = toupper(_ascii_key[temp]); // Result A-Z else result = _ascii_key[temp]; // Result a-z return(result); // Return result } else return(0); } // Interrupt service routine for EINT0 // void isr_int0(void) __irq { unsigned char i; // Define for counter loop if(_inp0(16)==0) // Check start bit true? { while(_inp0(16)==0); // wait for 1 after start bit for(i=0;i<10;i++) // For loop count 10 time(for receive data 8 bit) { while(_inp0(16)==1); // wait for 0 after data bit _code = _code>>1; // Shift data bit to right 1 time if(_inp0(15)) _code = _code | 0x8000; // Config data bit = 1 while(_inp0(16)==0); // wait for 1 after data bit } while(_inp0(16)==0); // wait for 1 after stop bit _code = _code>>6; _code &= 0x00FF; } EXTINT |= 0x1; // Clear interrupt flag EINT0 VICVectAddr = 0; // Acknowledge Interrupt } // Function initial PS/2 keyboard // void kb_init() { FIO0DIR &= ~(1<<15); // Config. output P0.15 DATA pin for keyboard FIO0DIR &= ~(1<<16); // Config. output P0.16 DATA pin for keyboard EXTMODE |= 0x1; // EINT0 Edge sensitive detection(Falling edge) PINSEL1 |= 0x01; // Enable EINT0 at P0.16 VICVectAddr0 = (unsigned)isr_int0; // Register Interrupt service routine name VICVectCntl0 |= (0x20 | 14); // EINT0 Interrupt VICIntEnable |= 1 << 14; // Enable EINT0 Interrupt }
Key
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9
Key
' = \ BKSP Space Tab Caps L Shift L Ctrl L GUI L Alt R Shift R Ctrl R GUI R Alt Apps Enter ESC F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 PrtScn Scroll Pause [ Insert
Key
Home Page Up Delete End Page Dwn
Pressed key data E0, 6C E0, 7D E0, 71 E0, 69 E0, 7A E0, 75 E0, 6B E0, 72 E0, 74 77 E0, 4A 7C 7B 79 E0, 5A 71 70 69 72 7A 6B 73 74 6C 75 7D 5B 4C 52 41 49 4A
F0, 1C F0, 32 F0, 21 F0, 23 F0, 24 F0, 2B F0, 34 F0, 33 F0, 43 F0, 3B F0, 42 F0, 4B F0, 3A F0, 31 F0, 44 F0, 4D F0, 15 F0, 2D F0, 1B F0, 2C F0, 3C F0, 2A F0, 1D F0, 22 F0, 35 F0, 1A F0, 45 F0, 16 F0, 1E F0, 26 F0, 25 F0, 2E F0, 36 F0, 3D F0, 3E F0, 46
F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C
F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C F0, 1C
NUM KP / KP * KP KP + KP EN KP . KP 0 KP 1 KP 2 KP 3 KP 4 KP 5 KP 6 KP 7 KP 8 KP 9 ] ; ' , . /
7E
E1,14,77,E1, F0,14,F0,77
54 E0, 70
Operation of isr_int0 function is filter 8-bit data to store in _code variable and convert to ASCII code by look-up table refer Table P7-1. In one key pressed, the data will appear 2 values. One is pressed key data, another is released key data. For examplr, S key is pressed. Data will be 0x1B. After release, the data will change to 0xF0. Key detection is operation of kb_getchar function. If detect key pressed, this function will return key value (in condition, that key must support by keyboard.h library). Internal operation of kb_getchar function will look over both pressed and releasaed key data (0xE0 and 0xF0). After that bring the key value to look up table for getting ASCII data and return final data. Addition about Caps Lock support, swap character format about lower case and upper case letter. The kb_getchar function will return key a as a (0x61). If Caps Lock key pressed and press key a again. this function will return ASCII of A (0x41) instead but not turn on LED Caps Lock at keyboard.
Procedure :
7.1 Build new project, in name keyboard_ps2. 7.2 Write the program Listing P7-2. Compile to keyboard_ps2.hex and download to microcontroller. Close LPC2000 Flash Utility program. 7.3 Open the Terminal program such as Hyper terminal or RS-232 Terminal to test the operation. Set baudrate to 9,600 bit per second. 7. 4 Connect serial port cable from computer RS-232 port to UARTCH.1 connector. 7.5 Connect PS/2 keyboard at PS/2 connecotr on JX-2148 board.
RESET BLUETOOTH ESD-02 SD/MMC STATUS 12MHz CTS RTS TxD RxD UART CH.1
+ +4.5-9V ON OFF
JTAG/DEBUG port
JX-2148
DEBUG EN.
ON USB
32.768kHz LPC2148
P1.19 P1.18 P1.17 P1.16
+3V BATT
LITIUM BATTERY 3V PIEZO 0.12-0.13 PS/2 0.22 CLK 0.16 DAT 0.15
G 0.02 0.03 +
TWI-0
PORT
SPI-0
USB
PORT
0.21
ISP
0.28
0.29
0.30 (A/D)
//-// // Program : Example for decode form PS/2 keyboard // Description : Get character from PS/2 keyboard // : and display at terminal program // : used UART1 communication // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : keyboard_ps2.c // C compiler : CodeSourcery G++ Lite Compiler //-// #include lpc214x.h // Header file for Phillips LPC2148 controller #include uart.h // Library for use module UART0,UART1 #include keyboard.h // Library for use PS/2 keyboard // Function for void init() { PLL0CFG=0x24; PLL0FEED=0xAA; PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; Initial system clock //
// Feed process
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; // Connect the PLL as the clock source // Feed process
MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch // (4 cclks in this case) MAMTIM=0x4; VPBDIV=0x02; // PCLK at 30 MHz } // Main Program // int main() { char dat; init(); // Initialize the system SCS = 0x03; // select the fast version of the I/O ports kb_init(); // Initial for used PS/2 keyboard uart1_init(9600); // Initial UART1 @ 9600 bps,8 bit data ,1 stop bit ,no parity bit while(1) // Infinite loop { dat = kb_getchar(); // Get character from keyboard uart1_putc(dat); // Display at terminal program } }
Listing P7-1 : keyboard_ps2.c file of keyboard_ps2 project for LPC2148 RTC experiment
7.6 Run program. Try to type any key on keyboard and watch the result on the terminal program. After that press Caps Lock key and re-type keyboard again. See the different result.
l Display the standard size 5x7 pixel character up to 14 characters 6 lines and
mono-tone graphic image.
l Size is 4.5 x 4.5 cm. l Easy to connection l Interface with any popular microcontroller.
- If interface with +3V logic system, user can connect GLCD with microcontroller port directly. - If interface with +5V system, user must connect through the attenuator circuit for redeuce the logic level from +5V to +3 or +3.3V or not over the supply voltage of GLCD5110. The figure P8-1 shows size and pin assignment of GLCD5110. The figure P8-2 shows the interfacing circuit with the microcontroller.
45mm. 39mm. 1
39mm. 45mm.
Pin
1 2 3 4 5
Name
Vcc GND SCE RESET D/C
Function
Supply voltage +2.7 to +3.3V Ground Enable pin - active low Reset pin - active low Data/Command selection pin "0" - Write data display "1" - Write command Serial data input pin Clock input pin Backlight LED control pin - active high (inclued limit current resistor 330 )
6 7 8
OUTPUT port OUTPUT port OUTPUT port SDO/MOSI SCK RESET SDIN +3.3V SCLK GND SCE D/C LED OUTPUT port
// /* Hardware Configuration GLCD5110 | JX-2148 ___________________ (1)VCC | VCC (2)GND | GND (3)SCE | P1.20 (4)RESET | P0.04 (5)D/C | P0.05 (6)SDIN | P0.06 (7)SCK | P0.07 (8)LED | P1.21 */ // #include lpc214x.h // Header file for Phillips LPC2148 controller #include in_out.h #ifndef _GLCD_H_ #define _GLCD_H_ #include stdio.h #ifndef NULL #define NULL #endif #define FALSE #define TRUE
0 1 84 48 ((LCD_X_RES * LCD_Y_RES) / 8)
/* Type definitions */ typedef char bool; typedef unsigned char byte; typedef unsigned int word; typedef enum { LCD_CMD = 0, LCD_DATA = 1 } LcdCmdData; typedef enum { PIXEL_OFF = PIXEL_ON = PIXEL_XOR = }lcdPixelMode; /* Public function prototypes */ void lcdInit(void); void lcdClear(void); void lcdUpdate(void); void lcdGotoXY(byte x,byte y);
0, 1, 2
lcdChar(byte ch); lcdString(byte _x,byte _y,byte *dataPtr); lcdPixel(byte x, byte y, lcdPixelMode mode); lcdLine(byte x1, byte y1, byte x2, byte y2, lcdPixelMode mode); configGlcd(); lcdBackLight(char set);
/*Private function prototypes*/ // Function prototypes are mandatory otherwise the compiler generates unreliable code. static void lcdSend(byte data, LcdCmdData cd); void delay_ms(long ms); /* Character generator This table defines the standard ASCII characters in a 5x7 dot format. */ static const byte FontLookup [][5] = { { 0x00, 0x00, 0x00, 0x00, 0x00 }, // sp { 0x00, 0x00, 0x2f, 0x00, 0x00 }, // ! { 0x00, 0x07, 0x00, 0x07, 0x00 }, // { 0x14, 0x7f, 0x14, 0x7f, 0x14 }, // # { 0x24, 0x2a, 0x7f, 0x2a, 0x12 }, // $ { 0x23, 0x13, 0x08, 0x64, 0x62 }, // % { 0x36, 0x49, 0x55, 0x22, 0x50 }, // & { 0x00, 0x05, 0x03, 0x00, 0x00 }, // { 0x00, 0x1c, 0x22, 0x41, 0x00 }, // ( { 0x00, 0x41, 0x22, 0x1c, 0x00 }, // ) { 0x14, 0x08, 0x3E, 0x08, 0x14 }, // * { 0x08, 0x08, 0x3E, 0x08, 0x08 }, // + { 0x00, 0x00, 0x50, 0x30, 0x00 }, // , { 0x10, 0x10, 0x10, 0x10, 0x10 }, // { 0x00, 0x60, 0x60, 0x00, 0x00 }, // . { 0x20, 0x10, 0x08, 0x04, 0x02 }, // / { 0x3E, 0x51, 0x49, 0x45, 0x3E }, // 0 { 0x00, 0x42, 0x7F, 0x40, 0x00 }, // 1 { 0x42, 0x61, 0x51, 0x49, 0x46 }, // 2 { 0x21, 0x41, 0x45, 0x4B, 0x31 }, // 3 { 0x18, 0x14, 0x12, 0x7F, 0x10 }, // 4 { 0x27, 0x45, 0x45, 0x45, 0x39 }, // 5 { 0x3C, 0x4A, 0x49, 0x49, 0x30 }, // 6 { 0x01, 0x71, 0x09, 0x05, 0x03 }, // 7 { 0x36, 0x49, 0x49, 0x49, 0x36 }, // 8 { 0x06, 0x49, 0x49, 0x29, 0x1E }, // 9 { 0x00, 0x36, 0x36, 0x00, 0x00 }, // : { 0x00, 0x56, 0x36, 0x00, 0x00 }, // ; { 0x08, 0x14, 0x22, 0x41, 0x00 }, // < { 0x14, 0x14, 0x14, 0x14, 0x14 }, // = { 0x00, 0x41, 0x22, 0x14, 0x08 }, // > { 0x02, 0x01, 0x51, 0x09, 0x06 }, // ? { 0x32, 0x49, 0x59, 0x51, 0x3E }, // @ { 0x7E, 0x11, 0x11, 0x11, 0x7E }, // A { 0x7F, 0x49, 0x49, 0x49, 0x36 }, // B { 0x3E, 0x41, 0x41, 0x41, 0x22 }, // C { 0x7F, 0x41, 0x41, 0x22, 0x1C }, // D { 0x7F, 0x49, 0x49, 0x49, 0x41 }, // E { 0x7F, 0x09, 0x09, 0x09, 0x01 }, // F { 0x3E, 0x41, 0x49, 0x49, 0x7A }, // G { 0x7F, 0x08, 0x08, 0x08, 0x7F }, // H { 0x00, 0x41, 0x7F, 0x41, 0x00 }, // I { 0x20, 0x40, 0x41, 0x3F, 0x01 }, // J { 0x7F, 0x08, 0x14, 0x22, 0x41 }, // K { 0x7F, 0x40, 0x40, 0x40, 0x40 }, // L
{ { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { { {
0x7F, 0x7F, 0x3E, 0x7F, 0x3E, 0x7F, 0x46, 0x01, 0x3F, 0x1F, 0x3F, 0x63, 0x07, 0x61, 0x00, 0x55, 0x00, 0x04, 0x40, 0x00, 0x20, 0x7F, 0x38, 0x38, 0x38, 0x08, 0x0C, 0x7F, 0x00, 0x20, 0x7F, 0x00, 0x7C, 0x7C, 0x38, 0x7C, 0x08, 0x7C, 0x48, 0x04, 0x3C, 0x1C, 0x3C, 0x44, 0x0C, 0x44,
0x02, 0x04, 0x41, 0x09, 0x41, 0x09, 0x49, 0x01, 0x40, 0x20, 0x40, 0x14, 0x08, 0x51, 0x7F, 0x2A, 0x41, 0x02, 0x40, 0x01, 0x54, 0x48, 0x44, 0x44, 0x54, 0x7E, 0x52, 0x08, 0x44, 0x40, 0x10, 0x41, 0x04, 0x08, 0x44, 0x14, 0x14, 0x08, 0x54, 0x3F, 0x40, 0x20, 0x40, 0x28, 0x50, 0x64,
0x0C, 0x08, 0x41, 0x09, 0x51, 0x19, 0x49, 0x7F, 0x40, 0x40, 0x38, 0x08, 0x70, 0x49, 0x41, 0x55, 0x41, 0x01, 0x40, 0x02, 0x54, 0x44, 0x44, 0x44, 0x54, 0x09, 0x52, 0x04, 0x7D, 0x44, 0x28, 0x7F, 0x18, 0x04, 0x44, 0x14, 0x14, 0x04, 0x54, 0x44, 0x40, 0x40, 0x30, 0x10, 0x50, 0x54,
0x02, 0x10, 0x41, 0x09, 0x21, 0x29, 0x49, 0x01, 0x40, 0x20, 0x40, 0x14, 0x08, 0x45, 0x41, 0x2A, 0x7F, 0x02, 0x40, 0x04, 0x54, 0x44, 0x44, 0x48, 0x54, 0x01, 0x52, 0x04, 0x40, 0x3D, 0x44, 0x40, 0x04, 0x04, 0x44, 0x14, 0x18, 0x04, 0x54, 0x40, 0x20, 0x20, 0x40, 0x28, 0x50, 0x4C,
0x7F 0x7F 0x3E 0x06 0x5E 0x46 0x31 0x01 0x3F 0x1F 0x3F 0x63 0x07 0x43 0x00 0x55 0x00 0x04 0x40 0x00 0x78 0x38 0x20 0x7F 0x18 0x02 0x3E 0x78 0x00 0x00 0x00 0x00 0x78 0x78 0x38 0x08 0x7C 0x08 0x20 0x20 0x7C 0x1C 0x3C 0x44 0x3C 0x44
}, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }, }
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
M N O P Q R S T U V W X Y Z [ 55 ] ^ _ a b c d e f g h i j k l m n o p q r s t u v w x y z
}; /* Global Variables */ static byte LcdCache [ LCD_CACHE_SIZE ]; static int LcdCacheIdx; static int LoWaterMark; static int HiWaterMark; static bool UpdateLcd; // Function delay // void delay_ms(long ms) // delay 1 ms per count @ CCLK 60 MHz { long i,j; for (i = 0; i < ms; i++ ) for (j = 0; j < 6659; j++ ); }
/* Name : lcdInit */ void lcdInit ( void ) { delay_ms(500) // Ensure delay for initial Pull-up on reset pin. // IOSET0 |= LCD_RST_PIN; out_0(4,1); // Set output bits on port B. delay_ms(10); // Toggle display reset pin. IOCLR0 |= LCD_RST_PIN; out_0(4,0); delay_ms(10); //delay_ms(10); //IOSET0 |= LCD_RST_PIN; out_0(4,1); // Enable SPI port: No interrupt, MSBit first, Master mode, CPOL->0, // CPHA->0, Clk/4,SPCR = 0x50; // Disable LCD controller // IOSET0 |= LCD_CE_PIN; out_1(20,1); // Disable SCE pin lcdSend( 0x21, LCD_CMD ); // LCD Extended Commands. lcdSend( 0xC8, LCD_CMD ); // Set LCD Vop (Contrast). lcdSend( 0x06, LCD_CMD ); // Set Temp coefficent. lcdSend( 0x13, LCD_CMD ); // LCD bias mode 1:48. lcdSend( 0x20, LCD_CMD ); // LCD Commands, Horizontal addressing mode. lcdSend( 0x0C, LCD_CMD ); // LCD in normal mode. LoWaterMark = LCD_CACHE_SIZE; HiWaterMark = 0; lcdClear(); lcdUpdate(); // Reset watermark pointers.
} /* Name : lcdClear Description : Clears the display. lcdUpdate must be called next. */ void lcdClear ( void ) { int i; for ( i = 0; i < LCD_CACHE_SIZE; i++ ) { LcdCache[i] = 0x00; } LoWaterMark = 0; // Reset watermark pointers. HiWaterMark = LCD_CACHE_SIZE - 1; UpdateLcd = TRUE; } /* Name : lcdGotoXY Description : Sets cursor location to xy location corresponding to basic font size. Argument(s) : x, y -> Coordinate for new cursor position. Range: 1,1 .. 14,6 */ void lcdGotoXY ( byte x, byte y ) { configGlcd(); LcdCacheIdx = (x - 1) * 6 + (y - 1) * 84; } void configGlcd() { if(glcd_ini==0) { glcd_ini=1; lcdInit(); lcdClear(); } }
/* Name : lcdSend Description : Sends data to display controller. Argument(s) : data -> Data to be sent cd -> Command or data (see/use enum) */ void lcdSend ( byte data, LcdCmdData cd ) { int j,data_in; // Enable display controller (active low). IOCLR0 |= LCD_CE_PIN; out_1(20,0); // Enable SCE pin //out_0(20,0); // Off if ( cd == LCD_DATA ) { // IOSET0 |= LCD_DC_PIN; out_0(5,1); } else { // IOCLR0 |= LCD_DC_PIN; out_0(5,0); } // Send data to display controller. Wait until Tx register empty. data_in = data; for ( j = 0; j <8; j++ ) { // IOCLR0 |= LCD_CLK_PIN; out_0(7,0); if ((data_in & 0x80)!=0) { // IOSET0 |= LCD_MOSI_PIN; out_0(6,1); } else { // IOCLR0 |= LCD_MOSI_PIN; out_0(6,0); } data_in=data_in<<1; out_0(7,1); // IOSET0 |= LCD_CLK_PIN; } // Disable display controller. // IOSET0 |= LCD_CE_PIN; out_0(20,1); } /* Name : lcdChar Description : Displays character at current cursor position and increment position. Argument(s) : size -> Font size. See enum. ch -> Character to write. */ void lcdChar (byte ch ) { byte i; configGlcd(); if ( LcdCacheIdx < LoWaterMark ) { LoWaterMark = LcdCacheIdx; // Update low marker. } if ( (ch < 0x20) || (ch > 0x7b) ) { // Convert to a printable character. ch = 92; } for ( i = 0; i < 5; i++ ) { LcdCache[LcdCacheIdx++] = FontLookup[ch - 32][i] << 1; } if ( LcdCacheIdx > HiWaterMark ) { HiWaterMark = LcdCacheIdx; } LcdCache[LcdCacheIdx++] = 0x00; // Horizontal gap between characters. }
/* Name : lcdString Description : Displays a character at current cursor location and increment location according to font size. Argument(s) : size -> Font size. See enum. dataPtr -> Pointer to null terminated ASCII string to display. */ void lcdString (byte _x,byte _y,byte *dataPtr) { configGlcd(); lcdGotoXY(_x,_y); while (*dataPtr) { lcdChar(*dataPtr++); } } /* Name : lcdPixel Description : Displays a pixel at given absolute (x, y) location. Argument(s) : x, y -> Absolute pixel coordinates mode -> Off, On or Xor. See enum. Return value : None. */ void lcdPixel(byte x, byte y, lcdPixelMode mode) { word index; byte offset; byte data; configGlcd(); if ( x > LCD_X_RES ) return; if ( y > LCD_Y_RES ) return; index = ((y / 8) * 84) + x; offset = y - ((y / 8) * 8); data = LcdCache[index]; if ( mode == PIXEL_OFF ) { data &= (~(0x01 << offset)); } else if ( mode == PIXEL_ON ) { data |= (0x01 << offset); } else if ( mode == PIXEL_XOR ) { data ^= (0x01 << offset); } LcdCache[index] = data; if ( index < LoWaterMark ) { LoWaterMark = index; // } if ( index > HiWaterMark ) { // HiWaterMark = index; } }
/* Name : lcdLine Description : Draws a line between two points on the display. Argument(s) : x1, y1 -> Absolute pixel coordinates for line origin. x2, y2 -> Absolute pixel coordinates for line end. mode -> Off, On or Xor. See enum. */ void lcdLine ( byte x1, byte y1, byte x2, byte y2, lcdPixelMode mode ) { int dx, dy, stepx, stepy, fraction; configGlcd(); dy = y2 - y1; dx = x2 - x1; if ( dy < 0 ) { dy = -dy; stepy = -1; } else { stepy = 1; } if ( dx < 0 ) { dx = -dx; stepx = -1; } else { stepx = 1; } dx <<= 1; dy <<= 1; lcdPixel( x1, y1, mode ); if ( dx > dy ) { fraction = dy - (dx >> 1); while ( x1 != x2 ) { if ( fraction >= 0 ) { y1 += stepy; fraction -= dx; } x1 += stepx; fraction += dy; lcdPixel( x1, y1, mode ); } } else { fraction = dx - (dy >> 1); while ( y1 != y2 ) { if ( fraction >= 0 ) { x1 += stepx; fraction -= dy; } y1 += stepy; fraction += dx; lcdPixel( x1, y1, mode ); } } UpdateLcd = TRUE; }
/* Name : lcdUpdate Description : Copies the LCD cache into the device RAM. Argument(s) : None. Return value : None. */ void lcdUpdate ( void ) { int i; if ( LoWaterMark < 0 ) LoWaterMark = 0; else if ( LoWaterMark >= LCD_CACHE_SIZE ) LoWaterMark = LCD_CACHE_SIZE - 1; if ( HiWaterMark < 0 ) HiWaterMark = 0; else if ( HiWaterMark >= LCD_CACHE_SIZE ) HiWaterMark = LCD_CACHE_SIZE - 1; // Set base address according to LoWaterMark. lcdSend( 0x80 | (LoWaterMark % LCD_X_RES), LCD_CMD ); lcdSend( 0x40 | (LoWaterMark / LCD_X_RES), LCD_CMD ); // Serialize the video buffer. for ( i = LoWaterMark; i <= HiWaterMark; i++ ) { lcdSend( LcdCache[i], LCD_DATA ); } LoWaterMark = LCD_CACHE_SIZE - 1; // Reset watermark pointers. HiWaterMark = 0; UpdateLcd = FALSE; } //************************************************************************* // Purpose : Draw a rectangle on a graphic LCD // Inputs : (x1, y1) - the start coordinate // (x2, y2) - the end coordinate // fill - YES or NO // color - ON or OFF // Dependencies: glcd_pixel(), glcd_line() //************************************************************************* void lcdRect( unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char fill, unsigned char color) { if(fill) { unsigned char i, xmin, xmax, ymin, ymax; if(x1 < x2) // Find x min and max { xmin = x1; xmax = x2; } else { xmin = x2; xmax = x1; } if(y1 < y2) // Find the y min and max
{ ymin = y1; ymax = y2; } else { ymin = y2; ymax = y1; } for(; xmin <= xmax; ++xmin) { for(i=ymin; i<=ymax; ++i) { lcdPixel(xmin, i, color); } } } else { lcdLine(x1, lcdLine(x1, lcdLine(x1, lcdLine(x2, }
} // Function for draw Progress Bar // void lcdProgBar( unsigned char _x, unsigned char _y, unsigned char width, unsigned char high, unsigned char percentage) { lcdRect(_x,_y,_x+width,_y+high,0,1); // Draw Beyon lcdRect(_x+1,_y+1,_x+width-1,_y+high-1,1,0); // Clear fill lcdRect(_x,_y,_x+((percentage*width)/100),_y+high,1,1); // Paint color @ percentage } void lcdBackLight(char set) { out_1(21,set); } #endif
void lcdClear(void);
example :
#include lpc214x.h #include glcd5110.h ................. void main() { ............... lcdClear(); // ................ }
Clear screen
lcdUpdate
Update GLCd screen with new mesasge and value. Use with lcdChar, lcdString, lcdPixel, lcdLine, lcdRect and lcdProgBar function.
syntax :
void lcdUpdate(void);
lcdGotoXY
Set column and row position or co-ordinator for displaying character. Maximum chararacter display is 14 x 6. The start point of screen; column 1 Row 1 is locared to top left corner of screen.
syntax :
lcdChar
Display one character on GLCD screen
syntax :
ch : Character display
example :
#include lpc214x.h #include glcd5110.h ................. void main() { ............... lcdGotoXY(12,2); // lcdChar(N); // lcdChar(o); // lcdChar(k); // lcdChar(i); // lcdChar(a); // lcdChar(5); // lcdChar(1); // lcdChar(1); // lcdChar(0); // lcdUpdate(); // ................ }
Result :
Set display position at Column 12 Line 2 Display N at Column 12 Line 2 Display o at Column 13 Line 2 Display k at Column 14 Line 2 Display i at Column 1 Line 3 Display a at Column 2 Line 3 Display 5 at Column 3 Line 3 Display 1 at Column 4 Line 3 Display 1 at Column 5 Line 3 Display 0 at Column 6 Line 3 Update screen
1 2 3 4 5 6
Column 12
lcdString
Display message on GLCD sceen
syntax :
#include lpc214x.h #include glcd5110.h ................. void main() { ............... lcdString(1,1,Hello world); // lcdString(1,4,Line 4 Demo); // lcdUpdate(); // ................ } Result :
1 2 3 4 5 6
lcdPixel
Set pixel display. The co-ordinator (0,0) is top left corner of screen.
syntax :
x : Row pixel position (0 to 83) y : Column pixel position (0 to 47) LcdPixelMode : Set color mode LcdPixelMode = 0 as white or none LcdPixelMode = 1 as black LcdPixelMode = 2 as toggle
example :
#include lpc214x.h #include glcd5110.h ................. void main() { ............... lcdPixel(5,3,1); // Plot black point at (5,3) position of screen lcdPixel(10,25,0); // Plot white dot at (10,25) position of screen lcdPixel(30,20,2); // Invert color of (30,20) position of screen lcdUpdate(); // Update screen ................ }
lcdLine
Draw a straight line.
syntax :
void lcdLine(byte x1, byte y1, byte x2, byte y2, LcdPixelMode mode);
parameter :
x1 : Start column position (0 to 83) y1 : Start row position (0 to 47) x2 : Destination column position (0 to 83) y2 : Destination row position (0 to 47) LcdPixelMode : Set color mode LcdPixelMode = 0 as white or none LcdPixelMode = 1 as black LcdPixelMode = 2 as toggle
example :
#include lpc214x.h #include glcd5110.h ................. void main() { ............... lcdLine(1,1,20,20,1); // Draw a straight line from (1,1) to (20,20) lcdLine(1,1,20,20,0); // Drwaw a straight line from (1,1) to (20,20) lcdUpdate(); // Update sceen ............... }
lcdRect
Draw a rectangle at setting position.
syntax :
void lcdRect( unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char fill, unsigned char color);
parameter :
x1 : Start row co-ordinator (0 to 83) y1 : Start column co-ordinator (0 to 47) x2 : Destination row co-ordinator (0 to 83) y2 : Destination column co-ordinator (0 to 47) fill : Fill color in the area fill = 0 No fill fill = 1 Fill color color : Mode of display color = 0 : White or none color = 1 : Black color = 2 : Toggle color
example :
#include lpc214x.h #include glcd5110.h void main() { lcdRect(5,5,25,25,0,1); // Draw a rectangular with left co-ordinator (5,5), // right co-ordinator (25,25), No fill and Black border lcdUpdate(); }
LcdProgBar
Create the Progress bar at setting position and shows shadow ratio.
syntax :
void LcdProgBar(unsigned char _x, unsigned char _y, unsigned char width, unsigned char high, unsigned char percentage)
parameter :
_x : Start column position (0 to 83) _y : Start row position (0 to 47) width : Number of pixel for progress bar width high : Number of pixel for progess bar height percentage : Shadow ratio for progress bar
example :
#include lpc214x.h #include glcd5110.h void main() { lcdLine(0,10,83,5,70); // Create the progress bar at (0,10), // width 83 pixels, height 5 pixels and 70% shadow ratio lcdUpdate(); // Update screen }
Result :
(0,10)
83 pixels width
5 pixels height
delay_ms
Delay in millisecond unit.
syntax :
#include lpc214x.h #include glcd5110.h ................. void main() { ............... lcdString(1,1,Hello LCD); // lcdString(1,2,0123456789"); // lcdUpdate(); // delay_ms(2000); // lcdClear(); // lcdString(1,1,This is test); // lcdString(1,2,DELAY 2 sec); // lcdUpdate(); // ................ }
Displays message Line 1 Displays message Line 2 Update screen Delay 2000 ms. (2 second) Clear screen Displays message Line 1 Displays message Line 2 Update screen
P0.07 P0.06
31 30 29 27 40 44
IC3 LPC2148
RESET
SDIN
SCLK
GND
5
C5 15pF C6 33pF +3.3V XTAL1 32.768kHz
+3.3V RTCX2 1
62 X1 61 X2 63 Vref 23 43 51 7
C7 33pF
XTAL2 12MHz
JP2 8HAB
+3.3V GND
DSP1 GLCD5110
59 VssA 49 Vbat
21 19 +3.3V
R21 10k
41 SW2 4-5-6
R22 22k
+3.3 V R20 1k
ISP
+V +3.3 V
SW3
SCE
RST
57
D/C
C4 15pF
RTCX1
ON OFF
JX-2148
DEBUG EN.
12MHz
UART CH.1
ON USB
32.768kHz LPC2148
P1.19 P1.18 P1.17 P1.16 G 0.02 0.03 +
+3V BATT
LITIUM BATTERY 3V PIEZO 0.12-0.13 PS/2 0.22 CLK 0.16 DAT 0.15
TWI-0
PORT
SPI-0
USB
PORT
0.21
SWITCH
ISP
0.28
0.29
0.30 (A/D)
//// // Program : Example for GLCD display // Description : Example for GLCD display on JX-2148 Board // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : glcd_01.c // C compiler : CodeSourcery G++ Lite Compiler //// #include lpc214x.h // Header file for Phillips LPC2148 controller #include glcd5110.h // Library for GLCD Nokia5110 // Initial system clock // void init() { PLL0CFG=0x24; // MSEL = 4,PSEL = 2 PLL0FEED=0xAA; // Feed process PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55; while(!(PLL0STAT & 0x400)) ; PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; MAMTIM=0x4; VPBDIV=0x02; } // Main Program // void main() { int cnt,i=0; // Counter variable constchar *pText1 = JX-2148; // Store message in Program constchar *pText2 = Graphic LCD; // Store message in Program constchar *pText3 = Nokia 5110; // Store message in Program init(); // Initialize the system SCS = 0x03; // select the fast version of the I/O lcdString(1,1,pText1); lcdString(1,2,pText2); lcdString(1,3,pText3); lcdUpdate(); lcdGotoXY(1,4); for (cnt=0; cnt<14; cnt++) { lcdChar(-); } // // // // Set message on line 1 Set message on line 2 Set message on line 3 Display message // PCLK at 30 MHz
// Feed process
// Wait until PLL Locked // Connect the PLL as the clock source // Feed process
// Enabling MAM and setting number of clocks // used for Flash memory fetch // (4 cclks in this case)
Listing P8-2 : glcd_01.c file of GLCD5110 simple message display for LPC2148 experiment (continue)
lcdUpdate(); lcdGotoXY(3,5); for (cnt='0'; cnt<='9'; cnt++) { lcdChar(cnt); } lcdUpdate(); lcdGotoXY(1,6); for (cnt'A'; cnt<'N'; cnt++) { lcdChar(cnt); } lcdUpdate(); while(1) { lcdBackLight(1); delay_ms(500); lcdBackLight(0); delay_ms(500); } }
// Display message // Infinite loop // // // // BackLight Delay 500 BackLight Delay 500 ON ms OFF ms
Listing P8-2 : glcd_01.c file of GLCD5110 simple message display for LPC2148 experiment (final)
Rectangular
Pixel dot
Straight line
//// // Program : Example for GLCD display // Description : Example for GLCD display on JX-2148 Board // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : glcd_02.c // C compiler : CodeSourcery G++ Lite Compiler //// #include lpc214x.h // Header file for Phillips LPC2148 controller #include glcd5110.h // Header file for Phillips LPC2148 controller // Initial system clock // void init() { PLL0CFG=0x24; // MSEL = 4,PSEL = 2 PLL0FEED=0xAA; // Feed process PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55;
// Feed process
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; // Connect the PLL as the clock source // Feed process
MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch // (4 cclks in this case) MAMTIM=0x4; VPBDIV=0x02;
// PCLK at 30 MHz } // Main Program // void main() { init(); // Initialize the system SCS = 0x03; // select the fast version of the I/O ports lcdPixel(60,20,1); // Draw Pixel at(60,20) lcdLine(40,30,25,40,1); // Draw Line at(60,20) lcdRect(5,5,25,25,0,1); // Draw Rectangle at(60,20) lcdUpdate(); // Display message while(1); }
Listing P8-3 : glcd_02.c file of GLCD5110 simple graphic display for LPC2148 experiment
P0.07 P0.06
+3.3V
31 30 29 27 40 44
IC3 LPC2148
C4 15pF
RESET
SDIN
SCLK
GND
+3.3V
1 3 5
C5 15pF C6 33pF +3.3V XTAL1 32.768kHz
62 61
C7 33pF
XTAL2 12MHz
DSP1 GLCD5110
JP2 8HAB
+3.3V GND
63
23 43 51 7
59 VssA 49 Vbat
41 SW2 4-5-6
R21 10k
ISP
+V +3.3 V
SW3
SCE
57
D/C
Procedure :
8.3.1 Build new project, in name glcd_03. 8.3.2 Write the program Listing P8-4. Compile to glcd_03.hex and download to microcontroller. Close LPC2000 Flash Utility program.
//// // Program : Example for GLCD display // Description : Example for GLCD display analog value with Progress bar // Frequency : Crystal 12 MHz at PLL 5x(CCLK = 60 MHz),PCLK = 30 MHz // Filename : glcd_03.c // C compiler : CodeSourcery G++ Lite Compiler //// #include lpc214x.h // Header file for Phillips LPC2148 controller #include glcd5110.h // Header file for Phillips LPC2148 controller // Initial system clock // void init() { PLL0CFG=0x24; // MSEL = 4,PSEL = 2 PLL0FEED=0xAA; // Feed process PLL0FEED=0x55; PLL0CON=0x1; PLL0FEED=0xAA; PLL0FEED=0x55;
// Feed process
while(!(PLL0STAT & 0x400)) ; // Wait until PLL Locked PLL0CON=0x3; PLL0FEED=0xAA; PLL0FEED=0x55; MAMCR=0x2; // Enabling MAM and setting number of clocks used for Flash memory fetch // (4 cclks in this case) MAMTIM=0x4; VPBDIV=0x02; } // Function for Read analog value from A/D 0.30 // unsigned int analog(void) { unsigned int val=0; AD0CR |= 0x01000000; // Start A/D Conversion while ((AD0DR3 & 0x80000000) == 0); // Wait for the conversion to complete val = ((AD0DR3 >> 6) & 0x03FF); // Extract the A/D result return(val); } // PCLK at 30 MHz // Connect the PLL as the clock source // Feed process
Listing P8-4 : glcd_03.c file of GLCD5110 graphic display with A/D converter for LPC2148 experiment (continue)
// Main Program // void main() { char str_adc[20]; // Buffer for keep string from analog value char str_percentage[10]; // Buffer for keep string from percentag unsigned int adc_val=0,percentage=0; // Variable for keep analog and percentage init(); // Initialize the system SCS = 0x03; // select the fast version of the I/O ports PINSEL1 |= 0x10000000; // Enable AD0.3 AD0CR = 0x00210608; // Setup A/D: 10-bit AIN0 @ 3MHz while(1) { adc_val = analog(); percentage = (adc_val*100)/1023; sprintf(str_adc,(A/D): %d lcdString(1,1,str_adc); sprintf(str_percentage,%d%% lcdString(7,3,str_percentage); // Set percentage value display on line 3 lcdProgBar(0,10,83,5,percentage); lcdUpdate(); } } // Display progress bar at // (0,10),width 83 pixel,high 5 pixel // Display message // Infinite loop // Get analog value from A/D 0.30 // Calculate to percentage
,adc_val); // Convert string to display analog value // Set analog value display on line 1 ,percentage); // Convert string to display percentage
Listing P8-4 : glcd_03.c file of GLCD5110 graphic display with A/D converter for LPC2148 experiment (final)
8.3.3 Run program. Adjust the variable resistor on JX-2148 board and see the result at GLCD 5110 screen. The GLCD5110 displays the conversion value and progress bar that get from A/D converter module of LPC2148.