39
ELECTRONICS INTRODUCTION GHI Electronics, LLC - Where Hardware Meets Software

Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

  • Upload
    lecong

  • View
    216

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

ELECTRONICSINTRODUCTION

GHI Electronics, LLC - Where Hardware Meets Software

Page 2: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Contents

Introduction.......................................................................................................................2

Overview.........................................................................................................................2

Guidelines.......................................................................................................................2

Current and Voltage...........................................................................................................3

Exercise...........................................................................................................................4

Logic Levels........................................................................................................................5

Microcontrollers.............................................................................................................5

Binary Numbers..............................................................................................................6

Exercise...........................................................................................................................6

Output Pins........................................................................................................................7

Wiring an LED...............................................................................................................10

Breadboards.................................................................................................................12

Controlling Transistors..................................................................................................13

Exercise.........................................................................................................................16

Input Pins.........................................................................................................................17

Floating Pins and Pull Resistors.....................................................................................17

Glitch Filter...................................................................................................................19

Wiring Buttons..............................................................................................................19

Exercise.........................................................................................................................19

Interrupt Pins...................................................................................................................20

Edges vs. Levels.............................................................................................................21

Events...........................................................................................................................22

Exercise.........................................................................................................................23

Page 1 of 2623

Page 3: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Introduction

The BrainPad circuit board is designed as a powerful educational tool that can be used to teach everyone from kids, to college students and professionals. Kids will start to learn programming using Visual Studio, one of the most widely used professional tools. College students and professionals that already know programming can use the BrainPad circuit board to learn about digital electronics and the connection between computing and the physical world.

Overview

Students will learn about voltage, logic levels and what true and false means in electronics vs. programming. GPIOs (General Purpose Inputs/Outputs), LEDs (Light-Emitting Diode) and buttons will be wired and used.

Guidelines

Prerequisites: None Ages 12 and up PC setup with Visual Studio, .NET Micro Framework and GHI Electronics’

software. Supplies: BrainPad, breadboard, wires, 330 ohm resistor and LED

Page 2 of 2623

Page 4: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Current and Voltage

Electricity is when electrons move from a power source to a device. The flow of electrons is called current. The more energy the device needs, the more electrons it will take, and the higher the current will be.

AA and AAA batteries are both 1.5 V but AA batteries one can store more energy. This means it will be able to provide more electrons in the same time constant or provide more time at the same current. Think of it as water containers, one is larger than the other. If you pour the same amount of water from both, the smaller one will be empty first. To empty them both at the same time, we would need to pour more water from the larger container.

Another way to think of current running through wires is like water running through a river. The current is how much water is flowing every minute. Current in electronics is measured in amperes or amps (A). Small electronic devices like the BrainPad will draw much less than one amp. It will use milliamperes or milliamps (mA). One milliamp is one thousandth of an amp, so 1,000 milliamps equals one amp.

Voltage (V) on the other hand, is how much force electrons are holding. The amount of energy (Watts) needed to run any device is the voltage multiplied by the current. In theory, we could use a million volt battery and the device connected would need very little current to run. Unfortunately, electronic devices can’t handle such high voltage, not to mention it could cause serious harm if mishandled.

Most electronics will only run on a constant 3.3 V. This takes using batteries directly out of the picture since their voltage drops as they drain. The solution is to add a voltage regulator that takes a higher voltage and lowers it down to a constant 3.3 V. USB ports on PCs provide 5 V so this works well.

Page 3 of 2623

Page 5: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

The BrainPad can be powered from a PC USB port or phone charger. The small circuit by the USB connector is the 3.3 V regulator as shown in Figure 1.

Figure 1 – 3.3 V regulator

Exercise

If the BrainPad’s measured current is 50 mA at the 5 V source, then how much energy (Watts) is needed to run the BrainPad?

Page 4 of 2623

Page 6: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Logic Levels

In digital electronics, everything is a logic zero or one. A one means there is voltage, and a zero means there is no voltage. However, what voltage defines a high and low? This depends on the system operating voltage, but most are 3.3 V. Logic high is any voltage higher than 2.7 V (3.3 × 0.8). Logic low is any voltage lower than 0.7 V (3.3 × 0.2).

Any voltage between 0.7 V and 2.7 V should not be used. Any voltage below zero or above 3.3 V is strictly prohibited and can damage the system. There is an exception to this rule. Some processors are 5 V tolerant. This means connecting up to 5 V to a pin is allowed. This does not mean the processor is powered off 5 V, it’s still only using 3.3 V.

Microcontrollers

The processor is a complex circuit that reads instructions from memory and acts upon them. For the processor to work, it needs memory that it can modify constantly (RAM) and it needs memory where it loads instructions on power up (FLASH). The processor also needs peripherals to connect the world around it. A peripheral can be as simple as GPIO (General Purpose Input Output) or as complex as a USB port. On larger systems like PCs, these individual components are multiple components placed on the motherboard. This won’t work for small systems so smaller versions of everything are combined into a single chip. This is called the microcontroller. The BrainPad’s microcontroller is located in the middle of the BrainPad. The microcontroller is the brain that understands and executes loaded programs.

Logic levels are found both internally and externally to the microcontroller. They are used externally to set one or more of the physical pins high or low. Pins can also be configured to read the state coming in.

Page 5 of 2623

Page 7: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Binary Numbers

In the microcontroller, numbers are always represented with logic levels. This numbering system is called binary. Below is a table (Table 1) showing decimal vs. binary format. Decimal is the numbering system we use, which is base 10. Any ideas where the 10 came from? Just count your fingers.

Table 1 – Values in decimal vs. binary format.

DECIMAL BINARY0 0

1 1

2 10

3 11

4 100

5 101

6 110

7 111

8 1000

9 1001

10 1010

Exercise

What are the values in binary?

What is 10 + 10? What is 111101010001 - 1? What is 1000 - 1?

Page 6 of 2623

Page 8: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Output Pins

The GPIO pins on the microcontroller can be set to logic outputs to provide a voltage on the output pin. This voltage level on the output can be on or off, which is logic high or low. If we set a pin to output logic high, the pin will have 3.3 V on it. When it’s logic low, the pin will have a zero voltage on it. This can be used to turn something on or off using code.

However, there is a small catch here. While a pin can have 3.3 V on it, it can’t provide a lot of current. The typical range provided to pins is 4 mA to 10 mA. The good news is that LEDs (Light Emitting Diodes) do not need a lot of current to work. These LEDs are used on the traffic light as shown in Figure 2.

Figure 2 – Here we see the red LED is connected to pin PA1.

Let’s dig into the code. Start a new NETMF (.NET Micro Framework) C# Console Application and add these References:

Microsoft.SPOT.Hardware GHI.Pins

Tip: This lesson assumes there is a PC already setup with the correct versions of Microsoft’s Visual Studio, NETMF and GHI Electronics’ NETMF SDK.

The BrainPad’s “brain” is a System on Chip named G30 from GHI Electronics. To use the actual GPIO, we will need Microsoft’s NETMF Hardware library. To access the specific pins, we will need GHI Electronics’ Pins library.

Note: NETMF was once called SPOT. The name is still there for backward-compatibility.

Page 7 of 2623

Page 9: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Constructing an OutputPort object (Example 1) on a pin will automatically change the pin to an output. The second argument in the constructor is used to set the pin high (true) or low (false).

using Microsoft.SPOT.Hardware;using GHI.Pins;

namespace BrainPad_Project{ public class Program { public static void Main() { OutputPort redLed = new OutputPort(G30.Gpio.PA1, true); } }}

Example 1 – This code turns the red LED on.

What about if we want to blink the LED? This can be accomplished just as easy by using Thread.Sleep as shown in Example 2.

using System.Threading;using Microsoft.SPOT.Hardware;using GHI.Pins;

namespace BrainPad_Project{ public class Program { public static void Main() { OutputPort redLed = new OutputPort(G30.Gpio.PA1, true);

while (true) { redLed.Write(true); Thread.Sleep(1000); redLed.Write(false); Thread.Sleep(1000); } } }}

Example 2 – This code blinks the red LED.

Microcontrollers execute extremely fast. A loop without delays will blink the LED faster than we can see. Using Thread.Sleep allows us to delay code execution by a number of milliseconds (ms). Here we use 1,000 ms to delay for one second between writes.

Page 8 of 2623

Page 10: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

To make using pins easier, we’ve provided two classes within the BrainPad object. The BrainPad.Expansion and BrainPad.Peripherals objects. To add get access to these objects, in the Solution Explorer Window right-click BrainPad_Project, select Add > Existing Item… and locate the BrainPad.cs file and click Add.

The BrainPad.Expansion object can be used to easily access the labeled pins (E1 through E16) on the expansion headers. The BrainPad.Peripherals object also makes identifying pins related to such things as the traffic light faster. These both help reduce the need to look at the schematic. For example, we could access the traffic light’s red LED pin based on the schematic using G30.PwmOutput.PA1 or we could use BrainPad.Peripherals.TrafficLight.Red

which is more descriptive and easier to remember.

Going forward all examples will use the BrainPad.Expansion and BrainPad.Peripherals objects when referring to pins.

Page 9 of 2623

Page 11: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Wiring an LED

The expansion header on the BrainPad has numerous GPIO pins. Any pin will work for wiring an LED but let’s use pin E1. Next, take a current limiting resistor around 330 ohms and twist one of its legs with the LED’s short leg as shown in Figure 3.

Figure 3 – A resistor leg is twisted together with the LED’s short leg.

Tip: It’s important to know which LED leg is the short one as LEDs are directional. Connecting the LED backwards will not damage the LED but it will not turn on.

Page 10 of 2623

Page 12: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Plug the untwisted leg of the resistor into the ground pin and plug the LED’s long leg into pin E1 as shown in Figure 4.

Figure 4 – The LED and resistor are now plugged in properly.

Modify the previous example (Example 2) to use the expansion pin E1.

OutputPort redLed = new OutputPort(BrainPad.Expansion.Gpio.E1, true);

Example 3 – Create an OutputPort using pin BrainPad.Expansion.Gpio.E1.

The program will now blink the wired LED.

Compare how the LED is wired to how the LEDs on the traffic light are connected by looking at the BrainPad’s schematics. The wiring is identical, except it’s using a different pin on the microcontroller.

Page 11 of 2623

Page 13: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Breadboards

A breadboard is useful for electronics circuit prototyping. It’s simply a plastic surface with holes in it. Internally there is a conductive metal that connects each row of pins as shown in Figure 5.

Figure 5 – This is a peek into the internals of a breadboard.

Let’s learn to use a breadboard by reworking the LED wiring we did earlier. Plug the LED into the breadboard. Each of its legs goes in a different row so they are not internally connected. Put one leg of the resistor into the same row as the LED’s short leg and the other leg into its own, unused row. Use two wires from the expansion header to connect the resistor to ground and the LED to PA7 as shown in Figure 6.

Page 12 of 2623

Page 14: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Figure 6 – A simple LED circuit using a breadboard.

The same previous program (without modification) can be used to blink the LED on the breadboard.

Controlling Transistors

Since microcontrollers’ pins are only capable of providing small amounts of current, how are high current devices controlled? This is done by controlling a device that uses very little current to determine if the pin is high or low. In turn, this device controls a more powerful on/off mechanism. A transistor comes in handy for this use as shown in Figure 7. This transistor is capable of driving up to 200 mA. This is enough to power a small toy car motor. It can also be used to turn on solenoids and relays.

Page 13 of 2623

Page 15: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Figure 7 – The transistor is used to control the DC motor.

There are many types of transistors but this course will only briefly cover Bipolar Junction NPN transistors. In a digital circuit, a transistor’s emitter will connect to ground and the base to the microcontroller. The base will also need a current limiting resistor like the LED. Finally, the collector will be connected to the device we are trying to control. On the BrainPad, these connections are available on the DC Motor header.

Page 14 of 2623

Page 16: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Connect a DC motor to the header (Figure 8) and change the previous program to use pin PA0. Increase the sleep to three seconds so you can see the motor turn on and off.

Figure 8 – DC motor is connected to the BrainPad.

The transistor has a limit of 200 mA but it can still drive a device with lesser current (like the LED we used before). The LED and its current limiting resistor can be wired to the same pins as shown in Figure 9.

Tip: Remember, LEDs are directional.

Page 15 of 2623

Page 17: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Figure 9 – Here we control an LED on the DC motor header.

Exercise

Toggle the pin that is connected to the buzzer 500 times per second.

Note: This is done for OutputPort demonstration only. The proper way to control the buzzer is through PWM (Pulse-Width Modulation) which is covered in another lesson.

Page 16 of 2623

Page 18: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Input Pins

When a GPIO pin is setup as an input, it will release control over that specific pin. There will be a voltage coming in to the pin from the other end. The voltage level coming in has to be within the allowed voltages. If we take a pin that is configured as am input and connect it to 3.3 V then the pin will read true (logic high) in the code. If the pin is connected to ground then the pin will read false (logic low). What about an unconnected pin, is it high or low? It is actually neither high nor low, it is floating.

Floating Pins and Pull Resistors

An unconnected pin is a floating pin. That is because any surrounding electrical noise may push the pin high or low, or even worse, oscillate the pin in between. Ideally, no digital input pin should be left unconnected. However, there is a solution for unconnected pins. An unconnected pin can be pulled high or low using a large resistor. Large resistors have a small effect as small amounts of current flow through them. They will work like a rubber band on the signal. If a large resistor is added between the pin and 3.3 V then the pin is pulled high by this resistor. A signal that is coming in that makes the pin low is safe even if we have a pull up resistor, and vice versa for pull down resistors.

A perfect use for pull up resistors is when using a button. Looking at the left button we can see it’s connected to pin PB10 and ground as shown in Figure 10.

Figure 10 – When the button is not pressed pin PB10 is floating. Pressing the button connects it to ground making it false (logic low).

Page 17 of 2623

Page 19: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

How does this work on the BrainPad if there are no resistors connected to the pin? Modern microcontrollers include built-in pull up and pull down resistors that can be activated through software using the Port.ResistorMode as shown in Example 4.

using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using GHI.Pins;

namespace BrainPad_Project{ public class Program { public static void Main() { InputPort upButton = new InputPort(BrainPad.Peripherals.Button.Up, true, Port.ResistorMode.PullUp);

while (true) { Debug.Print(upButton.Read().ToString()); Thread.Sleep(300); } } }}

Example 4 – This code reads the button in a loop, printing its state to the Output Window as shown in Figure 11.

Figure 11 – The Output Window prints the button’s state while looping.

Page 18 of 2623

Page 20: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Glitch Filter

A button is a mechanical switch. When a button is pressed, the internal metal contacts will bounce, causing the button to go from pressed to not pressed a few times in a short period. We may not notice this but microcontrollers will. These bounces are undesired and considered glitches. The NETMF system has a built-in glitch filter for input pins.

The glitch filter is the second argument in the InputPort constructor and setting it true will activate the glitch filter as shown in Example 7.

InputPort upButton = new InputPort(BrainPad.Peripherals.Button.Up, true, Port.ResistorMode.PullUp);

Example 5 – This code shows how to enable the glitch filter.

Wiring Buttons

Any button or switch can be wired to an input port but let us make our own button using just wires. Connect a wire to E1 on the expansion header and another wire to E8 (ground). Modify the code above to use pin BrainPad.Expansion.Gpio.E1 then touch the wires to each other and observe the Output Window as shown in Figure 12.

Figure 12 – Touching the wires together makes the pin low.

Exercise

Disable the glitch filter (set to false) and try to touch the wires. Change the code to read the pins at a fast rate with only one millisecond delay. Try to touch the wires quickly so the Output window will go from true to false with no glitches.

Page 19 of 2623

Page 21: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Interrupt Pins

Interrupt pins are simply input pins but have an additional feature. They can fire an event on change on the pin. This means we no longer need to loop and read the pin repeatedly. Instead, an even can be assigned to a pin, which is then fired automatically when the pin state changes.

The InterruptPort (Example 6) can still be used just like an InputPort. The only difference is that the constructor requires another argument to configure when the event will be fired (which can be set to none).

using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using GHI.Pins;

namespace BrainPad_Project{ public class Program { public static void Main() { InterruptPort upButton = new InterruptPort(BrainPad.Peripherals.Button.Up, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptNone);

while (true) { Debug.Print(upButton.Read().ToString()); Thread.Sleep(300); } } }}

Example 6 – This code shows how to construct an InterruptPort.

It is possible that not all pins on a microcontroller will support interrupts. The microcontroller used on the BrainPad supports interrupts with one exception; no two pin numbers on different ports can be used. Meaning PB0 and PA0 can’t be used as interrupts at the same time. However, PA1 and PB0 or PA0 and PB1 are okay.

Page 20 of 2623

Page 22: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Edges vs. Levels

A level interrupt means an event will fire as long as the pin is low. This means that the interrupt will continue to fire as long as the pin is low. To solve this, NETMF disables the interrupts internally and they need to be reactivated in the event handler. The event will still fire back again if the pin is still low. A better use is the edge interrupt.

The edge interrupt will only fire on change, where Edge low means the signal was high and went low, and vice versa. If we want an event when the button is pressed, we need edge low. The button will be high and when it’s pressed, it will go low.

NETMF also supports firing events on both edges, using edge both as shown in Example 7.

using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using GHI.Pins;

namespace BrainPad_Project{ public class Program { public static void Main() { InterruptPort upButton = new InterruptPort(BrainPad.Peripherals.Button.Up, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);

while (true) { Debug.Print(upButton.Read().ToString()); Thread.Sleep(300); } } }}

Example 7 – This code shows how to create an InterruptPort that fires an event on both edges.

Page 21 of 2623

Page 23: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Events

Adding events is standard, just find the appropriate event, in this case it’s OnInterrupt and add += followed by pressing tab twice to allow Visual Studio to automatically add a handler function as shown in Example 8.

Button.OnInterrupt += Button_OnInterrupt;

Example 8 – This code subscribes the Button_OnInterrupt method to the OnInterrupt event call list. Now the event can be handled when the button’s state changes.

The OnInterrupt event comes with three arguments as shown in Example 9. The first is the pin number. This is useful when multiple buttons are being used by the same interrupt handler. The second is the state of the pin when the interrupt was fired. The last is the time when the event fired. Which is important as the system may have been doing something else for a few milliseconds before the handler was called.

using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using GHI.Pins;

namespace BrainPad_Project{ public class Program { public static void Main() { InterruptPort upButton = new InterruptPort(BrainPad.Peripherals.Button.Up, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth); upButton.OnInterrupt += Button_OnInterrupt;

Thread.Sleep(Timeout.Infinite); }

static void Button_OnInterrupt(uint data1, uint data2, System.DateTime time) { Debug.Print(data1.ToString() + "-" + data2.ToString() + "-" + time.ToString()); } }}

Example 9 – This code uses an InterruptPort to print to the Output Window when the up button is pressed.

Page 22 of 2623

Page 24: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

An event can handle multiple event sources as shown in Example 10. It just needs to check the first argument to determine which pin raised the event.

using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using GHI.Pins;

namespace BrainPad_Project{ public class Program { public static void Main() { InterruptPort upButton = new InterruptPort(BrainPad.Peripherals.Button.Up, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth); upButton.OnInterrupt += Button_OnInterrupt;

InterruptPort downButton = new InterruptPort(BrainPad.Peripherals.Button.Down, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth); downButton.OnInterrupt += Button_OnInterrupt;

Thread.Sleep(Timeout.Infinite); }

static void Button_OnInterrupt(uint data1, uint data2, System.DateTime time) { if (data1 == (uint)BrainPad.Peripherals.Button.Up) Debug.Print("Up is: " + data2.ToString() + " @ " + time.ToString()); if (data1 == (uint)BrainPad.Peripherals.Button.Down) Debug.Print("Down is: " + data2.ToString() + " @ " + time.ToString()); } }}

Example 10 – This code shows how more than one InterruptPort can use the same interrupt handler.

The glitch filter will now show more when using the InterruptPort as interrupts are much faster than reading a pin in a loop.

Exercise

Read a button without a glitch filter and show the events in the Output window. Use wires for a button and observe the time shown for events firing. Then change the code to print the time elapsed from last event fired.

Page 23 of 2623

Page 25: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

Page 24 of 2623

Page 26: Introduction · Web viewBrainPad – Electronics – Introduction Page 1 of 23 GHI Electronics, LLC - Where Hardware Meets Software ELECTRONICS INTRODUCTION Contents Introduction2

BrainPad – Electronics – Introduction

GHIElectronics.com/support/brainpad

Page 25 of 2623