#include <reg52.h>
unsigned char code digitOptions[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x00};
unsigned char num[2]; // Define an array for display
unsigned char minutes = 99;
unsigned char seconds = 0; // Initialize seconds to 0
#define START_BUTTON_STATE 0x20 // P3^5
#define STOP_BUTTON_STATE 0x40 // P3^6
#define RESET_BUTTON_STATE 0x80 // P3^7
bit startButton = START_BUTTON_STATE;
bit stopButton = STOP_BUTTON_STATE;
bit resetButton = RESET_BUTTON_STATE;
bit buzzer = P1^4;
sbit w1 = P1^0;
sbit w2 = P1^1;
unsigned char menu = 0;
bit flag = 0;
void delay(unsigned int z) {
unsigned int i, j;
for (i = 0; i < z; i++) {
for (j = 0; j < 621; j++) {
}
}
}
void Timer0Init() {
TMOD = 0x01; // Use Timer 0 in mode 1
TH0 = 0xFC; // Initialize timer values for a 50ms delay
TL0 = 0x67;
ET0 = 1; // Enable Timer 0 interrupt
TR0 = 0; // Start Timer 0
EA = 1; // Enable global interrupts
}
void display() {
num[0] = seconds % 10;
num[1] = seconds / 10;
if (menu == 1) {
if (flag == 0) {
num[0] = 10;
num[1] = 10;
}
}
w1 = 1;
w2 = 0;
P2 = digitOptions[num[0]];
delay(1);
w1 = w2 = 1;
w1 = 0;
w2 = 1;
P2 = digitOptions[num[1]];
delay(1);
w1 = w2 = 1;
}
void key() {
if (startButton == 0) {
while (startButton == 0);
menu++;
if (menu == 1) {
TR0 = 1;
}
if (menu == 2) {
TR0 = 0;
menu = 0;
}
}
if (menu == 1) {
if (stopButton == 0) {
while (stopButton == 0);
seconds++;
if (seconds > 99) seconds = 99;
}
if (resetButton == 0) {
while (resetButton == 0);
seconds--;
if (seconds < 0) seconds = 0;
}
}
}
void main() {
Timer0Init();
while (1) {
key();
display();
if (menu == 0) {
if (stopButton == 0) {
while (stopButton == 0);
if (seconds > 0) {
TR0 = 1;
}
}
if (resetButton == 0) {
while (resetButton == 0);
TR0 = 0;
seconds = 0;
}
}
}
}
void Timer0_ISR() interrupt 1 {
TH0 = 0xFC;
TL0 = 0x67;
flag++;
if (flag == 10) {
flag = 0;
seconds--;
if (seconds == 0) {
TR0 = 0;
}
}
}
|