之前在这问过几个问题,都得到解答了,很感谢这里朋友,虽然素未谋面。呵呵,
我在使3E开发板里的AD,写了个程序。但是总也调不出来。我自己觉得可能是时序方面没没理解好。把程序贴上,希望大家帮我看下,哪里有问题,让我能调出AD来,谢谢了/
module adc(clk,reset,spi_miso,amp_cs,amp_shdn,spi_mosi,ad_conv,spi_sck,x);
input clk,reset;
input spi_miso; //AD变换后输出的数据
output amp_cs,amp_shdn,ad_conv,spi_sck;
output spi_mosi; //8位可编程增益
output x;
reg spi_mosi;
reg amp_cs,ad_conv,amp_shdn;
reg [1:0]counter1;
always@(posedge clk or posedge reset)
begin
if(reset)
begin
counter1<=2'd0;
end
else
begin
if(counter1==2'd2)
begin
counter1<=2'd0;
end
else
begin
counter1<=counter1+1;
end
end
end
reg spi_sck;
always@(posedge clk or posedge reset)
begin
if(reset)
spi_sck<=1'b0;
else
begin
if(counter1==2'd2)
spi_sck<=1'b1;
else
spi_sck<=1'b0;
end
end
reg [9:0]delay_count;
reg [9:0]num_count;
parameter state1 =6'b000001;
parameter state2 =6'b000010;
parameter state3 =6'b000011;
parameter state4 =6'b000100;
parameter state5 =6'b000101;
parameter state6 =6'b000110;
parameter state7 =6'b000111;
parameter state8 =6'b001000;
parameter state9 =6'b001001;
parameter state10=6'b001010;
parameter state11=6'b001011;
parameter state12=6'b001100;
parameter state13=6'b001101;
parameter state14=6'b001110;
reg[5:0]state; //状态信号
reg state_change; //状态改变信号
always@(posedge clk or posedge reset)
if(reset)
begin
state_change<=1'b0;
delay_count<=1'b0;
end
else
if(delay_count==num_count-1)
begin
state_change<=1'b1;
delay_count<=1'b0;
end
else
begin
state_change<=1'b0;
delay_count<=delay_count+1'b1;
end
reg[13:0]data1;
reg[3:0]count2;
reg[13:0]rcv_data;
always@(posedge state_change or posedge reset)
if(reset)
begin
state<=state1;
num_count<=10'd4;
data1<=14'd0;
count2<=4'd0;
rcv_data<=14'b0111_1111_1111_11;
end
else
case(state)
state1:begin
state<=state2;
num_count<=10'd2;
amp_cs<=1'b1;
amp_shdn<=1'b1;
ad_conv<=1'b1;
end
state2:begin
state<=state3;
num_count<=10'd60;
amp_cs<=1'b0;
amp_shdn<=1'b0;
ad_conv<=1'b1;
end
state3:begin //spi-mosi八位增益
state<=state4;
num_count<=10'd5;
spi_mosi<=1'h0;
ad_conv<=1'b1;
end
state4:begin
state<=state5;
num_count<=10'd5;
spi_mosi<=1'h0;
ad_conv<=1'b1;
end
state5:begin
state<=state6;
num_count<=10'd5;
spi_mosi<=1'h0;
ad_conv<=1'b1;
end
state6:begin
state<=state7;
num_count<=10'd5;
spi_mosi<=1'h1;
ad_conv<=1'b1;
end
state7:begin
state<=state8;
num_count<=10'd5;
spi_mosi<=1'h0;
ad_conv<=1'b1;
end
state8:begin
state<=state9;
num_count<=10'd5;
spi_mosi<=1'h0;
ad_conv<=1'b1;
end
state9:begin
state<=state10;
num_count<=10'd5;
spi_mosi<=1'h0;
ad_conv<=1'b1;
end
state10:begin
state<=state11;
num_count<=10'd5;
spi_mosi<=1'h1;
ad_conv<=1'b1;
end //spi-mosi八位增益
state11:begin
state<=state12;
num_count<=10'd4;
amp_cs<=1'b1;
amp_shdn<=1'b1;
ad_conv<=1'b0;
end
state12:begin
state<=state13;
num_count<=10'd202;
if(count2==13)
begin
rcv_data<=data1;
count2<=4'd0;
end
else
begin
data1<={spi_miso,data1[13:1]};
count2<=count2+1;
end
end
state13:begin
state<=state14;
num_count<=10'd2;
ad_conv<=1'b1;
amp_cs<=1'b1;
amp_shdn<=1'b1;
end
state14:begin
state<=state11;
num_count<=10'd4;
end
default:begin
state<=state1;
num_count<=10'd4;
end
endcase
reg x;
always@(posedge clk or posedge reset)
begin
if(reset)
x<=1'b0;
else
x<=rcv_data[13];
end
endmodule
我只用了rcv_data[13];,其他的没有使用。
共10条
1/1 1 跳转至页
sparten 3e,AD的使用问题。听说标题要很长····


5楼
简单看了一下。现在手头没有Sparten 3E的板,没法帮你验证。
提出以下一些建议:
1. 状态机的写法不好,可参考“三段式”写法,我以前发过,论坛上有;
2. 这段代码
state12:begin
state<=state13;
num_count<=10'd202;
if(count2==13)
begin
rcv_data<=data1;
count2<=4'd0;
end
else
begin
data1<={spi_miso,data1[13:1]};
count2<=count2+1;
end
end
我觉得你的本意是连续读出spi_miso,但你的程序不是这样的,因为读取一个后即可跳转到state13;
还有不明白为什么要设置一个可变的 num_count,并用state_change 作为时钟,这些写法都不好


10楼
一般用状态机实现,先把配置、读取等状态列清楚。
一般是大状态套小状态的方式实现。
像这种设计SPI时序接口的东西,一般在小状态中用Counter来计数就可以了,没必要再parameter状态。
共10条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【EEPW电子工程师创研计划】技术变现通道已开启~ | |
发原创文章 【每月瓜分千元赏金 凭实力攒钱买好礼~】 | |
【EEPW在线】E起听工程师的声音! | |
“我踩过的那些坑”主题活动——第001期 | |
高校联络员开始招募啦!有惊喜!! | |
【工程师专属福利】每天30秒,积分轻松拿!EEPW宠粉打卡计划启动! | |
送您一块开发板,2025年“我要开发板活动”又开始了! | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
多组DCTODC电源方案被打赏50分 | |
【我踩过的那些坑】STM32cubeMX软件的使用过程中的“坑”被打赏50分 | |
新手必看!C语言精华知识:表驱动法被打赏50分 | |
【我踩过的那些坑】杜绑线问题被打赏50分 | |
【我踩过的那些坑】STM32的硬件通讯调试过程的“坑”被打赏50分 | |
【我踩过的那些坑】晶振使用的问题被打赏100分 | |
【我踩过的那些坑】电感选型错误导致的处理器连接不上被打赏50分 | |
【我踩过的那些坑】工作那些年踩过的记忆深刻的坑被打赏10分 | |
【我踩过的那些坑】DRC使用位置错误导致的问题被打赏100分 | |
我踩过的那些坑之混合OTL功放与落地音箱被打赏50分 |