全部博文(113)
分类: 嵌入式
2011-07-01 10:59:55
图1 调用INT 14H时AL寄存器设置 |
#include #include #include #define STR "author:sbh" union REGS inregs,outregs; main() { //设置串口参数 init_rs232(); //写串口的例子 write_rs232(STR,strlen(STR)); //读串口的例子 read_rs232(); return(0); } init_rs232() { do{ inregs.h.ah=0; //AH=0表示初始化端口 inregs.h.al=0xe7; inregs.x.dx=0; //COM1 int86(0x14, &inregs, &outregs); }while(outregs.h.ah>=0x80); return(0); } write_rs232(char *string, int len) { int i; do{ inregs.h.ah=1;//发送AL寄存器的字符 inregs.h.al= *string; inregs.x.dx=0; int86(0x14, &inregs, &outregs); }while(outregs.h.al>=0x80); for(i=1;i inregs.h.ah=1; inregs.h.al=*(string+i); inregs.x.dx=0; int86(0x14, &inregs, &outregs); } } read_rs232() { do{ inregs.h.ah=2; //读取AL寄存器中的字符 inregs.x.dx=0; int86(0x14, &inregs, &outregs); }while(outregs.h.al!=3||outregs.h.ah>=0x80); return(0); } |
int _Cdecl int86(int intno, union REGS *inregs, union REGS *outregs); |
union REGS { struct WORDREGS x; struct BYTEREGS h; }; |
struct WORDREGS { unsigned int ax, bx, cx, dx, si, di, cflag /*进位标志*/, flags /*标志寄存器*/; }; |
struct BYTEREGS { unsigned char al, ah, bl, bh, cl, ch, dl, dh; }; |
Int _Cdecl int86x(int intno, union REGS inregs, union REGS outregs, struct SREGS segregs); |
struct SREGS { unsigned int es; unsigned int cs; unsigned int ss; unsigned int ds; }; |
图2 COM中断号 |
INT (Hex) | IRQ | Common Uses |
08 | 0 | System Timer |
09 | 1 | Keyboard |
0A | 2 | Redirected |
0B | 3 | Serial Comms. COM2/COM4 |
0C | 4 | Serial Comms. COM1/COM3 |
0D | 5 | Reserved/Sound Card |
0E | 6 | Floppy Disk Controller |
0F | 7 | Parallel Comms. |
70 | 8 | Real Time Clock |
71 | 9 | Reserved |
72 | 10 | Reserved |
73 | 11 | Reserved |
74 | 12 | PS/2 Mouse |
75 | 13 | Maths Co-Processor |
76 | 14 | Hard Disk Drive |
77 | 15 | Reserved |
/*dos.h*/ void _Cdecl setvect (int interruptno, void interrupt (*isr) ()); |
setvect(0x0C, PORT1INT); |
void interrupt PORT1INT() { int c; do { c = inportb(PORT1 + 5); if (c &1) { buffer[bufferin] = inportb(PORT1); bufferin++; if (bufferin == 1024) bufferin = 0; } } while (c &1); outportb(0x20, 0x20); } |
/*dos.h*/ void interrupt (* _Cdecl getvect(int interruptno)) (); |
oldport1isr = getvect(INTVECT); |
void interrupt (*oldport1isr)(); |
/* Name : Sample Comm's Program - 1024 Byte Buffer - buff1024.c */ /* Written By : Craig Peacock #include #include #include #define PORT1 0x3F8 /* Port Address Goes Here */ #define INTVECT 0x0C /* Com Port's IRQ here (Must also change PIC setting) */ /* Defines Serial Ports Base Address */ /* COM1 0x3F8 */ /* COM2 0x2F8 */ /* COM3 0x3E8 */ /* COM4 0x2E8 */ int bufferin = 0; int bufferout = 0; char ch; char buffer[1025]; void interrupt(*oldport1isr)(); void interrupt PORT1INT() /* Interrupt Service Routine (ISR) for PORT1 */ { int c; do { c = inportb(PORT1 + 5); if (c &1) { buffer[bufferin] = inportb(PORT1); bufferin++; if (bufferin == 1024) { bufferin = 0; } } } while (c &1); outportb(0x20, 0x20); } void main(void) { int c; outportb(PORT1 + 1, 0); /* Turn off interrupts - Port1 */ oldport1isr = getvect(INTVECT); /* Save old Interrupt Vector of later recovery */ setvect(INTVECT, PORT1INT); /* Set Interrupt Vector Entry */ /* COM1 - 0x0C */ /* COM2 - 0x0B */ /* COM3 - 0x0C */ /* COM4 - 0x0B */ /* PORT 1 - Communication Settings */ outportb(PORT1 + 3, 0x80); /* SET DLAB ON */ outportb(PORT1 + 0, 0x0C); /* Set Baud rate - Divisor Latch Low Byte */ /* Default 0x03 = 38,400 BPS */ /* 0x01 = 115,200 BPS */ /* 0x02 = 57,600 BPS */ /* 0x06 = 19,200 BPS */ /* 0x0C = 9,600 BPS */ /* 0x18 = 4,800 BPS */ /* 0x30 = 2,400 BPS */ outportb(PORT1 + 1, 0x00); /* Set Baud rate - Divisor Latch High Byte */ outportb(PORT1 + 3, 0x03); /* 8 Bits, No Parity, 1 Stop Bit */ outportb(PORT1 + 2, 0xC7); /* FIFO Control Register */ outportb(PORT1 + 4, 0x0B); /* Turn on DTR, RTS, and OUT2 */ outportb(0x21, (inportb(0x21) &0xEF)); /* Set Programmable Interrupt Controller */ /* COM1 (IRQ4) - 0xEF */ /* COM2 (IRQ3) - 0xF7 */ /* COM3 (IRQ4) - 0xEF */ /* COM4 (IRQ3) - 0xF7 */ outportb(PORT1 + 1, 0x01); /* Interrupt when data received */ printf("\nSample Comm's Program. Press ESC to quit \n"); do { if (bufferin != bufferout) { ch = buffer[bufferout]; bufferout++; if (bufferout == 1024) { bufferout = 0; } printf("%c", ch); } if (kbhit()) { c = getch(); outportb(PORT1, c); } } while (c != 27); outportb(PORT1 + 1, 0); /* Turn off interrupts - Port1 */ outportb(0x21, (inportb(0x21) | 0x10)); /* MASK IRQ using PIC */ /* COM1 (IRQ4) - 0x10 */ /* COM2 (IRQ3) - 0x08 */ /* COM3 (IRQ4) - 0x10 */ /* COM4 (IRQ3) - 0x08 */ setvect(INTVECT, oldport1isr); /* Restore old interrupt vector */ } |
/* Name : Sample Comm's Program - Polled Version - termpoll.c */ /* Written By : Craig Peacock #include #include #include 00000000000000000000#define PORT1 0x3F8 /* Defines Serial Ports Base Address */ /* COM1 0x3F8 */ /* COM2 0x2F8 */ /* COM3 0x3E8 */ /* COM4 0x2E8 */ void main(void) { int c; int ch; outportb(PORT1 + 1, 0); /* Turn off interrupts - Port1 */ /* PORT 1 - Communication Settings */ outportb(PORT1 + 3, 0x80); /* SET DLAB ON */ outportb(PORT1 + 0, 0x03); /* Set Baud rate - Divisor Latch Low Byte */ /* Default 0x03 = 38,400 BPS */ /* 0x01 = 115,200 BPS */ /* 0x02 = 57,600 BPS */ /* 0x06 = 19,200 BPS */ /* 0x0C = 9,600 BPS */ /* 0x18 = 4,800 BPS */ /* 0x30 = 2,400 BPS */ outportb(PORT1 + 1, 0x00); /* Set Baud rate - Divisor Latch High Byte */ outportb(PORT1 + 3, 0x03); /* 8 Bits, No Parity, 1 Stop Bit */ outportb(PORT1 + 2, 0xC7); /* FIFO Control Register */ outportb(PORT1 + 4, 0x0B); /* Turn on DTR, RTS, and OUT2 */ printf("\nSample Comm's Program. Press ESC to quit \n"); do { c = inportb(PORT1 + 5); /* Check to see if char has been */ /* received. */ if (c &1) { ch = inportb(PORT1); /* If so, then get Char */ printf("%c", ch); } /* Print Char to Screen */ if (kbhit()) { ch = getch(); /* If key pressed, get Char */ outportb(PORT1, ch); } /* Send Char to Serial Port */ } while (ch != 27); /* Quit when ESC (ASC 27) is pressed */ } |
c = inportb(PORT1 + 5); /* Check to see if char has been */ /* received. */ if (c &1) |
|