之前毕业设计写过的用在FPGA上的BH1750光线传感器verilog驱动代码,亲测可用,有什么问题欢迎留言。
module BH1750_drive(inputclk,//输入时钟=50Minput rst,//输入复位信号,低电平有效output scl,//输出I2C时钟管脚inout sda,//I2C数据管脚output dvi,//BH1750DVI上电配置管脚output[15:0]data_out//BH1750数据输出管脚);regr_scl;//I2C_SCL寄存器reg r_sda;//I2C_SDA寄存器reg ctl_sda;//sda输入/输出控制assign sda = ctl_sda ? r_sda : 1'bz; //配置三态接口 assign scl = r_scl; assign dvi = 1; //配置上电 reg [15:0] data; //数据采集管脚 assign data_out = data[15:0]; reg [11:0] cnt_100khz; //定义一个计数器,用于产生100Khz时钟 reg clk_100khz; //产生100Khz的时钟 always @ (podge clk or negedge rst) begin //podge上升沿触发或者negedge下降沿触发 if (!rst) begin cnt_1徐州生物工程职业技术学院00khz <= 12'd0;clk_100khz <= 1'b0; end el if (cnt_100khz < 12'd2499) cnt_100khz <= cnt_100khz + 1'b1; el begin cnt_100khz <= 12'd0;clk_100khz <= ~ clk_100khz;end endparameter [7:0] DEVICE_ADDR_W = 8'h46; //BH1750FVI设备地址+写 parameter [7:0] DEVICE_ADDR_R = 8'h47;//BH1750FVI设备地址+读parameter [7:0] DEVICE_POWER_OFF = 8'h00; //BH1750FVI设备关断 parameter [7:0] DEVICE_POWER_ON = 8'h01;//BH1750FVI设备上电parameter [7:0] DEVICE_MEASURE = 8'h13; //BH1750FVI设备测量环境光 reg [11:0] cnt; always@(podge clk_100khz or negedge rst) begin if(!rst) begin ctl_sda<=1'b1;r_sda<=1'b1; r_scl<=1'b1;cnt<=0;end el begincnt<=cnt+1'b1; ca(cnt) //外部地址+写+断电---------------------------------------------------------// 0: begin ctl_sda<=1'b1; end//START1:begin r_scl<=1'b1; end 2: begin r_sda<=1'b1;end3:begin r_sda<=1'b0; end //ADDRESS+WRITE 8'H464:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[7]; end 5: begin r_scl<=1'b1; end6:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[6]; end 7: begin r_scl<=1'b1; end8:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[5]; end 9: begin r_scl<=1'b1; end10:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[4]; end 11: begin r_scl<=1'b1; end12:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[3]; end 13: begin r_scl<=1'b1; end14:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[2]; end 15: begin r_scl<=1'b1; end16:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[1]; end 17: begin r_scl<=1'b1; end18:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[0]; end 19: begin r_scl<=1'b1; end20:begin r_scl<=1'b0; end 21: begin ctl_sda<=1'b0; end22:begin r_scl<=1'b1; end 23: begin r_scl<=1'b0;if(sda==1'b0) begin cnt<=24; end el cnt<=22; end //POWER OFF 8'H0024:begin ctl_sda<=1'b1; end 25: begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_OFF[7]; end26:begin r_scl<=1'b1 ; end 27: begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_OFF[6]; end28:begin r_scl<=1'b1 ; end 29: begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_OFF[5]; end30:begin r_scl<=1'b1 ; end 31: begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_OFF[4]; end32:begin r_scl<=1'b1 ; end 33: begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_OFF[3]; end34:begin r_scl<=班级文化布置1'b1 ; end 35: begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_OFF[2]; end36:begin r_scl<=1'b1 ; end 37: begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_OFF[1]; end38:begin r_scl<=1'b1 ; end 39: begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_OFF[0]; end40:begin r_scl<=1'b1 ; end 41: begin r_scl<=1'b0; end42:begin ctl_sda<=1'b0; end 43: begin r_scl<=1'b1; end44:begin r_scl<=1'b0; if(sda==1'b0) begincnt<=45;end elcnt<=43; end45: begin ctl_sda<=1'b1; end //STOP 46: begin r_scl<=1'b1; end47: begin r_sda<=1'b0; end 48: begin r_sda<=1'b1; end//外部地址+写+上电---------------------------------------------------------//49:b祖宗之法egin ctl_sda<=1'b1; end //START 50: begin r_scl<=1'b1; end51:begin r_sda<=1'b1; end 52: begin r_sda<=1'b0;end//ADDRESS+WRITE 8'H46 53: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_W[7]; end54:begin r_scl<=1'b1; end 55: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_W[6]; end56:begin r_scl<=1'b1; end 57: begin r_scl<=1'b0;r_sda<=DEVI阴历三月三CE_ADDR_W[5]; end58:begin r_scl<=1'b1; end 59: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_W[4]; end60:begin r_scl<=1'b1; end 61: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_W[3]; end62:begin r_scl<=1'b1; end 63: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_W[2]; end64:begin r_scl<=1'b1; end 65: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_W[1]; end66:begin r_scl<=1'b1; end 67: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_W[0]; end68:begin r_scl<=1'b1; end 69: begin r_scl<=1'b0; end70:begin ctl_sda<=1'b0; end 71: begin r_scl<=1'b1; end72: begin r_scl<=1'b0; if(sda==1'b0) begincnt<=73;end elcnt<=71; end//POWER OFF 8'H00 73: begin ctl_sda<=1'b1; end74:begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_ON[7]; end 75: begin r_scl<=1'b1 ; end76:begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_ON[6]; end 77: begin r_scl<=1'b1 ; end78:begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_ON[5]; end 79: begin r_scl<=1'b1 ; end80:begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_ON[4]; end 81: begin r_scl<=1'b1 ; end82:begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_ON[3]; end 83: begin r_scl<=1'b1 ; end84:begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_ON[2]; end 85: begin r_scl<=1'b1 ; end86:begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_ON[1]; end 87: begin r_scl<=1'b1 ; end88:begin r_scl<=1'b0 ; r_sda<=DEVICE_POWER_ON[0]; end 89: begin r_scl<=1'b1 ; end90: begin r_scl<=1'b0; end 91: begin ctl_sda<=1'b0; end92:begin r_scl<=1'b1; end 93: begin r_scl<=1'b0;if(sda==1'b0) begin cnt<=94; end el cnt<=92; end 94: begin ctl_sda<=1'b1;end//STOP95:begin r_scl<=1'b1; end 96: begin r_sda<=1'b0; end97:begin r_sda<=1'b1; end ////外部地址+写+读取单次---------------------------------------------------------// 98: begin ctl_sda<=1'b1;end//START99:begin r_scl<=1'b1; end 100: begin r_sda<=1'b1;end101:begin r_sda<=1'b0; end //ADDRESS+WRITE 8'H46102:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[7]; end 103: begin r_scl<=1'b1; end104:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[6]; end 105: begin r_scl<=1'b1; end106:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[5]; end 107: begin r_scl<=1'b1; end108:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[4]; end 109: begin r_scl<=1'b1; end110:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[3]; end 111: begin r_scl<=1'b1; end112:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[2]; end 113: begin r_scl<=1'b1; end114:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[1]; end 115: begin r_scl<=1'b1; end116:begin r_scl<=1'b0; r_sda<=DEVICE_ADDR_W[0]; end 117: begin r_scl<=1'b1; end118:begin r_scl<=1'b0; end 119: begin ctl_sda<=1'b0; end120:begin r_scl<=1'b1; end 121: begin r_scl<=1'b0;if(sda==1'b0) begin cnt<=122; end el cnt<=120; end //POWER OFF 8'H23122:begin ctl_sda<=1'b1; end 123: begin r_scl<=1'b0 ; r_sda<=DEVICE_MEASURE[7]; end124:begin r_scl<=1'b1 ; end 125: begin r_scl<=1'b0 ; r_sda<=DEVICE_MEASURE[6]; end126:begin r_scl<=1'b1 ; end 127: begin r_scl<=1'b0 ; r_sda<=DEVICE_MEASURE[5]; end128:begin r_scl<=1'b1 ; end 129: begin r_scl<=1'b0 ; r_sda<=DEVICE_MEASURE[4]; end130:begin r_scl<=1'b1 ; end 131: begin r_scl<=1'b0 ; r_sda<=DEVICE_MEASURE[3]; end132:begin r_scl<=1'b1 ; end 133: begin r_scl<=1'b0 ; r_sda<=DEVICE_MEASURE[2]; end134:begin r_scl<=1'b1 ; end 135: begin r_scl<=1'b0 ; r_sda<=DEVICE_MEASURE[1]; end136:begin r_scl<=1'b1 ; end 137: begin r_scl<=1'b0 ; r_sda<=DEVICE_MEASURE[0]; end138:begin r_scl<=1'b1 ; end 139: begin r_scl<=1'b0; end140:begin ctl_sda<=1'b0; end 141: begin r_scl<=1'b1; end142:begin r_scl<=1'b0; if(sda==1'b0) begincnt<=143;end elcnt<=141; end143: begin ctl_sda<=1'b1; end //STOP 144: begin r_scl<=1'b1; end145:begin r_sda<=1'b0; end 146: begin r_sda<=1'b1; end////读取测量值---------------------------------------------------------//2147:begin ctl_sda<=1'b1; end //START 2148: begin r_scl<=1'b1; end2149:begin r_sda<=1'b1; end 2150: begin r_sda<=1'b0;end//ADDRESS+WRITE 8'H46 2151: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_R[7]; end2152:begin r_scl<=1'b1; end 2153: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_R[6]; end2154:begin r_scl<=1'b1; end 2155: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_R[5]; end2156:begin r_scl<=1'b1; end 2157: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_R[4]; end2158:begin r_scl<=1'b1; end 2159: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_R[3]; end2160:begin r_scl<=1'b1; end 2161: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_R[2]; end2162:begin r_scl<=1'b1; end 2163: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_R[1]; end2164:begin r_scl<=1'b1; end 2165: begin r_scl<=1'b0;r_sda<=DEVICE_ADDR_R[0]; end2166:begin r_scl<=1'b1; end 2167: begin r_scl<=1'b0;r_sda<=1; end2168:begin ctl_sda<=1'b0; end 2169: begin r_scl<=1'b1; end2170:begin r_scl<=1'b0; if(sda==1'b0)begin cnt<=2171; endelcnt<=2169;end //READ2171:begin r_scl<=1'b1 ; end 2172: begin data[15]=sda ; end 2173: begin r_scl<=1'b0 ; end2174:begin r_scl<=1'b1 ; end 2175: begin data[14]=sda ; end 2176: begin r_scl<=1'b0 ; end2177:begin r_scl<=1'b1 ; end 2178: begin data[13]=sda ; end 2179: begin r_scl<=1'b0 ; end2180:begin r_scl<=1'b1 ; end 2181: begin data[12]=sda ; end 2182: begin r_scl<=1'b0 ; end2183:begin r_scl<=1'b1 ; end 2184: begin data[11]=sda ; end 2185: begin r_scl<=1'b0 ; end2186:begin r_scl<=1'b1 ; end 2187: begin data[10]=sda ; end 2188: begin r_scl<=1'b0; end2189:begin r_scl<=1'b1; end 2190: begin data[9]=sda ; end 2191: begin r_scl<=1'b0; end2192: begin r_scl<=1'b1; end 2193: begin data[8]=sda ; end 2194: begin r_scl<=1'b0; end2195:begin r_scl<=1'b1;ctl_sda<=1'b1;r_sda<=1'b0;end 2196: begin r_scl<=1'b1; end 2197:begin r_scl<=1'b0; ctl_sda<=1'b1; r_sda<=1'b1; end 2198: begin ctl_sda<=0;end 2199: begin r_scl<=1'b1 ; end2200:begin data[7]=sda ; end2201:begin r_scl<=1'b0 ; end 2202: begin r_scl<=1'b1 ; end2203:begin 语文核心素养data[6]=sda ; end2204:begin r_scl<=1'b0 ; end 2205: begin r_scl<=1'b1 ; end2206:begin data[5]=sda ; end2207:begin r_scl<=1'b0 ; end 2208: begin r_scl<=1'b1 ; end2209:begin data[4]=sda ; end2210:begin r_scl<=1'b0 ; end 2211: begin r_scl<=1'b1 ; end2212:begin data[3]=sda ; end2213:begin r_scl<=1'b0 ; end 2214: begin r_scl<=1'b1 ; end2215:begin data[2]=sda ; end2216: begin r_scl<=1'b0; end 2217: begin r_scl<=1'b1; end2218:begin data[1]=sda ; end2219:begin r_scl<=1'b0; end 2220: begin r_scl<=1'b1; end2221:begin data[0]=sda ; end2222:begin r_scl<=1'b0; end 2223: begin ctl_sda<=1'b1; r_sda<=1;enddefault:begin r_scl<=1'b1; ctl_sda<=1'b0; endendcaendendendmodule
本文地址:https://blog.csdn.net/Wuyo_7/article/details/107299080
本文发布于:2023-04-08 19:56:44,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/e4ceeba8357251a733dee1239d7e7052.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:BH1750光线传感器verilog驱动代码FPGA开发.doc
本文 PDF 下载地址:BH1750光线传感器verilog驱动代码FPGA开发.pdf
留言与评论(共有 0 条评论) |