Author Topic: Help on Code to emulate SPI or I2C thru USB COM ports  (Read 1319 times)

0 Members and 1 Guest are viewing this topic.

Offline acjacques

  • Newbie
  • Posts: 33
Help on Code to emulate SPI or I2C thru USB COM ports
« on: November 03, 2019, 04:13:15 pm »
Dear Friends
;
Bellow is the GOSUB (500-700) code that emulates  SPI communication thru native COM port to MAXIM186 ADC  8ch 12bit analog converter IC  . It was writed for the  old QB45/QB71 and desktop PC ...

I was mind if is possible this code works with USB COM ports of W10 notebooks...

Any adaption or suggestion how emulate SPI or I2C  communications ?

Thanks for your comments


Code: QB64: [Select]
  1.  
  2. 5 DIM V(8)
  3. 10 BA = &H2F8: REM COM2
  4. 20 OUT (BA + 3), 64
  5.  
  6. 120 FOR N = 0 TO 7: REM *****AQUI SELECIONA O NUMERO DE CANAIS*******
  7. 130 SEL = 7 - (N \ 2) - 4 * (N AND 1)
  8. 140 GOSUB 500: REM aquisita canais
  9.  
  10. 500 REM *************Aquisita canais MAX186 ******
  11. 510 CMD = 128 + 16 * SEL + 8 + 4 + 2
  12. 520 BIT = 128
  13. 530 FOR B = 1 TO 8
  14. 540 IF (CMD AND BIT) = BIT THEN RTS = 2 ELSE RTS = 0
  15. 550 OUT (BA + 4), RTS
  16. 560 OUT (BA + 4), RTS + 1
  17. 570 OUT (BA + 4), RTS
  18. 580 BIT = BIT \ 2
  19. 590 NEXT B
  20. 595 OUT (BA + 4), 0
  21. 600 FOR t = 1 TO 10: NEXT t
  22. 610 BIT = 2048: AUS = 0
  23. 630 FOR B = 1 TO 16
  24. 640 OUT (BA + 4), 1
  25. 650 OUT (BA + 4), 0
  26. 660 IF ((INP(BA + 6) AND 16) = 16) THEN AUS = AUS + BIT
  27. 670 BIT = BIT \ 2
  28. 680 NEXT B
  29. 692 V(N + 1) = AUS / 1000
  30. 700 RETURN
  31.  
  32.  

bellow is a I2C  code of BasicStamp  to read CH0  of MAX186

control is over 4 pins:

CS
DIN
DOUT
SCLK


read1: 'le canal 0 e armazena na  variavel AD(0): read ch 0 and store as variable AD(0)
low CS 'bring CS low
shiftout din,sclk,1,[%10001111] 'send the init string ' the high nibble is the channel number
shiftin dout,sclk,2,[AD(0)\12] 'get the 12 bit result
high CS 'bring CS high

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
Re: Help on Code to emulate SPI or I2C thru USB COM ports
« Reply #1 on: November 03, 2019, 04:47:04 pm »
Hi acjacques

Quote
code that emulates  SPI communication thru native COM port to MAXIM186 ADC  8ch 12bit analog converter IC
in QB64 OUT and INP are emulated, so you must see if for your program the emulation is supported
see here
http://www.qb64.org/wiki/INP    http://www.qb64.org/wiki/OUT
while this is about COM port http://www.qb64.org/wiki/OPEN_COM

Quote
Any adaption or suggestion how emulate SPI or I2C  communications ?
this question is for who can understand your word! Who knows what is SPI oe I2C!
Good Luck
Programming isn't difficult, only it's  consuming time and coffee

Offline acjacques

  • Newbie
  • Posts: 33
Re: Help on Code to emulate SPI or I2C thru USB COM ports
« Reply #2 on: November 03, 2019, 05:18:08 pm »
SPI= Serial Parallel Interface   = Protocol used for some chips to control and communications
I2C = Inter Integrated Circuit =  Bus developed by Philips to connect some electronics 
Both are very similar
They are serial synchonous communication