Microcontrollers and Sensors

30 downloads 244 Views 13MB Size Report
Microcontroller. (in flash memory) avr-libc. (things like sprintf). Other Libraries. (for anything not in libc). How Your Code Gets from Here to There. (on an Atmel ...
Microcontrollers and Sensors

Scott Gilliland - zeroping@gmail

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: I2C, Serial, USB device



Cheap - $5



Small



Programmable ∘

(actual size:

)

Often in C or Assembly

How Your Code Gets from Here to There (on a regular Linux computer)

C compiler

C code

(gcc)

Libc

Compiled Executable (on disk)

Operating System

(things like printf)

Other Libraries (for anything not in libc)

I386 processor

How Your Code Gets from Here to There (on an Atmel Atmega)

C code

C compiler

(on your desktop)

avr-libc (things like sprintf)

(gcc-avr)

Compiled Executable (.hex file)

Programmer

Other Libraries (for anything not in libc)

Microcontroller (in flash memory)

How Your Code Gets from Here to There (on an Atmel Atmega)

C code

C compiler

(on your desktop)

(gcc-avr)

avr-libc (things like sprintf)

Compiled Executable (.hex file)

Programmer

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 ●

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 ●

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] ●





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

1.http://www.nongnu.org/avr-libc/ 2.For example: http://www.atmel.com/dyn/resources/prod_documents/doc2467.pdf

Examples

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/ttyUSBn) 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

Atmel Atmega 128

Sparkfun FT232R Breakout Board

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

Sharp IR Proximity Sensors

Atmel Atmega 128

Sena Wireless ESD-100 Bluetooth-to-Serial module

Dlink USB Bluetooth Adapter

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 I2C to communicate with accelerometer and capacitive sensor

A word on BlueCore Microcontrollers



Pros: ∘

Tiny



Integrated Bluetooth radio



Fewer parts to put together

A word on BlueCore Microcontrollers



Cons: ∘

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

I2C 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 ●



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