pc机与单片机的通讯设计(编辑修改稿)内容摘要:

ASK reg */ define INTC_EOI 0x20 /* Interrupt controller EOI reg */ define MAX_PORTS 2 /* I/O ports (DOS limit) */ static int count = 0。 static COM _list[MAX_PORTS]。 /* I/O data structure */ static COM *1。 /* Pointers for interrupt actions */ static COM *2。 static COM *_xfer。 /* Transfer interrupt data structure */ COM *ser_init0(int port,char *ibuf,int isize, char *obuf,int osize)。 void ser_close0( COM *c )。 void (_interrupt _far int_ser1)( void )。 /* Int rtn for serial I/O COM 1 */ void (_interrupt _far int_ser2)( void )。 /* Int rtn for serial I/O COM 2 */ void (_interrupt _far int_ser_sup)( void )。 /* Support int actions */ COM *ser_init( int port,int baud,int bit,int parity,int isize,int osize ) { unsigned status。 char ch。 COM *c。 char *in_buf,*out_buf。 中国最庞大的下资料库 (整理 . 版权归原作者所有 ) 第 22 页 共 42 页 status = _bios_serial(_COM_INIT,port,(bit | parity | _COM_STOP2| baud ))。 in_buf = malloc( isize )。 if( in_buf == NULL ) return( NULL )。 out_buf = malloc( osize )。 if( out_buf == NULL ) return( NULL )。 c = ser_init0(port,in_buf,isize,out_buf,osize )。 clean_ser(c)。 return( c )。 } void ser_close(COM *c) { int i。 if( !cready ) return。 ser_close0(c)。 free( cin_buf )。 free( cout_buf )。 } 中国最庞大的下资料库 (整理 . 版权归原作者所有 ) 第 23 页 共 42 页 char serline( COM *c ) { if( !cready ) return(FALSE)。 if( cin_crt 0 ) return( TRUE )。 else return( FALSE )。 } int getsers( COM *c,int len,char *str ) { char ch。 int i。 i = 0。 while( ilen ) { while( !serhit(c) ) { if(kbhit()) return( 1 )。 } ch = 0x7f amp。 getser(c)。 switch( ch ) { case 0x0d: str[i++] = 39。 \039。 return( i )。 case 0x00: case 0x0a: break。 default: str[i++] = ch。 break。 中国最庞大的下资料库 (整理 . 版权归原作者所有 ) 第 24 页 共 42 页 } } str[i] = 39。 \039。 return( len )。 } int putsers( char *str, COM *c ) { int n,i,j。 n = strlen( str )。 for( i=0。 in。 i++ ) { while( !putser( str[i],c ) )。 } return( n )。 } char putser( char outch, COM *c ) { char val。 if( !cready ) return(FALSE)。 while( !cout_mt amp。 amp。 (cout_head == cout_tail) )。 if( !cout_full ) { cout_buf[cout_head++] = outch。 中国最庞大的下资料库 (整理 . 版权归原作者所有 ) 第 25 页 共 42 页 if( cout_head == cout_size ) cout_head = 0。 /* Reset buffer circularly */ } if( cout_head == cout_tail ) { cout_full = TRUE。 return( FALSE )。 } else cout_full = FALSE。 val = inp( SREG(LCR) )。 /* Reset DLAB for IER access */ val amp。 = 0x7F。 /* Clear IER access bit */ outp(SREG(LCR),val)。 val = inp( SREG(IER) )。 if( !(val amp。 0x02) ) /* Interrupt ON ? */ { cout_mt = FALSE。 /* Not MT now */ _disable()。 /* Interrupts OFF NOW */ outp(SREG(IER),0x03)。 /* RX amp。 TX interrupts ON */ _enable()。 /* Interrupts ON again */ } return( TRUE )。 } char serhit( COM *c ) { if( !cready ) return(FALSE)。 if( !cin_mt ) return( TRUE )。 中国最庞大的下资料库 (整理 . 版权归原作者所有 ) 第 26 页 共 42 页 else return( FALSE )。 } int getser( COM *c ) { int ch。 if( !cready ) return(FALSE)。 if( !serhit(c) ) return( 0 )。 _disable()。 ch = 0xff amp。 cin_buf[cin_tail++]。 if( cin_tail == cin_size ) cin_tail = 0。 if( cin_tail == cin_head ) cin_mt = TRUE。 if( ch == CR ) /* Keep track of CR39。 s */ cin_crt。 _enable()。 return( ch )。 } void clean_ser( COM *c ) { _disable()。 cin_head = 0。 中国最庞大的下资料库 (整理 . 版权归原作者所有 ) 第 27 页 共 42 页 cin_tail = 0。 cin_mt = TRUE。 cin_crt = 0。 _enable()。 } void tl_dtr( int flag,COM *c ) { char val。 if( !cready ) return。 val = inp(SREG(MCR))。 if( flag ) val |= 1。 else val amp。 = ~1。 outp(SREG(MCR),val)。 } void tl_rts( int flag, COM *c ) { char val。 if( !cready ) return。 val = inp(SREG(MCR))。 中国最庞大的下资料库 (整理 . 版权归原作者所有 ) 第 28 页 共 42 页 if( flag ) val |= 2。 else val amp。 = ~2。 outp(SREG(MCR),val)。 } COM *ser_init0(int port,char *ibuf,int isize, char *obuf,int osize) { int i。 char val。 COM *c。 while( port = MAX_PORTS ) /* Get port in range */ port。 for( i=0。 iMAX_PORTS。 i++ ) /* Select data structure */ { if( !_list[i].ready ) { c = amp。 (_list[i])。 break。 } } if( i == MAX_PORTS ) /* Not found */ return( NULL )。 cin_buf = ibuf。 cin_size = isize。 cin_mt = TRUE。 cin_head = 0。 cin_tail = 0。 cin_crt = 0。 中国最庞大的下资料库 (整理 . 版权归原作者所有 ) 第 29 页 共 42 页 cout_buf = obuf。 cout_size = osize。 cout_full = FALSE。 cout_mt = TRUE。 cout_head = 0。 cout_tail = 0。 switch( port ) { case 0: /* Here set up for COM1 */ cready = TRUE。 c_base = COM1_BASE。 cirq_mask = COM1_IRQ_MASK。 cirq_eoi = COM1_IRQ_EOI。 cint_number = COM1_INT_NUM。 _disable()。 1 = c。 cold = _dos_getvect( cint_number )。 _dos_setvect(cint_number,int_ser1)。 break。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。