Assignment 6 part B

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <reg66x.h>
sbit light=P2^0;
sbit switc=P2^1;
#define TH0_R 0xB7
#define TL0_R 0xFF
#define on 0
#define off 1
void tinit0(void); //timer 0 init function
void init_intt();
void set_light(unsigned int t);
unsigned int ttcounter=0;
void main()
{light=off;
init_intt();
tinit0(); //init timer 0
while(1)
{
if (switc==0) set_light(6000);
}}
void tinit0(void) //timer init fuction
{TMOD=0x09; //set timer 0 to mode 1 and gate 0
//TH0=0x6F; //set higher timer reg to 0
//TL0=0xFF; //set lower timer reg to 0
TR0=1;
} //return the pulse width
void init_intt(void)
{
ET0=1;
EA=1;
}
void timer_int (void) interrupt 1
{
TH0=0xB7;
TL0=0xFF;
ttcounter++;
}
void set_light(unsigned int t)
{light=on;
while(ttcounter<t);
light=off;
ttcounter=0;
}