0% found this document useful (0 votes)
56 views

AVR and C Programming Final

This document provides an overview of the ATmega8535 microcontroller, including its memory organization, pin configurations, I/O ports, and an example program to read inputs on ports A and B and output the sum on port C. It describes the microcontroller's Flash program memory, SRAM data memory, EEPROM, and I/O memory. It also outlines the functions of major pins including power, reset, and oscillator pins as well as the ports and their configuration via data direction and input registers.

Uploaded by

zbhp z
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

AVR and C Programming Final

This document provides an overview of the ATmega8535 microcontroller, including its memory organization, pin configurations, I/O ports, and an example program to read inputs on ports A and B and output the sum on port C. It describes the microcontroller's Flash program memory, SRAM data memory, EEPROM, and I/O memory. It also outlines the functions of major pins including power, reset, and oscillator pins as well as the ports and their configuration via data direction and input registers.

Uploaded by

zbhp z
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Table of Contents

1. Introduction to Microcontrollers

2. AVR layout , memory and IO

3. Simulation (software and Hardware)

a. AVR Studio 4.1


b. Proteus 7.1 SP2

4. Liquid Crystal Display (Hitachi HD44780)

5. 4*3 Keypad

6. Using Programmer and E2prom

7. Interrupts and Digital Sensors

8. ADC and Temperature Sensor

9. Stepper Motor

10. Serial Communication with PC and between


Microcontrollers

11. AVR Timers

a. Normal mode
b. CTC mode

12. Watchdog Timer


Introduction
y To define a microcontroller correctly we must first
understand the definition of a microprocessor and its
system.
Microprocessor definition
y A single chip that contains the CPU or most of the
computer and can perform two tasks:
y Execute a stored set of instructions to carry out user defined
tasks.
y Access external memory chips to both read and write data
from and to the memory.

Microprocessor To Memory
interface
Microcontroller Definition
y A microcontroller is a device which integrates a
number of the components of a microprocessor
system onto a single microchip.

y So a microcontroller combines onto the same


microchip :
y The CPU core.
y Memory (both ROM and RAM).
y Some parallel digital I/O.
y Some advanced peripherals.

Block diagram
y Advanced peripherals include:

y Timer modules to allow the microcontroller to perform tasks


for certain time periods.

y Serial I/O port to allow data to flow between the


microcontroller and other devices such as a PC or another
microcontroller.

y ADCs to allow the microcontroller to accept analogue input


data for processing.

Block Diagram
Why use a microcontroller?
y Reduce chip count

y Many applications do not require as much computing


power

y Reduced power consumption

y Reduced design cost

What does it Need?


y Power from power supply
y Memory
y Trigger signal (clock)
Memory Types
y RAM (Random Access Memory).
y ROM (Read Only Memory).
y EPROM (Erasable Rom).
y EEPROM (Electrically Erasable Rom).
y Flash.
AVR
Case Study
y Power supply using Adapter:

AVR Programmer Circuit


AVR ATmega8535 Memories
The AVR architecture has two main memory spaces, the Data Memory and the
Program Memory space.
In addition, the ATmega8535 features an EEPROM Memory for data storage.

In-System Reprogrammable Flash Program Memory


The ATmega8535 contains 8K bytes On-chip In-System Reprogrammable Flash
memory for program storage.

Since all AVR instructions are 16 or 32 bits wide, the Flash is organized as 4K x 16.

SRAM Data Memory


Next Figure shows how the ATmega8535 SRAM Memory is organized.

The 608 (000 to 25F Hexadecimal) Data Memory locations address the Register File,
the I/O Memory, and the internal data SRAM.

The first 96 (000 to 05F Hexadecimal) locations address the Register File and I/O
Memory

The next 512 locations address the internal data SRAM.


EEPROM Data Memory
The ATmega8535 contains 512 bytes of data EEPROM memory. It is organized as a
separate data space, in which single bytes can be read and written.
I/O Memory

All ATmega8535 I/Os and peripherals are placed in the I/O space.

The ATmega8535 provides the following features:

1- 8K bytes of In-System Programmable Flash with Read-While-Write


capabilities.
2- 512 bytes EEPROM.
3- 512 bytes SRAM
4- 32 general purpose I/O lines
5- 32 general purpose working registers
6- three flexible Timer/Counters with compare modes
7- internal and external interrupts
8- a serial programmable USART
9- a byte oriented Two-wire Serial Interface
10- an 8-channel 10-bit ADC with optional differential input stage with
programmable gain in TQFP package
11- a programmable Watchdog Timer with Internal Oscillator
12- an SPI(Serial Peripheral Interface) serial port
13- six software selectable power saving modes
Pin Configurations
Figure 1. Pin out ATmega8535

AVR Pins

Ports AVR has 4 I/O ports each is 8 bits wide:


x PORTA (PA0-PA7)
x PORTB (PB0-PB7)
x PORTC PC0-PC7)
x PORTD (PD0-PD7)
each port has some alternate function (not I/O) which will be discussed
later.

RESET (Pin 9) is a pin used to reset the


microcontroller when 0 is applied
on it
Reset circuit will be as the
following:

VCC (Pin 10) +5V of the power supply should be connected to this pin
GND(Pin 11) 0V of the power supply should be connected to this pin
XTAL2 Oscillator I/P and O/P
XTAL1 pins (used to connect
crystals or other
sources).

AVCC (Pin 30) +5V for supplying the ADC on PORTA should be connected to this pin
GND (Pin 31) 0V for supplying the ADC on PORTA should be connected to this pin
AREF Reference voltage for the ADC on PORTA

Note:

The ATMEGA8535 has an internal oscillator that runs from 1 MHz to 8 MHz and can
be configured to use it or override it and use external oscillators like crystals.

The choice can be done by programming fuses of the controller which has many other
functions like protection.

Major fuses

1- lock bits
2- clock options fuses (CKSEL3- CKSEL2- CKSEL1- CKSEL0)
I/O-Ports
All AVR ports have true Read-Modify-Write functionality when used as general digital I/O
ports. This means that the direction of one port pin can be changed without unintentionally
changing the direction of any other pin

Three I/O memory address locations are allocated for each port, one each for the Data
Register – PORTx, Data Direction Register – DDRx, and the Port Input Pins – PINx.

The Port Input Pins I/O location is read only, while the Data Register and the Data Direction
Register are read/write.

Reading the Pin Value


Independent of the setting of Data Direction bit DDxn, the port pin can be read through the
PINxn Register bit.
io.c
#include <stdio.h>
#include <delay.h>
#include <mega8535.h>
void main()
{
char a,b,c;
DDRA = 0x00;
DDRB = 0x00;
DDRC = 0xFF;

while(1)
{
a = PINA;
delay_ms(100);
b = PINB;
delay_ms(100);

c = a+b;
delay_ms(100);
PORTC = c;
}
}

Page 1
R9 R10 R11 R12 R13 R14 R15 R16
R8 R7 R6 R5 R4 R3 R2 R1 330R 330R 330R 330R 330R 330R 330R 330R
330R 330R 330R 330R 330R 330R 330R 330R

DSW1 U1 DSW2
OFF ON OFF ON
16 1 1 40 16 1
PB0/T0/XCK PA0/ADC0
15 2 2 39 15 2
PB1/T1 PA1/ADC1
14 3 3 38 14 3
PB2/AIN0/INT2 PA2/ADC2
13 4 4 37 13 4
PB3/AIN1/OC0 PA3/ADC3
12 5 5 36 12 5
PB4/SS PA4/ADC4
11 6 6 35 11 6
PB5/MOSI PA5/ADC5
10 7 7 34 10 7
PB6/MISO PA6/ADC6
9 8 8 33 9 8
PB7/SCK PA7/ADC7
DIPSW_8 14 22 DIPSW_8
PD0/RXD PC0/SCL
15 23
PD1/TXD PC1/SDA
16 24
PD2/INT0 PC2
17 25
PD3/INT1 PC3
18 26
PD4/OC1B PC4
19 27
PD5/OC1A PC5
20 28
PD6/ICP1 PC6/TOSC1
21 29
PD7/OC2 PC7/TOSC2
13
XTAL1
12 32
XTAL2 AREF
9 30
RESET AVCC
ATMEGA8535
LCD Functions 
The LCD Functions are intended for easy interfacing between C programs and alphanumeric 
LCD modules built with the Hitachi HD44780 chip or equivalent. 
The prototypes for these functions are placed in the file lcd.h located in the ..\INC subdirectory.  
This files must be #include ‐ed before using the functions. 
Prior to #include ‐ing the lcd.h file, you must declare which microcontroller port is used for 
communication with the LCD module. 
Example: 
/* the LCD module is connected to PORTC */ 
#asm 
    .equ __lcd_port=0x15 
#endasm 
/* now you can include the LCD Functions */ 
#include <lcd.h> 
The following LCD formats are supported in lcd.h: 1x8, 2x12, 3x12, 1x16, 2x16, 2x20, 4x20, 2x24 
and 2x40 characters. 
The LCD module must be connected to the port bits as follows: 

[LCD]  [AVR Port] 
RS (pin4)  Bit 0 
RD (pin 5)  Bit 1 
EN (pin 6) Bit2
DB4 (pin 11) Bit 4
DB5 (pin 12) Bit 5
DB6 (pin 13) Bit 6
DB7 (pin 14)  Bit 7 
 

You must also connect the LCD power supply and contrast control voltage, according to the 
data sheet. 

 
lcd.c
#asm
.equ __lcd_port=0x12 ;PORTd
#endasm
// include the LCD driver routines
#include <LCD.H>
#include <mega8535.h>
#include <delay.h>
#include <stdlib.h>
void main(void)
{
unsigned long int temp=0 ;
char str[25];

lcd_init(16);
lcd_putsf(" Hello");

delay_ms(1000);
lcd_clear();

// go on the second LCD line

lcd_gotoxy(0,1);
temp = 512;

ltoa(temp, str);
lcd_puts(str);

Page 1
LCD1
LM016L

VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
U1
E

1 40
PB0/T0/XCK PA0/ADC0
1
2
3

4
5
6

7
8
9
10
11
12
13
14
2 39
PB1/T1 PA1/ADC1
3 38
PB2/AIN0/INT2 PA2/ADC2
4 37
PB3/AIN1/OC0 PA3/ADC3
5 36
PB4/SS PA4/ADC4
6 35
PB5/MOSI PA5/ADC5
7 34
PB6/MISO PA6/ADC6
8 33
PB7/SCK PA7/ADC7
14 22
R1
PD0/RXD PC0/SCL
15 23 330R
PD1/TXD PC1/SDA
16 24
PD2/INT0 PC2
17 25
18
PD3/INT1 PC3
26
R2
PD4/OC1B PC4
19 27 330R

3
PD5/OC1A PC5
20 28
PD6/ICP1 PC6/TOSC1 R3
21
PD7/OC2 PC7/TOSC2
29
330R
A
1 2 3
13
12
XTAL1
32
R4
XTAL2 AREF
9
RESET AVCC
30 330R
R5
B
4 5 6
ATMEGA8535
330R
R6 C
7 8 9
330R
R7
330R
D
0 #
keypad.c
#asm
.equ __lcd_port=0x12 ;PORTd
#endasm
// include the LCD driver routines
#include <LCD.H>

#include <mega8535.h>
#include <delay.h>
#include <stdlib.h>
#include <keys.h>

void main(void)
{
unsigned long int temp=0 ;
char str[25];
lcd_init(16);

lcd_putsf("Enter Number");
//delay_ms(1000);
//lcd_clear();
temp=getkey();

// go on the second LCD line


// display the message
lcd_putsf("The Number is");
lcd_gotoxy(0,1);
ltoa(temp, str);
lcd_puts(str);
}

Page 1
Interrupts
Execution of a program happens serially. Commands are executed one by one; processors
can handle a single task at a time.

On the rising edge of the clock a new operation is carried out.

Interrupts are internal or external events asynchronous with the main program flow that
when they trigger, they get the microcontroller's attention.

We do not know in advance when they will occur, we just define how they are going to
be serviced.

When an interrupt signal occurs, the interrupt service routine (ISR) is executed
asynchronously with the main flow and then normal execution carries on.

There are many different sources of interrupts and each one has a corresponding ISR. The
micro has to know which event triggers which ISR so it needs a table (the vector table).

It looks at the vector table, it jumps to the indicated ISR, it executes it and then returns
back to the main program flow.

Codevision and Interrupts


To make use of these interrupt vectors, the compiler allows one to write special interrupt
procedures which are designated by writing the reserved word, interrupt, before the
procedure definition. The formal syntax is:
interrupt [vector number] void procedure_name(void)
In programs which use interrupts, the basic structure of the main () procedure is the
following:
- initialize the microcontroller normally
- set up the registers which control the interrupt(s) being used
- enable interrupts
- do the infinite loop which is the main program

At the end of the procedure the compiler generates a RETI instruction automatically.

Interrupt Vectors in ATmega8535


Status Register:

Bit 7 – I: Global Interrupt Enable

The Global Interrupt Enable bit must be set for the interrupts to be
enabled.
The individual interrupt enable control is then performed in separate
control registers.
The I bit is cleared by hardware after an interrupt has occurred, and is set
by the RETI instruction to enable subsequent interrupts.
The I bit can also be set and cleared by the software with the SEI and CLI
instructions.

External Interrupts

The External Interrupts are triggered by the INT0, INT1, and INT2 pins.

The External Interrupts can be triggered by a falling or rising edge or a low level
(INT2 is only an edge triggered interrupt).

When the External Interrupt is enabled and is configured as level triggered (only
INT0/INT1), the interrupt will trigger as long as the pin is held low.

MCU Control Register – MCUCR


The MCU Control Register contains control bits for interrupt sense control and general
MCU functions.

• Bit 3, 2 – ISC11, ISC10: Interrupt Sense Control 1 Bit 1 and Bit 0

The External Interrupt 1 is activated by the external pin INT1 if the SREG I-bit and the
corresponding interrupt mask in the GICR are set.

The level and edges on the external INT1 pin that activate the interrupt are defined in the
next Table.

If edge or toggle interrupt is selected, pulses that last longer than one clock period will
generate an interrupt. Shorter pulses are not guaranteed to generate an interrupt.
If low level interrupt is selected, the low level must be held until the completion of the
currently executing instruction to generate an interrupt.

• Bit 1, 0 – ISC01, ISC00: Interrupt Sense Control 0 Bit 1 and Bit 0

The External Interrupt 0 is activated by the external pin INT0 if the SREG I-flag and the
corresponding interrupt mask are set.

The timing of pulses is typical to INT1

MCU Control and Status Register – MCUCSR

• Bit 6 – ISC2: Interrupt Sense Control 2


The asynchronous External Interrupt 2 is activated by the external pin INT2 if the SREG
I-bit and the corresponding interrupt mask in GICR are set.

If ISC2 is written to zero, a falling edge on INT2 activates the interrupt. If ISC2 is written
to one, a rising edge on INT2 activates the interrupt.

Pulses on INT2 wider than the minimum pulse width will generate an interrupt. Shorter
pulses are not guaranteed to generate an interrupt.
General Interrupt Control Register – GICR

• Bit 7 – INT1: External Interrupt Request 1 Enable


When the INT1 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), the
external pin interrupt is enabled.
The Interrupt Sense Control1 bits 1/0 (ISC11 and ISC10) in the MCU General Control Register
(MCUCR) define whether the external interrupt is activated on the rising and/or falling edge of the
INT1 pin or level sensed.

Activity on the pin will cause an interrupt request even if INT1 is configured as an output.

The corresponding interrupt of External Interrupt Request 1 is executed from the INT1
Interrupt Vector.

• Bit 6 – INT0: External Interrupt Request 0 Enable

When the INT0 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), the
external pin interrupt is enabled.

The Interrupt Sense Control0 bits 1/0 (ISC01 and ISC00) in the MCU General Control
Register (MCUCR) define whether the external interrupt is activated on rising and/or
falling edge of the INT0 pin or level sensed.

Activity on the pin will cause an interrupt request even if INT0 is configured as an
output.

The corresponding interrupt of External Interrupt Request 0 is executed from the INT0
Interrupt Vector.

• Bit 5 – INT2: External Interrupt Request 2 Enable


When the INT2 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), the
external pin interrupt is enabled.

The Interrupt Sense Control2 bit (ISC2) in the MCU Control and Status Register
(MCUCSR) defines whether the external interrupt is activated on the rising or falling
edge of the INT2 pin.

Activity on the pin will cause an interrupt request even if INT2 is configured as an
output.
The corresponding interrupt of External Interrupt Request 2 is executed from the INT2
Interrupt Vector.

General Interrupt Flag Register – GIFR

• Bit 7 – INTF1: External Interrupt Flag 1

When an edge or logic change on the INT1 pin triggers an interrupt request, INTF1
becomes set (one).

If the I-bit in SREG and the INT1 bit in GICR are set (one), the MCU will jump to the
corresponding Interrupt Vector.

The flag is cleared when the interrupt routine is executed. Alternatively, the flag can be
cleared by writing a logical one to it.

This flag is always cleared when INT1 is configured as a level interrupt.

• Bit 6 – INTF0: External Interrupt Flag 0


When an edge or logic change on the INT0 pin triggers an interrupt request, INTF0
becomes set (one).

If the I-bit in SREG and the INT0 bit in GICR are set (one), the MCU will jump to the
corresponding Interrupt Vector.

The flag is cleared when the interrupt routine is executed. Alternatively, the flag can be
cleared by writing a logical one to it.

This flag is always cleared when INT0 is configured as a level interrupt.

• Bit 5 – INTF2: External Interrupt Flag 2


When an event on the INT2 pin triggers an interrupt request, INTF2 becomes set (one).

If the I-bit in SREG and the INT2 bit in GICR are set (one), the MCU will jump to the
corresponding

Interrupt Vector. The flag is cleared when the interrupt routine is executed.
Alternatively, the flag can be cleared by writing a logical one to it.
int.c
#include <delay.h>
#include <mega8535.h>
interrupt [2] void INT0(void)
{
PORTC= 0xFF;
delay_ms(600);
PORTC= 0x99;
delay_ms(600);
//GIFR!=0x40;
}

void main()
{
DDRC = 0xff;
PORTC= 0x00;
MCUCR=0x03;
GICR=0x40;
#asm
sei
#endasm
while(1)
{
PORTC= 0x00;
delay_ms(10);
}
}

Page 1
U1
1 40
PB0/T0/XCK PA0/ADC0
2 39
PB1/T1 PA1/ADC1
3 38
PB2/AIN0/INT2 PA2/ADC2
4 37
PB3/AIN1/OC0 PA3/ADC3
5 36
PB4/SS PA4/ADC4
6 35
PB5/MOSI PA5/ADC5
7 34
PB6/MISO PA6/ADC6
8 33
PB7/SCK PA7/ADC7
14 22
PD0/RXD PC0/SCL
15 23
PD1/TXD PC1/SDA
R1 16
PD2/INT0 PC2
24
150R 17 25
PD3/INT1 PC3
18 26
PD4/OC1B PC4
19 27
PD5/OC1A PC5
20 28
PD6/ICP1 PC6/TOSC1
21 29
PD7/OC2 PC7/TOSC2
13
XTAL1
12 32
XTAL2 AREF
9 30
RESET AVCC
ATMEGA8535
Using the Built-in ADC in AVR

The ATmega8535 features a 10-bit successive approximation ADC. The ADC is


connected to an 8-channel Analog Multiplexer which allows eight single-ended
voltage inputs constructed from the pins of Port A. The single-ended voltage
inputs refer to 0V (GND).

The ADC has a separate analog supply voltage pin, AVCC. AVCC must not differ
more than ±0.3V from VCC.

Internal reference voltages of nominally 2.56V or AVCC are provided On-chip

Operation

The ADC converts an analog input voltage to a 10-bit digital value through
successive approximation.

The minimum value represents GND and the maximum value represents the
voltage on the AREF pin minus 1 LSB.

Optionally, AVCC or an internal 2.56V reference voltage may be connected to


the AREF pin by writing to the REFSn bits in the ADMUX Register

The ADC is enabled by setting the ADC Enable bit, ADEN in ADCSRA.

Voltage reference and input channel selections will not go into effect until ADEN
is set.

The ADC generates a 10-bit result which is presented in the ADC Data
Registers,
ADCH and ADCL.
By default, the result is presented right adjusted, but can optionally be presented
left adjusted by setting the ADLAR bit in ADMUX.

Once ADCL is read, ADC access to data registers is blocked. This means that if
ADCL has been read, and a conversion completes before ADCH is read, neither
register is updated and the result from the conversion is lost. When ADCH is
read, ADC access to the ADCH and ADCL Registers is re-enabled.

The ADC has its own interrupt which can be triggered when a conversion
completes.
Starting a Conversion
1- Single Conversion Mode
A single conversion is started by writing a logical one to the ADC Start
Conversion bit, ADSC. This bit stays high as long as the conversion is in
progress and will be cleared by hardware when the conversion is completed. If a
different data channel is selected while a conversion is in progress, the ADC will
finish the current conversion before performing the channel change.

2- Auto Triggering Mode

Alternatively, a conversion can be triggered automatically by various sources.


Auto Triggering is enabled by setting the ADC Auto Trigger Enable bit, ADATE in
ADCSRA. The trigger source is selected by setting the ADC Trigger Select bits,
ADTS in SFIOR

When a positive edge occurs on the selected trigger signal, the ADC prescaler is
reset and a conversion is started. This provides a method of starting conversions
at fixed intervals. If the trigger signal still is set when the conversion completes, a
new conversion will not be started. If another positive edge occurs on the trigger
signal during conversion, the edge will be ignored.

3- Free Running Mode

Using the ADC Interrupt Flag as a trigger source makes the ADC start a new
conversion as soon as the ongoing conversion has finished. The ADC then
operates in Free Running mode, constantly sampling and updating the ADC Data
Register.

The first conversion must be started by writing a logical one to the ADSC bit in
ADCSRA.

Note:
ADSC can also be used to determine if a conversion is in progress.
The ADSC bit will be read as one during a conversion, independently of how the
conversion was started.

ADC Multiplexer Selection Register – ADMUX

• Bit 7:6 – REFS1:0: Reference Selection Bits

These bits select the voltage reference for the ADC, as shown in Table 84. If
these bits are changed during a conversion, the change will not go in effect until
this conversion is complete (ADIF in ADCSRA is set). The internal voltage
reference options may not be used if an external reference voltage is being
applied to the AREF pin.

• Bit 5 – ADLAR: ADC Left Adjust Result


The ADLAR bit affects the presentation of the ADC conversion result in the ADC
Data Register. Write one to ADLAR to left adjust the result. Otherwise, the result
is right adjusted. Changing the ADLAR bit will affect the ADC Data Register
immediately, regardless of any ongoing conversions.

• Bits 4:0 – MUX4:0: Analog Channel and Gain Selection Bits

The value of these bits selects which combination of analog inputs is connected
to the ADC. These bits also select the gain for the differential channels. See
Table 85 for details. If these bits are changed during a conversion, the change
will not go in effect until this conversion is complete (ADIF in ADCSRA is set).
ADC Control and Status Register A – ADCSRA

• Bit 7 – ADEN: ADC Enable


Writing this bit to one enables the ADC. By writing it to zero, the ADC is turned
off. Turning the ADC off while a conversion is in progress, will terminate this
conversion.

• Bit 6 – ADSC: ADC Start Conversion


In Single Conversion mode, write this bit to one to start each conversion. In Free
Running mode, write this bit to one to start the first conversion. The first
conversion after
ADSC has been written after the ADC has been enabled, or if ADSC is written at
the same time as the ADC is enabled, will take 25 ADC clock cycles instead of
the normal 13. This first conversion performs initialization of the ADC.
ADSC will read as one as long as a conversion is in progress. When the
conversion is complete, it returns to zero. Writing zero to this bit has no effect.

• Bit 5 – ADATE: ADC Auto Trigger Enable


When this bit is written to one, Auto Triggering of the ADC is enabled. The ADC
will start a conversion on a positive edge of the selected trigger signal. The
trigger source is selected by setting the ADC Trigger Select bits, ADTS in SFIOR.

• Bit 4 – ADIF: ADC Interrupt Flag


This bit is set when an ADC conversion completes and the Data Registers are
updated.
The ADC Conversion Complete Interrupt is executed if the ADIE bit and the I-bit
in SREG are set. ADIF is cleared by hardware when executing the corresponding
interrupt handling vector. Alternatively, ADIF is cleared by writing a logical one to
the flag.
Beware that if doing a Read-Modify-Write on ADCSRA, a pending interrupt can
be disabled. This also applies if the SBI and CBI instructions are used.

• Bit 3 – ADIE: ADC Interrupt Enable


When this bit is written to one and the I-bit in SREG is set, the ADC Conversion
Complete Interrupt is activated.

• Bits 2:0 – ADPS2:0: ADC Prescaler Select Bits


These bits determine the division factor between the XTAL frequency and the
input clock to the ADC.
• ADC9:0: ADC Conversion Result
These bits represent the result from the conversion,

Special Function IO Register –


SFIOR

• Bit 7:5 – ADTS2:0: ADC Auto Trigger Source


If ADATE in ADCSRA is written to one, the value of these bits selects which
source will trigger an ADC conversion. If ADATE is cleared, the ADTS2:0 settings
will have no effect.

A conversion will be triggered by the rising edge of the selected interrupt flag.
ADC.c
#include <mega8535.h>

// Alphanumeric LCD Module functions


#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>

#include <delay.h>
#include <stdlib.h>

#define ADC_VREF_TYPE 0x00

// ADC interrupt service routine


interrupt [ADC_INT] void adc_isr(void)
{
unsigned int adc_data;
float analog;
float temperature;
unsigned int temperature1;
char str[25];
lcd_clear();
// Read the AD conversion result
adc_data=ADCW;
// Place your code here
analog =adc_data*5;
analog = analog /1023;
temperature= analog/0.01;
temperature1 = (unsigned int) temperature;
itoa(temperature1,str);
lcd_puts(str);
}

// Declare your global variables here

void main(void)
{

// ADC initialization
// ADC Clock frequency: 500.000 kHz
// ADC Voltage Reference: AREF pin
// ADC High Speed Mode: Off
Page 1
ADC.c
// ADC Auto Trigger Source: None
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x89;
SFIOR&=0xEF;

// initialize the LCD for


// 2 lines & 16 columns
lcd_init(16);
// go on the second LCD line
lcd_gotoxy(3,1);

// display the message


lcd_putsf("Hello world");
delay_ms(500);
// Global enable interrupts
#asm("sei")

ADCSRA|=0x40;
while (1)
{
delay_ms(50);
ADCSRA|=0x40;
};
}

Page 2
LCD1
LM016L

1
VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7

U2
E

24.0 LM35
1
2
3

4
5
6

7
8
9
10
11
12
13
14

U1
1 40 2
PB0/T0/XCK PA0/ADC0 VOUT
2 39
PB1/T1 PA1/ADC1
3 38
PB2/AIN0/INT2 PA2/ADC2
4 37
PB3/AIN1/OC0 PA3/ADC3
5 36 3
PB4/SS PA4/ADC4
6 35
PB5/MOSI PA5/ADC5
7 34
PB6/MISO PA6/ADC6
8 33
PB7/SCK PA7/ADC7
14 22
PD0/RXD PC0/SCL
15 23
PD1/TXD PC1/SDA
16 24
PD2/INT0 PC2
17 25
PD3/INT1 PC3
18 26
PD4/OC1B PC4
19 27
PD5/OC1A PC5
20 28
PD6/ICP1 PC6/TOSC1
21 29
PD7/OC2 PC7/TOSC2
13
XTAL1
12 32
XTAL2 AREF
9 30
RESET AVCC
ATMEGA8535
Accessing the EEPROM
Accessing the AVR internal EEPROM is accomplished using global
variables, preceded by the
keyword eeprom

Example:

/* The value 1 is stored in the EEPROM during chip programming */


eeprom int alfa = 1;
eeprom char beta;
eeprom long arrayl[5];
/* The string is stored in the EEPROM during chip programming * /
eeprom char string[]="Hello";
void main(void)
{
int i;
/* Write directly the value 0x55 to the EEPROM */
alfa=0x55;
/* Read directly the value from the EEPROM */
i=alfa;
}

I
Serial Communication

The name "serial" comes from the fact that a serial port "serializes" data. That is, it takes
a byte of data and transmits the 8 bits in the byte one at a time

Typical character frame

Baud Rate (Communication Speed)

Baud rate is a measure of how fast data is moving between instruments that use serial
communication.

Serial Socket

Male Female

Microcontroller to PC serial connection


serial.c
/*****************************************************
Chip type : ATmega8535
Program type : Application
Clock frequency : 11.059200 MHz
/*****************************************************

#include <mega8535.h>

// Standard Input/Output functions


#include <stdio.h>
#include <delay.h>
#include <stdlib.h>

// Declare your global variables here

void main(void)
{
// Declare your local variables here
unsigned int n;
char str[15]={'m','b','v','c','1'};
float x ;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x47;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

while (1)
{
delay_ms(500);

printf("\n Please enter an unsigned integer: ");


scanf("%u", &n);
printf("The integer was %u\n\n ", n);
Page 1
serial.c

delay_ms(500);
printf("\n Please enter a char: ");
scanf("%c", &str[2]);
printf(" the character 2 in string is %c\n ",
str[2]);

delay_ms(500);
x =1.7;
ftoa(x,2,str);
printf(" the float is %s\n ",str);

delay_ms(500);
printf("\n Please enter a string: ");
scanf("%s", &str);
printf(" the string is %s\n ", str);
delay_ms(500);

printf("\n Please enter a float: ");


scanf("%s", &str);
x= atof(str);

delay_ms(500);
ftoa(x,5,str);
printf(" the float is %s\n ",str);
delay_ms(500);
};
}

Page 2
AVR Timers

Operation And Register


Description

Introduction
„ The AVR has different Timer types. Not all AVRs have
all Timers.

„ The timers basically only count clock cycles.

„ The timer clock can be


… equal to the system clock.
… slowed down by the prescaler first.

„ When using the prescaler you can achieve greater timer


values

1
The Prescaler
„ It’s an internal device that divides the
system clock on preset division values to
slow the i/p pulses to the timer (make the
timer count more pulses)

„ The prescaler can be set to 1, 8, 64, 256


or 1024 .

Examples
„ Ex1: if timer use no prescaler and the
Microcontroller works on 1MHz the timer
will count 1Million clock / second

„ EX2: if timer use a prescaler set to 8 and


the Microcontroller works on 1MHz the
timer will count 1Million/8 = 125000 clock /
second

2
Counters
„ The AVR timer can act as a timer or a
counter.
„ Timers count clock pulses (with or without
prescaler)
„ Counters count external pulses on i/o pins
(without prescaler)
… Ex1:T0 (pin 1, PortB.0) for counter 0
… Ex2:T1 (pin 2, PortB.1) for counter 1

Where does it count


„ Timers and counters has registers made to hold
their count .

„ TCNT register for each timer/counter unit is the


register that this timer/counter unit will count in.

… Ex1: TCNT0 for timer/counter 0 (8 bit register)


… Ex2: TCNT1 for timer/counter 1 (16 bit register)
… Ex3: TCNT2 for timer/counter 2 (8 bit register)

3
„ ATmega8535 has 3 timers
… 16 bit timer 1 (because it counts in 16 bits
register TCNT1)
… 8 bit timer 0 (because it counts in 8 bits
register TCNT0)
… 8 bit timer 2 (because it counts in 8 bits
register TCNT2)

Modes of Operation
„ Normal Mode
… In this mode the counting starts from 0 to the MAX
of the counting register.
… Then restarts from the BOTTOM (0).

„ Clear Timer on Compare Match (CTC) Mode


… In this mode the counting starts from 0 to the TOP.
… Then restarts from the BOTTOM (0).
… The TOP is a value placed in the Compare Match
registers or other fixed values.

4
„ Note: the compare match registers are registers
that holds values set buy user

„ The timer compares its current value to it and in


this mode when the timer value reaches the
compare value the timer clears itself and start
from 0.

„ The compare value can be changed by putting it


in OCR registers.
… OCR1A for timer 1
… OCR0 for timer 0
… OCR2 for timer 2

Timer Events
„ 1- Over flow event:
… It happens when the timer finishes a full cycle
and start a new one.
… It generates an overflow interrupt if the
interrupt is enabled.
… Each timer has its own overflow interrupt with
its own enable , flag and ISR.

5
2-Compare Match event
… Ithappens when the timer value matches the
value in the OCR register.

… Itgenerates a compare match interrupt if the


interrupt is enabled.

… Each timer has its own compare match


interrupt with its own enable , flag and ISR.

… PWM compare match O/P can be enabled or


disabled.

3-Input Capture Event


„ If the input capture is enabled and a pulse
is detected on ICP pin the value of the
timer count is stored in the Input Capture
register.

6
Applications
„ Make a motor work for 2 seconds then
stop for 3 seconds and so on.
T : desired delay time
„ 1sÆf/pre F : microcontroller frequency

„ TÆP P: no of pulses that timer counts in


desired delay time T
„ P = (T*F)/PRE PRE: prescaler value

„ For 2 seconds delay 1Mhz frequency and


prescaler 64

„ P= 2*1000000/64= 31250 pulses

„ The timer must count 31250 pulse to pass


2 seconds then it should generate an
interrupt.

„ For 3 seconds P= 46875 pulses

7
CLK
CE
RST

U1
1 40
PB0/T0/XCK PA0/ADC0
2 39
PB1/T1 PA1/ADC1
3 38
PB2/AIN0/INT2 PA2/ADC2
4 37
PB3/AIN1/OC0 PA3/ADC3
5 36
PB4/SS PA4/ADC4
6 35
PB5/MOSI PA5/ADC5
7 34
PB6/MISO PA6/ADC6
8 33
PB7/SCK PA7/ADC7
14 22
R1
PD0/RXD PC0/SCL
15 23 110R
PD1/TXD PC1/SDA
16 24
PD2/INT0 PC2
17 25
PD3/INT1 PC3
18 26
PD4/OC1B PC4
19 27
PD5/OC1A PC5
20 28
PD6/ICP1 PC6/TOSC1
21 29
PD7/OC2 PC7/TOSC2
13
XTAL1
12 32
XTAL2 AREF
9 30
RESET AVCC
ATMEGA8535
timer.c

#include <mega8535.h>
// Declare your global variables here
unsigned int temp1=31250;
unsigned int temp2=46875;

// Timer 1 output compare A interrupt service routine


interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Place your code here
if (PORTC == 1)
{
PORTC=0;
OCR1A= temp2;
}
else
{
PORTC=1;
OCR1A= temp1;
}

void main(void)
{
// Declare your local variables here

// Port C initialization
// Func7=out Func6=out Func5=out Func4=out Func3=out
Func2=out Func1=out Func0=out
// State7=T State6=T State5=T State4=T State3=T State2=T
State1=T State0=T
PORTC=0x00;
DDRC=0xff;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 15.625 kHz
// Mode: CTC top=OCR1A
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
Page 1
timer.c
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x0B;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x7A;
OCR1AL=0x12;
OCR1BH=0x00;
OCR1BL=0x00;

// Global enable interrupts


#asm("sei")
PORTC =0;
while (1)
{
// Place your code here

};
}

Page 2
6/29/2010

General Description of permanent magnet 
stepper motors
• 1‐Introduction:‐
9 Stepper motors are a kind of motors 
St t ki d f t
that rotate in precise steps. 

9 There are different types of stepper 
motors as Permanent magnet motors, 
V i bl R l
Variable Reluctance motors and  d
Hybrid motors. 
Figure1:Stepper motor

2‐ Permanent Magnet Stepper Motors:
9 Stepper Motors have several features which distinguish them 
from AC Motors, and DC Servo Motors including

1‐Brushless

brushes create sparks, undesirable in certain environments 
Space missions, for example 

2‐Holding Torque

Steppers have very good low speed and holding torque

1
6/29/2010

3‐Open loop positioning

Steppers can run 'open‐loop' without the 
y
need for any kind of encoder to determine the 
shaft position. Closed loop systems‐ systems 
that feed back position information
4‐Load Independent

rotation speed of a stepper is independent of 
rotation speed of a stepper is independent of
load, provided it has sufficient torque to 
overcome slipping 

PERMENANT MAGNET STEPPER MOTOR 
TYPES

1‐ Bipolar stepper motors 

2‐ Unipolar stepper motors

2
6/29/2010

2.1 Bipolar Stepper Motors
9The Bipolar Stepper motor has 2 coils. The 
coils are identical and are not electrically
coils are identical and are not electrically 
connected 

9The coils are activated, in sequence, to attract 
the rotor 

2.2 Unipolar Stepper Motors
• The Unipolar Stepper motor 
has 2 coils, simple lengths of 
has 2 coils simple lengths of
wound wire. The coils are 
identical and are not 
electrically connected. 
• Each coil has a center tap ‐ a 
wire coming out from the coil
wire coming out from the coil 
that is midway in length 
between its two terminals. 

Figure2: Unipolar stepper motor

3
6/29/2010

FINDING THE CENTER TAP IN A UNIPOLAR 
STEPPER
• Because of the long length of the wound wire, 
it h
it has a significant resistance (and inductance). 
i ifi t i t ( di d t )
You can identify the center tap by measuring 
resistance with a suitable ohm‐meter (capable 
of measuring low resistance <10 ohm) The 
resistance from a terminal to the center tap is 
half the resistance from the two terminals of a 
coil. 

3‐ Driving Unipolar Stepper Motors
• There are three ways to drive stepper motors 
(
(one phase on, two phase on or Half step), 
h t h H lf t )
each one has some advantages and 
disadvantages.
1‐One phase on
2‐Two Phases On Mode
2‐Two Phases On Mode
3‐Half Step Mode 

4
6/29/2010

3.1 One phase on
• In one phase mode, 
Step 1a 1b 2a 2b
each successive coil is
each successive coil is 
energized in turn.  1 1 0 0 0
• One phase mode 
produces Smooth  2 0 1 0 0
rotations and the 
lowest power
lowest power  3 0 0 1 0
consumption of the 
three modes.  4 0 0 0 1

4‐ How to find the sequence of any stepper 
motor coils
• There are two stages to 
sort out which wire is 
sort out which wire is
which in a 5 or 6 wire 
unipolar stepper motor 
1‐ Isolate the Common 
Power wire(s)
2‐ Identify the wires to the 
coils by applying pulses 
il b l i l
and analyzing the motor 
behavior 

5
6/29/2010

ISOLATION AND POWER INTERFACE

You might also like