电路设计,软件开发,专业项目团队承接电子产品开发!


ISPRO下载型编程器
火爆销售中

RS232/TTL转换器
全IC串口窃电

RS232/TTL转换线
全IC串口窃电

ByteBlasterMV Cable
CPLD/FPGA并口下载线

ByteBlaster II Cable ISPRO下载型编程器
支持S51/AVR/PIC
超级51开发板套件
前卫实用超值

PC/MCU/CPLD
三合一学习套件

全新万年历方案
AVR单片机开发板
 

Min51单片机仿真机
迷你型

ARM7开发板
仅售260元!
 

RS-232/RS-485双向接口电平转换器  

51单片机升级STM32
火爆销售中
MPLAB-ICD2
火爆销售中
LED51开发板
玩具又名图文显示棒

面板模块/开发板
经济危机明智决择

       

GSM MODEM GSM猫

SMS短信模块 工业级/全自动
火爆销售中

大功率数传电台

高速数传电台

您当前位置:电子技术资料 -> 外围驱动 -> 串口编程 ->

祝您阅读愉快!

ISPRO下载型编程器

RS232 TO TTL转换器
↑↑本文相关产品介绍↑↑

用高速输入输出口模拟一个全双工的串口


类别:技术文章
来源:未知
作者:未知
关键字:全双工串口
加入日期:2010-8-7

我想用80c196kc的高速输入口和高速输出口模拟一个全双工的串口,该怎么来实现?
IAR Application Note G - 001
1
IAR Application Note G - 001
Generic Software Uart
written in C
SUMMARY
This application note details a generic software UART written in C which
can be implemented on any microprocessor with a C compiler. It requires a
timer interrupt to be set to 3 times the baud rate, and two software-controlled
pins for the receive and transmit functions.
KEYWORDS
UART, C code
The Problem to be solved
Software UARTs are generally coded in assembly for speed purposes, but
with the increasing speed of processors, a software UART written in C is now
easily implemented and highly portable.
The Difficulties Involved
The implementation has three fundamental requirements:
1. A compiler must exist for the microprocessor.
2. A timer interrupt must be set to interrupt at three times the required
baud rate. Most microprocessors have a timer interrupt that can be
used for this, or an external clock interrupt could be used to achieve
the same goal.
3. There must be a transmit pin which can be set from the software, and a
receive pin which can be read by the software.
The Solution
The C source file must be linked into the user’s application with the necessary
interface routines listed as follows:
1. void get_rx_pin_status( void )
Returns 0 or 1 dependent on whether the receive pin is high or low.
2. void set_tx_pin_high( void )
Sets the transmit pin to the high state.
3. void set_tx_pin_low( void )
Sets the transmit pin to the low state.
4. void idle( void )
Background functions to execute while waiting for input.
5. void timer_set( int BAUD_RATE )
Sets the timer to 3 times the baud rate.
// UART.C
//
// Generic software uart written in C, requiring a timer set to 3 times
// the baud rate, and two software read/write pins for the receive and
// transmit functions.
//
// * Received characters are buffered
// * putchar(), getchar(), kbhit() and flush_input_buffer() are available
// * There is a facility for background processing while waiting for input
//
// Colin Gittins, Software Engineer, Halliburton Energy Services
//
// The baud rate can be configured by changing the BAUD_RATE macro as
// follows:
//
// #define BAUD_RATE 19200.0
//
// The function init_uart() must be called before any comms can take place
//
// Interface routines required:
// 1. get_rx_pin_status()
// Returns 0 or 1 dependent on whether the receive pin is high or low.
// 2. set_tx_pin_high()
// Sets the transmit pin to the high state.
// 3. set_tx_pin_low()
// Sets the transmit pin to the low state.
// 4. idle()
// Background functions to execute while waiting for input.
// 5. timer_set( BAUD_RATE )
// Sets the timer to 3 times the baud rate.
// 6. set_timer_interrupt( timer_isr )
// Enables the timer interrupt.
//
// Functions provided:
// 1. void flush_input_buffer( void )
// Clears the contents of the input buffer.
// 2. char kbhit( void )
// Tests whether an input character has been received.
// 3. char getchar( void )
// Reads a character from the input buffer, waiting if necessary.
// 4. void turn_rx_on( void )
// Turns on the receive function.
// 5. void turn_rx_off( void )
// Turns off the receive function.
// 6. void putchar( char )
// Writes a character to the serial port.
/*中国电子网www.ec66.com*/
#include <stdio.h>

#define BAUD_RATE 19200.0

#define IN_BUF_SIZE 256

#define TRUE 1
#define FALSE 0

static unsigned char inbuf[IN_BUF_SIZE];
static unsigned char qin = 0;
static unsigned char qout = 0;

static char flag_rx_waiting_for_stop_bit;
static char flag_rx_off;
static char rx_mask;
static char flag_rx_ready;
static char flag_tx_ready;
static char timer_rx_ctr;
static char timer_tx_ctr;
static char bits_left_in_rx;
static char bits_left_in_tx;
static char rx_num_of_bits;
static char tx_num_of_bits;
static char internal_rx_buffer;
static char internal_tx_buffer;
static char user_tx_buffer;

void timer_isr(void)
{
char mask, start_bit, flag_in;

// Transmitter Section
if ( flag_tx_ready )
{
if ( --timer_tx_ctr<=0 )
{
mask = internal_tx_buffer&1;
internal_tx_buffer >>= 1;
if ( mask )
{
set_tx_pin_high();
}
else
{
set_tx_pin_low();
}
timer_tx_ctr = 3;
if ( --bits_left_in_tx<=0 )
{
flag_tx_ready = FALSE;
}
}
}
// Receiver Section
if ( flag_rx_off==FALSE )
{
if ( flag_rx_waiting_for_stop_bit )
{
if ( --timer_rx_ctr<=0 )
{
flag_rx_waiting_for_stop_bit = FALSE;
flag_rx_ready = FALSE;
internal_rx_buffer &= 0xFF;
if ( internal_rx_buffer!=0xC2 )
{
inbuf[qin] =
internal_rx_buffer;
if ( ++qin>=IN_BUF_SIZE )
{
qin = 0;
}
}
}
}
else // rx_test_busy
{
if ( flag_rx_ready==FALSE )
{
start_bit = get_rx_pin_status();
// Test for Start Bit
if ( start_bit==0 )
{
flag_rx_ready = TRUE;
internal_rx_buffer = 0;
timer_rx_ctr = 4;
bits_left_in_rx =
rx_num_of_bits;
rx_mask = 1;
}
}
else // rx_busy
{
if ( --timer_rx_ctr<=0 )
{
// rcv
timer_rx_ctr = 3;
flag_in =
get_rx_pin_status();
if ( flag_in )
{

internal_rx_buffer |= rx_mask;
}
rx_mask <<= 1;
if ( --
bits_left_in_rx<=0 )
{

flag_rx_waiting_for_stop_bit = TRUE;
}
}
}
}
}
}

void init_uart( void )
{
flag_tx_ready = FALSE;
flag_rx_ready = FALSE;
flag_rx_waiting_for_stop_bit = FALSE;
flag_rx_off = FALSE;
rx_num_of_bits = 10;
tx_num_of_bits = 10;

set_tx_pin_low();

timer_set( BAUD_RATE );
set_timer_interrupt( timer_isr ); // Enable timer interrupt
}

char _getchar( void )
{
char ch;

do
{
while ( qout==qin )
{
idle();
}
ch = inbuf[qout] & 0xFF;
if ( ++qout>=IN_BUF_SIZE )
{
qout = 0;
}
}
while ( ch==0x0A || ch==0xC2 );
return( ch );
}

void _putchar( char ch )
{
while ( flag_tx_ready );
user_tx_buffer = ch;

// invoke_UART_transmit
timer_tx_ctr = 3;
bits_left_in_tx = tx_num_of_bits;
internal_tx_buffer = (user_tx_buffer<<1) | 0x200;
flag_tx_ready = TRUE;
}

void flush_input_buffer( void )
{
qin = 0;
qout = 0;
}

char kbhit( void )
{
return( qin!=qout );
}

void turn_rx_on( void )
{
flag_rx_off = FALSE;
}

void turn_rx_off( void )
{
flag_rx_off = TRUE;
}


您可能还会关注的相关技术资料:
暂无

中国电子网
www.EC66.com
QQ:34740080 EMAIL:iloveaoe@163.com
最佳浏览:1024*768
Copyright (C) 2001-2010 EC66.COM All Rights Reserved
Aoe/帝国 制作维护
粤ICP备18061901号