Introduction to Microcontroller

2

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

Clock Frequency CPU data size RAM
ROM I/O Power Consumption Society Of Innovative Electrical
Professionals

16 MHz 8 bits 1KB
16KB 32 pins 20mW

3GHz 32bits 1GB
160GB keyboard 65W
3

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

Society Of Innovative Electrical Professionals

4

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

Society Of Innovative Electrical Professionals

5

Why use a microcontroller?  It is programmable  A code decides what it does (controll)  Very easy to write a code then design and make a custom circuit for complex jobs example : Micromouse  Robotics  Solar panels  Automation  Hundreds of other application Society Of Innovative Electrical Professionals 6 .

less power (excelent for mobile units) and higher core speeds than CISCs for same technology. almost 10 times faster than CISC • less instructions you don't need complex instruction decoder. etc.ARM . AVR .  http://www. PIC .AVR Microcontroller  Lots of microcontroller families  8051 . Society Of Innovative Electrical Professionals 7 .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. so RISC cores need less transistors (chip area).

SPI. pulse width modulation. pull-up resistors. you can reprogram it without removing it from the circuit . analog comparator and timers are some of the features you will find in AVR devices. timers.  Apart form this almost all AVRs support In System Programming (ISP) i.e. Internal oscillators. 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 .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. ADC.

Society Of Innovative Electrical Professionals 9 .

AVR Studio 2. Win AVR (Packaged with AVR Studio 5) 3.1 or above Society Of Innovative Electrical Professionals 10 . Extreme Burner-AVR or Pony Prog For Simulation and Lab Experiments Proteus 7.Software     Software we are going to use are 1.

Different AVRs AVRs are generally classified in four board groups  tinyAVRs  megaAVRs  XMEGA AVRs  Application Specific AVRs  program memory  Pin packages Society Of Innovative Electrical Professionals 11 .

Different AVRs Society Of Innovative Electrical Professionals 12 .

Debugging is mostly not available in other software Society Of Innovative Electrical Professionals 13 .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 .

Society Of Innovative Electrical Professionals 14 .

. if the A/D Converter is not used.    • Society Of Innovative Electrical Professionals 15 .PA0): Port A also serves as an 8-bit bi-directional I/O port.Pin Description Vcc: Digital Voltage GND: Ground PortA (PA7 . • 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.

..• PortB (PB7 .PC0) . Timer/Counter1 Counter Source • PB0: T0.PB0) . External Interrupt Source 2 • PB1: T1. Timer/Counter0 Counter Source Society Of Innovative Electrical Professionals 16 . PortD (PD7 . PortC (PC7 ..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 0 Society Of Innovative Electrical Professionals 17 .Output pin for PWM mode timer function • (Generated by timer 1) • PD4: OC1B.• PD7: OC2.Output pin for PWM mode timer function • (Generated by timer 1) • PD3: INT1. Output pin for PWM mode timer function • (Generated by timer 2) • PD5: OC1A. External Interrupt Source 1 • PD2: INT0.

Society Of Innovative Electrical Professionals 18 .

it should be connected to VCC through a lowpass filter. XTAL1: External Crystal is connected  XTAL2:  AVCC: It is the supply voltage pin for Port A and the A/D Converter. • Society Of Innovative Electrical Professionals 19 . If the ADC is used.

Society Of Innovative Electrical Professionals 20 .

Pin Description Following pins are used for programming of AVR  MOSI. PB5  MISO. 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 . PB6  SCK.

Society Of Innovative Electrical Professionals 22 .

I/O Ports Society Of Innovative Electrical Professionals 23 .

More Details on Page 48 of Datasheet Society Of Innovative Electrical Professionals 24 .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.

Port A Data Register – PORTA Port A Data Direction Register – DDRA Port A Input Pins Address – PINA Society Of Innovative Electrical Professionals 25 .

• Configuring the pin of a port Each port pin consists of three register bits DDxn PORTxn PINxn Society Of Innovative Electrical Professionals 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 input piin. Pxn is configured as an output piin. Society Of Innovative Electrical Professionals 27 . • If DDxn is written logic zero.

Configuring the I/O Ports (PORTxn) • PORTxn: If a pin is configured as output then • If we write logic one. the pin is set low(zero) Society Of Innovative Electrical Professionals 28 . the pin is set high(one) • If we write logic zero.

Configuring the I/O Ports (PINxn) • PINx: • Independent of the setting of Data Direction bit DDxn. • The port pin can be read through the PINxn Register bit. So while using it as input read PINxn Register bit to get the input • DDRA=0x00 • PORTA=0x30 • x=PINA Society Of Innovative Electrical Professionals 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 Society Of Innovative Electrical Professionals 31 .

//reads and stores input at Port A in variable x PORTD=x+5. //Multiply x with 5 and outputs it on Port D } Society Of Innovative Electrical Professionals 32 . //configures Port D as output unsigned int x. //declare variable x x=PINA.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.

Society Of Innovative Electrical Professionals 33 .

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 . 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.

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 Society Of Innovative Electrical Society Of Innovative Electrical Professionals Professionals 35 .

C data type for AVR C in AVR GCC Society Of Innovative Electrical Society Of Innovative Electrical Professionals Professionals 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      Society Of Innovative Electrical Society Of Innovative Electrical Professionals Professionals 37 .

• Write a program which outputs AA (binary 10101010) on the port B Society Of Innovative Electrical Professionals 38 .

} Society Of Innovative Electrical Professionals 39 .• 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.

Society Of Innovative Electrical Professionals 40 .Example • Write a AVR program to get a byte of data from Port B. and then send it to Port C.

If it is less then 100 send it to port B otherwise. send it to Port D Society Of Innovative Electrical Professionals 41 .• Write an AVR program to get a byte of data from Port C.

send it to Port D Society Of Innovative Electrical Professionals 42 .• 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.

Bit wise operators in C • Following are the bit wise operator in C which are widely used for embedded systems programming  AND(&)  OR(|)  EX-OR(^)  Inverter(~)  Shift right(>>)  Shift left(<<) Society Of Innovative Electrical Professionals 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 Society Of Innovative Electrical Professionals 45 .

 In order to send ‘1’ to a bit 7 of register ‘x’ we use bit wise OR (|) operator x=x|0b10000000.Reading or writing a bit in a Register  Consider 8-bit variable ‘x’ and ‘y’  In order to send ‘0’ to a bit 7 of register ‘x’ we use bit wise AND (&) operator x=x&0b01111111.  If we want to read only bit 7 of variable ‘x’ in another variable ‘y’ we use bit wise AND (&) operator y=x&0b10000000. Society Of Innovative Electrical Professionals 46 .

when it opens. turns on the LED Society Of Innovative Electrical Professionals 47 . Write an AVR program to monitor the door sensor and. and an LED is connected to bit 7 of Port C.• A door sensor is connected to the bit 1 of Port B.

Bit wise operator on C (Shift operator) • Shift right Symbol >> Format of Shift Operator data>>number of bit to be shifted Example 0b00100000 >> 3 = 0b00000100 Shift left Symbol << Format of Shift Operator data<<number of bit to be shifted Example 0b00010000 << 3 = 0b10000000 Society Of Innovative Electrical Professionals 48 .

• Take input from the first four bits of Port A and output it to last four bits of Port A Society Of Innovative Electrical Professionals 49 .

PORTA=x&11110000. } } Society Of Innovative Electrical Professionals 50 .#include<avr/io. x= x<<4.h> unsigned char x. int main(void) { DDRA=0b11110000 while(1) { x=PINA&0b00001111.

If you want to learn it in detail … we recommend you to study from AVR Microcontroller by Mazidi ..• It was just basic intro about Microcontroller . (you can take this book from me in soft ) for more help contact SIEP … Society Of Innovative Electrical Professionals 51 .

Sign up to vote on this title
UsefulNot useful