Avr Tutorial
Introduction Application and programmer boards WinAVR Basic I/O ADC, timers USART, LCD Mehul Tikekar Chiraag Juvekar Electronics Club September 29, 2009
What is a microcontroller?
It is essentially a small computer Compare a typical microcontroller (uC) with a typical desktop ATmega16 Typical desktop
Clock frequency CPU data size RAM ROM
I/O
16MHz 8 bits 1KB 16KB
32 pins
3GHz 32 bits 1GB 160GB
Keyboard, monitor 65W
Power consumption 20mW
2
Why use a micro-controller?
It is programmable. A code (typically written in C) decides what it does Easier to write a code than design and make a custom circuit for complex jobs e.g. In a micromouse, a single uC controls the motors, finds distance from the walls, explores and solves the maze The same muC can be used in hundreds of applications http://instruct1.cit.cornell.edu/courses/ee476/Final Projects/ has 407 projects just on AVR microcontrollers as of 2009.
3
AVR microcontroller
• •
Lots of micro-controller families
o 8051, PIC, AVR, MSP430, ARM, etc.
http://www.instructables.com/id/How-to-choosea-MicroController/ • AVR: Cheap, easy to use, fast and lots of features • ATmega16: o 16KB flash memory o 1KB SRAM o Up to 16MHz clock o Four 8-bit I/O ports o ADC, timers, serial interface, etc. o 40 pin DIP, VDD = 5V 4
ATmega16 pin diagram
Port B Programming interface
Port A
Port D
Port C
5
Hardware and Software needed
Application board Programmer board WinAVR
6
Bare bones of the Application Board
7
On a bread-board
IC1
R2 LED1 C1 R1 Programming interface
8
Complete application board
9
USBasp – USB Programmer
www.fischl.de/usbasp for more information and usb driver
10
USBasp
11
WinAVR
A complete package for Windows
◦Programmer’s Notepad (user interface) ◦avr-gcc (C/C++ compiler) ◦Mfile (makefile generator) ◦avrdude (programmer software) Totally free! : http://winavr.sourceforge.net/
Programmer’s Notepad
USBasp
Application board
12
The hardware setup
13
Resources and Links
The ATmega16 datasheet – a 358 page bible Tutorials and sample codes: 1. 2. 3. http://www.avrtutor.com http://winavr.scienceprog.com http://kartikmohta.com/tech/avr/tutorial
Atmel application notes: projects, tutorials, codeexamples, miscellaneous information http://www.atmel.com/dyn/products/app_notes.asp?family_id=607
Forums: http://www.avrfreaks.net Advanced projects:
1. 2. http://instruct1.cit.cornell.edu/courses/ee476/FinalProjects http://www.obdev.at/products/vusb/projects.html 14
Configuring the microcontroller before running it the first time
Fuse bytes : high and low
Program them once before you start using the micro-controller Disable JTAG to free up PORTC for normal use Set the correct clock clock option
With the hardware set up, run in Command Prompt :
For 1MHz internal clock:
avrdude -c usbasp -P usb -p m16 -U hfuse:w:0xd9:m -U lfuse:w:0xe1:m
For 16MHz external crystal:
avrdude -c usbasp -P usb -p m16 -U hfuse:w:0xc9:m -U lfuse:w:0xef:m
Refer to datasheet sections on “System clock and clock options” and “Memory programming” for other clock options and their settings. Setting the wrong fuse values may render the uC unusable 15
Hello world
Blink an LED Choose a port and a pin In the code
1.
2. 3.
Set pin direction to output
Set pin output to high Wait for 250ms
4.
5. 6.
Set pin output to low
Wait for 250ms Go to 2
Relevant registers : DDR – set pin data direction PORT – set pin output PIN – read pin input
16
blink.c
#include // contains definitions for DDRB, PORTB #include // contains the function _delay_ms() int main(void) { DDRB = 0b11111111; // set all pins on PortB to output while(1) { PORTB = 0b00000001; // Set PortB0 output high, others low _delay_ms(250); // Do nothing for 250 ms PORTB = 0b00000000;...
Please join StudyMode to read the full document