Avr Book
Avr Book
Avr Book
INTRODUCTION TO AVR
GETTING STARTED WITH
1. AVR STUDIO
2. AVR OSP
3. SINA PROG
INTERFACING
1. LCD,KEYPAD
2. ADC,I2C,SERIAL,PWM
ATMEGA DEVELOPMENT BOARD
Atmega 16/32 Microcontroller
ATMEGA 16/32
MICROCONTROLLER
www.researchdesignlab.com Page 1
Atmega 16/32 Microcontroller
Table of Contents
OVERVIEW: .............................................................................................................................................. 3
PORTS: ................................................................................................................................................................... 3
INTERFACE............................................................................................................................................. 25
2. LCD .............................................................................................................................................................. 27
4. ADC ............................................................................................................................................................. 32
5. KEYPAD ....................................................................................................................................................... 36
CODE: .................................................................................................................................................................. 45
www.researchdesignlab.com Page 2
Atmega 16/32 Microcontroller
OVERVIEW:
ATmega16 is an 8-bit high performance microcontroller of Atmel’s Mega AVR
family with low power consumption. Atmega16 is based on enhanced RISC
architecture with 131 powerful instructions. Most of the instructions execute in one
machine cycle. Atmega16 can work on a maximum frequency of 16MHz.
PORTS:
There are 32 I/O (Input/Output) pins grouped as A, B, C & D with 8 pins in each
group. This group is called as PORT.
www.researchdesignlab.com Page 3
Atmega 16/32 Microcontroller
www.researchdesignlab.com Page 4
Atmega 16/32 Microcontroller
PIN DESCRIPTION:
• VCC: Digital supply voltage. (+5V)
• GND: Ground. (0 V) Note there are 2 ground Pins.
• Port A (PA7 - PA0)
Port A serves as the analog inputs to the A/D Converter. Port A also serves
as an 8-bit bi-directional I/O port, if the A/D Converter is not used. When
pins PA0 to PA7 are used as inputs and are externally pulled low, they will
source current if the internal pull-up resistors are activated. The Port A pins
are tri-stated when a reset condition becomes active, even if the clock is not
running.
www.researchdesignlab.com Page 5
Atmega 16/32 Microcontroller
• RESET: Reset Input. A low level on this pin for longer than the
minimum pulse length will generate a reset, even if the clock is not
running.
• AVCC: AVCC is the supply voltage pin for Port A and the A/D
Converter. It should be externally connected to VCC, even if the
ADC is not used. If the ADC is used, it should be connected to VCC
through a low-pass filter.
• AREF: AREF is the analog reference pin for the A/D Converter.
www.researchdesignlab.com Page 6
Atmega 16/32 Microcontroller
1. AVR STUDIO
Setup:
1. open AVR studio 4
www.researchdesignlab.com Page 7
Atmega 16/32 Microcontroller
4. Add your project name. Here I have given my project name as serial and
it will automatically create your initial file name in .c format
5. click on next
www.researchdesignlab.com Page 8
Atmega 16/32 Microcontroller
6. Select AVR simulator in debug platform and in the Device select the
Device your using as here I am using ATmega 16 i have selected that.(for
Atmega 32 select Atmega 32).
7. click on finish
www.researchdesignlab.com Page 9
Atmega 16/32 Microcontroller
8. This is the default window you will see after you click on finish and
project name called serial will get open
10. Type the program and save it .To compile the files first you need to add
files in your source for doing that right click on the source file and then
www.researchdesignlab.com Page 10
Atmega 16/32 Microcontroller
11.Now inside that source file their will be your file saved here the file
name I have used is urted
www.researchdesignlab.com Page 11
Atmega 16/32 Microcontroller
www.researchdesignlab.com Page 12
Atmega 16/32 Microcontroller
13. If your program has no errors it will be successfully build and it will
show the build window like this
www.researchdesignlab.com Page 13
Atmega 16/32 Microcontroller
www.researchdesignlab.com Page 14
Atmega 16/32 Microcontroller
3. First you need to configure your avr osp to check its connected to the
same com port or not and default Baud rate
www.researchdesignlab.com Page 15
Atmega 16/32 Microcontroller
4. you can manually select the device which you are using or else u can
even auto detect it
www.researchdesignlab.com Page 16
Atmega 16/32 Microcontroller
are using
6. after pressing auto detect you will see this means that your
microcontroller has been detected and it read to flash program on to it
www.researchdesignlab.com Page 17
Atmega 16/32 Microcontroller
program.
www.researchdesignlab.com Page 18
Atmega 16/32 Microcontroller
9. After clicking on program it will erase the previous content of the chip
and will flash the program on to the chip
www.researchdesignlab.com Page 19
Atmega 16/32 Microcontroller
www.researchdesignlab.com Page 20
Atmega 16/32 Microcontroller
10. After the completion of the flash it will compare the flash data if its
equal than it means that your flash is successful and it will show
“Equal”
www.researchdesignlab.com Page 21
Atmega 16/32 Microcontroller
• If You are using ISP Atmega Programmer to Burn the code In Your Microcontroller than this
Programmer will surely come in handy
• Its simple to use and You can easily use the Microcontroller For External Crystal Frequency
www.researchdesignlab.com Page 22
Atmega 16/32 Microcontroller
• If You Want to Change Internal Frequency Or want To use External Crystal Frequency than You
can change the Fuse.
(If Ext Crys Doesn’t work properly than Try changing the BC valve as shown in Below Pic)
www.researchdesignlab.com Page 23
Atmega 16/32 Microcontroller
• Usually For Atmega 16 it comes out to be BC 32 KHz and For Atmega 32 BC 16 KHz but try with
Other Valve if this Doesn’t Works.
www.researchdesignlab.com Page 24
Atmega 16/32 Microcontroller
INTERFACE
1. LED BLINKING
Here we are blinking the led making it on and off for a certain duration using
AT Mega 16
Circuit diagram:
www.researchdesignlab.com Page 25
Atmega 16/32 Microcontroller
Program:
# define F_CPU 1000000UL
#define FOSC 16000000L //here we define the clock frequency
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRB = 0xFF; //Makes PORTC as Output
While (1) //infinite loop
{
PORTB = 0xFF; //Turns ON All LEDs
_delay_ms(1000); //1 second delay
PORTB= 0x00; //Turns OFF All LEDs
_delay_ms(1000); //1 second delay
}
}
www.researchdesignlab.com Page 26
Atmega 16/32 Microcontroller
2. LCD :
CIRCUIT DIAGRAM:
Program:
# define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
www.researchdesignlab.com Page 27
Atmega 16/32 Microcontroller
PORTB=val;
PORTC = PORTC & (~(1<<RS));
_delay_ms(1); // here we provide a delay of 1 sec
ORTC = PORTC | ((1<<EN)); //make enable pin high
_delay_ms(1);
PORTC = PORTC & (~(1<<EN)); //make enable pin low
PORTB = ch;
PORTC = PORTC | ((1<<RS));//make register select pin high
_delay_ms(1);
PORTC = PORTC | ((1<<EN)); //make enable pin high
_delay_ms(1);
PORTC = PORTC & (~(1<<EN)); //make enable pin low
}
void LCD_WRT( char *string)
{
while(*string)
DATA_WRT(*string++);//will write the strings
int main(void)
{
//setting the display of the lcd
www.researchdesignlab.com Page 28
Atmega 16/32 Microcontroller
for(i=0;i<5;i++)
{
TEMP1=CMD[i]; //it will place the command in cmd array
CMD_WRT(TEMP1); //it will write all the cmd that is in the cmd array
}
while(1)
{
}
return 0;
}
www.researchdesignlab.com Page 29
Atmega 16/32 Microcontroller
#include <avr/io.h>
#include <util/delay.h>
// initialize PWM
void pwm_init()
{
// initialize timer0 in PWM mode
TCCR0 |= (1<<WGM00)|(1<<COM01)|(1<<WGM01)|(1<<CS00);
// make sure to make OC0 pin (pin PB3 for atmega32) as output pin
DDRB |= (1<<PB3);
}
void main()
{
uint8_t brightness;
// run forever
while(1)
{
// increasing brightness
for (brightness = 0; brightness < 255; ++brightness)
{
www.researchdesignlab.com Page 30
Atmega 16/32 Microcontroller
// decreasing brightness
for (brightness = 255; brightness > 0; --brightness)
{
// set the brightness as duty cycle
OCR0 = brightness;
www.researchdesignlab.com Page 31
Atmega 16/32 Microcontroller
4. ADC :
Program:
# define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
//#include <iom16.h>
#include <stdlib.h>
www.researchdesignlab.com Page 32
Atmega 16/32 Microcontroller
// initialize adc
void adc_init()
{
// AREF = AVcc
ADMUX = (1<<REFS0); //initialize admux
{
// select the corresponding channel 0~7
// ANDing with '7' will always keep the value
// of 'ch' between 0 and 7
ch &= 0b00000111; // AND operation with 7
ADMUX = (ADMUX & 0xF8)|ch; // clears the bottom 3 bits
before ORing
return (ADC);
}
int main(void)
{
uint16_t adc_result0;//, adc_result1;
char int_buffer[10]; //creating array of 10
unsigned char CMD[]={0x38,0x01,0x0f,0x06,0x80},TEMP1,i;
DDRB=0XFF;//set port b as output
DDRC = 0xFF;//(1<<RS)|(1<<EN);
_delay_ms(10);
for(i=0;i<5;i++)
{
www.researchdesignlab.com Page 34
Atmega 16/32 Microcontroller
itoa(adc_result0, int_buffer,10);
_delay_ms(1000);
www.researchdesignlab.com Page 35
Atmega 16/32 Microcontroller
5. KEYPAD :
Block diagram:
www.researchdesignlab.com Page 36
Atmega 16/32 Microcontroller
Program:
#include<avr/io.h>
#include <avr/keypad.h> // to initialize the keypad
void main(void)
{
DDRB=0xFF; //make PORTB as output
DDRD=0X07; //make PORTD pin 0, 1, 2 as output
DDRB=0X0F;
PORTC=0xFF; //make PORTC as output
unsigned char keypad_valve;
lcd_init();
while(1)
{
lcd_command_write(0x08); //display off cursor off
lcd_string_write("PRESS ANY KEY");
lcd_command_write(0xc0);//2nd line display
keypad_valve=read_keypad();
www.researchdesignlab.com Page 37
Atmega 16/32 Microcontroller
if(keypad_valve!=0xFF)
{
lcd_number_write(keypad_valve,10);//if key is pressed corresponding
valve will be displayed
lcd_data_write(' ');
}
else
;
_delay_ms(300);
}
}
www.researchdesignlab.com Page 38
Atmega 16/32 Microcontroller
SERIAL COMMUNICATION:
The USART transmission system differs to most other digital busses in that
it does not utilize a separate pin for the serial clock. An agreed clock rate is
preset into both devices, which is then used to sample the Rx/Tx lines at
regular intervals. Because of this, the USART requires only three wires for
bi-directional communication (Rx, Tx and GND).
www.researchdesignlab.com Page 39
Atmega 16/32 Microcontroller
Setting up HyperTerminal
www.researchdesignlab.com Page 40
Atmega 16/32 Microcontroller
3. Select your com port which you are using and press ok (to check
the com port go to device manger and check in which com port is the usart connected)
4. select the baud rate that you have set or just click Restore
default it will set the default valve and press ok
www.researchdesignlab.com Page 41
Atmega 16/32 Microcontroller
www.researchdesignlab.com Page 42
Atmega 16/32 Microcontroller
Code:
int main (void)
UCSRB = (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
Next, we need to tell the AVR what type of serial format we're using.
Looking again at the MEGA16 datasheet, we can see that the bits responsible for
the serial format are named UCSZ0 to UCSZ2, and are located in the USART
control register C named UCSRC.
Code:
UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes - URSEL bit
set to select the UCRSC register
The last thing to set for basic serial communications is the baud rate
register. The baud rate register is 16-bit, split into two 8-bit registers as is the case
with all 16-bit registers in the AVR device family. For this the baud valve need to
be found for that we have a formula
Where F_CPU is your AVR's system clock frequency (in Hz), and USART_BAUDRATE is the desired
communication baud rate.
Given my example project using a system clock of 7372800Hz and a baud rate of 9600, our formula
gives:
www.researchdesignlab.com Page 43
Atmega 16/32 Microcontroller
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte
of the UBRR register
UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the
UBRR register
We do this by the special register named UDR - short for "USART I/O
Data Register”. On the MEGA16, the Transmission Complete flag is located in the
control register UCSRA, and it is named TXC. Using this information we can
construct a simple wait loop which will prevent data from being written to the
UDR register until the current transmission is complete.
UDR = ByteToSend; // Send out the byte value in the variable "ByteToSend"
while ((UCSRA & (1 << TXC)) == 0) {}; // Do nothing until transmission complete flag set
However this is non-optimal. We spend time waiting after each byte which
could be better spent performing other tasks - better to check before a transmission
to see if the UDR register is ready for data. We can do this by checking the
USART Data Register Empty flag instead (called UDRE), also located in the
UCSRA control register of the MEGA16
www.researchdesignlab.com Page 44
Atmega 16/32 Microcontroller
while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be
written to it
UDR = ByteToSend; // Send out the byte value in the variable "ByteToSend"
while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been received and is ready
to be read from UDR
ReceivedByte = UDR; // Fetch the received byte value into the variable "ReceivedByte"
CODE:
#include <avr/io.h>
#include <avr/delay.h>
unsigned char
www.researchdesignlab.com Page 45
Atmega 16/32 Microcontroller
{
char ReceivedByte;
UCSRB = (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception
circuitry
UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes
UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the
high byte of the UBRR register
UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte
of the UBRR register
while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been
received and is ready to be read from UDR
ReceivedByte = UDR; // Fetch the received byte value into the variable
"ByteReceived"
//_delay_ms(1000);
while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for
more data to be written to it
UDR = ReceivedByte; // Echo back the received byte back to the computer
www.researchdesignlab.com Page 46
Atmega 16/32 Microcontroller
Related Products:
www.researchdesignlab.com Page 47
Atmega Development Board
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
Research
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
Design Lab
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
01010101010101010100101010101010101010101010101010101010101010101010101010101010101
www.researchdesignlab.com
Email: [email protected] I www.researchdesignlab.com
An ISO 9001- 2008 Certified Company
1. Power supply, 5V-12V 15. DB-9 female connector
2. 40 pin ZIF socket for IC mount. 16. 8x1 LED's
3. ISP connector* 17. 8 way DIP switch
4. Reset 18. RTC Module
5. Node connector 19. EEPROM
6. 4x1 7 segment display 20. 2x5x2 jumper node.
7. 26 pin raspberry connector 21. DC 5V connectors
8. Arduino Shield footprint 22. Analog to Digital output
9. ULN 2803 driver 23. 4x1 keypad
10. I2C bus 24. 16x2 LCD connectors
11. SPI bus 25. Node connector
12. XBEE footprint/XBEE Adaptor module 26. 4x4 Matrix Keypad
13. FT232 breakout board connector 27. DC 12V connectors
14. DC 3.3V connectors 28. Power ON switch
The Serial Peripheral Interface or SPI-bus is a simple 4-wire serial communications interface used
by many microprocessor/microcontroller peripheral chips that enables the controllers and
peripheral devices to communicate each other. Even though it is developed primarily for the
communication between host processor and peripherals, a connection of two processors via SPI is
just as well possible.
The SPI bus, which operates at full duplex (means, signals carrying data can go in both directions
simultaneously), is a synchronous type data link setup with a Master / Slave interface and can
support up to 1 megabaud or 10Mbps of speed. Both single-master and multi-master protocols are
possible in SPI. But the multi-master bus is rarely used and look awkward, and are usually limited
to a single slave.
The SPI Bus is usually used only on the PCB. There are many facts, which prevent us from using it
outside the PCB area. The SPI Bus was designed to transfer data between various IC chips, at very
high speeds. Due to this high-speed aspect, the bus lines cannot be too long, because their
reactance increases too much, and the Bus becomes unusable. However, its possible to use the SPI
Bus outside the PCB at low speeds, but this is not quite practical.
The peripherals can be a Real Time Clocks, converters like ADC and DAC, memory modules like
EEPROM and FLASH, sensors like temperature sensors and pressure sensors, or some other devices
like signal-mixer, potentiometer, LCD controller, UART, CAN controller, USB controller and
amplifier.
To transmit using device addressing, only the destination address must be configured. The
destination address can be specified using either the destination device's 64-bit address or its NI-
string. The XBee modules also support coordinator and broadcast addressing modes. Device
addressing in the AT firmware is configured using the DL, DH, or DN commands. In the API
firmware, the ZigBee Transmit Request API frame (0x10) can be used to specify destination
addresses.
To address a node by its 64-bit address, the destination address must be set to match the 64-bit
address of the remote. In the AT firmware, the DH and DL commands set the destination 64-bit
address. In the API firmware, the destination 64-bit address is set in the ZigBee Transmit Request
frame. ZigBee end devices rely on a parent (router or coordinator) to remain awake and receive
any data packets destined for the end device. When the end device wakes from sleep, it sends a
transmission (poll request) to its parent asking if the parent has received any RF data destined for
the end device. The parent, upon receipt of the poll request, will send an RF response and the
buffered data (if present). If the parent has no data for the end device, the end device may return
to sleep, depending on its sleep mode configuration settings. The following figure demonstrates
how the end device uses polling to receive RF data through its parent.
Features:
1. 56 byte nonvolatile RAM for data storage
2. 2-wire serial interface
3. Programmable square wave output signal
4. Automatic power-fail detect and switch circuitry
5. Consumes less than 500 nA in battery backup mode with oscillator running
6. Optional industrial temperature range -40°C to +85°C
7. Available in 8-pin DIP or SOIC
8. Recognized by Underwriters Laboratory
PIN DESCRIPTION
1. VCC - Primary Power Supply
2. X1, X2 - 32.768 kHz Crystal
Connection
3. VBAT - +3V Battery Input
4. GND - Ground
5. SDA - Serial Data
6. SCL - Serial Clock
7. SQW/OUT - Square
wave/Output Driver
20. Potentiometer
The Potentiometer Option allows the user to adjust the voltage reference by rotating a
potentiometers dial. Turning the potentiometer changes the voltage reference making it easier
to adjust the motor speed and also to set the duty cycle for PWM values (via programming).
21.4x1 keypad
Consequently, each data is sent to LCD in two steps: four higher bits are sent first (that normally
would be sent through lines D4-D7), four lower bits are sent afterwards. With the help of
initialization, LCD will correctly connect and interpret each data received. Besides, with regards
to the fact that data are rarely read from LCD (data mainly are transferred from microcontroller
to LCD) one more I/O pin may be saved by simple connecting R/W pin to the Ground. Such saving
has its price. Even though message displaying will be normally performed, it will not be possible
to read from busy flag since it is not possible to read from display.
Features:
1. Can display 224 different symbols.
2. Low power consumption.
3. 5x7 dot matrix format.
4. Powerful command set and user produced characters.
10k
FEATURES
1. Contact debouncing.
2. Easy to interface. PIN DETAILS
3. Interfaces to any microcontroller or microprocessor. pin 1-4: R0-R3:- Rows
4. Data valid output signal for interrupt activation. pin 5-8: C0-C3:- Columns
Programming Codes:
• LED BLINK • RTC
http://researchdesignlab.com/8051-i/o-code http://researchdesignlab.com/8051-rtc-code
• LCD • EEPROM
http://researchdesignlab.com/8051-lcd-code http://researchdesignlab.com/8051-eeprom-code
• KEYPAD • ADC
http://researchdesignlab.com/8051-keypad-code http://researchdesignlab.com/8051-adc-code.html
• UART • 7 Segment Display
http://researchdesignlab.com/8051-uart-code http://researchdesignlab.com/7-segment-atmel-code.html