Homework 3
Homework 3
Anshul Gour
12 September 2019
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;
2
{
// Do nothing
}
}
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);
}
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.
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
}
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;
}
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;
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++;
}
}