1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| #include "uart.h"
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.H>
#include <stddef.H>
#include <stdlib.H>
/***************************************/
/* */
/* fonction configuration du composant */
/* */
/***************************************/
void uart_16550( int *voie, unsigned int *vitesse,
int *nb_stop_bit, int *parity,
int *type_of_parity, int *nb_bit_data )
{
/* initialiser la configuration */
unsigned int choix;
clrscr(); /* efface l'ecran */
/*
* VITESSE
*/
/* mettre DLAB a 1 pour regler la vitesse */
outportb( *voie + LCR, 0x80 );
outportb( *voie + DRL, 0x0C );
outportb( *voie + DRH, 0x00 );
*vitesse = 9600;
/* mettre DLAB a 0 */
outportb( *voie + LCR, 0x00 );
/*
* PARITE ET BIT DE STOP
*/
outportb( *voie + LCR, 0x03 );
*nb_stop_bit = 1;
*parity = 0;
*type_of_parity = 0;
*nb_bit_data = 8;
}
/**************************************/
/* */
/* fonction d'emission d'un octet */
/* texte : caractere a emettre */
/* port : contient l'@ du port com X */
/* */
/**************************************/
void emission( unsigned char texte, int port )
{
while( ( inportb( port + LSR ) & 0x20 ) != 0x20 )
{
} /* boucle infini */
outportb( port + TDR, texte );
}
/***************************************/
/* */
/* fonction de reception d'un octet */
/* port : contient l'@ du port com X */
/* */
/***************************************/
unsigned char reception( int port )
{
unsigned char recu = 'n';
/* test si le registre de LSR a une donnee presente */
while( ( inportb( port + LSR ) & 0x01 ) != 0x01 )
{
} /* boucle infini */
recu = inportb( port + RDR );
return( recu );
}
/**********************************************************/
/* */
/* fonction de reception de N octet */
/* buffer : contient l'@ depart pour octets en reception */
/* Maxbuff : taille d'octet desire par l'utilisateur */
/* port : contient l'@ du port com X */
/* */
/**********************************************************/
void RChnX( unsigned char *buffer, int port, int taille )
{
int i;
for( i = 0 ; i < taille ; i++ ) /* test si le registre de stockage emission est vide */
{
/* test si le registre de LSR a une donnee presente */
while( ( inportb( port + LSR ) & 0x01 ) != 0x01 )
{
} /* boucle infinie */
*buffer = reception( port );
buffer++;
}
}
/***********************/
/* */
/* fonction principale */
/* */
/***********************/
int main( int argc, char **argv )
{
unsigned char data_emi_1_octet = 'p';
unsigned char data_recu_1_octet;
unsigned char data_recu_N_octet[50];
int i;
int poids;
uart_1655 port_serie1;
port_serie1.voie = COM1;
uart_16550( &port_serie1.voie, &port_serie1.baud, &port_serie1.n_bit_stop,
&port_serie1.parity, &port_serie1.odd_or_even, &port_serie1.n_bit_data );
for( i = 0 ; i < 50 ; i++ )
data_recu_N_octet[i] = '\x0';
/*
* LOCAL
*/
outportb( ( port_serie1.voie ) + MCR, 0x00 );
/**********************************/
/* */
/* reception emission sur 1 octet */
/* */
/**********************************/
/* emission */
fflush( stdin );
fflush( stdout );
data_emi_1_octet = 0x50;
emission( data_emi_1_octet, port_serie1.voie );
RChnX( data_recu_N_octet, port_serie1.voie, 7 );
printf( "\n%s|", data_recu_N_octet );
data_emi_1_octet = 0x49;
emission( data_emi_1_octet, port_serie1.voie );
RChnX( data_recu_N_octet, port_serie1.voie, 40 );
printf( "\n%s|", data_recu_N_octet );
return poids;
} |
Partager