ESD Lab1

758 views
678 views

Published on

Published in: Education, Business, Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
758
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
10
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

ESD Lab1

  1. 1. Lab 1 - Basic Input and Output (GPIO)GPIO, or General Purpose Input/Output, is the easiest way for you to interact with basicperipherals like buttons, LEDs, switches, and other components. It can also be used for morecomplex components like text and graphic LCDs, but for now well start with a few basiccomponents 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 withsome basic examples of how they work.IODIRIODIR controls the direction of the GPIO pin. You use this register to set a GPIO pin to eitherInput (0) or Output (1). For example, if we want to use our GPIO pin to send signals out fromthe microcontroller to some external device, we need to set a pin (for example GPIO 0.10) toOutput (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 itinside the microcontroller), we would need to set GPIO 0.10 to Input (0). That could beaccomplished the following code:GPIO0_IODIR &= ~(1 << 10);To set several pins to output at once we could do either of the following (which will produceidentical code when compiled):// Set GPIO 0.10, 0.11, and 0.15 to outputGPIO0_IODIR |= (1 << 10) | (1 << 11) | (1 << 15);// Set GPIO 0.10, 0.11, and 0.15 to output using hexadecimalGPIO0_IODIR |= 00008C00;IOSET and IOCLRIf your GPIO pin is set as Output (using the IODIR register mentionned above), you can useIOSET to set your GPIO pin to high (providing a small 3.3V electrical current) or IOCLR toset it to low (providing a connection to GND). You shouldnt really think about high being onand low being off, though, since ... as well see in the example below ... you can often turn adevice on by setting it low and off by setting it high, depending on how the components areconnected.There are LEDs provided on board for testing purposes, connected to GPIO pins 0.10 and0.11. Refer to the schematic of the development board. What this means is that if we want toturn the LEDs on, we need to complete the electrical circuit by setting GPIO pins 0.10 and 0.11ENP 505 Prof. Anish Goel Page 1
  2. 2. to GND, or low. This is accomplished with IOCLR. So, if we wanted to turn both LEDs ONand then OFF we would use the following code:// Make sure GPIO 0.10 and 0.11 are set to outputGPIO0_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);IOPINRegardless of whether your GPIO pins direction is set to Input or Output, you can use theIOPIN register to read the current state of every GPIO pin in the pin block (the collection of all32 pins in GPIO0). A 1 value means that the pin is currently high, and a 0 value means the pinis currently low. (Please note that since IOPIN returns the current state of ALL 32 pins in thepin block, you have to do a little bit of extra work to determine the value of one specific pin, butwell give you an example below.)You could read the IOPIN register, for example, to see if your LED was currently turned on oroff, where IOPIN would return 0 for pin 10 (LED1) if it was currently turned on (since settingthe GPIO pin to low turns the LED on) and 1 if the LED was off (since setting the GPIO pin highturns our LED off). Heres a simple method showing how to do this, including one way to readthe value of a single pin from the 32-bit value returned by IOPIN. This method will return 1 ifthe 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;}ENP 505 Prof. Anish Goel Page 2
  3. 3. Exercise: Toggling LEDs with a ButtonIf you take all of the information presented above, you should be able to put together a simpleprogram that will turn an LED on or off depending on whether a button is currently held down orreleased. Turn LED1 on when Button1 is pressed, and LED2 on when Button2 is pressed.To help you out if youre not really comfortable reading schematics, here is the basic informationyoull need to complete this exercise:Device GPIO Pin On StateLED1 0.10 Low / 0LED2 0.11 Low / 0Button1 0.15 Low / 0Button2 0.16 Low / 0ENP 505 Prof. Anish Goel Page 3

×