Вы находитесь на странице: 1из 25
Microcontrollers and Sensors
Microcontrollers and Sensors
Scott Gilliland - zeroping@gmail
Scott Gilliland - zeroping@gmail
Microcontrollers
Microcontrollers

Think “tiny computer on a chip”

8 to 128 pins

Runs on 3.3 to 5 Volts

8 to 20 Mhz

Uses µW to mW of power

~ 4 kilobytes of flash memory

~ 256 bytes of RAM

Lots of built-in hardware

Analog voltage readings

Communication: I 2 C, Serial, USB device

Cheap - $5

Small (actual size:

Programmable

)
)

Often in C or Assembly

How Your Code Gets from Here to There (on a regular Linux computer)
How Your Code Gets from Here to There
(on a regular Linux computer)
Compiled C compiler C code Executable (gcc) (on disk) Operating System Libc (things like printf)
Compiled
C compiler
C code
Executable
(gcc)
(on disk)
Operating System
Libc
(things like printf)
(gcc) (on disk) Operating System Libc (things like printf) Other Libraries (for anything not in libc)
Other Libraries (for anything not in libc)
Other Libraries
(for anything not in libc)
I386 processor
I386 processor
How Your Code Gets from Here to There (on an Atmel Atmega)
How Your Code Gets from Here to There
(on an Atmel Atmega)
Compiled C code C compiler Executable (on your desktop) (gcc-avr) (.hex file)
Compiled
C code
C compiler
Executable
(on your desktop)
(gcc-avr)
(.hex file)
Executable (on your desktop) (gcc-avr) (.hex file) avr-libc (things like sprintf) Other Libraries (for
Executable (on your desktop) (gcc-avr) (.hex file) avr-libc (things like sprintf) Other Libraries (for
avr-libc (things like sprintf)
avr-libc
(things like sprintf)
Other Libraries (for anything not in libc)
Other Libraries
(for anything not in libc)
Programmer Microcontroller (in flash memory)
Programmer
Microcontroller
(in flash memory)
How Your Code Gets from Here to There (on an Atmel Atmega)
How Your Code Gets from Here to There
(on an Atmel Atmega)
Compiled C code C compiler Executable (on your desktop) (gcc-avr) (.hex file) avr-libc Programmer (things
Compiled
C code
C compiler
Executable
(on your desktop)
(gcc-avr)
(.hex file)
avr-libc
Programmer
(things like sprintf)
Other Libraries
(for anything not in libc)
One big difference: there is no OS
Your code has full run of the processor
There is no OS-level threading
Microcontroller
(in flash memory)
Atmega Programming
Atmega Programming

USB In-System-Programmer

Reads and writes flash memory

6-pin connector to the Atmega

You need to wire this up yourself

Provide your own power to the system

We have several

Provide your own power to the system ∘ We have several ● USB In-System-Debugger ∘ Full
Provide your own power to the system ∘ We have several ● USB In-System-Debugger ∘ Full

USB In-System-Debugger

Full on-chip debugging

10-pin connector to the Atmega

More expensive

We haven't needed to buy one yet

A word on Avr-Libc [1]
A word on Avr-Libc [1]

Includes many things you get from regular Libc

stdio.h, string.h: printf family

stdlib.h, math.h: Malloc, normal C math

But not all

No Files

No “Standard Out”

Microcontroller-specific parts

Read high or low voltage levels on pins

Drive pins high or low

Access to hardware registers

Many hardware modules in the Atmega – each with hardware registers

Needed to do everything listed in the datasheet[2]

Interrupts

2.For example: http://www.atmel.com/dyn/resources/prod_documents/doc2467.pdf

Examples
Examples
An Example: Textile Touch-button Sensing
An Example:
Textile Touch-button Sensing

Controls Input/Output pins to do Capacitive sensing

Basically measures the capacitance of a line

Uses the USART module of the microcontroller

Acts much like an old serial port on a desktop

Uses two pins – TX and RX

Similar to RS232 – different voltage levels

Uses a USB-to-serial converter

One specifically made for our +5 volt signaling

Shows up as a file (/dev/ttyUSB n ) to a Linux machine; as COMn to Windows

Main program is just a while(forever) loop

Senses the capacitance of 4 line

Writes a few values to the computer over serial

Another Example: Gesture Watch
Another Example:
Gesture Watch

Sensing using 5 IR proximity sensors

Digital proximity sensors

On/Off

0V when nothing nearby

+5V when there is something nearby

Uses a Serial-Bluetooth module

Hooks up to the USART module of the Atmega

Shows up as a Bluetooth serial device

/dev/rfcommn under Linux

Main program is initialization plus a while loop

Puts the Bluetooth module into “discoverable” mode

This is done by sending “AT+BTMODE3”

Senses the state of 5 lines

Writes a few values to the Bluetooth module over serial

A Final Example: BlueSense Accelerometers
A Final Example:
BlueSense Accelerometers

Designed due to lack of small, low-power Bluetooth accelerometers

Measure movement in 3 directions

Also has capacitive sensing ability

Build to be extensible

Centered around a BlueCore microcontroller

A microcontroller with a Bluetooth radio built in

Uses I 2 C to communicate with accelerometer and capacitive sensor

with a Bluetooth radio built in ∘ Uses I 2 C to communicate with accelerometer and
A word on BlueCore Microcontrollers
A word on BlueCore Microcontrollers

Pros:

A word on BlueCore Microcontrollers ● Pros: ∘ Tiny ∘ Integrated Bluetooth radio ∘ Fewer parts

Tiny

Integrated Bluetooth radio

Fewer parts to put together

A word on BlueCore Microcontrollers
A word on BlueCore Microcontrollers

Cons:

A word on BlueCore Microcontrollers ● Cons: ∘ Too tiny – You need to have a

Too tiny – You need to have a custom-made board to solder down

Hard to develop for – Proprietary compiler, weird assembly

Difficult radio design for anyone not an Electrical Engineer

I 2 C Communication Protocol
I 2 C Communication Protocol

Master/Slave protocol for chips on the same board

Uses 2 wires (3 if you count ground)

Most microcontrollers support it

Including the Atmega – The I2C library from the 'wiring' project works great

Protocol is left up to the device

It's in the datasheet somewhere

Often defines how to read and write to register addresses

Ex: Accelerometers have several control registers, and registers for X, Y, and Z axes.

Overall Lessons
Overall Lessons

Go with the simplest solution that works

Wired communication is the easiest

A Bluetooth module complicates the design a small amount

A Bluetooth-enabled microcontroller complicates the design by an order of magnitude

Sometimes, this means not building anything new

We already have Bluetooth accelerometers and capacitive sensors

We have Wiimotes

We have “serial bitwackers” - USB-controlled I/O lines