#include "aduc812.h" #include "max.h" #include "driver_io.h" #include "driver_lcd.h" #include "interrupt.h" #include "timer.h" #include #include #include #define MILES 0 #define KILOMETERS 1 #define TANK_VALUE 0 #define PREDICTION 1 unsigned char metrica = KILOMETERS; unsigned char mode = TANK_VALUE; unsigned char row1[16]; unsigned char row2[16]; unsigned char tank_max_volume = 50; unsigned char fuel_threshold = 5; unsigned char consumption100km = 2; unsigned char consumption100miles = 1; unsigned char total_pos = 14; unsigned char sub_info = 0; unsigned char consumption = 0; unsigned char dip = 0; unsigned char old_dip = 0; unsigned char next_char = 0; unsigned char old_value = 10; char postfix[2]; int i = 0; void write_led(unsigned char value) { write_max(7, value); } void make_sound() { unsigned int sound = 0; int i; for (i = 0; i < 1000; i++) { sound = (sound == 0x00) ? 0x10 : 0x00; write_max(4, sound); if(i%100==0) write_led(0xff); if(i%200==0) write_led(0x00); } } void init(unsigned char speed) { TH1 = 0xFD; //Скорость 9600 TH0 = 0x00; TMOD = 0x22; //Таймер 1 и 0 в режиме autoreload TCON = 0x50; //Запуск таймеров 0 и 1 SCON = 0x50; //8 bit UART, разрешение приема PCON &= 0x7F; //Отключение дублирования скорости, установленной в TH1 init_timer(1000); write_string("ready\n"); EA = 1; ET0 = 1; } void format_row1(char value) { int i = 0; int pos = 0; row1[0] = '['; row1[15] = ']'; pos = value * total_pos / tank_max_volume; for ( i = 1; i <= pos; i++) { row1[i] = 0xff; } for (;i <= total_pos; i++) { row1[i] = ' '; } } void format_row2(unsigned char value) { int i = 0; for(i=0; i<16; i++) { row2[i]=0x20; } i = 0; row2[0] = 0 + '0'; row2[1] = 0 + '0'; row2[2] = 0 + '0'; row2[2] = value % 10 + 0x30; if(value >= 10) { value = value / 10; row2[1] = value % 10 + 0x30; } if(value >= 100) { value = value / 10; row2[0] = value % 10 + 0x30; } i=3; row2[i] = postfix[0]; i++; row2[i] = postfix[1]; i++; } void format_postfix() { if (mode == TANK_VALUE) { postfix[0] = 'L'; postfix[1] = ' '; } else if (metrica == KILOMETERS) { postfix[0] = 'k'; postfix[1] = 'm'; } else { postfix[0] = 'm'; postfix[1] = ' '; } } void lcd_controller(unsigned char volume, unsigned char additional_info) { format_postfix(); format_row1(volume); format_row2(additional_info); clear_lcd(); for (i = 0; i < 16; i++) { print_char_lcd(row1[i]); } goto_xy_lcd(1,2); for (i = 0; i < 16; i++) { print_char_lcd(row2[i]); } } unsigned char data_controller() { return get_tank_value(); } void visualise(unsigned char value) { dip = read_max(2); if (value == old_value && dip == old_dip) return; metrica = (dip & 0x1) ? MILES : KILOMETERS; mode = (dip & 0x2) ? TANK_VALUE : PREDICTION; consumption = (metrica == MILES) ? consumption100miles : consumption100km; sub_info = (mode == PREDICTION) ? value * consumption : value; lcd_controller(value, sub_info); if (value < fuel_threshold) { make_sound(); write_led(0xff); } else { write_led(0x00); } old_value = value; old_dip = dip; } void main() { unsigned char value = 0; init(S9600); clear_lcd(); //print_char_lcd(0x31); while(1) { value = data_controller(); visualise(value); } }