0
1.8kviews
ARM Programming with Embedded C(part 2)
1 Answer
0
16views

REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME REMOVEME

Pin 55: P0.20/ SSEL1/ MAT1.3/ EINT3 P0.20 is a GPIO digital pin I/O. MAT1.3 is a match o/p for Timer 1, channel 3. I SSEL1 is a Slave Select designed for SSP. Here, chooses the interface of SSP as a slave. EINT3 is an external interrupt 3-input. Pin56: P1.29/TCK P1.29 is a GPIO digital pin I/O TCK is a test CLK for an interface of JTAG.
Pin57: External Reset Input The device can be rearranged by a LOW on this pin, effecting Input/Output ports as well as peripherals for obtaining on their default conditions, & processor execution begins at address 0. Pin58: P0.23/VBUS P0.23 is a GPIO digital pin I/O VBUS specifies the existence of USB-bus power
Pin59: VSSA VSSA is an analog ground, and this must be the similar voltage like VSS, although it should be separated to reduce error and noise. Pin60: P1.28/TDI 60 P1.28 is a GPIO digital pin I/O TDI pin is a test data is used for interfacing JTAG
Pin61: XTAL2 XTAL2 is an o/p from the oscillator amplifier Pin62: XTAL1 XTAL1 is an i/p to the internal CLK generator as well as oscillator circuits
Pin63: VREF-ADC Reference This pin should be nominally equal or less than to the voltage VDD although it should be separated for reducing error as well as noise. Pin64: P1.27/TDO 64 P1.27 is a GPIO digital pin I/O TDO is a test data out used for interfacing JTAG

Start with programming

First step is for programming is to arrange GPIO pins. The general purpose I/O pins includes P0.0 to P0.31 and P1.16 to P1.31. Port-0 and Port-1 are 32-bit Input/output ports, and every bit of these ports can be controlled by an individual direction. The operations of port-0 & port-1 depend upon the function of a pin that is selected using the pin connected block. In Port-0, pins like P0.24, P0.26 & P0.27 are not obtainable whereas, in Port-1, the Pins 0 to 15 are not obtainable.

6.1 General Purpose Input Output

General-purpose input/output (GPIO) is pin on integrated circuits. It can be input or output pin whose behaviour is controlled at the run time. LPC2148 has two 32 bit general purpose I/O ports.

1. PORT 0 PORT0 is a 32-bit port Out of these 32 pins, 28 pins can be configured as either general purpose input or output. 1 of these 32 pins (P0.31) can be configured as general-purpose output only. 3 of these 32 pins (P0.24, P0.26 and P0.27) are reserved. Hence, they are not available for use.

2. PORT 1 PORT 1 is also a 32-bit port. Only 16 of these 32 pins (P1.16 – P1.31) are available for use as general-purpose input or output.

Every pin of these two ports has alternative functions. Example P0.0 can be configured as TXD pin for UART0 or PWM1 pin. The functionality of each pin is selected by using Pin Function Select Register.

Pin Function Select Registers

Pin Function Select Registers are 32 bit registers. They are used to select or configure specific pin functionality. There are 3 pin function select registers.

  1. PINSEL0 : - PINSEL0 is used to configure PORT0 pins P0.0 to P0.15.

  2. PINSEL1 : - PINSEL1 is used to configure PORT0 pins P0.16 to P0.31.

  3. PINSEL2 : - PINSEL2 is used to configure PORT1 pins P1.16 to P1.31.

Fast and Slow GPIO Registers

LC2148 have 5 Fast (also called Enhanced GPIO Features Registers) GPIO Registers and 4 Slow (also called Legacy GPIO Registers) GPIO Registers. These registers are available to control PORT0 and PORT1. The Slow Registers will allow backward compatibility with previous family devices using the existing codes. SLOW GPIO Registers: There are four Slow GPIO registers.

1) IOxPIN (GPIO Port Pin value register): This is 32 bit register. This is used read/write the value on Port (PORT0/PORT1). Masking used to ensure write to the desired pin. Examples:

1) Writing 1 to P0.4 using IO0PIN IO0PIN = IO0PIN | (1<<4)

2) Writing 0 to P0.4 using IO0PIN IO0PIN = IO0PIN & (~ (1<<4))

3) Writing F to P0.7-P0.4 IO0PIN = IO0PIN | (0x000000F0)

2) IOxSET (GPIO Port Output Set register): This is a 32-bit register. This register is used to make pins of Port (PORT0/PORT1) HIGH. Writing one to specific bit will make that pin HIGH.

3) IOxDIR (GPIO Port Direction control register): This is a 32-bit register. This register used to controls i controls the direction of each port pin. Setting a bit to ‘1’ will make corresponding pin as an output pin. Setting a bit to ‘0’ will make corresponding pin as an input pin.

4) IOxCLR (GPIO Port Output Clear register): This is a 32-bit register. This register is used to make pins of Port LOW. Writing one to specific bit makes that pin LOW. Examples:

a) Configure pin P0.0 to P0.3 as input pins and P0.4 to P0.7 as output pins. IO0DIR = 0x000000F0;

b) Configure pin P0.4 as an output. Then set that pin HIGH. IO0DIR = 0x00000010; OR IO0DIR = (1<<4); IO0SET = (1<<4);

c) Configure pin P0.4 as an output. Then set that pin LOW. IO0DIR = 0x00000010; OR IO0DIR = (1<<4); IO0CLR = (1<<4);

Fast GPIO Registers

There are five fast GPIO registers. Fast GPIO registers are relocated in ARM local bus. Because of this software access to GPIO pins will be 3.5 times faster. Therefore they are called as Fast GPIO Registers.

1) FIOxDIR (Fast GPIO Port Direction control register): This is a 32-bit register. This register will control the direction of each port pin. Setting a bit to ‘1’ will make corresponding pin as an output pin. Setting a bit to ‘0’ will make corresponding pin as an input pin.

2) FIOxMASK (Fast Mask register for port): This is a 32-bit register. This register controls the effect of fast registers (FIOxPIN, FIOxSET, FIOxCLR) on port pins. Setting a bit to ‘0’ will make corresponding pin access to the fast registers. We able to write/read the corresponding pin in fast mode using fast registers. Setting a bit to ‘1’ will make corresponding pin unaffected by fast registers.

3) FIOxPIN (Fast Port Pin value register using FIOMASK): This is a 32-bit register. This register is used to read/write the value on port pins, only if that corresponding port pins have access to fast register.

4) FIOxSET (Fast Port Output Set register using FIOMASK) : This is a 32-bit register. This register is used to make pins of Port HIGH. Writing one to specific bit makes that pin HIGH. Writing zero has no effect. Reading this register returns the current contents of the port output register.

5) FIOxCLR (Fast Port Output Clear register using FIOMASK): This is a 32-bit register. This register is used to make pins of Port LOW. Writing one to specific bit makes that pin LOW. Writing zeroes has no effect. Only bits enabled by ZEROES in FIOMASK can be altered. Examples:

a) Configure pin P0.0 to P0.3 as input pins and P0.4 to P0.7 as output pins. FIO0DIR = 0x000000F0;

b) Configure pin P0.4 as an output. Then set that pin HIGH. FIO0DIR = 0x00000010; OR FIO0DIR = (1<<4); FIO0SET = (1<<4);

c) Configure pin P0.4 as an output. Then set that pin LOW. FIO0DIR = 0x00000010; OR FIO0DIR = (1<<4); FIO0CLR = (1<<4);

Program:

Program for turning LED ON or OFF depending on the status of the pin. LED is interfaced to P0.0. A switch is interfaced to P0.1 to change the status of the pin.

enter image description here

Program:

 #include < lpc214x.h>

 #include < stdint.h>

int main(void)

{   

//PINSEL0 = 0x00000000; /* Configuring P0.0 to P0.15 as GPIO */

/* No need for this as PINSEL0 reset value is 0x00000000 */

IO0DIR = 0x00000001;        /* Make P0.0 bit as output bit, P0.1 bit as an input pin  */

while(1)

{       

if ( IO0PIN & (1<<1) )  /* If switch is open, pin is HIGH */

{

IO0CLR = 0x00000001;   /* Turn on LED */

}

else   /* If switch is closed, pin is LOW */

{

IO0SET = 0x00000001;    /* Turn off LED */

}       

}   

}

6.2 Timer Mode

Timer is type of clock which is used to measure time intervals. Timer requires clock to work and counter is similar to the timer but it works reverse of timer. Counter counts external events.

LPC2148 Timer& Counter LPC2148 has two 32-bit timers/counters: Timer0/Counter0 & Timer1/Counter1

• LPC2148 Timer has input of peripheral clock (PCLK) or an external clock. It counts the clock from these clock sources.

• LPC2148 Timer/Counter able to generate an interrupt signal at specified time value.

• LPC2148 has match registers that maintains count value which is continuously compared with the value of the Timer register. When the value in the Timer register matches with the value in the match register, specific action (timer reset, or timer stop, or generate an interrupt) is taken.

Timer0 Registers

1. T0IR (Timer0 Interrupt Register)

• It is an 8-bit read-write register.

• It consists of 4 bits for match register interrupts and 4 bits for compare register interrupts.

• If interrupt is generated, then the related bit in this register will be high, otherwise it will be low.

• Writing a 1 to any bit of this register will reset that interrupt.

enter image description here

T0IR (Timer0 Interrupt Register)

2. T0TCR (Timer0 Timer Control Register)

• It is an 8-bit read-write register.

• It is used to control the operation of the timer counter.

enter image description here

Timer’s Pre scale Counter (PC), or clear PC and increment Timer Counter (TC). 00 = Timer Mode: Every rising edge of PCLK T0TCR (Timer0 Timer Control Register)

• Bit 0 – Counter Enable

0 = Counters are disabled

1 = Timer counter and Pre scale counter are enabled for counting

• Bit 1 – Counter Reset

0 = Counter not reset

1 = Timer counter and Pre scale counter are synchronously reset on next positive edge of PCLK

3. T0CTCR (Timer0 Counter Control Register)

• It is an 8-bit read-write register.

• Used for selection between timer counter modes.

• When in counter mode, it is used for selection of pin and edges for counting.

Bits 1:0 – Counter/Timer Mode this field selects which rising edges of PCLK can increment

01 = Counter Mode: TC is incremented on rising edge on the capture input selected by Bits 3:2.

10 = Counter Mode: TC is incremented on falling edge on the capture input selected by Bits 3:2

01 = Counter Mode: TC is incremented on both edges on the capture input selected by Bits 3:2

Bits 3:2 – Count Input Select When bits 1:0 in this register are not 00, these bits select which capture pin(CAP) is sampled for clocking.

00 = CAP0.0

01 = CAP0.1

10 = CAP0.2

11 = CAP0.3

4. T0TC (Timer0 Timer Counter) It is a 32-bit timer counter. It is incremented when the Pre scale Counter (PC) reaches its maximum value held by Pre scaler Register (PR).

5. T0PR (Timer0 Prescale Register) It is a 32-bit register. It holds the maximum value of the Pre scale Counter.

6. T0MR0-T0MR3 (Timer0 Match Registers) These are 32-bit registers. The values stored in these registers are compared with the Timer Counter value. When the two values are equal, the timer can be reset or stop or an interrupt may be generated. The T0MCR controls what action should be taken on a match.

7. T0MCR (Timer0 Match Control Register) It is a 16-bit register. It controls what action is to be taken on a match between the Match Registers and Timer Counter.

enter image description here

T0MCR (Timer0 Match Control Register)

Bit 0 – MR0I (Match register 0 interrupt)

0 = This interrupt is disabled

1 = Interrupt on MR0. An interrupt is generated when MR0 matches the value in TC (Timer Counter)

Bit 1 – MR0R (Match register 0 reset)

0 = This feature is disabled

1 = Reset on MR0. The TC (Timer Counter) will be reset if MR0 matches it

• Bit 2 – MR0S (Match register 0 stop)

0 = This feature is disabled

1 = Stop on MR0. The TC (Timer Counter) and PC (Pre scale Counter) is stopped and Counter Enable bit in T0TCR is set to 0 if MR0 matches TC

• MR1, MR2 and MR3 bits function in the same manner as MR0 bits.

Example: Write a program for generating a delay of 100msec and blink LEDs using LPC2148 Timer.

enter image description here

PCLK = 30MHz Hence, we will load T0PR with a value 29, so that the TC will increment after every 30 PCLK rising edges. 30 MHz/30 = 1MHz. Which gives 1µsec time.To get 100msec time, we will have to load T0MR with 100000 (1 µsec * 1000 = 1msec, 1msec * 100 = 100msec. Hence, 1000 * 100 = 100000) This will give us a delay of 100msec.

Here we are using timer interrupt MR0. On each compare match between MR0 and TC, the ISR for Timer0 will be executed where we are toggling the pin connected to the LED.

Program:

 # include < lpc214x.h>

_irq void T0_ISR (void)

{

IO0PIN = ( IO0PIN ^ (0x00000100) ); /* Toggle P0.8 pin */ 

T0IR = ( T0IR | (0x01) ); 

VICVectAddr = 0x00;

}

int main (void)

{

VPBDIV = 0x00000002; /* For Pclk = 30MHz */
    /* We have configured Cclk=60MHz. Above instruction makes Pclk = Cclk/2 = 30MHz */

PINSEL0 = PINSEL0 | 0x00000020; /* Configure P0.2 as Capture 0.0 */

IO0DIR = ( IO0DIR | (0x00000100) ); /* 8 P0.8-P0.15 as output pins for LED port */

IO0PIN = IO0PIN | 0x00000100; /* Writing 1 to LED pin P0.8 */

VICVectAddr0 = (unsigned) T0_ISR; /* T0 ISR Address */

VICVectCntl0 = 0x00000024; /* Enable T0 IRQ slot */

VICIntEnable = 0x00000010; /* Enable T0 interrupt */

VICIntSelect = 0x00000000; /* T0 configured as IRQ */

T0TCR = 0x02; /* Reset TC and PR */

T0CTCR = 0x00; /* Timer mode, increment on every rising edge */

T0PR = 0x1D; /* Load Pre-Scalar counter with 29 (0 to 29 = 30), so that timer counts every 1usec */

T0MR0 = 100000; /* Load timer counter for 100msec delay, 1usec*1000*100 */

T0MCR = 0x0003; /* Interrupt generate on match and reset timer */

T0TCR = 0x01; /* Enable timer */

while (1);

}
Please log in to add an answer.