#pragma large #include "reg552.h" #include "absacc.h" #include "stdio.h" #include "math.h" typedef unsigned char uchar; typedef unsigned int uint; bit flag_az, flag_el, flag_ser, flag_af, flag_urmarire, flag_first, flag_delta; bit flag_pc, flag_sound, flag_zda_setat, flag_gga_setat, flag_demult_setat, flag_corectie, flag_pornire; uchar contor, digit_curent, tast, poz_ceas, temp, nrpct, aa, delta; uchar bcd_year, bcd_month, bcd_day, bcd_hour, bcd_minute, bcd_second; uchar second, minute, hour, day, month, year; uchar zeci, unitati; uchar memorie[11]; uchar wxser[150]; uchar buf[5]; uchar tasta_care(); int az_luna, el_luna, test_el, ucitit_az, ucitit_el; int az_dorit, el_dorit, az, el, contor_az, contor_el, zero_az, zero_el, t_sound; float anul, luna, ziua, ora, min, sec, F, G, H, J, K, L, M, N, O, Q, R, S, T, X; float lat, lon, demult_az, demult_el; float xdat[20]; float ydat[20]; long demult; #define W0 .822513 #define W1 .0362916457 #define W2 .995766 #define W3 .00273777852 #define W4 .974271 #define W5 .0338631922 #define W6 .0312525 #define W7 .0367481957 #define W8 .751213 #define W9 .036601102 #define X0 .0114842665 #define X1 .109763757 #define X2 .0222354947 #define X3 .00324631241 #define X4 .0037350046 #define X5 .00198967535 #define X6 .00102974426 #define X7 .000994837673 #define X8 .0897797367 #define X9 .0115069558 #define Y0 .108739248 #define Y1 .0222005881 #define Y2 .00254818071 #define Y3 .397821 #define Y4 .917463 #define Y5 .065709822 #define Y6 24.065712 #define Y7 6.646055 #define Y8 3.1415927 #define Y9 6.2831853 #define Z0 57.29578 #define FNA(X) Z0 * X #define FNB(X) (X - (int)(X)) * Y9 #define ADR_SEG 0x0140 #define ADR_TAST 0x0160 #define LEFT 0xbf #define RIGHT 0xdf #define UP 0xef #define DOWN 0xf7 #define RST 0xfb #define MEM 0xfd #define MOD 0xfe sbit P4_0 = 0xC0; sbit P4_1 = 0xC1; sbit P4_2 = 0xC2; sbit P4_3 = 0xC3; sbit P4_4 = 0xC4; sbit P4_5 = 0xC5; sbit P4_6 = 0xC6; sbit P4_7 = 0xC7; void initializare(); void pozitie(); void comanda_azimut(); void comanda_elevatie(); void scriu_memaz(); void scriu_memel(); void manual(); void urmarire(); void reset(); void calcul_luna(); void pozitie_luna(); void set_ceas(); void scrie_ceas(); void citeste_ceas(); void afiseaza_ceas(); void sterge_af_st(); void sterge_af_dr(); void sound(); void read_demult(); void read_coord(); void read_corectie(); void read_delta(); void delay(uint time); struct adr_latch { uint latch; uchar out; }; code uchar tab[7] = {0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe}; code uchar zec[14] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x05, 0x0a, 0x00, 0x5c}; code struct adr_latch adrese[11] = { {0x0100, 0xfe}, {0x0100, 0xfd}, {0x0100, 0xfb}, {0x0100, 0xf7}, {0x0100, 0xef}, {0x0100, 0xdf}, {0x0100, 0xbf}, {0x0100, 0x7f}, {0x0120, 0xfe}, {0x0120, 0xfd}, {0x0120, 0xfb}, }; void timer0() interrupt 1 { TL0 = 0x00; TH0 = 0xfc; XBYTE[adrese[digit_curent].latch] = 0xff; digit_curent++; if(digit_curent == 11) digit_curent = 0; XBYTE[ADR_SEG] = zec[memorie[digit_curent]]; XBYTE[adrese[digit_curent].latch] = adrese[digit_curent].out; sound(); } void serial() interrupt 4 { uchar crec; if(RI) { crec = S0BUF; if(flag_ser) { wxser[contor] = crec; contor ++; if(crec == 0x0d) { if ((contor == 8) && (wxser[3] == 0x20)) { memorie[0] = wxser[0] - 0x30; memorie[1] = wxser[1] - 0x30; memorie[2] = wxser[2] - 0x30; memorie[3] = wxser[5] - 0x30; memorie[4] = wxser[6] - 0x30; flag_ser = 0; } if ((wxser[2] == 'Z') && (wxser[16] != ',')) flag_zda_setat = 1; if ((wxser[2] == 'G') && (wxser[3] == 'G') && (wxser[41] == '1')) flag_gga_setat = 1; if ((contor == 11) && (wxser[0] == 'D') && (wxser[5] == 0x20)) flag_demult_setat = 1; if (wxser[0] == 'R') flag_corectie = 1; if ((contor == 3) && (wxser[0] == 'E')) flag_delta = 1; } if(contor > 150) contor = 0; } if(((crec == 'W') && (contor != 40)) || (crec == '$')){ flag_ser = 1; contor = 0; } RI = 0; } else TI = 0; } void inc_az() interrupt 6 { if(flag_az) contor_az++; else contor_az--; CTI0 = 0; } void inc_el() interrupt 7 { if(flag_el) contor_el++; else contor_el--; CTI1 = 0; } float corectie(float yy) { uchar a; float unghi; if (yy < ydat[0]) unghi = 0; if (yy >= ydat[nrpct - 1]) unghi = 90; for(a = 0; a < nrpct - 1; a++) { if ((yy >= ydat[a]) && (yy < ydat[a+1])) { unghi = ((xdat[a+1] - xdat[a]) * (yy - ydat[a])) / (ydat[a+1] - ydat[a]) + xdat[a]; } } return unghi; } void pozitie() { if (P4_6 == 1) az = contor_az * (360.0 / demult_az); else { ADCON = 8; do{} while ((ADCON & 0x18) != 0x10); ADCON = ADCON & 0xc0; ucitit_az = (int)ADCH * 4; ucitit_az += (int)((ADCON >> 6) & 0x03); az = ucitit_az * 0.3515625; } if (P4_7 == 1){ if (P4_5 == 0) el = contor_el * (90.0 / demult_el); else el = corectie(contor_el); } else { ADCON = 9; do{} while ((ADCON & 0x18) != 0x10); ADCON = ADCON & 0xc0; ucitit_el = (int)ADCH * 4; ucitit_el += (int)((ADCON >> 6) & 0x03); el = ucitit_el * 0.087890625; } if((az >= 0) && (az < 360)) { memorie[5] = (uchar)(az / 100); memorie[6] = (uchar)((az % 100) / 10); memorie[7] = (uchar)(az % 10); } else { memorie[5] = 0x0c; memorie[6] = 0x0c; memorie[7] = 0x0c; } if((el >= 0) && (el < 91)) { memorie[8] = (uchar)(el / 10); memorie[9] = (uchar)(el % 10); } else { memorie[8] = 0x0c; memorie[9] = 0x0c; } } void comanda_azimut() { az_dorit = (uint)memorie[0] * 100 + (uint)memorie[1] * 10 + (uint)memorie[2]; if((az_dorit >= 0) && (az_dorit < 360)) { if(az < (az_dorit - delta)) { P4_0 = 0; P4_1 = 1; flag_az = 1; } if(az > (az_dorit + delta)) { P4_0 = 1; P4_1 = 0; flag_az = 0; } if((az >= (az_dorit - delta)) && (az <= (az_dorit + delta))) { P4_0 = 0; P4_1 = 0; } } } void comanda_elevatie() { el_dorit = (uint)memorie[3] * 10 + (uint)memorie[4]; if((el_dorit >= 0) && (el_dorit < 91)) { if(el < (el_dorit - delta)) { P4_2 = 0; P4_3 = 1; flag_el = 1; } if(el > (el_dorit + delta)) { P4_2 = 1; P4_3 = 0; flag_el = 0; } if((el >= (el_dorit - delta)) && (el <= (el_dorit + delta))) { P4_2 = 0; P4_3 = 0; } } } void delay(uint time) { uint i; for (i = 1; i < time; i++) {} } uchar tasta_care() { uchar n; n = 0; tast = XBYTE[ADR_TAST]; tast = tast | 0x80; if(tast != 0xff) { do { if((tast | 0xbf) == 0xbf) break; n++; tast = tast << 1; } while (1); return tab[n]; } else return 0x00; } void scriu_memaz() { memorie[0] = (uchar)(az_dorit / 100); memorie[1] = (uchar)((az_dorit % 100) / 10); memorie[2] = (uchar)(az_dorit % 10); } void scriu_memel() { memorie[3] = (uchar)(el_dorit / 10); memorie[4] = (uchar)(el_dorit % 10); } void manual() { uchar n; pozitie(); for (n = 0; n < 5; n++) memorie[n] = 0x0c; if(tasta_care() != 0x00) { if(tasta_care() == RST) reset(); if((tasta_care() == RIGHT) && (az < 359)) { P4_0 = 0; P4_1 = 1; flag_az = 1; } else P4_1 = 0; if((tasta_care() == LEFT) && (az > 0)) { P4_0 = 0; P4_0 = 1; flag_az = 0; } else P4_0 = 0; if((tasta_care() == UP) && (el < 90)) { P4_2 = 0; P4_3 = 1; flag_el = 1; } else P4_3 = 0; if((tasta_care() == DOWN) && (el > 0)) { P4_2 = 0; P4_2 = 1; flag_el = 0; } else P4_2 = 0; if(tasta_care() == MEM) { flag_sound = 1; for (n = 0; n < 5; n++) { buf[n] = memorie[n + 5]; memorie[n] = memorie[n + 5]; } delay(30000); for (n = 0; n < 5; n++) memorie[n] = 0x0c; } } else{ P4_0 = 0; P4_1 = 0; P4_2 = 0; P4_3 = 0; } } void urmarire() { uchar n; for (n = 0; n < 5; n++) memorie[n] = memorie[n + 5]; az_dorit = az; el_dorit = el; do { if(tasta_care() == MEM) { for (n = 0; n < 5; n++) memorie[n] = buf[n]; flag_sound = 1; } if((tasta_care() == RIGHT) && (az_dorit < 359)) { az_dorit ++; scriu_memaz(); delay(600); } if((tasta_care() == LEFT) && (az_dorit > 0)) { az_dorit --; scriu_memaz(); delay(600); } if((tasta_care() == UP) && (el_dorit < 90)) { el_dorit ++; scriu_memel(); delay(2400); } if((tasta_care() == DOWN) && (el_dorit > 0)) { el_dorit --; scriu_memel(); delay(2400); } pozitie(); comanda_azimut(); comanda_elevatie(); tast = XBYTE[ADR_TAST]; if((tast & 0x80) == 0x00) break; if(tasta_care() == MOD) { flag_sound = 1; delay(30000); flag_urmarire = 0; break; } } while (1); } void reset() { uchar n; P4_1 = 0; P4_3 = 0; for(n = 5; n < 10; n++) memorie[n] = 0x0c; n = 5; flag_af = 1; do { zero_az = contor_az; zero_el = contor_el; if (P4_6 == 1) P4_0 = 1; if (P4_7 == 1) P4_2 = 1; if(n < 10) { if(flag_af) memorie[n] = 0x0d; else memorie[n] = 0x0c; n++; flag_sound = 1; delay(30000); } else { n = 5; flag_af = !flag_af; delay(30000); } if((zero_az == contor_az) && (zero_el == contor_el)) { contor_az = 0; contor_el = 0; P4_0 = 0; P4_2 = 0; break; } P4_0 = 0; P4_2 = 0; } while (1); } void calcul_luna() { citeste_ceas(); if (unitati == 1) flag_first = 1; if (((unitati == 0) && (flag_first)) || (flag_pornire)) { flag_pornire = 0; flag_first = 0; memorie[10] = 0x0b; F = lon / Z0; G = lat / Z0; H = cos(G); G = sin(G); J = anul; if(luna < 3) { luna = luna + 12; J --; } J = 365 * J + 30 * luna + ziua + ((int)(J / 4)) - 32.5; while (luna > 7) { luna = luna - 5; J = J + 3; } J = J + ((int)(luna / 2)); K = ora / 24 + min / 1440 + sec / 86400; L = J + K; M = FNB(W0 + W1 * L); N = FNB(W2 + W3 * L); O = 2 * FNB(W4 + W5 * L); Q = FNB(W6 + W7 * L); R = sin(O); S = sin(M); T = sin(M - O); L = FNB(W8 + W9 * L) + X0 * R + X1 * S - X2 * T - X3 * sin(N) + X4 * sin(2 * M) - X5 * sin(2 * Q) - X6 * sin(2 * M - O) - X7 * sin(M + N - O); M = X8 * sin(Q + X9 * R + Y0 * S - Y1 * T) - Y2 * sin(Q - O); N = cos(M); M = sin(M); O = N * sin(L); Q = O * Y3 + M * Y4; Q = atan(Q / sqrt(1 - Q * Q)); R = cos(Q); S = sin(Q); T = N * cos(L) / R; L = atan((O * Y4 - M * Y3) / R / T); M = Y5 * J; M = M + Y6 * K + Y7 - ((int)(M / 24)) * 24; if(T < 0) L = L + Y8; else if(L < 0) L = L + Y9; K = Y9 * (M - ((int)(M / 24)) * 24) / 24 - L; if(K < 0) K = K + Y9; else if(K > Y9) K = K - Y9; L = -F - K; M = H * cos(L) * R + S * G; N = sqrt(1 - M * M); M = atan(M / N); O = (S - G * sin(M)) / H / cos(M); L = atan(sin(L) * R / N / O); if(O < 0) L = L + Y8; else if(L < 0) L = L + Y9; az_luna = FNA(L); test_el = FNA(M); if(test_el < 0) el_luna = 0; else el_luna = test_el; memorie[10] = 0x0c; } } void pozitie_luna() { memorie[0] = (uchar)(az_luna / 100); memorie[1] = (uchar)((az_luna % 100) / 10); memorie[2] = (uchar)(az_luna % 10); memorie[3] = (uchar)(el_luna / 10); memorie[4] = (uchar)(el_luna % 10); } void citeste_ceas() { XBYTE[0x9ff8] = 64; temp = XBYTE[0x9fff]; zeci = temp / 16; unitati = temp % 16; year = zeci * 10 + unitati; anul = year + 100; temp = XBYTE[0x9ffe]; zeci = temp / 16; unitati = temp % 16; luna = month = zeci * 10 + unitati; temp = XBYTE[0x9ffd]; zeci = temp / 16; unitati = temp % 16; ziua = day = zeci * 10 + unitati; temp = XBYTE[0x9ffb]; zeci = temp / 16; unitati = temp % 16; ora = hour = zeci * 10 + unitati; temp = XBYTE[0x9ffa]; zeci = temp / 16; unitati = temp % 16; min = minute = zeci * 10 + unitati; temp = XBYTE[0x9ff9]; XBYTE[0x9ff8] = 0; zeci = temp / 16; unitati = temp % 16; sec = second = zeci * 10 + unitati; } void set_ceas() { sterge_af_st(); poz_ceas = 1; citeste_ceas(); flag_sound = 1; delay(30000); do { memorie[8] = poz_ceas; if((tasta_care() == RST) && (poz_ceas < 6)) { poz_ceas++; flag_sound = 1; delay(30000); } switch(poz_ceas) { case 1: if((tasta_care() == LEFT) && (year > 0)) year --; if((tasta_care() == RIGHT) && (year < 99)) year ++; memorie[6] = year / 10; memorie[7] = year % 10; bcd_year = memorie[6] * 16 + memorie[7]; delay(10000); break; case 2: if((tasta_care() == LEFT) && (month > 1)) month --; if((tasta_care() == RIGHT) && (month < 12)) month ++; memorie[6] = month / 10; memorie[7] = month % 10; bcd_month = memorie[6] * 16 + memorie[7]; delay(10000); break; case 3: if((tasta_care() == LEFT) && (day > 1)) day --; if((tasta_care() == RIGHT) && (day < 31)) day ++; memorie[6] = day / 10; memorie[7] = day % 10; bcd_day = memorie[6] * 16 + memorie[7]; delay(10000); break; case 4: if((tasta_care() == LEFT) && (hour > 0)) hour --; if((tasta_care() == RIGHT) && (hour < 23)) hour ++; memorie[6] = hour / 10; memorie[7] = hour % 10; bcd_hour = memorie[6] * 16 + memorie[7]; delay(10000); break; case 5: if((tasta_care() == LEFT) && (minute > 0)) minute --; if((tasta_care() == RIGHT) && (minute < 59)) minute ++; memorie[6] = minute / 10; memorie[7] = minute % 10; bcd_minute = memorie[6] * 16 + memorie[7]; delay(10000); break; case 6: if((tasta_care() == LEFT) && (second > 0)) second --; if((tasta_care() == RIGHT) && (second < 59)) second ++; memorie[6] = second / 10; memorie[7] = second % 10; bcd_second = memorie[6] * 16 + memorie[7]; delay(10000); break; } // end switch(poz_ceas) if((tasta_care() == RST) && (poz_ceas == 6)) { scrie_ceas(); afiseaza_ceas(); break; } } while(1); } void scrie_ceas() { XBYTE[0x9ff8] = 128; XBYTE[0x9fff] = bcd_year; XBYTE[0x9ffe] = bcd_month; XBYTE[0x9ffd] = bcd_day; XBYTE[0x9ffc] = 1; XBYTE[0x9ffb] = bcd_hour; XBYTE[0x9ffa] = bcd_minute; XBYTE[0x9ff9] = bcd_second; XBYTE[0x9ff8] = 0; } void afiseaza_ceas() { XBYTE[0x9ff8] = 64; temp = XBYTE[0x9fff]; XBYTE[0x9ff8] = 0; memorie[6] = temp / 16; memorie[7] = temp % 16; memorie[8] = 1; flag_sound = 1; delay(60000); delay(60000); delay(60000); delay(60000); XBYTE[0x9ff8] = 64; temp = XBYTE[0x9ffe]; XBYTE[0x9ff8] = 0; memorie[6] = temp / 16; memorie[7] = temp % 16; memorie[8] = 2; flag_sound = 1; delay(60000); delay(60000); delay(60000); delay(60000); XBYTE[0x9ff8] = 64; temp = XBYTE[0x9ffd]; XBYTE[0x9ff8] = 0; memorie[6] = temp / 16; memorie[7] = temp % 16; memorie[8] = 3; flag_sound = 1; delay(60000); delay(60000); delay(60000); delay(60000); XBYTE[0x9ff8] = 64; temp = XBYTE[0x9ffb]; XBYTE[0x9ff8] = 0; memorie[6] = temp / 16; memorie[7] = temp % 16; memorie[8] = 4; flag_sound = 1; delay(60000); delay(60000); delay(60000); delay(60000); XBYTE[0x9ff8] = 64; temp = XBYTE[0x9ffa]; XBYTE[0x9ff8] = 0; memorie[6] = temp / 16; memorie[7] = temp % 16; memorie[8] = 5; flag_sound = 1; delay(60000); delay(60000); delay(60000); delay(60000); XBYTE[0x9ff8] = 64; temp = XBYTE[0x9ff9]; XBYTE[0x9ff8] = 0; memorie[6] = temp / 16; memorie[7] = temp % 16; memorie[8] = 6; flag_sound = 1; delay(60000); delay(60000); delay(60000); delay(60000); } void sterge_af_st() { memorie[5] = 0x0c; memorie[6] = 0x0c; memorie[7] = 0x0c; memorie[8] = 0x0c; memorie[9] = 0x0c; } void sterge_af_dr() { memorie[0] = 0x0c; memorie[1] = 0x0c; memorie[2] = 0x0c; memorie[3] = 0x0c; memorie[4] = 0x0c; } void sound() { if(flag_sound) { PWM0 = 128; t_sound ++; if(t_sound >= 100) { t_sound = 0; flag_sound = 0; PWM0 = 0; } } } void read_demult() { demult = demult_el; memorie[0] = 0; memorie[1] = (uchar)(demult / 1000); memorie[2] = (uchar)((demult % 1000) / 100); memorie[3] = (uchar)((demult % 100) / 10); memorie[4] = (uchar)(demult % 10); demult = demult_az; memorie[5] = 0; memorie[6] = (uchar)(demult / 1000); memorie[7] = (uchar)((demult % 1000) / 100); memorie[8] = (uchar)((demult % 100) / 10); memorie[9] = (uchar)(demult % 10); flag_sound = 1; delay(60000); delay(60000); delay(60000); delay(60000); } void read_corectie() { for(aa = 0; aa < nrpct; aa++){ demult = ydat[aa]; memorie[0] = 0; memorie[1] = (uchar)(demult / 1000); memorie[2] = (uchar)((demult % 1000) / 100); memorie[3] = (uchar)((demult % 100) / 10); memorie[4] = (uchar)(demult % 10); demult = xdat[aa]; memorie[5] = 0; memorie[6] = (uchar)(demult / 10); memorie[7] = (uchar)(demult % 10); memorie[8] = (uchar)((aa + 1) / 10); memorie[9] = (uchar)((aa + 1) % 10); flag_sound = 1; delay(60000); delay(60000); delay(60000); delay(60000); } } void read_coord() { demult = lon * 100; if (demult < 0 ) demult = - demult; memorie[0] = (uchar)(demult / 10000); memorie[1] = (uchar)((demult % 10000) / 1000); memorie[2] = (uchar)((demult % 1000) / 100); memorie[3] = (uchar)((demult % 100) / 10); memorie[4] = (uchar)(demult % 10); demult = lat * 1000; if (demult < 0 ) demult = - demult; memorie[5] = (uchar)(demult / 10000); memorie[6] = (uchar)((demult % 10000) / 1000); memorie[7] = (uchar)((demult % 1000) / 100); memorie[8] = (uchar)((demult % 100) / 10); memorie[9] = (uchar)(demult % 10); flag_sound = 1; delay(60000); delay(60000); delay(60000); delay(60000); } void read_delta() { memorie[0] = delta; memorie[1] = delta; memorie[2] = delta; memorie[3] = delta; memorie[4] = delta; memorie[5] = delta; memorie[6] = delta; memorie[7] = delta; memorie[8] = delta; memorie[9] = delta; flag_sound = 1; delay(60000); delay(60000); delay(60000); delay(60000); } void initializare() { EA = 0; P4_0 = 0; P4_1 = 0; P4_2 = 0; P4_3 = 0; digit_curent = 0; S0CON = 0x70; TMOD = 0x21; TL0 = 0x00; TH0 = 0xfc; TH1 = 0xfd; //9600 TR1 = 1; TR0 = 1; ET0 = 1; IT0 = 1; IT1 = 1; IEN1 = 0x03; CTCON = 0x55; PWMP = 15; flag_pornire = 1; flag_urmarire = 0; flag_ser = 0; flag_sound = 0; t_sound = 0; flag_pc = 0