Académique Documents
Professionnel Documents
Culture Documents
Introduction
A microcontroller is a small computer on a single Integrated Circuit containing a processor core, memory, and programmable input/output peripherals
ATmega16
Desktop PC
Microprocessor
CPU is stand-alone, RAM, ROM, I/O, timer are separate
Microcontroller
CPU, RAM, ROM, I/O and timer are all on a single chip
designer can decide on the amount of ROM, RAM and I/O ports. expansive versatility general-purpose
fix amount of on-chip ROM, RAM, I/O port Less expensive for applications in which cost, power and space are critical single-purpose
Features
Microcontrollers are electronic circuits that can be programmed to carry out a vast range of tasks Multifunction, Bi-directional General Purpose I/O ports with configurable, built-in pull-up resistors In-System Programmable using ISP, JTAG, or High Voltage methods
AVR Microcontroller
Lots of microcontroller families 8051 , PIC , AVR ,ARM , etc. http://www.instructables.com/id/How-to-choose-aMicroController/ Cost: AVR = PIC > 8051 (by 8051 I mean the 8051 family) Availability: AVR = PIC <8051 Speed: AVR > PIC > 8051 ( preferred in Robotics) Efficient RISC (Reduced Instruction Set Computer) architecture, almost 10 times faster than CISC
less instructions you don't need complex instruction decoder, so RISC cores need less transistors (chip area), less power (excelent for mobile units) and higher core speeds than CISCs for same technology.
Society Of Innovative Electrical Professionals
AVR Microcontroller
Atmel's AVR microcontrollers have a RISC core running single cycle instructions and a well-defined I/O structure that limits the need for external components. Internal oscillators, timers, SPI, pull-up resistors, pulse width modulation, ADC, analog comparator and timers are some of the features you will find in AVR devices. Apart form this almost all AVRs support In System Programming (ISP) i.e. you can reprogram it without removing it from the circuit . Also the programmer used for ISP is easier to build compared to the parallel programmer required for many old uCs.
Society Of Innovative Electrical Professionals 8
Software
Software we are going to use are 1. AVR Studio 2. Win AVR (Packaged with AVR Studio 5) 3. Extreme Burner-AVR or Pony Prog For Simulation and Lab Experiments Proteus 7.1 or above
10
Different AVRs
AVRs are generally classified in four board groups tinyAVRs megaAVRs XMEGA AVRs Application Specific AVRs program memory Pin packages
11
Different AVRs
12
Software Required
AVR Studio 4 : we generally use AVR Studio for this Many other software are also available for the purpose like MikroC you can use any of them The greatest advantage of AVR Studio is that it is provided by Atmel as a freeware AVR Studio 4 provides a complete set of features including debugger , Debugging is mostly not available in other software
13
14
Pin Description
Vcc: Digital Voltage GND: Ground PortA (PA7 ..PA0): Port A also serves as an 8-bit bi-directional I/O port, if the A/D Converter is not used. Port pins can provide internal pull-up resistors (selected for each bit) Alternate Functions of Port A Port A serves as the analog inputs to the A/D Converter.
Society Of Innovative Electrical Professionals 15
PortB (PB7 ..PB0) , PortC (PC7 ..PC0) , PortD (PD7 ..PD0) Common Features These all ports are 8-bit bi-directional I/O port Port pins can provide internal pull-up resistors (selected for each bit) PB3: Output pin for PWM PB2: INT2, External Interrupt Source 2 PB1: T1, Timer/Counter1 Counter Source PB0: T0, Timer/Counter0 Counter Source
Society Of Innovative Electrical Professionals 16
PD7: OC2, Output pin for PWM mode timer function (Generated by timer 2) PD5: OC1A,Output pin for PWM mode timer function (Generated by timer 1) PD4: OC1B,Output pin for PWM mode timer function (Generated by timer 1) PD3: INT1, External Interrupt Source 1 PD2: INT0, External Interrupt Source 0
Society Of Innovative Electrical Professionals 17
18
XTAL1:
19
20
Pin Description
Following pins are used for programming of AVR MOSI, PB5 MISO, PB6 SCK, PB7 RESET AREF Care should be taken while using these pins for different purposes If these pins are used as an input and are connected to some sensor or a switch and while programming these are being pulled low or high controller will not be programmed So if these pins are being used for some purpose and are connected to some sensor/switch it should be disconnected while programming
Society Of Innovative Electrical Professionals 21
22
I/O Ports
23
I/O Ports
There are three registers associated with each port DDRx (Data direction Register ) PORTx (PORTA for portA, PORTD for portD) PINx (Port Input Pins ) These registers have individual bits assigned to each pin of the port For Example: Bit DDA1 of Register DDRA is associated with the direction of pin 1 of port A Similarly bit PORTB6 is associated with the pin 6 of port B.
More Details on Page 48 of Datasheet
24
25
Configuring the pin of a port Each port pin consists of three register bits DDxn PORTxn PINxn
26
DDxn: The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one, Pxn is configured as an output piin. If DDxn is written logic zero, Pxn is configured as an input piin.
27
28
29
DDRx(Data Direction Register) is first given the appropriate value to set the different pins of the port input or output Example: DDRA=0b00001111 now this means that first four pins of port A are input configured and last four bits are output configured PORTx Register is used to output the desired data on the port Example: PORTA=0b11000011 now first two pins of port A which are input configured are pulled high where as the last two pins of port A which are output configured give output of logic one
Society Of Innovative Electrical Professionals 30
when ever we have to read a port whether it is input or output configured we read the PINx register of that port Example: x=PINA now what ever is the data at port A it is stored in variable x
31
Example 1
Take input from PORTA add it with 5 and send it as an output on port D void main() {DDRA=0b00000000; //configures Port A as input DDRD=0b11111111; //configures Port D as output unsigned int x; //declare variable x x=PINA; //reads and stores input at Port A in variable x PORTD=x+5; //Multiply x with 5 and outputs it on Port D }
32
33
Compilers produce Hex files that is loaded in the microcontroller Size of this Hex file is one of the main concern for microcontroller programming because microcontrollers have limited on-chip flash Assemble language produces Hex file which are much smaller than C, but programming in Assembly is often tedious and time consuming C programming is less time consuming and is much easier to write but the Hex file produced is much larger
Society Of Innovative Electrical Society Of Innovative Electrical Professionals Professionals 34
Programming in C
Following are the few main reason for programming in C rather then in Assembly It is easier and less time consuming C is easier to modify and update You can use codes available in function libraries C code is portable to other microcontrollers with little or no modifications
35
36
C language Statements
Following are the statements which are most commonly used while microcontroller programming in C For loop While loop Do/while If/else statements Switch statement
37
38
Write a program which outputs AA (binary 10101010) on the port B #include<avr/io.h> //standard AVR header int main(void) { DDRB=0xFF; //Setting Port B as output PORTB=0xAA; }
39
Example
Write a AVR program to get a byte of data from Port B, and then send it to Port C.
40
Write an AVR program to get a byte of data from Port C. If it is less then 100 send it to port B otherwise, send it to Port D
41
Write an AVR program to get a byte of data from Port C. If it is less then 100 send it to port B otherwise, send it to Port D
42
43
AND(&) It should not be confused with the logical AND(&&) operator 0x35 & 0x0F = 0x05 00110101 & 00001111=00000101 OR(|) It should not be confused with logical OR(||) operator 0x04 | 0x68=0x6C 00000100 | 1101000=1101100
Society Of Innovative Electrical Professionals 44
XOR(^) 0x54 ^ 0x78 = 0x2C 1010100 ^ 1111000 = 0101100 Inverter(~) ~0x55 = 0xAA ~01010101 = 10101010
45
A door sensor is connected to the bit 1 of Port B, and an LED is connected to bit 7 of Port C. Write an AVR program to monitor the door sensor and, when it opens, turns on the LED
47
Take input from the first four bits of Port A and output it to last four bits of Port A
49
#include<avr/io.h> unsigned char x; int main(void) { DDRA=0b11110000 while(1) { x=PINA&0b00001111; x= x<<4; PORTA=x&11110000; } }
Society Of Innovative Electrical Professionals 50
It was just basic intro about Microcontroller .. If you want to learn it in detail we recommend you to study from AVR Microcontroller by Mazidi . (you can take this book from me in soft ) for more help contact SIEP
51