搜索
bottom↓
回复: 0

fpga i2c驱动问题

[复制链接]

出0入0汤圆

发表于 2014-3-18 11:57:33 | 显示全部楼层 |阅读模式
先附上程序:
module i2cinout(clk,res,func,adress,data,slaveadress,enflag,scl,sda,recive,endflag);
        input func,enflag,clk,res;
        input[7:0] adress,data,slaveadress;
        output reg scl,endflag;
        output reg[7:0] recive;
        inout sda;
        reg[7:0] state[1:0];
        reg[7:0] i;
        reg sdalink,sdadata;
assign sda=sdalink?sdadata:1'bz;
always @(posedge clk)begin
        if(res==0)begin
                state[0]<=1;
                state[1]<=1;
                endflag<=0;
                recive<=0;
                i<=0;
                sdalink<=1;
        end
        else if(enflag==0)begin
                endflag<=0;
                state[0]<=1;
        end
        else if(func==0)begin
                case(state[0])
                        0:begin
                                endflag<=1;
                        end
                        1:start;
                        2:sendbyte(slaveadress);
                        3:sendbyte(adress);
                        4:sendbyte(data);
                        5:stop;
                        6:state[0]<=0;
                endcase
        end
        else if(func==1)begin
                case(state[0])
                        0:begin
                                endflag<=1;
                        end
                        1:start;
                        2:sendbyte(slaveadress);
                        3:sendbyte(adress);
                        4:start;
                        5:sendbyte(slaveadress+1);
                        6:recivebyte;
                        7:sendack(1);
                        8:stop;
                        9:state[0]<=0;
                endcase
        end
end
task start;
        case(state[1])
                1:begin
                        scl<=1'bz;
                        sdadata<=1'bz;
                        state[1]<=state[1]+1;
                end
                2:begin
                        sdadata<=0;
                        state[1]<=state[1]+1;
                end
                3:begin
                        scl<=0;
                        state[1]<=1;
                        state[0]<=state[0]+1;
                end
        endcase
endtask
task stop;
        case(state[1])
                1:begin
                        sdadata<=0;
                        scl<=1'bz;
                        state[1]<=state[1]+1;
                end
                2:begin
                        sdadata<=1'bz;
                        state[1]<=1;
                        state[0]<=state[0]+1;
                end
        endcase
endtask
task sendack;
        input ack;
        case(state[1])
                1:begin
                        if(ack) begin
                        sdadata<=1'bz;
                        end
                        else begin
                        sdadata<=0;
                        end
                        scl<=1'bz;
                        state[1]<=state[1]+1;
                end
                2:begin
                        scl<=0;
                        state[1]<=1;
                        state[0]<=state[0]+1;
                end
        endcase
endtask
task sendbyte;
        input[7:0] datain;
        case(state[1])
                1:begin
                        if(i<8)begin
                                if((datain<<i)&8'h80)begin
                                        sdadata<=1'bz;
                                end
                                else begin
                                        sdadata<=0;
                                end
                                scl<=1'bz;
                                state[1]<=state[1]+1;
                        end
                        else begin
                        i<=0;
                        state[1]<=state[1]+2;
                        end
                end
                2:begin
                        scl<=0;
                        i<=i+1;
                        state[1]<=state[1]-1;
                end
                3:begin
                        sdalink<=0;
                        scl<=1'bz;
                        state[1]<=state[1]+1;
                end
                4:begin
                        //recive<=sda;
                        scl<=0;
                        state[1]<=state[1]+1;
                end
                5:begin
                        sdalink<=1;
                        state[1]<=1;
                        state[0]<=state[0]+1;
                end
        endcase
endtask
task recivebyte;
        case(state[1])
                1:begin
                        sdadata<=1'bz;
                        sdalink<=0;
                        state[1]<=state[1]+1;
                end
                2:begin
                        if(i<8)begin
                                recive<=(recive<<1);
                                scl<=1'bz;
                                state[1]<=state[1]+1;
                        end
                        else begin
                        i<=0;
                        state[1]<=state[1]+2;
                        end
                end
                3:begin
                        recive<=recive+sda;
                        i<=i+1;
                        scl<=0;
                        state[1]<=state[1]-1;
                end
                4:begin
                        sdalink<=1;
                        state[1]<=1;
                        state[0]<=state[0]+1;
                end
        endcase
endtask
endmodule
说明:
func=0的时候是写i2c,=1的时候是读
enflag是使能的标志,是1的时候才运行。
endflag=1的时候表示运行完成
recive是读到的数据。
请问哪里有问题啊,为什么我读出来的数据全是11111111呢?(也就是sda一直是高电平)
我连的是mpu6050

阿莫论坛20周年了!感谢大家的支持与爱护!!

曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-8-27 01:21

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表