四线电阻屏verilog驱动程序,基于ADS7843

来源:互联网 发布:优化算法 知乎 编辑:程序博客网 时间:2024/05/09 20:14
//本人出入FPGA,可能程序写的不好,请指正。
1
module touch( 2 CLK,RSTn, 3 4 TOUCH_CS, 5 TOUCH_IRQ, 6 TOUCH_BY, 7 TOUCH_CLK, 8 TOUCH_MISO, 9 TOUCH_MOSI, 10 11 test 12 ); 13 input CLK; 14 input RSTn; 15 16 input TOUCH_BY; 17 output TOUCH_CS; 18 //input TOUCH_IRQ; 19 inout TOUCH_IRQ; 20 output TOUCH_CLK; 21 input TOUCH_MISO; 22 output TOUCH_MOSI; 23 24 output [3:0]test; 25 reg TOUCH_CSr; 26 reg TOUCH_CLKr; 27 reg TOUCH_MOSIr; 28 29 //check the errer 30 reg [7:0] count_irq; 31 reg irq_flag; 32 always @(posedge CLK or negedge RSTn) 33 if(!RSTn)begin 34 count_irq <= 8'd0; 35 irq_flag <= 1'b0;end 36 else if(count_irq == 8'd20) 37 begin 38 count_irq <= 8'd0; 39 irq_flag <= 1'b1; 40 end 41 else 42 count_irq <= count_irq + 1'b1; 43 44 assign TOUCH_IRQ = (irq_flag)? 1'bz:1'b0; 45 // 46 reg [7:0] count_500ns; 47 48 parameter T500ns = 8'd24;//25*20ns = 500ns = 0.5us 49 reg spi_clk_flag; 50 //*************************************************** 51 `define spi_clk_open 1'b1 52 `define spi_clk_close 1'b0 53 //*************************************************** 54 always @(posedge CLK or negedge RSTn) 55 if(!RSTn) 56 count_500ns <= 8'd0; 57 else if(count_500ns == T500ns || (!spi_clk_flag)) 58 count_500ns <= 8'd0; 59 else if(spi_clk_flag) 60 count_500ns <= count_500ns + 1'b1; 61 62 reg [7:0] count_2us; 63 reg count_2us_flag; 64 //************************************************* 65 `define count_2us_open 1'd1 66 `define count_2us_close 1'd0 67 //************************************************* 68 always @(posedge CLK or negedge RSTn) 69 if(!RSTn) 70 count_2us <= 8'd0; 71 else if(count_2us == 8'd99 || (!count_2us_flag)) 72 count_2us <= 8'd0; 73 else if(count_2us_flag) 74 count_2us <= count_2us + 1'b1; 75 else 76 count_2us <= count_2us; 77 78 //************************************************************** 79 `define touch_clk_pos 8'd2 80 `define touch_clk_neg T500ns/2 81 `define high 1'b1 82 `define low 1'b0 83 //************************************************************** 84 85 parameter touch_cmd_x = 8'b1001_0000;// x_pos 86 parameter touch_cmd_y = 8'b1101_0000;// y_pos 87 reg [7:0] state_touch; 88 reg [7:0] count_cmd_bit; 89 reg [7:0] count_dat_bit; 90 reg [11:0] touch_x1data; 91 reg [11:0] touch_x2data; 92 reg [7:0] count_sample; 93 always @(posedge CLK or negedge RSTn) 94 if(!RSTn) 95 begin 96 state_touch <= 8'd0; 97 spi_clk_flag <= `spi_clk_close; 98 count_cmd_bit <= 8'd0; 99 count_sample <= 8'd0;100 end101 else 102 case (state_touch) 103 8'd0:104 if(!TOUCH_IRQ )105 begin106 state_touch <= 8'd1;107 count_sample <= 8'd0;108 TOUCH_CSr <= `low;109 spi_clk_flag <= `spi_clk_close;110 count_2us_flag <= `count_2us_close;111 TOUCH_CLKr <= `low;112 end113 else114 begin115 state_touch <= 8'd0;116 TOUCH_CSr <= `high;117 TOUCH_CLKr <= `low;118 end119 8'd1:120 begin121 state_touch <= 8'd2;122 spi_clk_flag <= `spi_clk_open;123 TOUCH_CSr <= `low;124 count_cmd_bit <= 8'd9;125 count_dat_bit <= 8'd16;126 end 127 8'd2://write cmd128 if(count_500ns == `touch_clk_pos)//wait posedge clk129 begin130 state_touch <= 8'd3;131 TOUCH_CLKr <= `low;132 TOUCH_MOSIr <= touch_cmd_x[count_cmd_bit-2'b10];133 count_cmd_bit <= count_cmd_bit - 1'b1;134 end135 else136 state_touch <= 8'd2;137 8'd3:138 if(count_cmd_bit == 8'd0)139 begin140 TOUCH_MOSIr <= 1'd0;141 TOUCH_CLKr <= `low;142 if(count_2us == 8'd99)143 begin 144 state_touch <= 8'd4;145 count_2us_flag <= `count_2us_close;146 count_dat_bit <= 8'd16;147 spi_clk_flag <= `spi_clk_open;148 149 end150 else 151 begin 152 state_touch <= 8'd3;153 count_2us_flag <= `count_2us_open;154 spi_clk_flag <= `spi_clk_close;155 end156 end157 else if(count_500ns == `touch_clk_neg)158 begin 159 state_touch <= 8'd2;160 TOUCH_CLKr <= `high;161 end162 else state_touch <= 8'd3;163 8'd4://read data164 if(count_500ns == `touch_clk_neg)//wait negedge clk165 begin166 state_touch <= 8'd5;167 TOUCH_CLKr <= `high;168 touch_x1data[count_dat_bit-1'b1] <= TOUCH_MISO;169 end170 else171 state_touch <= 8'd4;172 8'd5:173 begin174 state_touch <= 8'd6;175 count_dat_bit <= count_dat_bit - 1'b1;176 end177 8'd6:178 if(count_dat_bit == 8'd0)179 begin180 state_touch <= 8'd7;181 TOUCH_CLKr <= `low;182 end183 else if(count_500ns == `touch_clk_pos)184 begin185 TOUCH_CLKr <= `low;186 state_touch <= 8'd4;187 end188 else 189 state_touch <= 8'd6;190 191 8'd7: 192 begin193 state_touch <= 8'd8;194 spi_clk_flag <= `spi_clk_close; //close the spi clock195 TOUCH_CSr <= `high;196 end197 8'd8:198 if(count_2us == 8'd99)199 begin 200 state_touch <= 8'd0;201 count_2us_flag <= `count_2us_close;202 end203 else 204 begin 205 state_touch <= 8'd8;206 count_2us_flag <= `count_2us_open;207 end208 209 endcase210 assign TOUCH_CLK = TOUCH_CLKr;211 assign TOUCH_CS = TOUCH_CSr;212 assign TOUCH_MOSI = TOUCH_MOSIr; 213 assign test = touch_x1data[3:0];//显示采集的第四位数据 215 endmodule

 

0 0
原创粉丝点击