library
ieee
use
ieee.std_logic_1164.all
use
ieee.std_logic_arith.all
use
ieee.std_logic_unsigned.all
entity
dac_ds
is
port(reset
:in
std_logic
clk
:in
std_logic
din
:in
std_logic_vector(7
downto
0)--Signed
integer
dout
:out
std_logic)
end
dac_ds
architecture
arch_dac_ds
of
dac_ds
is
signal
error
:std_logic_vector(9
downto
0)--Error
accumulator
is
2
bits
larger
constant
zeros:std_logic_vector(7
downto
0):=(others=>'0')
begin
process(reset,clk,din)
variable
val
:std_logic_vector(9
downto
0)
begin
if
reset='1'then
error<=(others=>'0')
dout<='0'
elsif
clk'event
and
clk='1'
then
--val:=din+errordin
is
sign
extended
to
nbits+2
val:=(din(din'high)&din(din'high)&din)+error
if
val(val'high)='0'then
dout<='1'
error<=val+("11"&
zeros)
else
dout<='0'
error<=val+("01"&zeros)
end
if
end
if
end
process
end
arch_dac_ds
//看看这个程序,可以参考一下。//0832各种波型输出演示 //P1.1P1.0 = 00时 正弦波 //P1.1P1.0 = 01时 锯齿波 //P1.1P1.0 = 10时 方波 //P1.1P1.0 = 11时 三角波 //需要连接的导线://(1)译码器Y0接DAC0832的CS //(2)P3.6即XWR插孔接DAC0832的WR //(3)8芯的排线连接8排针的P0口和DAC0832的D7-D0 //(4)串并转换区的+5v接DAC0832的参考电压Vref插孔#include <reg51.h>#include <ABSACC.H>//absacc.h是C51中绝对地址访问函数的头文件 #define daport XBYTE[0x8000]//将daport定义为外部I/O口,地址8000H,则74ls138的Y0有效 sbit P1_1=P1^1sbit P1_0=P1^0 unsigned char i,j,k bit updown //三角波的上升/下降 unsigned char code sinn[64]={ 198,204,210,216,222,228,233,237,242,245,249,251,253,255,255,255, 255,254,252,250,247,243,239,235,230,224,219,213,207,201,194,188, 181,175,169,163,158,152,147,143,139,136,133,131,129,128,128,128, 129,130,132,135,138,142,146,151,156,162,168,174,180,186,193,199 }//正弦的数值在128-255之间,所以输出的正弦幅值在0-5v void main(void) { updown=0 i=128 j=0 while(1) {if (P1_1==1){ if (P1_0==0) //输出占空比为25%的方波 {if (j<=40) i=255//i=255时输出的电压为+5velse if(j<=160) i=0//i=0时输出的电压为-5v else j=0 } else //输出-5v—+5v的三角波 { if (updown==0) //上升 { if (i==255) updown=1 else i=i+5//i++} else //下降 { if (i==0) updown=0//if(i==128) updown=0i=128输出的电压为0 else i=i-5//i--}} } else{ if (P1_0==0) //正弦波 { i=sinn[j] if (j>=63) j=0 } else //锯齿波 { if (i==255) i=128 else i++ } } daport=i for (k=0k<2k++) j++ }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)