module asyn(clk,rst,wr,wr_bit,addr,bit_in,data_in,t1_ow,rxd,txd,intr,data_out,pcon,scon,rx_sbuf);
input clk;
input rst;
input wr;
input wr_bit;
input [7:0] addr;
input bit_in;
input [7:0] data_in;
input t1_ow;
input rxd;
output txd;
output intr;
output [7:0] data_out;
output [7:0] scon;
output [7:0] pcon;
output [7:0] rx_sbuf;
reg txd;
reg [7:0] scon;
reg [7:0] pcon;
assign intr=scon[1]|scon[0];
//
wire t1_ow_final;
reg div2_flag;
reg t1_ow_buf;
reg t1_ow_final_buf;
// signals with transmit part
reg [7:0] tx_sbuf;
reg send_n; //active low,indicate that it is transmiting datas
wire wr_sbuf_en;
assign wr_sbuf_en=(wr==1'b1&&addr==8'h99);
reg wr_sbuf_en_mem;
wire shift_en;
reg [3:0] tx_next_state;
reg [3:0] tx_current_state;
parameter IDLE=11,START=0,D0=1,D1=2,D2=3,D3=4,D4=5,D5=6,D6=7,D7=8,TB8_BIT=9,STOP_BIT=10; //states define
reg [3:0] osc_cnt;
reg [3:0] tx_sample_cnt;
reg tx_clk;
//signals with receive part
reg [3:0] rx_next_state;
reg [3:0] rx_current_state;
reg [3:0] rx_sample_cnt;
wire one_bit;
reg rxd_buf;
reg [10:0] sbuf_rxd_tmp;
reg [7:0] rx_sbuf;
reg sample_7,sample_8,sample_9;
reg receive;
//signals with both transmiting and receiving parts
//always @()
//begin
//SM0=scon[7];
//SM1=scon[6];
//SM2=scon[5];
//REN=scon[4];
//TB8=scon[3];
//RB8=scon[2];
//TI=scon[1];
//RI=scon[0];
//end
always @(posedge clk or posedge rst) //register scon
begin
if (rst)
scon <=8'b0100_0000;
else if ((wr) & !(wr_bit) & (addr==8'h98))
scon <=data_in;
else if ((wr) & (wr_bit) & (addr[7:3]==5'b10011))
scon[addr[2:0]]<=bit_in;
else if (tx_next_state==STOP_BIT)
scon[1] <=1'b1;
else
case(rx_next_state)
START:scon[0]<=1'b0;
IDLE:if(rx_current_state==STOP_BIT)
begin
case (scon[7:6])
2'b00: scon[0] <= 1'b1;
2'b01: if(scon[5])
if(one_bit)
scon[0] <= 1'b1;
else
scon[0] <= 1'b0;
else
scon[0] <= 1'b1;
2'b10,2'b11: if(scon[5])
scon[0]<=sbuf_rxd_tmp[9];
else
scon[0]=1'b1;
endcase
end
endcase
end
//
//power control register
//
wire smod;
assign smod = pcon[7];
always @(posedge clk or posedge rst)
begin
if (rst)
pcon <= 8'b0000_0000;
else if ((addr==8'h87) & (wr) & !(wr_bit))
pcon <= data_in;
end
always @(posedge clk or posedge rst) //osc_cnt
if(rst)
osc_cnt<=4'b0000;
else if(osc_cnt==4'b1011)
osc_cnt<=4'b0000;
else
osc_cnt<=osc_cnt+1'b1;
always @(posedge clk or posedge rst) //t1_ow_buf
if(rst)
t1_ow_buf<=1'b0;
else if(t1_ow)
t1_ow_buf<=~t1_ow;
always @(posedge clk or posedge rst) //div2_flag
if(rst)
div2_flag<=1'b0;
else if(~t1_ow_buf&t1_ow)
div2_flag<=~div2_flag;
assign t1_ow_final=(pcon[7]==1'b1)t1_ow:t1_ow&div2_flag;
//transmit part
always @(posedge clk or posedge rst) //t1_ow_final_buf
if(rst) t1_ow_final_buf<=1'b0;
else t1_ow_final_buf<=t1_ow_final;
always @(posedge clk or posedge rst) //tx_sample_cnt
if(rst)
begin
tx_sample_cnt<=4'b0000;
end
else if(t1_ow_final_buf==1'b0&&t1_ow_final==1'b1)
tx_sample_cnt<=tx_sample_cnt+1'b1;
always @(posedge clk or posedge rst)
if(rst)
wr_sbuf_en_mem<=1'b0;
else if (wr_sbuf_en&&wr_sbuf_en_mem<=1'b0)
wr_sbuf_en_mem<=1'b1; //represent that the tx_sbuf has been loaded
else if(tx_current_state==STOP_BIT)
wr_sbuf_en_mem<=1'b0;
assign shift_en=wr_sbuf_en_mem==1'b1&&tx_sample_cnt==4'b1111&&osc_cnt==4'b1011&&t1_ow_final_buf==1'b0&&t1_ow_final==1'b1;
always @(posedge clk or posedge rst)
if(rst)
tx_sbuf<=8'b0000_0000;
else if (wr_sbuf_en&&wr_sbuf_en_mem<=1'b0)
tx_sbuf<=data_in;
else if(send_n==1'b0&&shift_en)
tx_sbuf<=tx_sbuf>>1'b1;
//the three always phrase below is the 3 parts discription of state machine
always @()//(tx_current_state or wr_sbuf_en_mem or tx_sample_cnt or osc_cnt or TH1 or TL1 or shift_en)
begin
case(tx_current_state)
IDLE: if(shift_en) //
tx_next_state=START;
else
tx_next_state= IDLE;
START:if(shift_en)
tx_next_state=D0;
else
tx_next_state=START;
D0: if(shift_en)
tx_next_state=D1;
else
tx_next_state=D0;
D1: if(shift_en)
tx_next_state=D2;
else
tx_next_state=D1;
D2: if(shift_en)
tx_next_state=D3;
else
tx_next_state=D2;
D3: if(shift_en)
tx_next_state=D4;
else
tx_next_state=D3;
D4: if(shift_en)
tx_next_state=D5;
else
tx_next_state=D4;
D5: if(shift_en)
tx_next_state=D6;
else
tx_next_state=D5;
D6: if(shift_en)
tx_next_state=D7;
else
tx_next_state=D6;
D7: if(shift_en)
if(scon[7:6]==2'b00||scon[7:6]==2'b01)
tx_next_state=STOP_BIT;
else
tx_next_state=TB8_BIT;
else
tx_next_state=D7;
TB8_BIT: tx_next_state=STOP_BIT;
STOP_BIT: if(tx_sample_cnt==4'b1111&&osc_cnt==4'b1011&&t1_ow_final==1'b1)
tx_next_state=IDLE;
else
tx_next_state=STOP_BIT;
default:tx_next_state=IDLE;
endcase
end
always @(posedge clk or posedge rst)
if(rst)
tx_current_state<=IDLE;
else
tx_current_state<=tx_next_state;
always @(posedge clk or posedge rst)
if(rst)
begin
txd<=1'b1;
send_n<=1'b1;
end
else
case(tx_next_state)
IDLE:
begin
send_n<=1'b1;
txd<=1'b1;
end
START:begin
send_n<=1'b0;
txd<=1'b0;
end
D0: txd<=tx_sbuf[0];
D1: txd<=tx_sbuf[0];
D2: txd<=tx_sbuf[0];
D3: txd<=tx_sbuf[0];
D4: txd<=tx_sbuf[0];
D5: txd<=tx_sbuf[0];
D6: txd<=tx_sbuf[0];
D7: txd<=tx_sbuf[0];
TB8_BIT: txd<=scon[3];
STOP_BIT:
begin
txd<=1'b1;
end
endcase
//receiving part
assign rx_shift_en=rx_sample_cnt==4'b1111&&t1_ow_final_buf==1'b0&&t1_ow_final==1'b1;
always @(posedge clk or posedge rst) //describe the rxd_buf
if(rst)
rxd_buf<=1'b0;
else
rxd_buf<=rxd;
always @(posedge clk or posedge rst) //describe the rx_sample_cnt
if(rst)
rx_sample_cnt<=4'b0000;
else if(rxd_buf==1'b1&&rxd==1'b0) //此条件启动接收过程
rx_sample_cnt<=4'b0000;
else if(t1_ow_final)
rx_sample_cnt<=rx_sample_cnt+1'b1;
always @(posedge clk)
if(rx_sample_cnt==4'b0110&&t1_ow_final)
sample_7<=rxd;
always @(posedge clk)
if(rx_sample_cnt==4'b0111&&t1_ow_final)
sample_8<=rxd;
always @(posedge clk)
if(rx_sample_cnt==4'b1000&&t1_ow_final)
sample_9<=rxd;
assign one_bit=sample_7&&sample_8||sample_7&&sample_9||sample_8&&sample_9;
//the three always phrase below is the 3 parts discription of state machine
always @()
begin
case(rx_current_state)
IDLE: if(rxd_buf==1'b1&&rxd==1'b0&&scon[4]==1'b1) //检测到了rxd从1到0的跳变
rx_next_state=START;
else
rx_next_state= IDLE;
START: if(rx_shift_en)
if(~one_bit)
rx_next_state=D0;
else
rx_next_state=IDLE;
else
rx_next_state=START;
D0: if(rx_shift_en)
rx_next_state=D1;
else
rx_next_state=D0;
D1: if(rx_shift_en)
rx_next_state=D2;
else
rx_next_state=D1;
D2: if(rx_shift_en)
rx_next_state=D3;
else
rx_next_state=D2;
D3: if(rx_shift_en)
rx_next_state=D4;
else
rx_next_state=D3;
D4: if(rx_shift_en)
rx_next_state=D5;
else
rx_next_state=D4;
D5: if(rx_shift_en)
rx_next_state=D6;
else
rx_next_state=D5;
D6: if(rx_shift_en)
rx_next_state=D7;
else
rx_next_state=D6;
D7: if(rx_shift_en)
if(scon[7:6]==2'b00||scon[7:6]==2'b01)
rx_next_state=STOP_BIT;
else
rx_next_state=TB8_BIT;
else rx_next_state=D7;
TB8_BIT: if(rx_shift_en)
rx_next_state=STOP_BIT;
else
rx_next_state=TB8_BIT;
STOP_BIT: if(rx_shift_en)
rx_next_state=IDLE;
else
rx_next_state=STOP_BIT;
endcase
end
always @(posedge clk or posedge rst)
if(rst)
rx_current_state<=IDLE;
else
rx_current_state<=rx_next_state;
always @(posedge clk or posedge rst)
if(rst)
receive<=1'b0;
else
case(rx_next_state)
IDLE:
receive<=1'b0;
START:begin
receive<=1'b1;
sbuf_rxd_tmp<=10'b11_1111_1111;
end
D0,D1,D2,D3,D4,D5,D6,D7: if(rx_shift_en) sbuf_rxd_tmp<={sbuf_rxd_tmp[10:0],one_bit};
STOP_BIT:
receive<=1'b1; // end of receiving
endcase
//
//serial port buffer (receive)
//
always @(posedge clk or posedge rst)
begin
if (rst)
rx_sbuf<=8'h00;
else if (rx_next_state==STOP_BIT)
case (scon[7:6])
2'b00,2'b01:rx_sbuf<=sbuf_rxd_tmp[7:0];
2'b10,2'b11:rx_sbuf<=sbuf_rxd_tmp[8:1];
endcase
end
endmodule
什么是Ajax
>
写一个类,将你要的参数都封装进去。然后通过IAsyncResultAsyncState进行传递。
// 1)定义类,封装你要的任何东西class StateObject
{
public Socket frd{get;set;}
pubic ComboBox combo{get;set;}
//可以添加任何你需要的东西
//……
}
// 2)在异步调用前,实例化StateObject
StateObject state = new StateObject();
statefrd = frd;
statecombo = comBox_client;
AsyncCallback callBack = new AsyncCallback(sendCallBack);
frdsocketBeginSend(msg, 0, msgLength, SocketFlagsNone, callBack, state);
//3)在回调函数中使用StateObject
private void sendCallBack(IAsyncResult ar)
{
StateObject state = (StateObject)arAsyncState;
//从 state中 取出frd, combo ……
Socket frd = statefrd;
ComboBox combo = statecombo;
try
{
frdEndSend(ar);
}
catch (SystemException ex)
{
comboInvoke(removeFriend, combo, friends,frd)
}
}
; 摘要 本文介绍了在Windows平台下串口开发的方法 并给出一个使用Delphi设计的远程数据采集的实例 引言在工业生产实践中 使用PC机对Inprise公司推出的Delphi是一种功能强大的高级编程语言 其具有可视化面向对象的特征 特别适合Windows平台下的图形界面和用户程序的编制 本文就介绍在Windows平台下用Delphi开发串口的方法和使用Delphi设计的一个实现远程串行数据采集的实例 串口工作原理及软件实现方法串口进行通信的方式有两种 同步通信方式和异步通信方式 同步通信方式要求通信双方以相同的时钟频率进行 而且准确协调 通过共享一个单个时钟或定时脉冲源保证发送方和接收方的准确同步 效率较高 异步通信方式不要求双方同步 收发方可采用各自的时钟源 双方遵循异步的通信协议 以字符为数据传输单位 发送方传送字符的时间间隔不确定 发送效率比同步传送效率低 在Windows平台下 Win API支持同步和异步两种I/O *** 作 同步 *** 作的方式的程序设计相对比较简单 但是I/O *** 作函数在I/O *** 作结束前不能返回 这将挂起调用线程 直到I/O *** 作结束 异步 *** 作方式要相对复杂一些 但是可以让I/O *** 作在后台运行 而不会挂起调用线程 这在大数据量通信情况下对改善调用线程的响应速度是相当有效的 同时由于Win x和WinNT下对串行通信的处理不同 这就导致了在Win x下开发的同步方式串行通信程序在NT下会发生工作线程之间的协作阻塞 即当读线程在等待WaitCommEvent的时候 写线程不能正常工作 停在那里 整个程序处于瘫痪状态 这个问题是Windows的API函数处理串行通信的一个BUG 所以对于适应性强的程序都是选择异步方式 下面 本文对在Windows平台下对串口进行开发的方法进行介绍 . 汇编程序直接读写串口汇编语言的编译效率和执行效率都很高 使用汇编语言直接对串口进行 *** 作可以部分弥补串行通信速度较慢的缺陷 具体做法是 用汇编语言编写读 写串口的函数 在通信程序中直接调用 或者在Delphi中直接内嵌汇编程序进行对端口的读写 例如 a mov dx h mov ax eh int h end ; 这样可以达到直接读到端口的效果 但是在WinNT和Win 下 系统使用了保护机制 不允许用户态的程序直接读取端口 所以在WinNT和Win 下 这种方法不能被允许执行 Ring 的用户态的程序要进入Ring 去读写端口必须先提供一个驱动(sys)程序 然后通过DLL导出函数供用户程序调用 显然 这种方法比较不容易实现 . 使用API函数进行串口编程Windows系统通信一般都以WOSA(Windows Open Service Architecture 即Windows开放式服务体系)模型为基础 在此模型中位于上层的应用程序通过调用各种通信API(Application Programming Interfaces 即应用程序接口)与位于下层的设备驱动程序进行数据交换 在Windows平台下 Windows将设备看作是文件进行管理 对设备的 *** 作也可以看作是对文件的 *** 作 Win API提供了CreateFile() WriteFile() ReadFile() WaitForSingleObject() WaitForMultipleObjects() CreateEvent() CreateMutex() CreateSemaphore() CreateThread()等函数 其基本步骤如下 ( ) 利用CreateFile()函数打开串口 该函数有七个参数 其中dwCreationDistribution参数取OPEN_EXISTING 表明打开的串口对应于实际的物理串口 lpFileName参数是要打开的串口名称 如 DwFlagsAndAttributes参数决定对串口的 *** 作是同步 *** 作还是异步 *** 作 DwDesiredAccess参数是访问方式 可取GENERIC_READ或GENERIC_WRITE DwShareMode参数是共享模式 对串口物理设备必须取 LpSecurityAttributes参数是安全属性 取值为NULL DwFlagsAndAttributes是文件属性和标识 一般取值为FILE_ATTRIBUTE_NORMAL 该函数返回串口 *** 作的句柄 ( ) 对该串口句柄对应的设备进行配置 如波特率 数据位 停止位 是否奇偶校验等 这部分首先使用GetCommState()函数得到当前的串口配置信息 将这些信息存放在一个DCB结构中 然后对该DCB结构里面的内容进行重新按要求设置 最后调用SetCommState()函数使修改的设置生效 ( ) 配置串口事件 SetCommMask()函数可以设置多个串口信息事件 其串口的信息事件可以是以下任意的组合 EV_BREAK 在输入时Windows检测到中断 EV_CTS CTS信号改变状态 EV_DSR DSR信号改变状态 EV_ERR 发生线状态错误 EV_RING 检测到振铃指示 EV_RLSD 接收线信号探测状态发生改变 EV_RXCHAR 接收缓冲区里收到字符 EV_RXFLAG 收到事件字符 并放入接收缓冲区 EV_TXEMPTY 输出缓冲区空 ( ) 创建串口监视线程监视串口事件 首先使用WaitForSingleObject() WaitForCommEvent() WaitForMultipleObjects()等等待函数对线程进行控制 当没有数据收发时 将线程阻塞 减少其CPU的资源占用 当有数据收发时 线程自动启动 完成数据的收发 最后调用CreateThread()函数启动线程 ( ) 串口使用结束 用CloseHandle()函数关闭串口 回收资源 由此可见 利用Win API函数编写串口通信程序比较复杂 需要掌握大量的系统和通信知识 其优点是实现的功能强大 应用面广泛 适合编写较为复杂的地层次应用程序 lishixinzhi/Article/program/Delphi/201311/24926
以上就是关于急求一个verilog编写的异步串行通信的程序,最好有校验位,459785950@qq.com。答的好可以再加分全部的内容,包括:急求一个verilog编写的异步串行通信的程序,最好有校验位,459785950@qq.com。答的好可以再加分、什么叫AJAX技术、c#异步通信中回调函数AsyncCallback如何传递参数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)