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

Homework 3

The document contains code to configure timers 0 and 1 on a microcontroller to generate interrupts at specific frequencies. It also includes code snippets to display the results. Timer 0 is configured to generate an interrupt every 2ms (500Hz) by selecting an overflow rate that utilizes the best prescalar value. Timer 1 is configured in auto-reload mode to generate an interrupt every 10ms (1000Hz). The code snippets show toggling LEDs based on the different timer rates to display the frequencies. Additional code is provided to increment time variables like seconds, minutes, hours, etc. using a timer interrupt executing at 100Hz.

Uploaded by

Anshul Gour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Homework 3

The document contains code to configure timers 0 and 1 on a microcontroller to generate interrupts at specific frequencies. It also includes code snippets to display the results. Timer 0 is configured to generate an interrupt every 2ms (500Hz) by selecting an overflow rate that utilizes the best prescalar value. Timer 1 is configured in auto-reload mode to generate an interrupt every 10ms (1000Hz). The code snippets show toggling LEDs based on the different timer rates to display the frequencies. Additional code is provided to increment time variables like seconds, minutes, hours, etc. using a timer interrupt executing at 100Hz.

Uploaded by

Anshul Gour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Homework 3

Anshul Gour
12 September 2019

1 Write a code to configure Timer 0 for an overflow interrupt at


a rate of 500 Hz. Select the ”Best” prescalar and document why
it is ”Best”.
#include <reg52.h>
sbit LED = P0^0;

void timer_isr() interrupt 1


{
TH0= 0XE3; // Reload the timer value
TL0= 0X06;
LED = ~LED; // Toggle the led
}
void main()
{
TMOD= 0X00; //Timer0 mode 1
TH0= 0XE3; // Load the timer value
TL0= 0X06;
TR0= 1; //turn ON timer zero
ET0= 1; // Enable Timer 0 interrupt
EA= 1; //Enable Global Interrupt bit
while(1)
{
// Do nothing
}
}
2 Write a code to configure Timer 1 for compare match interrupt
to occur to 1000Hz and auto-reload.
//Generate 1000Hz frequency
#include <reg52.h> //include header file

1
sbit LED= P2^1;
int i;
void timer0() interrupt 3
{
i++;
if(i==2)
LED= ~LED; ///Toggle LED
i=0;
}

void main()
{
TMOD= 0x20; //timer 1 auto reload mode 2
TH1= 0x1A; // set timer for 230 counts
EA=1; //Enable global interrupt
ET1=1; //enable timer 1 interrupt
TR1=1; //start timer 1
while(1);
}
4. Write some code to display the results of the previous three
steps.Include your code here.
#include <reg52.h>
sbit LED = P0^0;

void timer_isr() interrupt 1


{
TH0= 0XE3; // Reload the timer value
TL0= 0X06;
LED = ~LED; // Toggle the led
}
void main()
{
TMOD= 0X00; //Timer0 mode 1
TH0= 0XE3; // Load the timer value
TL0= 0X06;
TR0= 1; //turn ON timer zero
ET0= 1; // Enable Timer 0 interrupt
EA= 1; //Enable Global Interrupt bit
while(1)

2
{
// Do nothing
}
}

Figure 1: Display of 500 Hz frequency

//Generate 1000Hz frequency


#include <reg52.h> //include header file

sbit LED= P2^1;


int i;
void timer0() interrupt 3
{
i++;
if(i==2)
LED= ~LED; ///Toggle LED
i=0;
}

void main()
{

3
TMOD= 0x20; //timer 1 auto reload mode 2
TH1= 0x1A; // set timer for 230 counts
EA=1; //Enable global interrupt
ET1=1; //enable timer 1 interrupt
TR1=1; //start timer 1
while(1);
}

Figure 2: Display of 1000 Hz frequency

4
After Laboratory Assignment 2

1.What is a prescalar
Ans. A prescaler is an electronic counting circuit used to reduce a high
frequency electrical signal to a lower frequency by integer division. The
prescaler takes the basic timer clock frequency (which may be the CPU clock
frequency or may be some higher or lower frequency) and divides it by some
value before feeding it to the timer, according to how the prescaler registers
are configured. The prescaler values that may be configured might be limited
to a few fixed values (powers of 2). The purpose of the prescaler is to allow
the timer to be clocked at the rate a user desires.

3.With a 12 Hz clock, can you use an 8 bit timer/counterto


generate an interrupt at 1 hz rate with a 12 Mhz clock
Ans No, with a 12 Hz clock we can not use an 8 bit timer/counter to
generate at 1 Hz rate because the max count that we can produce with 8 bit
timer is 256 that means we can not generate frequency less than 1953 Hz.
So it is not possible.
Max count = 256
Time for 1 tick= 1us for 12 hz clock
Max delay = 256 us
Frequency = 1953 Hz

4.If you only have an 8 bit timer / counter available, how can
you modify your ISR so that you could run your code at 1 Hz rate
with 12 Mhz clock

#include <reg52.h>
unsigned int x=0;
void delay() interrupt 1
{
x++;
if(x==1953) //// 500ms delay (256*1953= 499968)
{
P1=~P1; /// led toggles after 500ms
x=0;
}
}
void main()
{
TMOD= 0x02; ///Timer 0 mode 2

5
TR0=1; /// Start the timer
TH0=0; ///
IE= 0x82; ///Enable timer 0 interrupt
while(1); // infinite loop
}

6. Create a data structure that could be used with a timer


interrupt executing at a rate of 100 Hz to accumulate an entire year
of incremental time. Include member variables to contain seconds,
minutes, hours, days,weeks, years. Choose the appropriate variable
type. Simulate the code and include the necessary snippets of the
code.

struct Time
{
int sec;
int min;
int hour;
int days;
int weeks;
int years;
};

void main()
{
struct Time T1;
T1.sec= 0;
T1.min=0;
T1.hour= 0;
T1.weeks=0;
T1.years=0;
}

7.Implement some conditional code to update seconds,minutes,


hours, days, weeks, years based on the the timer ISR rate. Simulate
the code and include necessary snippets of the code.
#include <reg52.h>

struct Time
{

6
int sec;
int min;
int hour;
int days;
int weeks;
int years;
};

int csec=0;
void timer() interrupt 1
{
csec++;
TH1= 0xD8;
TL1= 0xEF;
}

void main()
{
struct Time T1;
T1.sec= 0;
T1.min=0;
T1.hour= 0;
T1.weeks=0;
T1.years=0;

TMOD= 0x01; //Timer 0 mode 1


TH0 = 0xD8;
TL0= 0xEF; // Load timer
TR0= 1; //Start the timer
EA= 1; // Enable global interrupt
ET0= 1; // Enable timer 1 interrupt

while(1)
{
if (csec==100)
csec= 0;
T1.sec++;
P1= T1.sec;
}
if (T1.sec==60)
{

7
T1.sec=0;
T1.min++;
P2= T1.min;
}
if (T1.min==60)
{
T1.min=0;
T1.hour++;
}
if (T1.hour==24)
{
T1.hour=0;
T1.days++;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy