Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

In The Name of Allah

AUTOMATIC CONTROL
SYSTEMS
Ali Karimpour
Associate Professor
Ferdowsi University of Mashhad

Reference:
1. ATMEGA 32 datasheet
2. AVR033: Getting Started with the Code Vision AVR C Compiler
lecture 5

Lecture 5

The AVR Programming and


Simulation

2
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Contents

Introduction to AVR
Specially ATMEGA32 (One of the AVR ATMEGA microcontrollers family)

Code Vision
Programming a microcontroller through high level language(C)

Proteus
The Proteus provides detailed instructions on how to create new simulator models,
using schematic techniques.

Simple Examples
3
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Features
 High-performance, Low-power Atmel®AVR®
8-bit Microcontroller
 131 Powerful Instructions – Most Single-clock
Cycle Execution
 32 × 8 General Purpose Working Registers
 32Kbytes of In-System Self-programmable Flash
program memory (W/E: 10,000 times)
 1024Bytes EEPROM (W/E: 100,000 times)
 Data retention: 20 years at 85°C/100 years at 25°C
 Programming of Flash, EEPROM, Fuses, and
4
Lock Bits through the JTAG Interface
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Features
 Operating Voltages
• – 2.7V - 5.5V for ATmega32L
• – 4.5V - 5.5V for ATmega32
 Speed Grades
• 0 - 8MHz for ATmega32L
• 0 - 16MHz for ATmega32
 Power Consumption at 1MHz, 3V, 25°C
• Active: 1.1mA
• Idle Mode: 0.35mA
• Power-down Mode: < 1μA
5
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Peripheral Features


 Two 8-bit Timer/Counters and one 16-bit
 Four PWM Channels
 8-channel, 10-bit ADC
 Programmable Serial USART
 Programmable Watchdog Timer with Separate On-
chip Oscillator
 On-chip Analog Comparator
 Internal Calibrated RC Oscillator
 External and Internal Interrupt Sources
 Six Sleep Modes: Idle, ADC Noise Reduction, Power-
save, Power-down, Standby and Extended Standby6
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Peripheral Features


One of the most important role of Registers is tuning the application of each unit

ATMEGA 32

7
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Peripheral Features


 Timer/Counter

8
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Peripheral Features


 Timer/Counter

9
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Peripheral Features


 PWM Channel

10
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Peripheral Features


 Programmable Serial USART

11
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Peripheral Features


 Watchdog Timer

Program must
reset Watchdog
timer before
MCU RESET
occurring

12
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Peripheral Features


 Interrupt Sources

External Interrupts

Internal Interrupts

13
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Peripheral Features


 External Interrupt Sources

What is the importance of External Interrupts?


14
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Peripheral Features


 Internal Interrupt Sources

What is the importance of Internal Interrupts? 15


Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Peripheral Features


 Sleep Mode

Power-down Mode: < 1μA

16
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Pin Descriptions


VCC Digital supply voltage.

GND Ground.

Port A (PA7..PA0)
Port A is an 8-bit bi-directional I/O port.

Port B (PB7..PB0)
Port B is an 8-bit bi-directional I/O port.

Port C (PC7..PC0)
Port C is an 8-bit bi-directional I/O port.

17
Port D (PD7..PD0) Port D is an 8-bit bi-directional I/O Ali
port.
Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

ATMEGA32 Pin Descriptions


RESET
Reset Input.

XTAL1
Input to the inverting Oscillator amplifier
and input to the internal clock operating
circuit.
XTAL2
Output from the inverting Oscillator
amplifier.

AVCC
AVCC is the supply voltage pin for Port A
and the A/D Converter.

AREF AREF is the analog reference pin for the A/D Converter. 18
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5
ATMEGA32 Pin Descriptions
Port A Pins Alternate Functions

ADC0-7

Port A serves as the analog inputs to the A/D


Converter.

19
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5
ATMEGA32 Pin Descriptions
Port B Pins Alternate Functions

XCK/T0
T0 (Timer/Counter0 External Counter Input)
XCK (USART External Clock Input/Output)

T1
T1 (Timer/Counter1 External Counter Input)

INT2/AINO
AIN0 (Analog Comparator Positive Input)
INT2 (External Interrupt 2 Input)

20
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5
ATMEGA32 Pin Descriptions
Port D Pins Alternate Functions

PD2
INT0 (External Interrupt 0 Input)

PD3
INT1 (External Interrupt 1 Input)

21
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Code Vision AVR


Programming a microcontroller through high level language(C)

22
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Code Vision AVR

1. Opening Code vision

2. Creating new project

23
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Code Vision AVR

3. Chip selection

4. Clock setting

24
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Code Vision AVR

5. Port setting
- Selecting port
- Input / Output selection (DDRx)

- Pull up / Output value selection


(PORTx)

x can be: A / B / C / D
25
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Code Vision AVR

6. External Interrupts setting


- Interrupt Number selection
- Interrupt Mode determination
(Low level / Any change /
Falling Edge / Rising Edge)

26
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Code Vision AVR

7. Timers setting
- Timer Number selection
- Timer or Counter determination
- Timer Frequency determination (CSxx)
- Timer Mode determination (WGMxx)
- Timer Interrupt (OCIEx / TOIEx)
- Timer Initial Values (TCNTx / OCRx)

x can be: 0 / 1 / 2
27
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Code Vision AVR

8. Serial USART setting


- Serial Dirrection selection
- Serial Interrupt enabling
(RXCIE / TXCIE)
- Serial Baud rate determination (UBRR)
- Serial Communication Parameters
determination (UCSRC)
- Serial Mode selection

28
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Code Vision AVR

9. LCD setting
- LCD Port selection
- LCD Selection
- LCD Pin Out explanation

29
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Code Vision AVR


10. Completing the Project

- Save source file

- Save Project file

- Save Code Wizard


AVR Project file

30
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Code Vision AVR


Generated Program Save , Compile and Run the Terminal (Serial) Terminal Setting
Built Program

Template Codes

Message Window

31
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Code Vision AVR

 Generated Program.txt
 Generated Program.dox
 Generated Program.pdf

32
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Code Vision AVR


 place your code in determined
places
 Save Compile and Build your
Program
Save , Compile and
 InformationBuilt
Window
Program shows the
occurred Warnings and Errors
 Now The Hex file of your Code
is Generated here:
 Project path/Exe/

33
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Proteus 7 Professional
The Proteus provides detailed instructions on how to create new
simulator models, using schematic techniques.

34
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Proteus 7 Professional

1. Opening Proteus
 1. Opening Proteus.
 2. Placing the devices of the circuit.
 3. Wiring the circuit.
 3. Programming the micro (ATmega32).
 4. Adding probes and sensors if needed.
 5. Simulating the circuit.

35
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Proteus 7 Professional

Select mode

Component mode

Wiring mode

Pick new object

Power / GND / etc

More Powers and


Generators

Voltage and
Current Probes

Seneor and
Instrumentation

Run the simulation

36
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Proteus 7 Professional
You can search
for your device
here

OR find it
from the
Category

37
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Proteus 7 Professional

38
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Proteus 7 Professional

Some devices that has been


used in next examples

39
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Proteus 7 Professional

40
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Simple Examples
Simple programming and simulation examples

41
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Simple Examples
 Example1
 Used Blocks:

 I/O ports

42
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Simple Examples
 Example2
 Used Blocks:

 I/O ports

 Counter0

43
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Simple Examples
 Example3
 Used Blocks:

 I/O ports

 Counter0

 PWM2

44
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Simple Examples
 Example4
 Used Blocks:

 I/O ports

 Counter0

 PWM2

 Interrupt

45
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5

Simple Examples
 Example5
 Used Blocks:

 I/O ports

 Counter0

 PWM2

 Interrupt

 USART

46
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013
lecture 5
A manual control system with brain
microcontroller

47
Ali Karimpour, Hamed Moll-Ahmadian Apr 2013

You might also like