esd lab1

3

Click here to load reader

Upload: anishgoel

Post on 19-Jun-2015

527 views

Category:

Education


0 download

TRANSCRIPT

Page 1: ESD Lab1

ENP 505 Prof. Anish Goel Page 1

Lab 1 - Basic Input and Output (GPIO) GPIO, or General Purpose Input/Output, is the easiest way for you to interact with basic peripherals like buttons, LEDs, switches, and other components. It can also be used for more complex components like text and graphic LCDs, but for now we'll start with a few basic components that are relatively easy to get working.

In order to get started with GPIO, you need to understand the four 'registers' that control it: IODIR, IOSET, IOCLR and IOPIN. Each of these registers is explained in detail below with some basic examples of how they work.

IODIR

IODIR controls the 'direction' of the GPIO pin. You use this register to set a GPIO pin to either Input (0) or Output (1). For example, if we want to use our GPIO pin to send signals 'out' from the microcontroller to some external device, we need to set a pin (for example GPIO 0.10) to 'Output' (1). We could do that with the following code:

GPIO0_IODIR |= (1 << 10);

If we wanted to use our GPIO pin to receive information from the outside world (reading it inside the microcontroller), we would need to set GPIO 0.10 to 'Input' (0). That could be accomplished the following code:

GPIO0_IODIR &= ~(1 << 10);

To set several pins to output at once we could do either of the following (which will produce identical code when compiled):

// Set GPIO 0.10, 0.11, and 0.15 to output GPIO0_IODIR |= (1 << 10) | (1 << 11) | (1 << 15); // Set GPIO 0.10, 0.11, and 0.15 to output using hexadecimal GPIO0_IODIR |= 00008C00; IOSET and IOCLR

If your GPIO pin is set as Output (using the IODIR register mentionned above), you can use IOSET to set your GPIO pin to 'high' (providing a small 3.3V electrical current) or IOCLR to set it to 'low' (providing a connection to GND). You shouldn't really think about high being 'on' and low being 'off', though, since ... as we'll see in the example below ... you can often turn a device 'on' by setting it low and 'off' by setting it 'high', depending on how the components are connected.

There are LEDs provided on board for testing purposes, connected to GPIO pins 0.10 and 0.11. Refer to the schematic of the development board. What this means is that if we want to turn the LEDs 'on', we need to complete the electrical circuit by setting GPIO pins 0.10 and 0.11

Page 2: ESD Lab1

ENP 505 Prof. Anish Goel Page 2

to GND, or 'low'. This is accomplished with IOCLR. So, if we wanted to turn both LEDs ON and then OFF we would use the following code:

// Make sure GPIO 0.10 and 0.11 are set to output GPIO0_IODIR |= (1 << 10) | (1 << 11); // Turn the LEDs on using IOCLR (which gives a GND connection) GPIO0_IOCLR |= (1 << 10) | (1 << 11); // Turn the LEDs off using IOSET (which supplies 3.3V and breaks our circuit) GPIO0_IOSET |= (1 << 10) | (1 << 11); IOPIN

Regardless of whether your GPIO pin's direction is set to Input or Output, you can use the IOPIN register to read the current 'state' of every GPIO pin in the pin block (the collection of all 32 pins in GPIO0). A 1 value means that the pin is currently 'high', and a 0 value means the pin is currently 'low'. (Please note that since IOPIN returns the current state of ALL 32 pins in the pin block, you have to do a little bit of extra work to determine the value of one specific pin, but we'll give you an example below.)

You could read the IOPIN register, for example, to see if your LED was currently turned on or off, where IOPIN would return 0 for pin 10 (LED1) if it was currently turned on (since setting the GPIO pin to low turns the LED on) and 1 if the LED was off (since setting the GPIO pin high turns our LED off). Here's a simple method showing how to do this, including one way to read the value of a single pin from the 32-bit value returned by IOPIN. This method will return '1' if the supplied pin is currently 'high', and '0' if it is currently low:

int getPinState(int pinNumber) { // Read the current state of all pins in GPIO block 0 int pinBlockState = GPIO0_IOPIN; // Read the value of 'pinNumber' int pinState = (pinBlockState & (1 << pinNumber)) ? 1 : 0; // Return the value of pinState return pinState; }

Page 3: ESD Lab1

ENP 505 Prof. Anish Goel Page 3

Exercise: Toggling LEDs with a Button If you take all of the information presented above, you should be able to put together a simple program that will turn an LED on or off depending on whether a button is currently held down or released. Turn LED1 on when Button1 is pressed, and LED2 on when Button2 is pressed. To help you out if you're not really comfortable reading schematics, here is the basic information you'll need to complete this exercise:

Device GPIO Pin 'On' State LED1 0.10 Low / 0 LED2 0.11 Low / 0 Button1 0.15 Low / 0 Button2 0.16 Low / 0