Kelly Zona      Fab Academy 2014

Embedded Programming

Untitled from Kelly Zona on Vimeo.

Hello Stepper Board running a stepper motor

Study Guide: Microcontrollers

This study guide has some general information on programming, but is tailored specifically for the Fab Academy Hello World boards and the ATtiny44A microcontroller.

- Fab Academy focuses on Harvard architecture with a RISC instruction set in a microcontroller, using FTDI

ISP
-In-System Programmer - The ability of some programmable logic devices, microcontrollers, and other embedded devices to be programmed while installed in a complete system, rather than requiring the chip to be programmed prior to installing it into the system.

Microcontroller
- A microprocessor that requires a range of separate peripherals- memory, IO.., etc.  in the case of the FabISP and most of the Fab Hello World boards, this is the ATtiny44A.

ATtiny44A Structure
8+4 Pin, with separate PortA and PortB
The others pins are GND and VCC.

ATtiny44A Memory

ATtiny44A has 3 types:
Flash (this is where we are programming to)
EEPROM
SRAM

Port Control Registers
DDRx- controls the I/O status of the port pins (controls PORTx, using 0 or 1), "x" stands for port alphabet, units are bits
PORTx- controls the output status of pins (In or Out), PORTx Register controls PORTx
PINx- reads the input status of pins
*register and physical pins are both referred to as PORTx*
Each bit controls a single pin on the port, ex 4th bit controls 4th pin on the port, ex- DDRB = 0b10001000= 3rd and 7th pins of portb are output
If for example you write an 8 bit value but only 7 pins, the 8th bit will be ignored

Programming

Program calls on each pin then sets it as an input or output, and either open or closed.

Languages

High Level: Python- needs interpretation
Mid-Level: C- needs compilation- GCC, is the gnu c compiler
Low-Level: machine code

Workflows/ Toolchains
There are a few ways one can go about programming their board:

1. IDE- Integrated Development Environment- Complete environment that does all the programming functions
        Atmel Studio
        Ardunio

2. Compiler- Used to load programs (see languages section). Once you have a compiler, you need to talk to the processor (host communication).                    Compiling C code creates a hex file.  Microcontrollers read hex codes, not C.

        GCC-the GNU (an operating system) C compiler
        WinAVR- a C compiler for windows- Toolchain (GCC plus AVRlibC) for AVRs bundled for Windows

3. Interpreter- Uploads code to a chip.  You must already have the hex file, so C code is not necessary.
        AVRDUDE- Also an ICE, or In-circuit emulator, without a graphical interface.

- Host Communication
- Allows you to talk to the processor, once you have a compiler.  In the case of the FabISP, we are using a FTDI cable.  FTDI goes from USB to logic-level serial.

- Bootloader
- A program that loads programs (the processor can self-program its own memory).  A program that you load in through the ISP, then you can use the serial communication to program it.



References
http://vimeo.com/89523063
http://fabacademy.org/archives/2013/students/buzon.jose/electronics.html#w08
http://iamsuhasm.wordpress.com/tutsproj/avr-gcc-tutorial/


Study Guide: Stepper Motors

Stepper Motor- A brushless DC electric motor that divides full rotation into equal steps.  Good for applications where precision of positioning is needed.

ModesWave, Full, and Half have nothing to do with how far the motor is turning, rather, they deal with the interaction of the electromagnets in the motor
http://electrosome.com/interfacing-stepper-motor-8051-keil-c-at89c51/

PWM
- Pulse Width Modulation- a way to get analog results with digital means.  Digital control switches on and off, creating a square wave.  PWM changes the portion of time the signal spends on or off (the "on" time is the pulse width), and so by changing pulse width, one can generate analog values.  A full step waveform approximates a sine wave, and this is why stepper motors vibrate so much. 
http://arduino.cc/en/Tutorial/PWM


References:http://arduino.cc/en/Tutorial/StepperUnipolar

Programming the Hello Stepper Unipolar Board 

Programming: Atmel Studio

I was able to install Atmel Studio on my PC without any complications.  
I discovered that you need different drivers for the ARVISP mkII, depending on whether you are programming from AVRdude or Atmel Studio.  For Atmel Studio, you should use the ones provided by Atmel as part of the normal installation process.

Atmel Studio

Connect the Hello Stepper Board's J1 ISP Header to the AVRISPmkii, and connect the AVRISPmkii to the computer via the USB cable.
Connect the Hello Stepper Board's J2 Header to the power supply.
You should get a green light on the AVRISPmkii.

New Project> GCC C Executable project
Device Selection> ATtiny44A

Erase all of the instructional text that is already in the command window (where it says "... Please write your application code... etc. ...")
Paste the C code into the command window.  The code to do a half-step for the unipolar stepper board is found here:
http://academy.cba.mit.edu/classes/output_devices/stepper/hello.stepper.44.half.c

Save File

Build> Compile (you should not get any error messages, warnings are sometimes OK)
Build> Build Solution (this generates the .hex file, which is what the microcontroller reads)

Tools> Device Programming: [Tool: AVRISP mkii, Device: ATtiny44A, Interface: ISP] > Apply.  
>Target Voltage- Read (should be 5V)
>Device Signature- Read
>Memories- Click on the ellipses next to Flash and navigate to the folder where your project is saved.  There should now be a .hex file in there. Select this file, then hit the 'Program' button.  This should give you a "verified" message, so that you know the device has been properly programmed.

Connect the Motor to the J3 Stepper Header and watch it run!


Programming: Arduino

http://academy.cba.mit.edu/content/tutorials/akf/embedded_programming_arduinoIDE.html

    Steps to use Arduino IDE
    -choose board
    -choose programmer
    -burn boot loader
    -once boot loader is burned, can remove ISP
    -upload program


Programming: AVRdude

I discovered that you need different drivers for the ARVISP mkII, depending on whether you are programming from AVRdude or Atmel Studio.  For AVRdude, the proper drivers can be found here:
avrispmkii_libusb-win32_1.2.1.0.zip

In order to use this program, I downloaded the make file from the fab academy site: http://academy.cba.mit.edu/classes/output_devices/stepper/hello.stepper.44.half.make

I then opened this file in Notepad and saved the file as "stepper.44.half.make", in a folder labeled as "stepper" on my desktop.

In Terminal, I navigated to the proper folder:
cd desktop/stepper

In the command line I typed the following:
make -f hello.stepper.44.half.make program-avrisp2

Got an error message, so no luck with this process yet...


Editing the Code

The C code I used to program the motor is available here:http://academy.cba.mit.edu/classes/output_devices/stepper/hello.stepper.44.full.c

When I first programmed the motor using all the default numbers in the code, moment was extremely quick- it was more of a vibration than movement.  I edited this section of the code:

#define on_delay() _delay_us(600) // PWM on time
#define off_delay() _delay_us(5) // PWM off time
#define PWM_count 190 // number of PWM cycles


This gave me a somewhat smooth back and fourth rotation.  From what I have gleaned from some tutorials, it seems that the rotation angle of the motor is proportional to the input pulse.  I also learned that a range of  speeds can be achieved, and speed is proportional to input pulse frequency.

But I notice that the motor seemed to run back and fourth at a wide angle, then reduce the angle with each rotation, until it hit a mid-point, and then increased the angle of rotation, until it hit the start angle again.  


References:
http://www.omega.com/prodinfo/stepper_motors.html
http://www.instructables.com/id/Bipolar-Stepper-Motor/step5/C-Code/