RE:Puerto
Publicado por
Alex (1 intervención) el 26/03/2001 21:35:24
Mira haber si con esto es suficiente, si tienes dudas dímelo
Un saludo
Alex.
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#define PORT1 0x3E8 // Direccion del puerto COM1
#define INTVECT 0x0C // irq 4
int bufferin = 0;
int bufferout = 0;
unsigned char ch;
char buffer[1025];
void interrupt (*oldport1isr)();
void interrupt PORT1INT() // Rutina de servicio de interrupciones para 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); // Salva el Vector de interrupciones
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 = 56,700 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 , (inportb(PORT1 + 3) & 0X7F));
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)); // Pone el PIC para com1 e irq4
outportb(PORT1 + 1 , 0x01); // interrupcion cuado recibes datos
printf("\nPuerto Serie. Pulsar ESC para salir \n");
do {
if (bufferin != bufferout){
ch = buffer[bufferout];
bufferout++;
if (bufferout == 1024) {bufferout = 0;}
printf("%u\n",ch);
printf("%f volts\n",(float)ch/256 * 5);
}
if (kbhit()){
c = getch();
outportb(PORT1, c);
}
} while (c !=27);
printf("Fin");
outportb(PORT1 + 1 , 0); /* Turn off interrupts - Port1 */
outportb(0x21,(inportb(0x21) | 0x10)); /* MASK IRQ using PIC */
/* COM1 (IRQ4) - 0x10 */
setvect(INTVECT, oldport1isr); // Restaura el alteriar vector de interrupciones
}