版块导航
正在加载中...
客户端APP下载
论文辅导
调剂小程序
登录
注册
帖子
帖子
用户
本版
应《网络安全法》要求,自2017年10月1日起,未进行实名认证将不得使用互联网跟帖服务。为保障您的帐号能够正常使用,请尽快对帐号进行手机号验证,感谢您的理解与支持!
24小时热门版块排行榜
>
论坛更新日志
(7943)
>
考研
(3981)
>
导师招生
(2207)
>
休闲灌水
(331)
>
虫友互识
(310)
>
文献求助
(216)
>
考博
(184)
>
硕博家园
(180)
>
论文投稿
(113)
>
公派出国
(74)
>
教师之家
(70)
>
基金申请
(67)
>
招聘信息布告栏
(64)
>
博后之家
(52)
>
材料综合
(26)
>
找工作
(18)
小木虫论坛-学术科研互动平台
»
专业学科区
»
信息科学
»
模拟串口的程序
1
1/1
返回列表
查看: 138 | 回复: 0
只看楼主
@他人
存档
新回复提醒
(忽略)
收藏
在APP中查看
当前主题已经存档。
sdlj8051
金虫
(著名写手)
应助: 0
(幼儿园)
贵宾: 0.1
金币: 1149.8
帖子: 2254
在线: 18.1小时
虫号: 71297
[
资源
]
模拟串口的程序
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
]
回复此楼
» 猜你喜欢
265分求调剂不调专业和学校有行学上就
已经有7人回复
0856化工专硕求调剂
已经有11人回复
298求调剂
已经有6人回复
一志愿中南大学理学化学
已经有5人回复
272求调剂
已经有6人回复
0856材料求调剂
已经有12人回复
291分工科求调剂
已经有10人回复
295求调剂
已经有7人回复
0857调剂
已经有3人回复
材料学调剂
已经有11人回复
1楼
2007-01-08 01:33:25
已阅
回复此楼
关注TA
给TA发消息
送TA红花
TA的回帖
相关版块跳转
数理科学综合
机械
物理
数学
农林
食品
地学
能源
信息科学
土木建筑
航空航天
转基因
我要订阅楼主
sdlj8051
的主题更新
1
1/1
返回列表
☆ 无星级
★ 一星级
★★★ 三星级
★★★★★ 五星级
如果回帖内容含有宣传信息,请如实选中。否则帐号将被全论坛禁言
普通表情
龙
兔
虎
猫
高级回复
(可上传附件)
百度网盘
|
360云盘
|
千易网盘
|
华为网盘
在新窗口页面中打开自己喜欢的网盘网站,将文件上传后,然后将下载链接复制到帖子内容中就可以了。
最具人气热帖推荐
[查看全部]
作者
回/看
最后发表
[
考研
]
265分求调剂不调专业和学校有行学上就
+5
礼堂丁真258
2026-02-28
7/350
2026-03-01 19:12
by
Js512888
[
考研
]
材料类求调剂
+10
wana_kiko
2026-02-28
11/550
2026-03-01 18:11
by
海嵙Y
[
考研
]
高分子化学与物理调剂
+6
好好好1233
2026-02-28
11/550
2026-03-01 17:47
by
好好好1233
[
考博
]
26申博
+4
想申博!
2026-02-26
6/300
2026-03-01 17:32
by
想申博!
[
考研
]
化工专硕348,一志愿985求调剂
+5
弗格个
2026-02-28
8/400
2026-03-01 17:25
by
sunny81
[
考研
]
化工专硕342,一志愿大连理工大学,求调剂
+3
kyf化工
2026-02-28
4/200
2026-03-01 16:49
by
yywzz
[
考研
]
311求调剂
+6
亭亭亭01
2026-03-01
6/300
2026-03-01 15:41
by
324616
[
考研
]
304求调剂
+6
曼殊2266
2026-02-28
7/350
2026-03-01 15:14
by
wjLi2017
[
考研
]
材料工程274求调剂
+3
Lilithan
2026-03-01
3/150
2026-03-01 14:58
by
ms629
[
考研
]
298求调剂
+9
人间唯你是清欢
2026-02-28
12/600
2026-03-01 14:23
by
Ducount.Y
[
考研
]
材料284求调剂,一志愿郑州大学英一数二专硕
+10
想上岸的土拨鼠
2026-02-28
10/500
2026-03-01 14:12
by
yc258
[
考研
]
302材料工程求调剂
+4
Doleres
2026-03-01
5/250
2026-03-01 11:52
by
liqiongjy
[
考研
]
调剂
+3
简木ChuFront
2026-02-28
3/150
2026-03-01 11:46
by
王伟要上岸啊
[
考研
]
317一志愿华南理工电气工程求调剂
+6
Soliloquy_Q
2026-02-28
11/550
2026-03-01 11:14
by
歌liekkas
[
考研
]
311求调剂
+9
南迦720
2026-02-28
10/500
2026-03-01 10:55
by
sunny81
[
硕博家园
]
2025届双非化工硕士毕业,申博
+3
更多的是
2026-02-27
4/200
2026-03-01 10:04
by
ztg729
[
论文投稿
]
求助coordination chemistry reviews 的写作模板
10
+3
ljplijiapeng
2026-02-27
4/200
2026-03-01 09:07
by
babero
[
论文投稿
]
Optics letters投稿被拒求助
30
+3
luckyry
2026-02-26
4/200
2026-03-01 09:06
by
babero
[
考研
]
272求调剂
+4
田智友
2026-02-28
4/200
2026-03-01 06:43
by
刘兵
[
高分子
]
求环氧树脂研发1名
+3
孙xc
2026-02-25
11/550
2026-02-28 16:57
by
ichall
信息提示
关闭
请填处理意见
关闭
确定