📄 cpld与单片机at89c51双向通信.txt
字号:
/*CPLD接收VHDL源程序如下*/
Library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity cuanxing is
port (clk,cxin,cs:in std_logic;
cxout:out std_logic_vector(7 downto 0));
end;
architecture rtl of cuanxing is
signal shift:std_logic_vector(7 downto 0) ;八位暂存变量并行输出
begin
process(clk)
begin
if(cs='0')then
shift<=(others=>'0');若未被选中,输出全零
elsif(clk'event and clk='1')then ;若上升沿到达clk时,被选中。
shift(7 downto 1)<=shift(6 downto 0) ;八位数据前移一位
shift(0)<=cxin;最低位由cxin输入
end if;
end process;
cxout<=shift;将八位变量送至端口
end rtl;
/*与之相对应的单片机控制子程序如下(待发数据存放在A中)*/
CS EQU P1.4
EN EQU P1.5
DCLOCK EQU P1.6
DOUT EQU P1.7
CONV:PUSH 07H
MOV R7,#8 ;将移位个数8存入R7
CLR DCLOCK
SETB CS ;选中移位寄存器
CLR EN
CLR C
JXL:RLC A ;左移一位,将待发数据送至CY
MOV DOUT,C ;送至端口
ACALL YS1MS
SETB DCLOCK ;给一个上升沿,将数据移入移位寄存器
ACALL YS1MS
CLR DCLOCK
DJNZ R7,JXL ;若未到8次则传送下一位
SETB EN ;八位命令字全部移入,给EN一个上升沿,使CPLD执行相应操作
ACALL YS1MS
POP 07H
RET
二. 可编程逻辑器件到单片机的串行通信
可编程逻辑器件到单片机的串行通信与单片机到可编程逻辑器件的串行通信类似,只不过八位寄存器改为并入串出,其端口如图2所。当单片机的cs=1时,寄存器被选中;当load=1时,待发的数据被加载到bxin上;当clk上升沿到来时,将数据一位一位移出至bxout上,与此同时,单片机一位一位接收到自 bxout上的数据。在clk八个上升沿后,加载到bxin的数据便被传送至单片机的A寄存器中(其VHDL和单片机源程序由于篇幅所限,不予给出)。
可编程逻辑器件发送数据到单片机的VHDL源程序
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity chuanchu is
port (clk,load:in std_logic;
bxin:in std_logic_vector(7 downto 0);
bxout:out std_logic
);
end;
architecture rtl of chuanchu is
signal sel:std_logic_vector(2 downto 0);
begin
process(clk)
begin
if(load='0')then
sel<=sel;
elsif(clk'event and clk='1') then
if sel="111" then sel <="000";
else sel<=sel+1;
end if;
end if;
case sel is
when "000" => bxout<=bxin(0);
when "001" => bxout<=bxin(1);
when "010" => bxout<=bxin(2);
when "011" => bxout<=bxin(3);
when "100" => bxout<=bxin(4);
when "101" => bxout<=bxin(5);
when "110" => bxout<=bxin(6);
when "111" => bxout<=bxin(7);
when others => bxout<='X';
end case;
end process;
end rtl;
单片机接收中断服务子程序:
RECE:
CLR ES
CLR RI
JB 00H,RECE1
MOV A,SBUF
CJNE A,#3FH,RECE2 ;接收的不是'?'号,则退出
MOV A,#2EH
MOV SUBF,A ;发送应答信号'.'(2EH)
JNB TI,$
CLR TI
SETB 00H
SETB ES
RETI
R4ECE2:
MOV A,#24H
MOV SUBF,A ;发送应答信号'$'(24H)
JNB TI,$
CLR TI
SETB ES
RETI
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -