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

Sheet 5

Problem 1:
1- Using Timer0 and non-inverted Fast PWM mode, write a program that
generates a wave with frequency of 62.5 kHz and duty cycle of 60%. Assume
XTAL= 16 MHz.

Answer:
Fast PWM
F=fosc / (256*Np) =16 MHZ/(256*Np)
At f = 62.5 KHz Np=1
Non-inverted
D=((OCR0+1)/256)*100
At D=60% OCR0=152.6 ≈ 153
Code:
#include “avr/io.h”
#define F_CPU 16000000UL
Int main() {
DDRB|= (1<<3);
TCCR)=(1<<WGM00)|((1<<WGM01)|(1<<COM01)|(1<<SC00);
OCR0=153;
while(1);
return 0;
}
2- Using Timer0 and inverted Phase correct PWM mode, write a program that
generates a wave with frequency of 1960 Hz and duty cycle of 20%. Assume
XTAL = 1 MHz.

Answer:
Phase correct PWM
F=fosc / (510*Np) =1 MHZ/(510*Np)
At f = 1960 Hz Np=1
inverted
D=((255-OCR0)/255)*100
At D=20% OCR0=204
Code:
#include “avr/io.h”
#define F_CPU 1000000UL
Int main() {
DDRB|= (1<<3);
TCCR)=(1<<WGM00)|(1<<COM01)|(1<<COM00)|( (1<<SC00);
OCR0=204;
while(1);
return 0;
}
3- As shown in the figure, a switch is connected to PB0. Using Timer0 and non-
inverted Phase correct PWM, write a program that generates a wave with
frequency of f = 2 KHz. When the switch is closed the duty cycle
is 20%, and when it is open the duty cycle is 85%. Assume XTAL = 8 MHz.
Answer:
Phase correct PWM
F=fosc / (510*Np) =8 MHZ/(510*Np)
At f = 2 KHz Np=8
Non-inverted
D=(OCR0/255)*100
At D=20% OCR0=51
At D=85% OCR0=216.75 ≈217
Code:
#include “avr/io.h”
#define F_CPU 8000000UL
#define bitisset(reg,bit) ((reg)&(1<<(bit))
#define bitisclear(reg,bit) !((reg)&(1<<(bit))
Void PWM_init()
{
DDRB|= (1<<3);
TCCR0=(1<<WGM00)|(1<<COM01)| (1<<SC00);
Int main() {
DDRB &=~ (1<<0);
PORTB |= (1<<0);
PWM_init();
While(1){
If (bitisset(PINB,PB0){
OCR0=51;
}
Else {
OCR0=217;
}
}
return 0;
}
4- Why do we put a driver between the microcontroller and the DC motor?
How do we change a DC motor’s rotation direction?
Answer:
to change the direction and speed of the motor and to provide a sufficient
current to the motor.
By setting IN1 HIGH and IN2 LOW will drive the motor in a specific
direction , while setting IN1 LOW and IN2 HIGH will drive it in the reverse
direction.
Problem 2:
1- Show the framing of the letter ASCII ‘Z’ (0101 1010), no parity, 1 stop bit.
Answer: 1 0101 1010 0

2- For XTAL = 10 MHz, find the UBRR value (in both decimal and hex) for
each of the following baud rates. (a) 9600 (b) 4800 (c) 1200
Answer:
UBRR=(fosc/(16*baud rate)-1)
At baud rate=9600 , fosc = 10MHz
UBRR= 64.1≈ 64= 0x40

3- What is the baud rate if we use UBRR = 15 to program the baud rate?
Assume XTAL = 10 MHz.
Answer:
UBRR=(fosc/(16*baud rate)-1)
Baud rate =39062.5

4- Find the UBRR for the following baud rates if XTAL = 16 MHz and U2X
= 1 (a) 9600 (b) 19200 (c) 38400 (d) 57600
Answer:
UBRR=(fosc/(8*baud rate)-1)
At baud rate=9600 , fosc = 16MHz
UBRR= 207.3≈ 207
5- Find the baud rate error for the following baud rates if XTAL = 11.0592
MHz and XTAL = 12 MHz and U2X = 1.
(a) 9600 (b) 19200 (c) 38400 (d) 57600
Answer:
UBRR=(fosc/(8*baud rate)-1)
At baud rate = 9600
For XTAL=11.0592 MHz
UBRR=143
Error=((UBRRactual-UBRRDesired)/ UBRRDesired)*100%
Or (((Baud rate(true)-Baud rate(desired))/Baud rate(desired))*100%
For XTAL=12MHz
UBRR=155.25≈155
Error=(155.25-155)*100/155= 0.16%
6- Write an AVR C program to transmit serially the message “The earth is but
one country and mankind its citizens” continuously at 57,600 baud rate
using USART data empty interrupt.

Answer:
UBRR=(fosc/(16*baud rate)-1)
Using XTAL=16MHz
UBRR=16.3≈ 16
Code:
#include <avr\io.h>
#include <avr\interrupt.h>
ISR(USART_UDRE_vect)
{
if (str[index] != '\0'){
UDR = str[index];
Index++;
}
else {
index=0;
}
int main (void)
{
unsigned char str[30]= " The earth is but one country and mankind its
citizens. ";
int index=0;
UCSRB = (1<<TXEN)|(1<<UDRIE);
UCSRC = (1<< UCSZ1)|(1<<UCSZ0)|(1<<URSEL);
UBRRL = 0x10;
sei();
while (1);
return 0;
}

You might also like