63
Y OONG H OR M ENG . [email protected] . 6460 6717 Ngee Ann Polytechnic 1

Power the world with mbed LPC1768

  • Upload
    yoonghm

  • View
    1.450

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Power the world with mbed LPC1768

YOONG HOR MENG . [email protected] . 6460 6717Ngee Ann Polytechnic 1

Page 3: Power the world with mbed LPC1768

Ngee Ann Polytechnic 3

Page 4: Power the world with mbed LPC1768

Ngee Ann Polytechnic 4

Page 5: Power the world with mbed LPC1768

Ngee Ann Polytechnic 5

http://www.synvox.ch/lpc1768/lpc1768_mbed_pinout.pdf

Page 6: Power the world with mbed LPC1768

Ngee Ann Polytechnic 6

http://mbed.org/handbook/DigitalOut

http://mbed.org/users/yoonghm/notebook/digital-output/

DigitalOut

http://mbed.org/projects/libraries/api/mbed/trunk/wait_api

Page 7: Power the world with mbed LPC1768

Ngee Ann Polytechnic 7

Circuit 2

mbed

p5

Circuit 1

+3.3V

mbedp5

When p5 is LOW (0 V), LED is ON When p5 is HIGH (+3.3 V), LED is ON

What is the use of the resistor?

DigitalOut

Page 8: Power the world with mbed LPC1768

Ngee Ann Polytechnic 8

http://ledcalculator.net/

Page 9: Power the world with mbed LPC1768

Ngee Ann Polytechnic 9

Digital IO pins are at 3.3 V, 40 mA each, 400 mA maximum total

How do you drive an application with larger current?

http://cq.cx/interface.pl

www.youtube.com/watch?v=Te5YYVZiOKs

Page 10: Power the world with mbed LPC1768

Ngee Ann Polytechnic 10

Using BJT Using Darlington Pair Using Darlington Pair with Relay

Using Power MOSFET

Page 11: Power the world with mbed LPC1768

BusOut

Ngee Ann Polytechnic 11

#include "mbed.h"

#define WAIT_SECOND 0.1

// LSB ... MSB

BusOut Bar(p20, p19, p18, p17, p16,

p15, p14, p13, p12, p11);

DigitalOut Led(LED1);

int main() {

Led = 0;

while(1) {

// Shift from LSB to MSB

for (int i = 0; i < 10; i++) {

Led = !Led;

Bar = 1 << i;

wait(WAIT_SECOND);

}

}

}

Page 12: Power the world with mbed LPC1768

BusOut

Ngee Ann Polytechnic 12

#include "mbed.h"

#define WAIT_SECOND 0.1

// LSB ... MSB

BusOut Bar(p20, p19, p18, p17, p16,

p15, p14, p13, p12, p11);

DigitalOut Led(LED1);

int main() {

Led = 0;

while(1) {

// Shift from LSB to MSB

for (int i = 0; i < 10; i++) {

Led = !Led; Bar = 1 << i; wait(WAIT_SECOND);

}

// Shift from MSB to LSB

for (int i = 0; i < 10; i++) {

Led = !Led; Bar = 0x0200 >> i; wait(WAIT_SECOND);

}

}

}

Page 13: Power the world with mbed LPC1768

BusOut

Ngee Ann Polytechnic 13

#include "mbed.h"

#define WAIT_SECOND 0.1

// LSB ... MSB

BusOut Bar(p20, p19, p18, p17, p16,

p15, p14, p13, p12, p11);

DigitalOut Led(LED1);

int main() {

Led = 0; Bar = 3;

wait(WAIT_SECOND); // Start from LSB first

while(1) {

// Shift from LSB to MSB

for (int i = 1; i < 10; i++) {

Led = !Led; Bar = 3 << i; wait(WAIT_SECOND);

}

// Shift from MSB to LSB

for (int i = 1; i < 10; i++) {

Led = !Led; Bar = 0x0300 >> i; wait(WAIT_SECOND);

}

}

}

Page 14: Power the world with mbed LPC1768

DigitalIn

Ngee Ann Polytechnic 14

http://mbed.org/handbook/DigitalIn

http://mbed.org/users/yoonghm/notebook/digital-input/

Page 15: Power the world with mbed LPC1768

Ngee Ann Polytechnic 15

• Pin p5 is pull-up to +3.3 V (HIGH)• Negligible current flow into pin p5

+3.3V

mbedp5 S1

• Pin p5 is shorted to ground (LOW)• Resistor limits the current

// Toggle LED1 when button is pressed

#include "mbed.h"

DigitalIn enable(p5);

DigitalOut myled(LED1);

int main() {

while (1) {

if (!enable) {

myled = !myled;

wait(0.2);

}

}

}

+3.3V

mbedp5 S1

Page 16: Power the world with mbed LPC1768

Ngee Ann Polytechnic 16

// Toggle LED1 when button is pressed

#include "mbed.h"

DigitalIn enable(p5);

DigitalOut myled(LED1);

int main() {

while(1) {

if (enable) {

myled = !myled;

wait(0.2);

}

}

}

• Pin p5 is pull-down to ground (LOW)

+3.3V

mbed

p5 S1

• Pin p5 is connected to +3.3 V (HIGH)

+3.3V

mbed

p5 S1

Page 17: Power the world with mbed LPC1768

Ngee Ann Polytechnic 17

• Pin p5 is pull-up to +3.3 V (HIGH) viainternal pull-up resistor

• Pin p5 is shorted to ground (LOW)• Pull-up resistor limits the current

// Toggle LED1 when button is pressed

#include "mbed.h"

DigitalIn enable(p5);

DigitalOut myled(LED1);

int main()

{

enable.mode(PullUp);

while(1) {

if (!enable) { // p5 is +3.3 V

myled = !myled;

wait(0.2);

}

}

}

mbedp5 S1

mbedp5 S1

Page 18: Power the world with mbed LPC1768

Ngee Ann Polytechnic 18

• Pin p5 is pull-down to 0 V (LOW)via internal resistor

• Pin p5 is shorted to 3.3 V (HIGH)• Internal pull-down resistor limits

the current

// Toggle LED1 when button is pressed

#include "mbed.h"

DigitalIn enable(p5);

DigitalOut myled(LED1);

int main()

{

enable.mode(PullDown);

while(1) {

if (enable) { // p5 is +3.3 V

myled = !myled;

wait(0.2);

}

}

}

+3.3V

mbed

p5 S1

+3.3V

mbed

p5 S1

Page 19: Power the world with mbed LPC1768

Ngee Ann Polytechnic 19

ASICI/O

Devices

VCCA VCCB

If VccA is not equal to VccB

1. VOH of the driver must be greater than the VIH of the receiver

2. The VOL of the driver must be less than the VIL of the receiver

3. The output voltage from the driver must not exceed the I/O voltage tolerance of the receiver

Logic Level Translation

Page 20: Power the world with mbed LPC1768

Ngee Ann Polytechnic 20

http://www.nxp.com/documents/application_note/AN240.pdf

Page 21: Power the world with mbed LPC1768

Ngee Ann Polytechnic 21

http://mbed.org/handbook/InterruptIn

Interrupt service routine or callback

InterruptIn

Page 22: Power the world with mbed LPC1768

Ngee Ann Polytechnic 22

Rising Edge

http://mbed.org/users/AjK/notebook/regarding-interrupts-use-and-blocking/

InterruptIn

Page 23: Power the world with mbed LPC1768

InterruptIn

Ngee Ann Polytechnic 23

Page 24: Power the world with mbed LPC1768

InterruptIn

Ngee Ann Polytechnic 24

mbed

p5 S1

Example: Intrusion Detection Systemp5 goes low when an intrusion is detected

3.3

Voltage at pin 5

time

0

#include "mbed.h"

InterruptIn intruder(p5);

DigitalOut alarm(LED1);

DigitalOut led(LED2);

void intr() {alarm = 1; }

int main() {

intruder.mode(PullUp);

intruder.fall(&intr);

led = 1; alarm = 0; // no alarm

while (1) {

LPC_SC->PCON = 0x00;

_WFI(); // wait for interrupt

led = !led;

}

}

Page 25: Power the world with mbed LPC1768

Ngee Ann Polytechnic 25

#include "mbed.h"

InterruptIn intruder(p5);

DigitalOut alarm(LED1);

void intr() { alarm = 1; }

int main() {

intruder.mode(PullUp);

intruder.fall(&intr);

alarm = 0; // no alarm

while (1) {

LPC_SC->PCON = 0x00;

__wfi(); // wait for interrupt

}

}

#include "mbed.h"

DigitalIn intruder(p5);

DigitalOut alarm(LED1);

int main() {

intruder.mode(PullUp);

alarm = 0; // no alarm

while (1) {

if (intruder == 0) {

alarm = 1;

}

}

}

Page 26: Power the world with mbed LPC1768

InterruptIn

printf() malloc()

new()

wait()

__disable_irq()

__enable_irq()

Ngee Ann Polytechnic 26

Page 27: Power the world with mbed LPC1768

Ngee Ann Polytechnic 27

http://mbed.org/handbook/Debugging

printf(...) – Print out message on a PC terminal via USBerror(...) – Print out message on a PC terminal via USB and stop

Onboard LEDs blink blue in a distinctive pattern when it encounters run time errorThe program will stop running

You need the following software as stated in

http://mbed.org/handbook/Windows-serial-configuration

http://www.extraputty.com/

Page 28: Power the world with mbed LPC1768

Ngee Ann Polytechnic 28

#include "mbed.h"

DigitalOut myled1(LED1);

DigitalOut myled2(LED2);

int main() {

int i = 5;

printf("Hi!\n");

while (i--) {

myled1 = !myled1;

wait(0.25);

}

printf("Alive!\n");

while (1) {

myled2 = !myled2;

wait(0.25);

}

}

#include "mbed.h"

DigitalOut myled1(LED1);

DigitalOut myled2(LED2);

int main() {

int i = 5;

printf("Hi!\n");

while (i--) {

myled1 = !myled1;

wait(0.25);

}

error("Dead!\n");

while (1) {

myled2 = !myled2;

wait(0.25);

}

}

Page 29: Power the world with mbed LPC1768

Timer

Ngee Ann Polytechnic 29

http://mbed.org/handbook/Timer

Page 30: Power the world with mbed LPC1768

Ngee Ann Polytechnic 30

Timer

Page 31: Power the world with mbed LPC1768

Timeout

Ngee Ann Polytechnic 31

http://mbed.org/handbook/Timeout

Page 32: Power the world with mbed LPC1768

Timeout

Ngee Ann Polytechnic 32

Page 33: Power the world with mbed LPC1768

Ticker

Ngee Ann Polytechnic 33

http://mbed.org/handbook/Ticker

Page 34: Power the world with mbed LPC1768

Ngee Ann Polytechnic 34

Ticker

Page 35: Power the world with mbed LPC1768

Ngee Ann Polytechnic 35

AnalogIn

http://mbed.org/handbook/AnalogIn

http://mbed.org/users/yoonghm/notebook/analog-input/

Page 36: Power the world with mbed LPC1768

Ngee Ann Polytechnic 36

AnalogIn

Page 37: Power the world with mbed LPC1768

Ngee Ann Polytechnic 37

Page 38: Power the world with mbed LPC1768

Ngee Ann Polytechnic 38

Page 39: Power the world with mbed LPC1768

Ngee Ann Polytechnic 39

Page 40: Power the world with mbed LPC1768

Ngee Ann Polytechnic 40

mbed

p20

GND

R1

R2

#include "mbed.h"

AnalogIn ain1(p20);

AnalogIn ain2(p19);

float vtotal;

float vr2;

int main() {

while (1){

vr2 = ain1.read() * 3.3;

vtotal = ain2.read() * 3.3;

printf("VR1 = %f ", vtotal - vr2);

printf("VR2 = %f ", vr2);

printf("VTotal= %f\n", vtotal);

wait(1.0);

}

}

VOUT

p19

Page 41: Power the world with mbed LPC1768

Ngee Ann Polytechnic 41

mbed p20

GND

220

100

+5V

Page 42: Power the world with mbed LPC1768

Ngee Ann Polytechnic 42

AnalogOut

http://mbed.org/handbook/AnalogOut

http://mbed.org/users/yoonghm/notebook/analog-output/

Change wait(0.0001) to wait(0.001) to see effect faster

Page 43: Power the world with mbed LPC1768

Ngee Ann Polytechnic 43

AnalogOut

Page 44: Power the world with mbed LPC1768

Ngee Ann Polytechnic 44

TextLCD

http://mbed.org/cookbook/Text-LCD

LCD mbed

Pin Number Pin Name Pin Number Pin Name

1 GND 1 GND

2 VCC 39 VU

3 VO 1 GND

4 RS 15 p15

5 RW 1 GND

6 E 16 p16

7 D0 Not Connected

8 D1 Not Connected

9 D2 Not Connected

10 D3 Not Connected

11 D4 17 p17

12 D5 18 p18

13 D6 19 p19

14 D7 20 p20

15 LED+ 39 VU

16 LED- 1 GND

Page 45: Power the world with mbed LPC1768

Ngee Ann Polytechnic 45

TextLCD

Page 46: Power the world with mbed LPC1768

PWM1 PWM6

Ngee Ann Polytechnic 46

Page 47: Power the world with mbed LPC1768

PwmOut

Ngee Ann Polytechnic 47

W = Pulse WidthT = PeriodD = Duty cycle

Page 48: Power the world with mbed LPC1768

Ngee Ann Polytechnic 48

PwmOut

The default period is 0.020s, pulse width is 0

Page 49: Power the world with mbed LPC1768

PwmOut

LED1 LED4 PwmOut

Ngee Ann Polytechnic 49

Page 50: Power the world with mbed LPC1768

PwmOut

Ngee Ann Polytechnic 50

#include "mbed.h"

PwmOut led1(LED1);

PwmOut led2(LED2);

PwmOut led3(LED3);

PwmOut led4(LED4);

int main()

{

while (1) {

for (float f = 0.0; f < 1.0; f += 0.001) {

led1 = f;

led2 = f * f;

led3 = (f + 1.0) / 2.0;

led4 = 1.0 - f;

wait(0.01);

}

}

}

Take note that • All LEDs are assigned values

from 0.0 to 1.0• The brightness of the LEDs color

changed abruptly

Page 51: Power the world with mbed LPC1768

PwmOut

Ngee Ann Polytechnic 51

#include "mbed.h"

#include <math.h>

#ifndef M_PI

#define M_PI 3.1415

#endif

PwmOut led1(LED1);

PwmOut led2(LED2);

PwmOut led3(LED3);

PwmOut led4(LED4);

int main()

{

float f = 0.0;

while (1) {

Take note that • Using 4 sinusoidal waveforms, spaced 90o apart, the

brightness of the LEDs varies smoothly• M_PI is a constant in <math.h> which is not defined in

the C library from mbed.org• All sinusoidal values are computed dynamically only when

it is needed. It is a bit CPU intensive

// Add 1.0 to ensure all values are positive

led1 = sin( (f ) * M_PI / 180.0) + 1.0;

led2 = sin( (f + 90.0) * M_PI / 180.0) + 1.0;

led3 = sin( (f + 180.0) * M_PI / 180.0) + 1.0;

led4 = sin( (f + 270.0) * M_PI / 180.0) + 1.0;

f += 1.0;

wait(0.01);

}

}

Page 52: Power the world with mbed LPC1768

PwmOut

Ngee Ann Polytechnic 52

#include "mbed.h"

#include <math.h>

#ifndef M_PI

#define M_PI 3.1415

#endif

#define STP 256 /* Step */

#define LED 4 /* Number of LEDs

int main()

{

PwmOut Leds[] = {(LED1), (LED2), (LED3), (LED4)};

float y[STP][LED]; // Store all values

float res = 360 / STP; // Resolution (degree per step)

float x = 0.0; // Current x

float ph = 360 / LED; // Phase different btw two successive LEDs

for (int i = 0; i < STP; i++) {

for (int j = 0; j < LED; j++) {

y[i][j] = sin( (x + ph * j) * M_PI / 180.0) + 1.0;

}

x += res; // increment x by each resolution

}

Page 53: Power the world with mbed LPC1768

Ngee Ann Polytechnic 53

while (1) {

for (int i = 0; i < STP; i++) {

for (int j = 0; j < LED; j++) {

Leds[j] = y[i][j];

}

count++;

wait(0.01);

}

}

}

PwmOut

Take note that • The code above run significantly faster than pervious example but consume slightly

more memory in flash and RAM

Page 54: Power the world with mbed LPC1768

1. RX

2. TX

3. GND

Start Stop

Ngee Ann Polytechnic 54

8-bit Data Byte

Parity

Stop

Start

Optional

Page 55: Power the world with mbed LPC1768

Ngee Ann Polytechnic 55

Space Space

Mark

Time

Voltage

+25V

+3V

-3V

-25V

Logic '0'

Logic '1'

Transition Region

Page 56: Power the world with mbed LPC1768

Vcc

Ngee Ann Polytechnic 56

Space Space

Mark

Time

Voltage

Vcc

V1H

V0L

Logic '0'

Logic '1'

Transition Region

0

Page 57: Power the world with mbed LPC1768

Ngee Ann Polytechnic 57

+5 V

+15 V

-15 V

t

t

1 0 0 1 1 1 1 10 0 0 0

Idle Start B0 B1 B3 B6 Stop IdleB2 B4 B5 B7

Transmission of the letter ‘J’ from μC to PC

Logic value

Meaning

Signal level at the μC output pin (TX)

Signal level at the PC input pin (RX)

ASCII character ‘J’ is represented by • 0x4A or• 01001010

μC PC

Level Shifter

TX

RX

RX

TX

TX

RX

RX

TX

LSB MSB

LSBMSB

Page 58: Power the world with mbed LPC1768

Serial

Ngee Ann Polytechnic 58

mbed Device

p9(TX)

p10(RX)

RX

TX

PCUSB

Page 59: Power the world with mbed LPC1768

Serial

Ngee Ann Polytechnic 59

Page 60: Power the world with mbed LPC1768

Ngee Ann Polytechnic 60

http://www.ftdichip.com/Products/Cables/USBTTLSerial.htm

Page 61: Power the world with mbed LPC1768

Ngee Ann Polytechnic 61

Page 62: Power the world with mbed LPC1768

Ngee Ann Polytechnic 62

If both devices are from the same power source, use CMOS Output.In this case, the input current to the microcontroller is actually taken from the +3V, and via the Source terminal of the MOSFET of the OUT pin of the Voltage Detector.

If both devices are from different power sources, use Open Drain Output.In this case, the input current to the microcontroller is actually taken from +5V, and via the Drain terminal of the N-channel MOSFET of the OUT pin of the Voltage Detector. A resistor is necessary to reduce the amount of current drawn.

Page 63: Power the world with mbed LPC1768

Ngee Ann Polytechnic 63