#include #include #include #include #include #include #include "TB386EX.h" #define TIMER_INT 0x72 /* 8259 registers */ #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 */ typedef unsigned char BYTE; BYTE master_org, slave_org; #define STEPPORT 0x201 /* Step Motor Pulse Output */ #define STEPPOWER 0x203 /* Step Motor Power Level */ BYTE MPhaseOne[] = { 0x22,0x88,0x11,0x44 }; /* 1상 구동 */ BYTE MPhaseTwo[] = { 0x55,0x66,0xAA,0x99 }; /* 2상 구동 */ BYTE MPhase1_2[] = { 0x11,0x55,0x44,0x66,0x22,0xAA,0x88,0x99 }; /* 1-2상 구동 */ BYTE MPhaseRotate[] = {0x11,0x15,0x54,0x56,0x42,0x4A,0x68,0x69, 0x21,0x25,0xA4,0xA6,0x82,0x8A,0x98,0x99}; /* 회전 구동 */ volatile int HardDelayTimerCount; unsigned long AbsHardTimerCount; void Delay_10ms(int count) { HardDelayTimerCount = 0; for (;;) if (HardDelayTimerCount > count) break; } void interrupt NewIRQ10(void) /*IRQ10*/ { /* code for Exam-B Here */ HardDelayTimerCount++; AbsHardTimerCount++; 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 InitTrace386Port(void) { outportb(P1CFG, 0x00); outportb(P1DIR, 0xEB); outportb(P2DIR, 0x7F); outportb(P3CFG, 0xBC); outportb(P3DIR, 0xFC); outportb(P3LTC, 0xFD); } void Exam1(void) { int i; printf("EXAM_A\nSingle Phase 100 step Forward Drive\n"); outportb(STEPPOWER, 0xF5); for (i = 0; i < 100; i++) { outportb(STEPPORT, MPhaseOne[i % 4]); Delay_10ms(2); } outportb(STEPPOWER, 0xFF); } void Exam_A1(void) { int i; printf("EXAM_A1\nSingle Phase 100 step Backward Drive\n"); outportb(STEPPOWER, 0xF5); for (i = 100; i > 0; i--) { outportb(STEPPORT, MPhaseOne[i % 4]); Delay_10ms(2); } outportb(STEPPOWER, 0xFF); } void Exam_A2(void) { int i; printf("EXAM_A2\nDouble Phase 100 step Forward Drive\n"); outportb(STEPPOWER, 0xF5); for (i = 0; i < 100; i++) { outportb(STEPPORT, MPhaseOne[i % 4]); Delay_10ms(2); } outportb(STEPPOWER, 0xFF); } void Exam_A3(void) { int i; printf("EXAM_A3\nSingle-Double Phase 100 step Forward Drive\n"); outportb(STEPPOWER, 0xF5); for (i = 0; i < 100; i++) { outportb(STEPPORT, MPhase1_2[i % 8]); Delay_10ms(2); } outportb(STEPPOWER, 0xFF); } int MaxCount = 4000; void speed(void) { long num, mok, mod; num = 293904; /* (5.2*3.14) * 1000000 / 200 * 3600 / 100000 */ mok = num / MaxCount / 200; mod = num - mok * MaxCount * 200; mod = mod * 1000 / MaxCount / 200; if(mod >= 100) { printf("%5dusec %ld.%ldKm/h \n", MaxCount, mok, mod); } else if(mod >= 10) { printf("%5dusec %ld.0%ldKm/h \n", MaxCount, mok, mod); } else { printf("%5dusec %ld.00%ldKm/h \n", MaxCount, mok, mod); } } void Exam_B(void) { int i; unsigned int CM, Speed_int, Speed_mod; unsigned long Speed, Sec; char ch; printf("EXAM_B\nSingle Phase Forward Drive(Speed +/-)\n"); outportb(STEPPOWER, 0xF5); InitTimer1(MaxCount); for ( i=0; ; i++ ) { if(kbhit()) { ch=getch(); if(ch == '+') { if(MaxCount > 200) { MaxCount -= 50; InitTimer1(MaxCount); printf("%d\n", MaxCount); } } else if(ch == '-') { if(MaxCount < 4000) { MaxCount += 50; InitTimer1(MaxCount); printf("%d\n", MaxCount); } } else if(ch == 'q') { break; } speed(); } outportb(STEPPORT, MPhaseOne[i % 4]); Delay_10ms(2); } outportb(STEPPOWER, 0xFF); } void Exam_C(void) { int i; Exam_A3(); printf("EXAM_A\nSingle Phase 100 step Forward Drive\n"); outportb(STEPPOWER, 0xF5); for (i = 0; i < 560; i++) { outportb(STEPPORT, MPhaseRotate[i % 16]); Delay_10ms(2); } outportb(STEPPOWER, 0xFF); Exam_A3(); } void main(void) { char junk; InitTrace386Port(); InitTimer1(4000); Exam1(); junk = getch(); Exam_A1(); junk = getch(); Exam_A2(); junk = getch(); Exam_A3(); junk = getch(); Exam_B(); junk = getch(); Exam_C(); outportb(STEPPORT, 0x00); outportb(STEPPOWER, 0xFF); CloseTimer1(); }