#include #include #include #include #include #include #include #include "tb386ex.h" #define SONIC_TRIGGER_PORT P1LTC #define SONIC_TRIGGER_BIT 0x04 #define BAR_LED 0x203 /* Bar LED On Board*/ typedef unsigned int WORD; typedef unsigned char BYTE; /* --------------------------------------------------------------------------- *---------------------------------------------------------------------------*/ void InitTrace386Port(void) { outportb(P1CFG, 0x00); outportb(P1DIR, 0xEB); outportb(P2DIR, 0x7F); outportb(P3CFG, 0xBC); outportb(P3DIR, 0xFC); outportb(P3LTC, 0xFD); } /* --------------------------------------------------------------------------- Timer Support Start *---------------------------------------------------------------------------*/ #define IRSM OCW2MDOS /* Interrupt Controller Master */ #define IRSS OCW2SDOS /* Interrupt Controller Slave */ #define IMM OCW1MDOS /* Interrupt Mask Master */ #define IMS OCW1SDOS /* Interrupt Mask Slave */ #define EOI 0x20 /* end of interrupt command */ #define TIMER_INT 0x72 volatile int HardDelayTimerCount; unsigned char master_org, slave_org; void interrupt NewIRQ10(void) /*IRQ10*/ { HardDelayTimerCount++; outportb(IRSS, EOI); /* End-Of-Interrupt to Master 8259A */ outportb(IRSM, EOI); /* End-Of-Interrupt to Slave 8259A */ } void InitTimer1(unsigned short MaxCount) /* Counter Input = 1000KHz */ { disable(); outportb(TMRCONDOS, 0x74); /* Timer1 => MODE2 */ outportb(TMR1DOS, MaxCount & 0xFF); outportb(TMR1DOS, (MaxCount >> 8) & 0xFF); setvect(TIMER_INT, NewIRQ10); master_org = inportb(IMM); /* Save PIC master's mask */ outportb(IMM, master_org & 0xFB); /* Enable IRQ2 */ slave_org = inportb(IMS); /* Save PIC slave's mask */ outportb(IMS, slave_org & 0xFB); /* Enable IRQ10 */ enable(); } void CloseTimer1(void) { disable(); outportb(IMM, master_org); /* Restore IRQ2 */ outportb(IMS, slave_org); /* Restore IRQ10 */ enable(); } void Delay_10ms(int count) /* delay 함수 */ { HardDelayTimerCount = 0; for (;;) if (HardDelayTimerCount > count) break; } /*--------------------------------------------------------------------------*/ WORD Get_Timer(void) /* Input Clock = 1MHz ==> 1 tick = 1 usec */ { WORD LSB; outportb(TMRCONDOS, 0x00); /* Counter 0 latch */ LSB = inportb(TMR1DOS); return ((WORD)inportb(TMR1DOS) << 8) + LSB; } void Delay_usec(int usec) { WORD tmp = Get_Timer(); while (Get_Timer() > (tmp - usec)) ; } /*--------------------------------------------------------------------------*/ #define SONIC_INT 0x0F BYTE SonicRxFlag; WORD TimerValue; void interrupt NewInt0F(void) { SonicRxFlag = 1; TimerValue = Get_Timer(); outportb(IRSM, EOI); } void Enable_IRQ7(void) { disable(); outportb(IMM, inportb(IMM) & ~0x80); setvect(SONIC_INT, NewInt0F); enable(); } void Disable_IRQ7(void) { disable(); outportb(IMM, inportb(IMM) | 0x80); enable(); } /*--------------------------------------------------------------------------*/ void Exam1(void) { int index = 0; WORD Time0; printf("Exam1\n"); for (;;) { printf("SonarTest Loop=%d ===> ", index++); HardDelayTimerCount = 0; while (!HardDelayTimerCount) ; SonicRxFlag = 0; outportb(SONIC_TRIGGER_PORT, ~SONIC_TRIGGER_BIT); Time0 = Get_Timer(); Delay_usec(50); outportb(SONIC_TRIGGER_PORT, 0xFF); while (HardDelayTimerCount != 2) { if (SonicRxFlag) { break; } } if (SonicRxFlag) printf("RX OK %d %d => %d\n", Time0, TimerValue, Time0 - TimerValue); else printf("NO RX !!\n"); if (kbhit() && (getch() == 'q')) break; Delay_10ms(100); } } void Exam1A(void) { int index = 0; WORD Time0; int led_onoff; printf("Exam1A\n"); for (;;) { printf("SonarTest Loop=%d ===> ", index++); HardDelayTimerCount = 0; while (!HardDelayTimerCount) ; SonicRxFlag = 0; outportb(SONIC_TRIGGER_PORT, ~SONIC_TRIGGER_BIT); Time0 = Get_Timer(); outportb(BAR_LED, 0xFE); Delay_usec(50); outportb(SONIC_TRIGGER_PORT, 0xFF); while (HardDelayTimerCount != 2) { if (SonicRxFlag) { break; } } if (SonicRxFlag) { printf("RX OK %d %d => %d\n", Time0, TimerValue, Time0 - TimerValue); outportb(BAR_LED, 0x7F); } else { for(led_onoff = 1 ; led_onoff <= 10 ; led_onoff++) { if(led_onoff%2 == 1) { outportb(BAR_LED, 0xFE); } else { outportb(BAR_LED, 0xFF); } Delay_10ms(10); } printf("NO RX !!\n"); } if (kbhit() && (getch() == 'q')) break; Delay_10ms(100); outportb(BAR_LED, 0xFF); } } /*실제거리=sqrt(pow(측정거리, 2)-pow(옵셋, 2)) - 보정거리*/ #define RANGE(x) sqrt(pow((x-25)*0.017, 2) - pow(7.5, 2)) - 1 void Exam2A(void) { int index = 0; WORD Time0; printf("Exam2A\n"); for (;;) { printf("SonarTest Loop=%d ===> ", index++); HardDelayTimerCount = 0; while (!HardDelayTimerCount) ; SonicRxFlag = 0; outportb(SONIC_TRIGGER_PORT, ~SONIC_TRIGGER_BIT); Time0 = Get_Timer(); Delay_usec(50); outportb(SONIC_TRIGGER_PORT, 0xFF); while (HardDelayTimerCount != 2) { if (SonicRxFlag) { break; } } if (SonicRxFlag) { printf("RX OK %d %d => %7.2fcm\n", Time0, TimerValue, RANGE(Time0 - TimerValue)); } else { printf("NO RX !!\n"); } if (kbhit() && (getch() == 'q')) break; Delay_10ms(100); } } void Exam2B(void) { int index = 0; WORD Time0; int led_onoff; float dis; printf("Exam2B\nAlmost Exact (21-41)\n"); for (;;) { printf("SonarTest Loop=%d ===> ", index++); HardDelayTimerCount = 0; while (!HardDelayTimerCount) ; SonicRxFlag = 0; outportb(SONIC_TRIGGER_PORT, ~SONIC_TRIGGER_BIT); Time0 = Get_Timer(); Delay_usec(50); outportb(SONIC_TRIGGER_PORT, 0xFF); while (HardDelayTimerCount != 2) { if (SonicRxFlag) { break; } } if (SonicRxFlag) { dis=RANGE(Time0 - TimerValue); if(dis>20 && dis<42) { outportb(BAR_LED, 0x7F); printf("RX OK %d %d => %7.2fcm\n", Time0, TimerValue, RANGE(Time0 - TimerValue)); } else { for(led_onoff = 1 ; led_onoff <= 10 ; led_onoff++) { if(led_onoff%2 == 1) { outportb(BAR_LED, 0xFE); } else { outportb(BAR_LED, 0xFF); } Delay_10ms(10); } printf("NOT Exact AREA !!\n"); } } else { printf("NO RX !!\n"); } if (kbhit() && (getch() == 'q')) break; Delay_10ms(100); } } char *DISTANCE[201] = { "21.0", "21.1", "21.2", "21.3", "21.4", "21.5", "21.6", "21.7", "21.8", "21.9", "22.0", "22.1", "22.2", "22.3", "22.4", "22.5", "22.6", "22.7", "22.8", "22.9", "23.0", "23.1", "23.2", "23.3", "23.4", "23.5", "23.6", "23.7", "23.8", "23.9", "24.0", "24.1", "24.2", "24.3", "24.4", "24.5", "24.6", "24.7", "24.8", "24.9", "25.0", "25.1", "25.2", "25.3", "25.4", "25.5", "25.6", "25.7", "25.8", "25.9", "26.0", "26.1", "26.2", "26.3", "26.4", "26.5", "26.6", "26.7", "26.8", "26.9", "27.0", "27.1", "27.2", "27.3", "27.4", "27.5", "27.6", "27.7", "27.8", "27.9", "28.0", "28.1", "28.2", "28.3", "28.4", "28.5", "28.6", "28.7", "28.8", "28.9", "29.0", "29.1", "29.2", "29.3", "29.4", "29.5", "29.6", "29.7", "29.8", "29.9", "30.0", "30.1", "30.2", "30.3", "30.4", "30.5", "30.6", "30.7", "30.8", "30.9", "31.0", "31.1", "31.2", "31.3", "31.4", "31.5", "31.6", "31.7", "31.8", "31.9", "32.0", "32.1", "32.2", "32.3", "32.4", "32.5", "32.6", "32.7", "32.8", "32.9", "33.0", "33.1", "33.2", "33.3", "33.4", "33.5", "33.6", "33.7", "33.8", "33.9", "34.0", "34.1", "34.2", "34.3", "34.4", "34.5", "34.6", "34.7", "34.8", "34.9", "35.0", "35.1", "35.2", "35.3", "35.4", "35.5", "35.6", "35.7", "35.8", "35.9", "36.0", "36.1", "36.2", "36.3", "36.4", "36.5", "36.6", "36.7", "36.8", "36.9", "37.0", "37.1", "37.2", "37.3", "37.4", "37.5", "37.6", "37.7", "37.8", "37.9", "38.0", "38.1", "38.2", "38.3", "38.4", "38.5", "38.6", "38.7", "38.8", "38.9", "39.0", "39.1", "39.2", "39.3", "39.4", "39.5", "39.6", "39.7", "39.8", "39.9", "40.0", "40.1", "40.2", "40.3", "40.4", "40.5", "40.6", "40.7", "40.8", "40.9", "41.0" }; void Exam2C(void) { int index = 0; WORD Time0; int led_onoff; int i; printf("Exam2C\nAlmost Exact (21-41)\n"); for (;;) { printf("SonarTest Loop=%d ===> ", index++); HardDelayTimerCount = 0; while (!HardDelayTimerCount) ; SonicRxFlag = 0; outportb(SONIC_TRIGGER_PORT, ~SONIC_TRIGGER_BIT); Time0 = Get_Timer(); Delay_usec(50); outportb(SONIC_TRIGGER_PORT, 0xFF); while (HardDelayTimerCount != 2) { if (SonicRxFlag) { break; } } if (SonicRxFlag) { i = (Time0 - TimerValue - 1400) / 5.7; if(i>=0 && i<=201) { outportb(BAR_LED, 0x7F); printf("RX OK %d %d => %scm\n", Time0, TimerValue, DISTANCE[i]); } else { for(led_onoff = 1 ; led_onoff <= 10 ; led_onoff++) { if(led_onoff%2 == 1) { outportb(BAR_LED, 0xFE); } else { outportb(BAR_LED, 0xFF); } Delay_10ms(10); } printf("NOT EXACT AREA !!\n"); } } else { printf("NO RX !!\n"); } if (kbhit() && (getch() == 'q')) break; Delay_10ms(100); } } /*--------------------------------------------------------------------------* *--------------------------------------------------------------------------*/ void main(void) { BYTE P1DIRCFG; InitTimer1(10000); InitTrace386Port(); outportb(P1DIR, ~SONIC_TRIGGER_BIT); outportb(SONIC_TRIGGER_PORT, 0xFF); Enable_IRQ7(); Exam1(); Exam1A(); Exam2A(); Exam2B(); Exam2C(); Disable_IRQ7(); CloseTimer1(); }