版块导航
正在加载中...
客户端APP下载
论文辅导
调剂小程序
登录
注册
帖子
帖子
用户
本版
应《网络安全法》要求,自2017年10月1日起,未进行实名认证将不得使用互联网跟帖服务。为保障您的帐号能够正常使用,请尽快对帐号进行手机号验证,感谢您的理解与支持!
24小时热门版块排行榜
>
论坛更新日志
(1444)
>
考研
(153)
>
导师招生
(132)
>
论文投稿
(40)
>
虫友互识
(34)
>
博后之家
(30)
>
公派出国
(23)
>
基金申请
(22)
>
教师之家
(22)
>
考博
(22)
>
硕博家园
(11)
>
找工作
(10)
>
论文道贺祈福
(9)
>
材料工程
(3)
>
签证指南
(3)
>
材料综合
(2)
小木虫论坛-学术科研互动平台
»
专业学科区
»
信息科学
»
模拟串口的程序
1
1/1
返回列表
查看: 142 | 回复: 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
]
回复此楼
» 猜你喜欢
284求调剂
已经有10人回复
一志愿山东大学药学学硕求调剂
已经有4人回复
07化学280分求调剂
已经有4人回复
298-一志愿中国农业大学-求调剂
已经有12人回复
求材料,环境专业调剂
已经有3人回复
335求调剂
已经有5人回复
求调剂
已经有7人回复
一志愿吉大化学322求调剂
已经有4人回复
环境学硕288求调剂
已经有8人回复
341求调剂(一志愿湖南大学070300)
已经有6人回复
高级回复
1楼
2007-01-08 01:33:25
已阅
回复此楼
关注TA
给TA发消息
送TA红花
TA的回帖
相关版块跳转
数理科学综合
机械
物理
数学
农林
食品
地学
能源
信息科学
土木建筑
航空航天
转基因
我要订阅楼主
sdlj8051
的主题更新
1
1/1
返回列表
☆ 无星级
★ 一星级
★★★ 三星级
★★★★★ 五星级
如果回帖内容含有宣传信息,请如实选中。否则帐号将被全论坛禁言
普通表情
龙
兔
虎
猫
高级回复
(可上传附件)
百度网盘
|
360云盘
|
千易网盘
|
华为网盘
在新窗口页面中打开自己喜欢的网盘网站,将文件上传后,然后将下载链接复制到帖子内容中就可以了。
最具人气热帖推荐
[查看全部]
作者
回/看
最后发表
[
考研
]
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
信息提示
关闭
请填处理意见
关闭
确定