Arm Programs Gpio
Arm Programs Gpio
GPIO
GPIO-LPC2129
P0.0 to P0.15 <======> PINSEL0 (32-Bit Register) ; (Reg_Bit_No= x*2+1: x*2) corresponds to P0.x
P0.16 to P0.30 <======> PINSEL1 (32-Bit Register); (Reg_Bit_No= x*2+1: x*2) EXCEPT P0.26
1. Write a code to set P0.20 GPIO pin as o/p and HIGH without affecting other pins.
Logic:
II Configure the direction of the Pin P0.20 as o/p pin, using IO0DIR register
CODE:
#include<LPC2129.H>
main(){
PINSEL1 &= ~(1<<8) ; To Reset PINSEL1(8) { alternative code (PINSEL1 &= 0xFFFF FEFF) }
PINSEL1 &= ~(1<<9) ; To Reset PINSEL1(9) ) { alternative code (PINSEL1 &=0xFFFF FDFF) }
IO0DIR |=(1<<20); To Set IO0DIR.20 and to make P0.20 in o/p mode ;{ alternative code (IO0DIR |=0x0010 0000) }
IO0SET |=(1<<20); To Set IO0SET.20 and to make P0.20 HIGH ;{ alternative code (IO0SET |=0x0010 0000) }
}
2. Write a code to configure P1.23 GPIO pin as o/p and make LOW without affecting other pins.
Logic:
II Configure the direction of the Pin P1.23 as o/p pin, using IO1DIR register
CODE:
#include<LPC2129.H>
main(){
PINSEL2 &= ~(1<<3) ; To make p1.23 GPIO { alternative code (PINSEL2 &= 0xFFFF FFF7) }
3. GPIO –programming to turn - ON and turn - OFF LEDs ( Assume Common Anode)
CODE:
include <LPC2129.H>
void Delay(void){
int i,j;
for(j=0;j<1000;j++);
int main(void){
while(1){
IO0CLR | = (1<<10) | (1<<11); To clear P0.10, P0.11 and hence TURN ON LED
Delay();
IO0SET | = (1<<10) | (1<<11); To set P0.10, P0.11 and hence TURN OFF LED
Delay();
return (0);
OR
else TEST_PIN=0;
ii) program to turn LED ON when button is pressed and OFF when button is released.
Assumptions:
2. Pin P0.15 ( From push button; LOW ==> button is pressed and HIGH =Released)
LOGIC:
4. Turn ON & OFF LED with some delay to test the functionality
i) Turn LED ON by setting P0.10 'LOW' ; Turn LED OFF by setting P0.10 'HIGH'
code:
#include "lpc2129.h"
#define LED_PIN (1 << 10)
int PinsPort0 =IO0PIN; // Read the current state of all pins in GPIO ==> PORT0
int pinState = (PinsPort0 & (1 << pinNumber)) ? 1 : 0; // Read the value of 'pinNumber'
void Delay(void){
int i,j;
for(j=0;j<1000;j++);
int main(void){
// To TEST LED
Delay();
Delay();
while (1) {
}
ADC
cclk=processor clock = Crystal freq= 60MHz(Max)
*******************************************************************
LPC 2129 has 4-channel , 10-bit ADC(Successive Approx), conv Time = 2.44 mSec,
Basic clocking for the A/D converter is provided by the VPB clock. A programmable divider is included to scale
this clock to the 4.5 MHz (max) clock needed by the successive approximation process. A fully accurate
conversion requires 11 of these clocks.
***************************************************************************
Burst Mode; use Ain3, processor clock frequency=60MHz,Use ADC clock = 3MHz
4clk/3bits
ADCR: 0x 002F0908
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 9 8 7 6 5 4 3 2 1 0
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
X X X x 0 0 0 0 0 0 1 x 1 1 1 1 0 0 0 0 1 0 0 1 x x x x 1 0 0 0
ADDR
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 9 8 7 6 5 4 3 2 1 0
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
D D D D D D D D D D D
o 9 8 7 6 5 4 3 2 1 0
n
e
include<lpc2129.h>
int main(void){
while(1) A2D();
void A2D(void){
do{
}
UART
P0.0 / TxD0 / PWM1 P0.1 / RxD0 / PWM3 / EINT0
UART Programming
Write a C code to transmit the message "SERIAL" first, then what ever data is received
serially, should be sent back serially using UART0. Assume 8-bit character size, 1-
pclk 30 MHz
value= = =195 .31≈195≈0 xC 3
// 16 X 9600 16 X 9600
#include <lpc2129.h>
#include <stdio.h>
void init_serial (void);
int putchar (int ch);
int getchar (void);
int main(void){
char *Ptr = "SERIAL";
VPBDIV = 0x02; // pclk = 30MHz
init_serial();
while (*Ptr){
putchar(*Ptr++);
}
while(1){
putchar(getchar());
}
}
void init_serial (void){
PINSEL0 = 0x00000005; // TX & RX pins are configured
U0LCR = 0x0000009B; //8-bit character ;1-stop bit; even parity & Enable access to Divisor Latch
Configure the following LPC 2129 pins ( Ref fig1) i) AIN 2 ii) RxD0 iii)TxD0 iv) EINT3 for the
desired function. 6
i)AIN 2; p0.29 (01); ===> PINSEL1[27:26]=01
ii) RxD0 ; p0.1(01) ==> PINSEL0[3:2]=01 iii)TxD0:p0.0(01) ==> PINSEL0[1:0]=01
iv) EINT3; p0.9(11)===> PINSEL0[19:18]=11
PINSEL0 |=0x000C0005;
PINSEL0 &=0xfffffff5;
PINSEL1 |=0x04000000;
PINSEL1 &=0xf7ff ffff;