Professional Documents
Culture Documents
Equipment needed:
• AVR trainer kit
• ATMega32 Microcontroller
• CodeVisionAVR ( IDE )
• Programmer (To write Hex code)
• Proteus software (Simulator)
1. Introduction:
A microcontroller often serves as the “brain” of a mechatronic system. Like a mini, self-contained
computer, it can be programmed to interact with both the hardware of the system and the user. Even the most
basic microcontroller can perform simple math operations, control digital outputs, and monitor digital inputs.
1
Memory ROM, SRAM, SRAM, FLASH Flash, SRAM, Flash, SDRAM,
FLASH EEPROM EEPROM
ISA CISC Some features of RISC RISC
RISC
Memory Von Neumann Harvard Modified Modified Harvard
Architecture architecture architecture architecture
Power Average Low Low Low
Consumption
Families 8051 variants PIC16,PIC17, Tiny, Atmega, ARMv4,5,6,7 and
PIC18, PIC24, Xmega, special series
PIC32 purpose AVR
Community Vast Very Good Very Good Vast
Manufacturer NXP, Atmel, Silicon Microchip Atmel (Now Apple, Nvidia,
Labs, Dallas, occupied by Qualcomm,
Cyprus, Infineon, Microchip) Samsung
etc. Electronics, and TI
etc.
Cost (as Very Low Average Average Low
compared to
features provide)
Other Feature Known for its Cheap Cheap, effective High speed
Standard operation
Popular AT89C51, P89v51, PIC18fXX8, Atmega8, 16, 32, LPC2148, ARM
Microcontrollers etc. PIC16f88X, Arduino Cortex-M0 to
PIC32MXX Community ARM Cortex-M7,
etc.
Two 16-bit Timer/Counters Two 8-bit Timer/Counters with Separate Prescalers and Compare
Modes, One 16-bit Timer/Counter with Separate Prescaler,
Compare Mode, and Capture Mode, Real Time Counter with
Separate Oscillator, Four PWM Channels
No internal ADC 8-channel, 10-bit ADC
2
1.3 ATmega32 IC Package:
On board Peripherals
1. ATMEGA32
2. Two 3A/220V Relay.
3. One RGB LED.
4. USB-TTL.
5. LM45 temperature sensor.
6. Potentiometer.
3
2. CodeVisionAVR:
4
Follow all the steps.
1. Download and open CVAVR. Goto file > new.
5
4. Now a window opens for including various functions or header files in your program automatically.
First select your controller from chip option.
5. If you used LCD in your project then go to LCD and select LCD port.
6
6. After including all your requirements go to file>generate, save and exit.
7. Now this window opens 3 times save your project at the location you want and typr the file name
like evm in all 3 windows. All 3 file names must be same
7
8. Now a project window opens where necessary header files are included automatically. You also can
include any header file you want from the CVAVR library.
11. Declare all the variables above and scroll down to see while(1) function and write your code in
while(1).
8
12. Click the button as shown in window for building your project.
13. Now a windows open make sure there is no error and no warning.
9
3. Proteus:
Proteus has many features to generate both analog and digital result. However, this lab will focus on
only tools that will be used in digital schematic designs.
10
Figure: Pick up 100 ohms resistor from Proteus library
By double clicking on the microcontroller bring a screen like below and change on the three places
as marked.
11
Steps:
1. Open Proteus ISIS Schematic Capture
2. Select the Component Mode from the left Toolbar
3. Click On P (Pick From Libraries)
4. Add all the required components
5. Place the components on the workspace
6. Wire up the circuit
7. Click on Play Button on the bottom left to start simulation
DDRx (Data Direction Register) configures data direction of port pins. Means its setting determines
whether port pins will be used for input or output. Writing 0 to a bit in DDRx makes corresponding port
pin as input, while writing 1 to a bit in DDRx makes corresponding port pin as output.
Example:
o to make all pins of port A as input pins :
DDRA = 0b00000000;
o to make all pins of port A as output pins
: DDRA = 0b11111111;
o to make lower nibble of port B as output and higher nibble as input
: DDRB = 0b00001111;
PINx (Port IN) used to read data from port pins. In order to read the data from port pin, first you have
to change port’s data direction to input. This is done by setting bits in DDRx to zero. If port is made
output, then reading PINx register will give you data that has been output on port pins.
12
Now there are two input modes. Either you can use port pins as tri stated inputs or you can activate
internal pull up. It will be explained shortly.
Example:
o to read data from port A.
DDRA = 0x00; //Set port A as input
x = PINA; //Read contents of port A
When you set bits in DDRx to 1, corresponding pins become output pins. Now you can write data into
respective bits in PORTx register. This will immediately change state of output pins according to data
you have written.
Example:
o to output 0xFF data on port b
DDRB = 0b11111111; //set all pins of port B as outputs
PORTB = 0xFF; //write data on port
o to output data in variable x on port a
DDRA = 0xFF; //make port A as output
PORTA = x; //output variable on port
When you set bits in DDRx to 0, i.e. make port pins as inputs, then corresponding bits in PORTx register
are used to activate/deactivate pull-up registers associated with that pin. In order to activate pull-up
resister, set bit in PORTx to 1, and to deactivate (i.e to make port pin tri stated) set it to 0.
NOTE: while using on-chip ADC, ADC port pins must be configured as tri-stated input.
Example:
o to make port a as input with pull-ups enabled and read data from port a
DDRA = 0x00; //make port A as input
PORTA = 0xFF; //enable all pull-ups
y = PINA; //read data from port a pins
13
o to make port B as tri stated input
DDRB = 0x00; //make port B as input
PORTB = 0x00; //disable pull-ups and make it tri state
o to make lower nibble of port A as output, higher nibble as input with pull-ups enabled
DDRA = 0x0F; //lower nibble -> output, higher nibble -> input
PORTA = 0xF0; //lower nibble -> set output pins to 0, higher nibble -> enable pull-ups
5. Revising Embedded C:
5.1 Data Types, for loop, while loop and array in C for AVR:
14
Figure: Schematic diagram for Example 1, 2 ,3 ,4
Example 1:
Turn on LEDs connected on PORTD and turn off LEDs after fixed delay. [This is an example of for loop]
#include <mega32.h>
void main(void)
{
unsigned int y= 0; // answer why y is unsigned int ; hint data size
DDRD=0b11111111; // set port D as output DDRD= 0xFF / 255
PORTD=0b11111111; // PORTD= 0xFF / 255
for(y=0; y<65535; y++) // very the value of y for different delay time
{
}
PORTD=0b00000000; // PORTD= 0x00 / 0
}
15
// bit implementation of I/O [Compiler dependent-> be careful]
#include <mega32.h>
void main(void)
{
unsigned int y= 0;
DDRD.0=1;
DDRD.1=1;
DDRD.2=1;
DDRD.3=1;
DDRD.4=1;
DDRD.5=1;
DDRD.6=1;
DDRD.7=1;
PORTD.0=1;
PORTD.1=1;
PORTD.2=1;
PORTD.3=1;
PORTD.4=1;
PORTD.5=1;
PORTD.6=1;
PORTD.7=1;
for(y=0; y<65535; y++) //same as for(y=0; y<65535; y++); very the value of y for different delay time
{
}
PORTD=0b00000000; // PORTD= 0x00 / 0
}
Example 2:
Write a C program to toggle all bits of PORTD 100 times with delay. [This is an example of nested for loop]
#include <mega32.h>
void main(void)
{
unsigned char x= 0; // answer why x is unsigned int ; hint : data size
unsigned int y= 0;
DDRD=0b11111111; //set port D as output DDRD= 0xFF /
255 for(x=0; x<100; x++)
{
PORTD=0b11111111; // PORTD= 0xFF / 255
for(y=0; y<65535; y++); // for delay
PORTD=0b00000000; // PORTD= 0x00 / 0
for(y=0; y<65535; y++); // for delay
}
}
16
Example 3:
Write a C program to toggle all bits of PORTD infinitely with delay. [This is an example of nested for loop]
#include <mega32.h>
void main(void)
{
unsigned int y= 0;
DDRD=0b11111111; //set port D as output DDRD= 0xFF / 255
for( ; ; ) // infinite loop : equivalent to while (1)
{
PORTD=0b11111111; // PORTD= 0xFF / 255
for(y=0; y<65535; y++); // for delay
PORTD=0b00000000; // PORTD= 0x00 / 0
for(y=0; y<65535; y++); // for delay
}
}
Example 4:
Write a C program to show the contents of an array in PORTD. [This is an example of array]
#include <mega32.h>
void main(void)
{
unsigned char mylist [10] = {0,1,2,3,4,5,6,7,8,9};
unsigned char y= 0;
unsigned int x=0;
DDRD=0b11111111; //set port D as output DDRD= 0xFF / 255
for(y=0 ;y<10 ;y++ ) // this loop will iterate 10 times
{
PORTD=mylist [y];
for(x=0; x<65535; x++); // for delay
}
}
#include <mega32.h>
#include <delay.h>
void main(void)
{
unsigned char x= 0;
DDRD=0b11111111;
for(x=0; x<100; x++)
{
PORTD=0b11111111
delay_ms(1000); // 1000ms delay
PORTD=0b00000000;
delay_ms(500); // 500ms delay
}
}
17