24小时热门版块排行榜    

查看: 142  |  回复: 0
当前主题已经存档。

sdlj8051

金虫 (著名写手)


[资源] 模拟串口的程序

CODE:
// 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.

#include

#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;
}

  




 

[ Last edited by sdlj8051 on 2007-1-8 at 01:50 ]
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 sdlj8051 的主题更新
☆ 无星级 ★ 一星级 ★★★ 三星级 ★★★★★ 五星级
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 081700 调剂 267分 +7 迷人的哈哈 2026-03-23 7/350 2026-03-23 23:19 by 星空星月
[考研] 一志愿国科过程所081700,274求调剂 +3 三水研0水立方 2026-03-23 3/150 2026-03-23 23:11 by MajorWen
[考研] 269求调剂 +4 我想读研11 2026-03-23 4/200 2026-03-23 21:25 by pswait
[考研] 一志愿重庆大学085700资源与环境,总分308求调剂 +7 墨墨漠 2026-03-23 8/400 2026-03-23 20:36 by Creta
[考研] 336求调剂 +4 收到VS 2026-03-20 4/200 2026-03-23 19:02 by macy2011
[考研] 276求调剂。有半年电池和半年高分子实习经历 +8 材料学257求调剂 2026-03-23 9/450 2026-03-23 13:01 by ztnimte
[考研] 307求调剂 +11 冷笙123 2026-03-17 11/550 2026-03-22 20:16 by edmund7
[考研] 一志愿 西北大学 ,070300化学学硕,总分287,双非一本,求调剂。 +3 晨昏线与星海 2026-03-20 3/150 2026-03-22 16:00 by ColorlessPI
[基金申请] 山东省面上项目限额评审 +4 石瑞0426 2026-03-19 4/200 2026-03-22 08:50 by Wei_ren
[考研] 278求调剂 +9 烟火先于春 2026-03-17 9/450 2026-03-21 17:47 by 学员8dgXkO
[考研] 268求调剂 +9 简单点0 2026-03-17 9/450 2026-03-21 15:37 by lature00
[考研] 332求调剂 +3 凤凰院丁真 2026-03-20 3/150 2026-03-21 10:27 by luoyongfeng
[考研] 一志愿天津大学化学工艺专业(081702)315分求调剂 +12 yangfz 2026-03-17 12/600 2026-03-21 03:30 by JourneyLucky
[考研] 301求调剂 +10 yy要上岸呀 2026-03-17 10/500 2026-03-21 03:14 by JourneyLucky
[考研] 材料工程(专)一志愿985 初试335求调剂 +3 hiloiy 2026-03-17 4/200 2026-03-21 03:04 by JourneyLucky
[考研] 332求调剂 +4 ydfyh 2026-03-17 4/200 2026-03-21 02:20 by JourneyLucky
[考研] 一志愿吉林大学材料学硕321求调剂 +11 Ymlll 2026-03-18 15/750 2026-03-20 19:40 by 丁丁*
[论文投稿] 申请回稿延期一个月,编辑同意了。但系统上的时间没变,给编辑又写邮件了,没回复 10+3 wangf9518 2026-03-17 4/200 2026-03-19 23:55 by babero
[考研] 085600材料与化工调剂 324分 +10 llllkkkhh 2026-03-18 12/600 2026-03-19 14:33 by llllkkkhh
[考研] 收复试调剂生 +4 雨后秋荷 2026-03-18 4/200 2026-03-18 14:16 by elevennnne
信息提示
请填处理意见