/* Timer driver */ #include "aduc812.h" #include "timer.h" #include "interrupt.h" unsigned char time; unsigned long init_value; unsigned char emulated_tank_volume = 30; unsigned char medium_volume = 30; unsigned char low_add_volume = 20; unsigned char period = 50; unsigned char dif = 3; unsigned char threshold = 4; unsigned char del = 0; unsigned char get_tank_value() { return emulated_tank_volume; } void emulator() { //if (time % period == 0) emulated_tank_volume = emulated_tank_volume - 1; //if (time % period == 0) emulated_tank_volume = emulated_tank_volume - next_rand() % dif; if (time == 254) { del++; if (del == 10) { del = 0; if (emulated_tank_volume > 1) emulated_tank_volume = emulated_tank_volume - next_rand() % dif; } } if (emulated_tank_volume < threshold) emulated_tank_volume = next_rand() % medium_volume + low_add_volume; } void process_timer(void) interrupt 1 { time++; TH0 = (0xFF00 & init_value) >> 8; TL0 = (0x00FF & init_value); emulator(); } // Получение текущей метки времени в миллисекундах. unsigned char get_current_millis(void) { unsigned char temp = 0; EA = 0; temp = time; EA = 1; return temp; } // Измерение количества миллисекунд, прошедших с временной метки t0 и до текущего времени. unsigned char get_time_interval(unsigned char t0) { return get_current_millis() - t0; } // Задержка на t миллисекунд. void sleep(unsigned char delay) { unsigned char startTime = get_current_millis(); while (get_time_interval(startTime) < delay); } // Инициализация таймера. void init_timer(unsigned long frequency) { time = 0; set_vector(0x200B, (void *) process_timer); init_value = 65536 - (921600 / frequency); TH0 = (0xFF00 & init_value) >> 8; TL0 = (0x00FF & init_value); // Разрешение прерываний от таймера 0 TMOD |= 0x01; ET0 = 1; // Маска прерывания таймера 1 TR0 = 1; // Разрешаем таймеру 0 считать } unsigned char pre_rand = 17; unsigned char next_rand(void) { unsigned char timerValue = get_current_millis(); unsigned char newRandom = (timerValue * pre_rand + timerValue) % 256; pre_rand = newRandom; return newRandom; }