24小时热门版块排行榜    

Znn3bq.jpeg
查看: 149  |  回复: 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 的主题更新
☆ 无星级 ★ 一星级 ★★★ 三星级 ★★★★★ 五星级
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 化工学硕294分,求导师收留 +18 yzyzx 2026-04-12 18/900 2026-04-13 10:20 by sophia_93
[考研] 085600材料与化工349分求调剂 +9 李木子啊哈哈 2026-04-12 10/500 2026-04-13 08:45 by Sammy2
[考研] 复试调剂 +14 积极向上; 2026-04-10 16/800 2026-04-12 20:02 by gruyclewee
[考研] 332求调剂 +14 蕉蕉123 2026-04-10 14/700 2026-04-12 00:27 by 蓝云思雨
[考研] 288求调剂,一志愿华南理工大学071005 +18 ioodiiij 2026-04-08 18/900 2026-04-11 20:25 by liyun12321
[考研] 085400 328分 求调剂 +10 喂你一个大橙子 2026-04-09 14/700 2026-04-11 19:53 by lqspecial
[考研] 269求调剂 +11 啊啊我我 2026-04-07 11/550 2026-04-11 16:45 by vgtyfty
[考研] 求调剂,一志愿大连理工大学354分 +5 雨声余生 2026-04-11 6/300 2026-04-11 16:12 by 雨声余生
[考研] 275求调剂 +9 1624447980 2026-04-08 10/500 2026-04-11 10:20 by Delta2012
[考研] 346,工科求调剂 +3 moser233 2026-04-09 3/150 2026-04-11 10:04 by zhq0425
[考研] 化学工程与技术324调剂 +23 孙常华 2026-04-09 25/1250 2026-04-11 00:07 by 骑牛渡寒江
[考研] 一志愿211,化学310分,本科重点双非,求调剂 +23 努力奋斗112 2026-04-08 23/1150 2026-04-10 23:29 by 314126402
[考研] 考研调剂 +26 硕星赴 2026-04-09 27/1350 2026-04-10 22:24 by 猪会飞
[考研] 吉大计算机技术331分,英语六级,求调剂 +3 峰峰021116 2026-04-09 3/150 2026-04-10 20:01 by chemisry
[考研] 348求调剂 +3 candyyyi 2026-04-09 3/150 2026-04-09 17:20 by 段伟艳
[考研] 308求调剂 +17 墨墨漠 2026-04-06 17/850 2026-04-09 09:25 by 壹往無前
[考研] 22408 调剂材料 +7 我叫ez 2026-04-06 8/400 2026-04-07 17:12 by 蓝云思雨
[考研] 328求调剂 +4 ghhh88888 2026-04-06 5/250 2026-04-07 14:45 by ghhh88888
[考研] 0854求调剂 +9 亨氏番茄沙司 2026-04-06 10/500 2026-04-07 14:37 by shdgaomin
[考研] 调剂 +4 mcbbc 2026-04-06 5/250 2026-04-07 12:33 by upczlm1989
信息提示
请填处理意见