20121211 作業2 20121211 作業2

//----------------------------------------------------------------
//
// Project : station_1_02
// File mane : station_1_02.c
// Hex file: station_1_02.hex
// Note :The second C program of station 1
// Compiler : Keil C
// Version : ver4(04/12/2012)
// Revision : 1. to modify the connection of buzzer(P3.0 -> P3.5)
// 2. to modify the frequency of buzzer
// 資訊四A 9879005 黃階輝 日期:20121211 微處理機應用實務
//----------------------------------------------------------------
#include <reg51.h>
sbit P1_7 = P1^7; // INPUT
sbit P3_5 = P3^5; // BUZZER
sbit P3_3 = P3^3; // PB_SW
int t = 65536-501; // So=784Hz, 65536-716=64820
void delay(int);
main()
{
unsigned char a;
unsigned char b;
int j;
IE = 0x84;
TMOD = 0x01;
TCON = 0x00;
P2 = 0xff;
while(1)
{
a = 0xf0;
if(P1_7 == 0)
{
for (j = 1; j <= 10; j++)
{
P2 = ~a^b;
delay(20); // 20ms *40 = 800ms = 0.8s
a = ~a^b;
}
P2 = 0xff;
}
}
}
void delay(int count) // delay 20ms
{
int m, n;
for(m = 1; m <= count; m++)
{
for(n = 1; n <= 2000; n++)
{
;
}
}

}
void EX1_int(void) interrupt 2
{
int j;
for(j = 1; j <= 250; j++)
{
P3_5 = 1;
TH0 = t /256;
TL0 = t % 256;
TR0 = 1;
while(TF0 == 0)
{
;
}
TF0 = 0;
P3_5 = 0;
TH0 = t /256;
TL0 = t % 256;
TR0 = 1;
while(TF0 == 0)
{
;
}
TF0 = 0;
}
}