You are on page 1of 27

CSE362 / ECE441

Digital System Interface

Lecture – 9

The AVR timer programming

Dr. Ahmed Abouelmagd


- When we want to count an event, we connect the external event source
to the clock pin of the counter register. Then when an event occurs
externally, the content of the counter is incremented,
- In this way, the content of the counter represents how many times an
event has occurred.
- When we want to generate time delays, we connect the oscillator to the
clock pin of the counter.
- So, when the oscillator ticks, the content of the counter is incremented,
- As a result, the content of the counter register represents how many
ticks have occurred from the time we have cleared the counter.
- Since the speed of the oscillator in a microcontroller is known, we can
calculate tick period , and from the content of counter register we will
know how much time has elapsed.
Example:
In a microcontroller with an oscillator of 1MHz ; the content of the
counter register increments once per second. So, if we want a time delay of
100 microsecond, we should clear the counter and wait until it becomes
equal to 100.
Hex code
Example 1:
Write a C program to toggle only the AVR PORTB.4 bit
continuously every 70 μs Use: Timer0, Normal mode, 1:8
prescaler to create the delay and XTAL frequency = 8 MHz.

Solution:

0100 0110
AVR Interrupts and Polling services

A single microcontroller can serve several devices in two


methods:
1. Interrupt method:
- Whenever any device needs the microcontroller’s service,
the device notifies the microcontroller by sending an
interrupt signal.
- Upon receiving an interrupt signal, the microcontroller
stops whatever it is doing and serving the device.
2. Polling method:
- The microcontroller continuously monitors the states of a
given device.
- When the status condition is met, it performs the service.
- After that it moves on to monitor the next device until each
one is serviced.
Interrupts versus polling

Interrupts Polling
User Priority Assign Can not assign
It checks all devices in
a round trip.
Time No waste of time Waste time because It
polling devices that do
not need service.

Ignore (mask request) It can It can not because it


checks all devices.
Example 2:

For AVR microcontroller, using Timer0 and Timer1 interrupts,


write a C program in which:
1. PORTA counts up every time Timer1 overflows. It overflows
once per second.
2. A pulse is fed into Timer0 where Timer0 is used as counter and
counts up. Whenever the counter reaches 200, it will toggle the
pin PORTB.6.
For AVR microcontroller, using Timer0 and Timer1 interrupts,
write a C program in which:
1. PORTA counts up every time Timer1 overflows. It overflows
once per second.
2. A pulse is fed into Timer0 where Timer0 is used as counter and
counts up. Whenever the counter reaches 200, it will toggle the
pin PORTB.6.
Solution:
For AVR microcontroller, using Timer0 and Timer1 interrupts,
write a C program in which:
1. PORTA counts up every time Timer1 overflows. It overflows
once per second.
2. A pulse is fed into Timer0 where Timer0 is used as counter and
counts up. Whenever the counter reaches 200, it will toggle the
pin PORTB.6.
AVR Parallel and Serial ports

- The parallel port:


A parallel port allows data to be sent simultaneously down
several cables at once.
- The serial port:
A serial port is a single line that allows a PC to transmit
or receive data one bit at a time. Compared to a parallel
port, the data transfer rate of a serial port is slower.
USART …..universal synchronous asynchronous receiver
transmitter.
- The synchronous mode can be used to transfer data
between the AVR and external peripherals such as ADC
and EEPROMs.
- The asynchronous mode is used to connect the AVR-based
system to the X86PC serial port.
Five registers associated with the USART:
- UDR ----- USART Data Register
- UCSRA, UCSRB, USCRC ----- USART Control Status
Registers
- UBRR----- USART Baud Rate Register.

Fosc ---- oscillator frequency


X---- is the value we load into UBRR register.
Example 3:
Write a C program for the AVR to send the message “The Earth is
but One Country.” To the serial port continuously at 9600 baud,
Use 8-bit data and 1 stop bit. Assume XTAL-8MHz.
Remark: "9600 baud" means that the serial port is capable of
transferring a maximum of 9600 bits per second.
Write a C program for the AVR to send the message “The Earth is
but One Country.” To the serial port continuously at 9600 baud,
Use 8-bit data and 1 stop bit. Assume XTAL-8MHz.

You might also like