+ All Categories
Home > Documents > AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN...

AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN...

Date post: 25-Apr-2020
Category:
Upload: others
View: 15 times
Download: 2 times
Share this document with a friend
34
AVR Programming in C Lecture# 12 Microprocessor System and Interfacing Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 1
Transcript
Page 1: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

AVR Programming

in CLecture# 12

Microprocessor System and Interfacing

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

1

Page 2: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Sessional - 1

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

2

Average Marks

Question 1 – 5.74

Question 2 – 2.33

Overall – 6.92

Page 3: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Example 1

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

3

#include <avr/io.h>

#define serpin 3

int main (void)

{

unsigned char conbyte = 0x44;

unsigned char regALSB;

unsigned char x;

regALSB = conbyte;

DDRC |= (1 << serpin)

for (x = 0; x < 8; x ++)

{

if (regALSB & 0x01)

PORTC |= (1<<serpin);

else

PORTC &= ~(1<<serpin);

regALSB = regALSB >> 1;

}

return 0;

}

Write and AVR C program to send the value 44H serially one bit at

a time via PORTC, pin 3. The LSB should go first.

conbyte 0 1 0 0 0 1 0 0

regALSB

x 0 0 0 0 0 0 0 0

DDRC 0 0 0 0 1 0 0 0

regALSB 0 1 0 0 0 1 0 0

0x01 0 0 0 0 0 0 0 1

regALSB&0x01 0 0 0 0 0 0 0 0

1<<serPin 0 0 0 0 1 0 0 0

False Condition

~(1<<serpin) 1 1 1 1 0 1 1 1

PORTC & U U U U 0 U U U

regALSB >> 1 0 0 1 0 0 0 1 0

regALSB 0 1 0 0 0 1 0 0

Page 4: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Example 1

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

4

#include <avr/io.h>

#define serpin 3

int main (void)

{

unsigned char conbyte = 0x44;

unsigned char regALSB;

unsigned char x;

regALSB = conbyte;

DDRC |= (1 << serpin)

for (x = 0; x < 8; x ++)

{

if (regALSB & 0x01)

PORTC |= (1<serpin);

else

PORTC &= ~(1<<serpin);

regALSB = regALSB << 1;

}

return 0;

}

Write and AVR C program to send the value 44H serially one bit at

a time via PORTC, pin 3. The LSB should go first.

conbyte 0 1 0 0 0 1 0 0

regALSB 0 1 0 0 0 1 0 0

x 0 0 0 0 0 0 0 1

DDRC 0 0 0 0 1 0 0 0

regALSB 0 0 1 0 0 0 1 0

0x01 0 0 0 0 0 0 0 1

regALSB&0x01 0 0 0 0 0 0 0 0

1<<serPin 0 0 0 0 1 0 0 0

False Condition

~(1<<serpin) 1 1 1 1 0 1 1 1

PORTC & U U U U 0 U U U

regALSB >> 1 0 0 0 1 0 0 0 1

Page 5: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Example 1

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

5

#include <avr/io.h>

#define serpin 3

int main (void)

{

unsigned char conbyte = 0x44;

unsigned char regALSB;

unsigned char x;

regALSB = conbyte;

DDRC |= (1 << serpin)

for (x = 0; x < 8; x ++)

{

if (regALSB & 0x01)

PORTC |= (1<serpin);

else

PORTC &= ~(1<<serpin);

regALSB = regALSB << 1;

}

return 0;

}

Write and AVR C program to send the value 44H serially one bit at

a time via PORTC, pin 3. The LSB should go first.

conbyte 0 1 0 0 0 1 0 0

regALSB 0 1 0 0 0 1 0 0

x 0 0 0 0 0 0 1 0

DDRC 0 0 0 0 1 0 0 0

regALSB 0 0 0 1 0 0 0 1

0x01 0 0 0 0 0 0 0 1

regALSB&0x01 0 0 0 0 0 0 0 1

1<<serPin 0 0 0 0 1 0 0 0

True Condition

(1<<serpin) 0 0 0 0 1 0 0 0

PORTC | U U U U 1 U U U

regALSB >> 1 0 0 0 0 1 0 0 0

Page 6: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Example 1

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

6

#include <avr/io.h>

#define serpin 3

int main (void)

{

unsigned char conbyte = 0x44;

unsigned char regALSB;

unsigned char x;

regALSB = conbyte;

DDRC |= (1 << serpin)

for (x = 0; x < 8; x ++)

{

if (regALSB & 0x01)

PORTC |= (1<serpin);

else

PORTC &= ~(1<<serpin);

regALSB = regALSB << 1;

}

return 0;

}

Write and AVR C program to send the value 44H serially one bit at

a time via PORTC, pin 3. The LSB should go first.

conbyte 0 1 0 0 0 1 0 0

regALSB 0 1 0 0 0 1 0 0

x 0 0 0 0 0 0 1 1

DDRC 0 0 0 0 1 0 0 0

regALSB 0 0 0 0 1 0 0 0

0x01 0 0 0 0 0 0 0 1

regALSB&0x01 0 0 0 0 0 0 0 0

1<<serPin 0 0 0 0 1 0 0 0

False Condition

~(1<<serpin) 1 1 1 1 0 1 1 1

PORTC | U U U U 0 U U U

regALSB >> 1 0 0 0 0 0 1 0 0

Page 7: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

AVR Hardware

Connections

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

7Lecture# 12

Microprocessor System and Interfacing

Page 8: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

ATMEGA32 PIN Connection

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

8

XTAL1 and XTAL2

Crystal oscillator is connected to input pins XTAL1 and

XTAL2

Needs two capacitors which are connected to ground

ATmega32 can have speeds in range 0MHz to 16MHz

Clock options can be selected using FUSE bits

Page 9: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

ATMEGA32 PIN Connection

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

9

RESET

Reset is Active LOW

When low pulse is applied microcontroller will terminate all

activities

Contents of SRAM and registers are cleared

All ports will be input (as DDRs are 0)

CPU will start from 0x00000 after RESET

Page 10: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

ATMEGA32 PIN Connection

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

10

Uses a momentary switch for reset

circuitry

Put a capacitor between RESET and

GND to filter noise

Diode protects the RESET pin from

being powered by capacitor when

power is off

Page 11: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

AVR Fuse Bits

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

11

Used to choose features of AVR

Incorrect programming can cause the system failure

SPIEN bit to 0, disables SPI programming mode. Chip cannot

be programmed now

Using lock bits, access to FLASH Memory, Adds security

Page 12: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

AVR Fuse Bits

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

12

Fuse bits and Oscillator Clock Source

Controlled using CKSEL0 – CKSEL3

Default Choice is internal clock oscillator

No external crystal connected

3% inaccuracy (not suitable for precise timing applications)

CKSEL0 – CKSEL3 Frequency

0001 1 MHz

0010 2 MHz

0011 4 MHz

0100 8 MHz

Page 13: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

AVR Fuse Bits

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

13

Fuse bits and Oscillator Clock Source

External RC oscillator internal clock oscillator

Resistor and Capacitor to XTAL1 pin

RC determine clock speed using

𝑓 =1

3𝑅𝐶

Variable clock can be obtained ?

Potentiometer

CKSEL0 – CKSEL3 Frequency

0101 <0.9 MHz

0110 0.9 – 3.0 MHz

0111 3.0 – 8.0 MHz

1000 8.0 – 12.0 MHz

Page 14: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

AVR Fuse Bits

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

14

Fuse bits and Oscillator Clock Source

By setting CKSEL3 – CKSEL0 to 0000 we can use external

clock source

Connect XTAL1 and XTAL2 pins to an external crystal

oscillator

A more powerful clock source

Page 15: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

AVR Timer

Programming in

Assembly and CLecture# 12

Microprocessor System and Interfacing

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

15

Page 16: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

AVR Timer Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

16

Time delay based on events

Counter registers are used for this purpose

External source to the clock of register, register increments

at event and time delay can be calculated

Oscillator can be connected to clock pin, at each oscillator

tick (a known time delay), register is incremented

Flag is set when counter register overflows

Page 17: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

AVR Timer Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

17

One way to program is

Clear counter at start

Wait until counter reaches a certain number

Second way is

Load counter register with a value

Wait until the register overflows

Different timers are available

8-bit and 16-bit wide

ATMEGA32 timers, Timer 0, 2 (8-bit) and Timer 2 (16-bit)

Page 18: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

AVR Timer Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

18

Every timer needs a clock pulse to tick

Internal clock source

Frequency of crystal oscillator is fed to timer

Used for time delay generation

Called a timer

External clock source

Feeds pulses through AVR pins

Called a counter

Page 19: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

AVR Timer Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

19

Basic Registers

TCNTn (timer / counter):

A counter

At reset it is 0

Counts up at each pulse

Load/read its value using

IN/OUT

TOVn (timer overflow) flag:

At timer overflows this flag is

set

Page 20: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

AVR Timer Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

20

Basic Registers

TCCRn (timer/counter control

register):

Controls the mode of

operation

OCRn (output compare

register):

Compares OCR with TCNT

If equal OCF (output

compare flag, in TIFR

register) will be set

Page 21: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Timer0 Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

21

Page 22: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Timer0 Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

22

TCCR0 register

Used for control of timer0

CS02:CS00 (Timer0 clock source)

Bit No 7 6 5 4 3 2 1 0

Name FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00

D2 D1 D0 Timer0 Clock Selector

0 0 0 No Clock Source (Timer / Counter Stopped)

0 0 1 clk (no prescaling)

0 1 0 clk / 8

0 1 1 clk / 64

1 0 0 clk / 256

1 0 1 clk / 1024

1 1 0 External clock source on T0 pin (Falling Edge)

1 1 1 External clock source on T0 pin (Rising Edge)

Page 23: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Timer0 Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

23

WGM00:01

Four different modes

D6 D3 Timer0 mode selector

0 0 Normal

0 1 CTC (Clear Timer on Compare Match)

1 0 PWM, phase correct

1 1 Fast PWM

Page 24: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Timer0 Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

24

TIFR (Timer/Counter Interrupt Flag Register)

Contains flags of different timers

TOV0

1 = Counter is overflow

0 = No overflow

If overflow occurs, flag remains 1, unless program clears it

To clear the flag write 1 to it

Bit No 7 6 5 4 3 2 1 0

Name OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0

Page 25: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Timer0 Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

25

Normal Mode Operation

Contents increment on each clock

Rolls over after $FF to $00

Sets TOV0 flag bit in TIFR register

Page 26: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Timer0 Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

26

Steps to Program in Normal Mode

Load TCNT0 register with initial value

Load TCCR0 register to control mode (8-bit or 16-bit)

and pre-scalar option

Keep monitoring timer overflow flag (TOV0)

Stop timer by disconnecting the clock source

Clear TOV0 flag for the next round

Go back to step 1 to load TCNT0 again

Page 27: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Example 2

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

27

- Set PORTB.5 as output

- Call delay function

- Toggle PORTB.5

- CALL Delay again in loop

- Load TCNT register

- Set TCCR0 register

- Read Flag from TIFR

- If flag is 0, keep counting

- Stop timer if flag is set

- Clear TOV0 flag in TIFR

-

Create a 50% duty cycle on PORTB.5 bit. Timer0 is used to

generate the time delay.

Page 28: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Example 2

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

28

.INCLUDE “M32DEF.INC”

LDI R20, HIGH (RAMEND)

OUT SPH, R20

LDI R20, LOW (RAMEND)

OUT SPL, R20

LDI R16, 1<<5 ; Set value to toggle PORTB.5

SBI DDRB, 5 ; Set PortB.5 output

CBI PORTB, 5 ; Clear PB5

BEGIN: CALL DELAY ; Call timer delay

SBI PORTB, 5 ; set PB5

CALL DELAY

CBI PORTB, 5 ; clear PB5

RJMP BEGIN

DELAY:

Create a 50% duty cycle on PORTB.5 bit. Timer0 is used to

generate the time delay.

Page 29: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Example 2

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

29

DELAY:

LDI R10, 0xF2 ; setting counter initial value

OUT TCNT0, R10 ; load timer0

LDI R20, 0x01

OUT TCCR0, R20 ; Timer0, Normal Mode,

; internal clock, no pre-scalar

AGAIN: IN R20, TIFR ; read TIFR Flag

SBRS R20, TOV0 ; skip if TOV0 flag is set

RJMP AGAIN

LDI R20, 0x00

OUT TCCR0, R20 ; Stop Timer

LDI R20, 1<<TOV0 ;

OUT TIFR, R20 ; clear TOV0 for next cycle

RET

Create a 50% duty cycle on PORTB.5 bit. Timer0 is used to

generate the time delay.

Page 30: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Timer0 Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

30

Finding values to be loaded into (TCNT0)

Calculate the period using

𝑇𝑐𝑙𝑜𝑐𝑘 =1

𝐹𝑇𝑖𝑚𝑒𝑟; 𝐹𝑇𝑖𝑚𝑒𝑟 is freq. of clock

Divide the desired time delay with 𝑇𝑐𝑙𝑜𝑐𝑘 , which

gives no of clocks (n) required.

Perform 256 – n to obtain value (V) to store in TCNT0

register

Convert the value in hex and store in TCNT0 register

Page 31: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Timer0 Programming

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

31

Pre-scalar and generating a larger time delay

Time delay depends on two factors

Crystal Frequency

Timer’s 8-bit register (set TCNT0 = 0 and count to 255)

What to do for values for delays greater than 255 cc ?

Use pre-scalar and divide clock by 8 – 1024.

Clock is divided before it is fed to timer

Page 32: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Example 3

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

32

Assuming XTAL1 = 8MHz, write a program in Assembly to generate

a square wave of 125 Hz frequency on pin PORTB.3. Use Timer0,

Normal mode, with prescaler = 256.

Calculate steps required:

(a) T = 1 / 125 Hz = 8 ms, the period of square wave

(b) 1/2 of it for high and low portions of pulse = 4 ms

(c) (4 ms / .125 us) = 32000

(d) 32000 / 256 = 125

(e) 256 – 125 = 131 (0x83)

(f) TCNT0 = 0x83

What will be the values for TCNT and TCCR registers ?

Page 33: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Example 3

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

33

What will be the values for TCNT and TCCR registers ?

0 0 0 0 0 1 0 0

Page 34: AVR Programming in Csaqazi.com/EEE342/FA19_MP_Lecture12_20191022_AVR_Hardware_… · ATMEGA32 PIN Connection Dr. Sohaib Ayyaz Qazi COMSATS University Islamabad 8 XTAL1 and XTAL2 Crystal

Example 3

Dr. Sohaib Ayyaz QaziCOMSATS University Islamabad

34

DELAY:

LDI R10, ??? ; setting counter initial value

OUT TCNT0, R10 ; load timer0

LDI R20, ???

OUT TCCR0, R20 ; Timer0, Normal Mode,

; internal clock, prescaler 256

AGAIN: IN R20, TIFR ; read TIFR Flag

SBRS R20, TOV0 ; skip if TOV0 flag is set

RJMP AGAIN

LDI R20, 0x00

OUT TCCR0, R20 ; Stop Timer

LDI R20, 1<<TOV0 ;

OUT TIFR, R20 ; clear TOV0 for next cycle

RET

Assuming XTAL1 = 8MHz, write a program in Assembly to generate

a square wave of 125 Hz frequency on pin PORTB.3. Use Timer0,

Normal mode, with prescaler = 256.


Recommended