kunal5959
Junior Member level 3
- Joined
- Jul 26, 2011
- Messages
- 31
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 1,644
Code VHDL - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity I2c_master is port( clk : in std_logic; --system clock reset_n : in std_logic; --active low reset SDA : inout std_logic; --data read from slave LED : out std_logic_vector(7 downto 0); I2CCLOCK : out std_logic); end i2c_master; architecture logic of I2c_master is component countertest is port (Aclr : in std_logic; clock : in std_logic; Q : out std_logic_vector(7 downto 0) ); end component; signal data_clk, i2c_clk : std_logic := '1'; --clock edges for sda signal sda_out : std_logic := '1'; --internal sda signal ledsig1 : std_logic_vector(7 downto 0); --latched in address and read/write signal tx1 : std_logic_vector(7 downto 0) := "00000000"; --data received from slave MSB signal tx2 : std_logic_vector(7 downto 0) := "00000000"; --data received from slave LSB shared variable step : integer range 0 to 83; begin --generate the timing for the bus clock (scl_clk) and the data clock (data_clk) process(data_clk, reset_n) begin if(reset_n = '1') then -- reset asserted sda_out <= '1'; --- set sda pin to high impedance--- set scl pin to high impedance i2c_clk <= '1'; elsif(data_clk'event and data_clk = '1') then case step is when 0 => sda_out <= '1'; i2c_clk <= '1'; step := step+1; when 1 => sda_out <= '0'; step := step+1; when 2 => --- 1st write byte -- i2c_clk <= '0'; sda_out <= '1'; step := step+1; when 3 => sda_out <= '0'; step := step+1; when 4 => sda_out <= '0'; step := step+1; when 5 => sda_out <= '1'; step := step+1; when 6 => sda_out <= '0'; step := step+1; when 7 => sda_out <= '0'; step := step+1; when 8 => sda_out <= '0'; step := step+1; when 9 => sda_out <= '0'; step := step+1; when 10 => -- slv ack sda_out <= 'Z'; step := step+1; when 11 => sda_out <= '0'; step := step+1; when 12 => sda_out <= '0'; step := step+1; when 13 => sda_out <= '0'; step := step+1; when 14 => sda_out <= '0'; step := step+1; when 15 => sda_out <= '0'; step := step+1; when 16 => sda_out <= '0'; step := step+1; when 17 => sda_out <= '0'; step := step+1; when 18 => sda_out <= '1'; step := step+1; when 19 => --slv_ack sda_out <= 'Z'; step := step+1; when 20 => sda_out <= '0'; step := step+1; when 21 => sda_out <= '0'; step := step+1; when 22 => sda_out <= '0'; step := step+1; when 23 => sda_out <= '0'; step := step+1; when 24 => sda_out <= '0'; step := step+1; when 25 => sda_out <= '1'; step := step+1; when 26 => sda_out <= '0'; step := step+1; when 27 => sda_out <= '0'; step := step+1; when 28 => sda_out <= 'Z';---slv_ack step := step+1; when 29 => sda_out <= '1'; step := step+1; when 30 => sda_out <= '0'; step := step+1; when 31 => sda_out <= '0'; step := step+1; when 32 => sda_out <= '0'; step := step+1; when 33 => sda_out <= '0'; step := step+1; when 34 => sda_out <= '0'; step := step+1; when 35 => sda_out <= '1'; step := step+1; when 36 => sda_out <= '1'; step := step+1; when 37 => sda_out <= 'Z';--slv_ack step := step+1; when 38 => sda_out <= '1'; step := step+1; when 39 => sda_out <= '0'; step := step+1; when 40 => sda_out <= '0'; step := step+1; when 41 => sda_out <= '1'; step := step+1; when 42 => sda_out <= '0'; step := step+1; when 43 => sda_out <= '0'; step := step+1; when 44 => sda_out <= '0'; step := step+1; when 45 => sda_out <= '0'; step := step+1; when 46 => --slv_ack sda_out <= 'Z'; step := step+1; when 47 => sda_out <= '0'; step := step+1; when 48 => sda_out <= '0'; step := step+1; when 49 => sda_out <= '0'; step := step+1; when 50 => sda_out <= '0'; step := step+1; when 51 => sda_out <= '0'; step := step+1; when 52 => sda_out <= '0'; step := step+1; when 53 => sda_out <= '0'; step := step+1; when 54 => sda_out <= '0'; step := step+1; when 55 => sda_out <= 'Z';--slv_ack step := step+1; when 56 => sda_out <= '1'; step := step+1; when 57 => sda_out <= '0'; step := step+1; when 58 => sda_out <= '0'; step := step+1; when 59 => sda_out <= '1'; step := step+1; when 60 => sda_out <= '0'; step := step+1; when 61 => sda_out <= '0'; step := step+1; when 62 => sda_out <= '0'; step := step+1; when 63 => sda_out <= '1'; step := step+1; when 64 => sda_out <= 'Z';--slv_ack step := step+1; when 65 => tx1(0) <= sda_out; step := step+1; when 66 => tx1(1) <= sda_out; step := step+1; when 67 => tx1(2) <= sda_out; step := step+1; when 68 => tx1(3) <= sda_out; step := step+1; when 69 => tx1(4) <= sda_out; step := step+1; when 70 => tx1(5) <= sda_out; step := step+1; when 71 => tx1(6) <= sda_out; step := step+1; when 72 => tx1(7) <= sda_out; step := step+1; when 73 => sda_out <= '0'; -- mstr_ack step := step+1; when 74 => tx2(0) <= sda_out; step := step+1; when 75 => tx2(1) <= sda_out; step := step+1; when 76 => tx2(2) <= sda_out; step := step+1; when 77 => tx2(3) <= sda_out; step := step+1; when 78 => tx2(4) <= sda_out; step := step+1; when 79 => tx2(5) <= sda_out; step := step+1; when 80 => tx2(6) <= sda_out; step := step+1; when 81 => tx2(7) <= sda_out; step := step+1; when 82 => sda_out<='0'; --mstr_ack step := step+1; when 83 => sda_out<='0'; i2c_clk<='1'; --stop Bit step := step+1; when 84 => sda_out<='1'; step:=0; end case; end if; end process; process(data_clk) begin if (data_clk'event and data_clk = '0'and (step >= 3) and (step <= 84)) then I2CCLOCK <= not data_clk; else I2CCLOCK <= i2c_clk; end if; end process; SDA <= sda_out; data_clk <= ledsig1(7); LED <= tx1; countertesting : countertest port map (Clock => CLK, Aclr => reset_n, Q => ledsig1); end logic;
I am a begginner in VHDL so please pardon any silly mistakes. I wanted to activate ADS1015 ADC chip through I2c bus by sending appropriate write bytes containing address and register information. I have tried to write a very simple program so i do not make any mistakes. The problem is that i am a bit confused how to use Tristate buffer. Since as i read in other post we just need connect the bus to internal signal (sda_out in my case) when SDA acts as output for writing to slave and I have disconnected it from internal bus when i need to read data received from from SLave. I directly set the sda_out to High impedance instead of using a tristate buffer. Could that be a problem...I am able to send write data from my master to slave(ADC) but i never get an Acknowledgement ( it remains in High Impedance state) and neither am i able to read the data received from slave.
Please check this link for ADs1015 datasheet https://www.ti.com/lit/ds/symlink/ads1015.pdf.
I am using following testbench to simulate in Modelsim ,..
Code VHDL - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 library ieee; use ieee.std_logic_1164.all; entity TB is end TB; architecture behavioral of TB is component i2c_master -- ports port (clk : IN STD_LOGIC; --system clock reset_n : IN STD_LOGIC; --active low reset SDA : inout std_logic; LED: out std_logic_vector(7 DOWNTO 0); I2CCLOCK : out std_logic); end component; signal clk,ena: std_logic := '0'; --signal SW5: std_logic := '0'; signal reset_n, I2CCLOCK: std_logic := '1'; signal SDA :std_logic; SIGNAL data_clk : STD_LOGIC:='0'; --clock edges for sda SIGNAL sda_out : STD_LOGIC ; -- internal sda SIGNAL data_tx : std_logic_vector(7 DOWNTO 0); -- latched in data to write to slave SIGNAL data_rx_MSB : std_logic_vector(7 DOWNTO 0); -- data received from slave MSB SIGNAL data_rx_LSB : std_logic_vector(7 DOWNTO 0); --data received from slave LSB SIGNAL bit_cnt : INTEGER RANGE 0 TO 7 := 7; --tracks bit number in transaction SIGNAL stretch : STD_LOGIC := '0'; --identifies if slave is stretching scl begin clock : process begin wait for 10 ns; clk <= not clk; end process ; stimulus : process begin wait for 4040 ns; reset_n <= '0'; wait; end process; i2c_master_0 : i2c_master -- port map port map( -- Inputs clk => clk, reset_n => reset_n, SDA => SDA, LED=>open, I2CCLOCK =>I2CCLOCK ); --SDA<=ADCoutput; end behavioral;
Please guide me.
I wanted to attach oscilloscope images of signals being generated but I cannot find any option to attach images here.
Last edited by a moderator: