Sie sind auf Seite 1von 34

Electronics Club, IIT Delhi

16
th
Feb, 2014
Circuits with Discrete Components
Whats the big idea?
Lots of functionality packed
into a single chip.

Multipurpose Programmable
ICs: Microcontrollers.
Micro-Controllers
Some popular ones: Atmel AVR, PIC, 8051, MSP430

Well use AVR ATmega8 for now.
Another way to look at a uC
Till now we saw the uC as a configurable IC.
But actually, it is a miniature computer, found in many
devices around us!



Generally speaking, if a device has buttons and a
digital display, chances are it also has a programmable
microcontroller brain.
uCs are used in almost all control applications.


What do they contain?
ATmega8 Pin Diagram
Barebones Circuit for ATmega8
Power Pins: V
cc
, AV
cc
,
GND

Reset Pin

16 MHz crystal
oscillator (X1)

22uF caps C1 & C2


Hardware Programmer: USBasp
Burning the code also needs a hardware
programmer, which forms the interface between the
laptop and the uC. Well use USBasp programmer.
Programming an MCU
Well use Win-AVR (avr-gcc + avrdude) to program the
ATmega8 MCU (Others like AVR studio & CV-AVR can
also be used).
1. Write a C program .c file
2. Compile the program with AVR-gcc Executable
and Linkable Format (.elf file)
3. Copy specific sections of .elf file (Linking) Intel
HEX format (.hex file)
4. Burn the HEX file to ATmega8 using AVRdude.
Code Blinking an LED
Compiling Code
avr-gcc -mmcu=atmega8 -Wall -Os -o blink.elf blink.c

-mmcu, tells the compiler which AVR microcontroller the code is being
compiled for.
-Wall turns on all reasonable compiler warnings which will help make
sure you're writing good code.
-Os is the optimization flag which tells the compiler to optimize the
code for efficient space utilization.
-o argument specifies blink.elf as the output filename.
Finally blink.c is the input filename.
Generating .ELF file
avr-objcopy -j .text -j .data -O ihex blink.elf blink.hex

-j specifies Memory Sections to copy from the ELF file. It is used twice,
once to copy the .text section and once for the .data sections. The .text
section contains the machine instructions which make up the program.
The .data section contains various static data used in the program.
-O ihex option specifies Intel HEX as the output format.
blink.elf is passed as the input file and blink.hex is specified as the
output file.
Burn the code to MCU
avrdude -p m328p -c usbasp -e -U flash:w:blink.hex

-p specifies the AVR microcontroller part number being
programmed.
-c specifies the AVR programmer you are using.
-e erases the chip before writing the new contents.
-U flash:w:blink.hex performs a memory operation: flash is the
type of memory, w is for a "write" operation, and blink.hex is the
name of the file to write. In other words, "write blink.hex to flash
memory".
Writing Code General Format
GPIO General Purpose Input Output
We need to set registers each having 8 (or 16) bits.

DDRx: Data Direction Register for Port x.
Used to specify whether the port is input or output.

PORTx: PORT register for Port x.
Used to write digital values to pins of port x.

PINx: PIN register for Port x.
Used to read digital values at pins of port x.
DDRx Register
DDRx Data Direction Register for PORT x




Writing 1 makes the pin Output, 0 makes the pin
Input.
Example: Set PB0 as output.
DDRB |= 0x01;
DDRB |= 0b00000001;



PORTx Register
PORTx Data Register for PORT x




Configures the value at a pin, if it is an output.
Example:
Produce 5 V at pin PB5: PORTB |= 0b00100000;
Produce 0 V at pin PB3: PORTB &= ob11110111;
PINx Register
PINx Input Pins Address for PORT x



Reads the value at a pin, if it is an input pin.
Example:
Check if pin PB5 is 1: if(PINB &= 0x20)
{ // Do something
}

Bit Operations in C
Bitwise AND:
Eg. 0b00001101 & 0b00000111 = 0b00000101
Bitwise OR:
Eg. 0b00001001 | 0b00000111 = 0b00001111

Bitwise AND and Bitwise OR are used as mask
operators to change only specific bits in an 8-bit value,
without disturbing all other bits:
PORTB |= 0b00001000 (Will set ONLY the 4
th
bit as 1)
PORTB &= ~0b00001000 (Will set ONLY the 4
th
bit as 0)

Bit Operations in C
Each bit of a register has a special name.
_BV(bitname) will generate the required binary number.
Eg: _BV(PB3) means 0b00001000
i.e. 4
th
bit of PORTB register
So to set 4
th
bit of PORTB as:
1 => PORTB |= _BV(PB3)
0 => PORTB &= ~_BV(PB3)

Writing the blink LED program
Connecting a Switch
In this configuration, the 10kOhm resistor is called a Pull-down resistor.
If it is not used, the input voltage when the switch is open is floating
(indeterminate).
Switch closed: 5 V Switch open: 0 V
Program to use a switch
Timers
Timers
A Timer is usually an 8-bit or 16-bit register.
Values starts from 0 and goes up to 255 (or 65535).
Timer value increases by 1, after each clock period.
When the timer reaches its maximum value, in the
next cycle, its value becomes 0 again and the process
repeats itself.
Advantages of Timers
The timer frequency can be factors of the base
frequency of the MCU.
Timers are independent of the CPU. Dont need CPU
processing time.
CPU interacts with Timers through Interrupts.


Timer Modes
Timers work in three modes:
Normal
Clear Timer on Compare (CTC) and
Fast PWM

Lots to explore, but we will learn about Fast PWM
mode.

PWM (Pulse Width Modulation)
Simple method of obtaining analog output of any value
between 0 and 5V.
Desired output is x% of 5V.
If T
on
= x% then average value is x% of 5V.


PWM mode
PWM code
Lots more to Explore!!!
Try it on your own in the upcoming workshop.
(Sunday, 23
rd
Feb, 2014: II-401)
Electronics Club & Tryst14
present

Das könnte Ihnen auch gefallen