版块导航
正在加载中...
客户端APP下载
论文辅导
调剂小程序
登录
注册
帖子
帖子
用户
本版
应《网络安全法》要求,自2017年10月1日起,未进行实名认证将不得使用互联网跟帖服务。为保障您的帐号能够正常使用,请尽快对帐号进行手机号验证,感谢您的理解与支持!
24小时热门版块排行榜
>
论坛更新日志
(9702)
>
考研
(4544)
>
导师招生
(2820)
>
虫友互识
(437)
>
休闲灌水
(401)
>
考博
(290)
>
文献求助
(274)
>
硕博家园
(252)
>
论文投稿
(151)
>
公派出国
(97)
>
招聘信息布告栏
(96)
>
教师之家
(92)
>
基金申请
(82)
>
博后之家
(68)
>
材料综合
(26)
>
找工作
(22)
小木虫论坛-学术科研互动平台
»
专业学科区
»
信息科学
»
模拟串口的程序
1
1/1
返回列表
查看: 139 | 回复: 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
]
回复此楼
» 猜你喜欢
求调剂
已经有4人回复
一志愿郑大材料学硕298分,求调剂
已经有5人回复
材料学硕318求调剂
已经有12人回复
材料化工调剂
已经有13人回复
材料学硕318求调剂
已经有5人回复
292求调剂
已经有6人回复
0856材料与化工,270求调剂
已经有6人回复
江苏省农科院招调剂1名
已经有3人回复
0854复试调剂 276
已经有3人回复
265分求调剂不调专业和学校有行学上就
已经有8人回复
高级回复
1楼
2007-01-08 01:33:25
已阅
回复此楼
关注TA
给TA发消息
送TA红花
TA的回帖
相关版块跳转
数理科学综合
机械
物理
数学
农林
食品
地学
能源
信息科学
土木建筑
航空航天
转基因
我要订阅楼主
sdlj8051
的主题更新
1
1/1
返回列表
☆ 无星级
★ 一星级
★★★ 三星级
★★★★★ 五星级
如果回帖内容含有宣传信息,请如实选中。否则帐号将被全论坛禁言
普通表情
龙
兔
虎
猫
高级回复
(可上传附件)
百度网盘
|
360云盘
|
千易网盘
|
华为网盘
在新窗口页面中打开自己喜欢的网盘网站,将文件上传后,然后将下载链接复制到帖子内容中就可以了。
最具人气热帖推荐
[查看全部]
作者
回/看
最后发表
[
考研
]
0854复试调剂 276
+3
wmm9
2026-03-01
3/150
2026-03-01 23:13
by
热情沙漠
[
考研
]
265分求调剂不调专业和学校有行学上就
+6
礼堂丁真258
2026-02-28
8/400
2026-03-01 22:50
by
jian_
[
考研
]
275求调剂
+3
明远求学
2026-03-01
3/150
2026-03-01 22:29
by
刘兵
[
硕博家园
]
博士自荐
+7
科研狗111
2026-02-26
11/550
2026-03-01 22:24
by
哲平L
[
考研
]
材料类求调剂
+10
wana_kiko
2026-02-28
12/600
2026-03-01 22:10
by
海嵙Y
[
考研
]
272求调剂
+6
田智友
2026-02-28
6/300
2026-03-01 21:40
by
公瑾逍遥
[
考研
]
298求调剂
+6
axyz3
2026-02-28
6/300
2026-03-01 19:00
by
18137688336
[
考研
]
272求调剂
+6
材紫有化
2026-02-28
6/300
2026-03-01 18:58
by
18137688336
[
考研
]
0856材料求调剂
+11
hyf hyf hyf
2026-02-28
12/600
2026-03-01 18:57
by
18137688336
[
考研
]
材料学调剂
+9
提神豆沙包
2026-02-28
11/550
2026-03-01 18:15
by
ms629
[
考博
]
26申博
+4
想申博!
2026-02-26
6/300
2026-03-01 17:32
by
想申博!
[
考研
]
328求调剂
+3
aaadim
2026-03-01
5/250
2026-03-01 17:29
by
njzyff
[
考研
]
307求调剂
+5
wyyyqx
2026-03-01
5/250
2026-03-01 15:21
by
Fff-1
[
考研
]
304求调剂
+6
曼殊2266
2026-02-28
7/350
2026-03-01 15:14
by
wjLi2017
[
考研
]
295复试调剂
+3
简木ChuFront
2026-03-01
3/150
2026-03-01 14:27
by
zzxw520th
[
硕博家园
]
2025届双非化工硕士毕业,申博
+3
更多的是
2026-02-27
4/200
2026-03-01 10:04
by
ztg729
[
基金申请
]
面上模板改不了页边距吧?
+5
ieewxg
2026-02-25
6/300
2026-03-01 00:10
by
addressing
[
考研
]
304求调剂
+3
52hz~~
2026-02-28
5/250
2026-03-01 00:00
by
52hz~~
[
高分子
]
求环氧树脂研发1名
+3
孙xc
2026-02-25
11/550
2026-02-28 16:57
by
ichall
[
硕博家园
]
【博士招生】太原理工大学2026化工博士
+4
N1ce_try
2026-02-24
8/400
2026-02-26 08:40
by
N1ce_try
信息提示
关闭
请填处理意见
关闭
确定