Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : PM: Confused about SYSTEM I2C and SETPIN sda,scl,I2C....
Page 2 of 3 | |||||
Author | Message | ||||
phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 1769 |
RTC command - manual page 113 "These chips are I2C devices and must be connected to the two I2C pins with appropriate pullup resistors. If the I2C bus is already open the RTC command will use the current settings, otherwise it will temporarily open the connection with a speed of 100 kHz." So 400kHz does not seem to be a problem. Adapted for System I2C and scanning DS1307 module with 24C32 memory. Print "System I2C Address Scanner" ' Prep table Print " 0 1 2 3 4 5 6 7 8 9 A B C D E F" ' loop col/row For y=0 To 7 Print Hex$(y,2);": "; For x=0 To 15 addr = y*16+x ' calc address I2C write addr, 0, 1, &H00 ' write zerp to that adress If MM.I2C=0 Then ' check for errors Print Hex$(addr,2);" "; ' found one! Else Print "-- "; ' nothing here.. EndIf Next x Next y System I2C Address Scanner 0 1 2 3 4 5 6 7 8 9 A B C D E F 00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 01: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 02: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 03: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 04: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 05: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 06: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 07: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > Edited 2022-08-26 13:26 by phil99 |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8566 |
if(Option.SYSTEM_I2C_SDA){ ExtCfg(Option.SYSTEM_I2C_SCL, EXT_BOOT_RESERVED, 0); ExtCfg(Option.SYSTEM_I2C_SDA, EXT_BOOT_RESERVED, 0); gpio_set_function(PinDef[Option.SYSTEM_I2C_SCL].GPno, GPIO_FUNC_I2C); gpio_set_function(PinDef[Option.SYSTEM_I2C_SDA].GPno, GPIO_FUNC_I2C); if(PinDef[Option.SYSTEM_I2C_SDA].mode & I2C0SDA){ I2C0locked=1; i2c_init(i2c0, 100000); gpio_pull_up(PinDef[Option.SYSTEM_I2C_SCL].GPno); gpio_pull_up(PinDef[Option.SYSTEM_I2C_SDA].GPno); I2C_enabled=1; I2C0SDApin=Option.SYSTEM_I2C_SDA; I2C0SCLpin=Option.SYSTEM_I2C_SCL; I2C_Timeout=100; } else { I2C1locked=1; i2c_init(i2c1, 100000); gpio_pull_up(PinDef[Option.SYSTEM_I2C_SCL].GPno); gpio_pull_up(PinDef[Option.SYSTEM_I2C_SDA].GPno); I2C2_enabled=1; I2C1SDApin=Option.SYSTEM_I2C_SDA; I2C1SCLpin=Option.SYSTEM_I2C_SCL; I2C2_Timeout=100; } if(Option.RTC)RtcGetTime(1); } |
||||
Quazee137 Guru Joined: 07/08/2016 Location: United StatesPosts: 522 |
a little mod to phil99's scanner Print "System I2C and I2C2 Address Scanner" ' Prep table print "For I2C" Print " 0 1 2 3 4 5 6 7 8 9 A B C D E F" ' loop col/row For y=0 To 7 Print Hex$(y,2);": "; For x=0 To 15 addr = y*16+x ' calc address I2C write addr, 0, 1, &H00 ' write zerp to that adress If MM.I2C=0 Then ' check for errors Print Hex$(addr,2);" "; ' found one! Else Print "-- "; ' nothing here.. EndIf Next x Next y ' SetPin gp3,gp2,i2c2 ' change pins to your usage I2C2 open 100,1000 print "For I2C2" Print " 0 1 2 3 4 5 6 7 8 9 A B C D E F" ' loop col/row For y=0 To 7 Print Hex$(y,2);": "; For x=0 To 15 addr = y*16+x ' calc address I2C2 write addr, 0, 1, &H00 ' write zerp to that adress If MM.I2C=0 Then ' check for errors Print Hex$(addr,2);" "; ' found one! Else Print "-- "; ' nothing here.. EndIf Next x Next y I2C2 close I have a box full of 1307's and DHT12's that I'm playing with as room loggers saving every 20 mins to on board eproms. New windows coming next mid next month and want to see how much better the are so need old windows ref. I was running Analog clock on a pico and added the I2C2 to test out the DHT12 code and had a DS3231 on I2C and a 1307 on I2C2. If I unplugged either and did RTC gettime both gave me the correct time but only been testing for a week. I had set the time on them while plugged in to a MM170 both seem to keep ok time. MM170 boards out in the field started with the 1307's but over time was switched out for the DS3231's. So now playing with a box full of the 1307's LOL. So much Thanks to Geoff, Peter and all other's making MMBasic so much FUN. Quazee137 Edited 2022-08-26 18:26 by Quazee137 |
||||
Volhout Guru Joined: 05/03/2018 Location: NetherlandsPosts: 3490 |
Thanks Peter ! I assumed this was the case, but was mislead by expressions from others. I apologize for not investigating it myself before raising the issue. Happy holidays... PicomiteVGA PETSCII ROBOTS |
||||
pwillard Senior Member Joined: 07/06/2022 Location: United StatesPosts: 274 |
With a *proper* crystal, the DS1307 only loses about a minute per month. With the poorly matched crystal most commonly found on DS1307 break-out boards... It's much worse and what gives them a bad reputation (rightfully so). But they are still horrible when compared to the likes of DS3231 (primarily because they are not leaving the crystal timing issues up to an external part). |
||||
phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 1769 |
Some extras for Quazee137's upgraded version. Print "PicoMite System I2C and I2C2 Address Scanner" Print ' Prep table print "For System I2C" Print " 0 1 2 3 4 5 6 7 8 9 A B C D E F" ' loop col/row For y=0 To 7 Print Hex$(y,2);": "; For x=0 To 15 addr = y*16+x ' calc address I2C write addr, 0, 1, &H00 ' write 0 to that adress If MM.I2C=0 Then ' check for errors Print Hex$(addr,2);" "; ' found one! device=addr Else Print "-- "; ' nothing here.. EndIf Next x If device=&H68 Then :Print "RTC DS3232, DS1307 etc"; : device=0 : EndIf If (device>&H49) And (device<&H58) Then :Print "24Cxx EEPROM"; : device=0 : EndIf If device=&H3C Then :Print "LCD SSD1306"; : device=0 : EndIf Next y SetPin gp3,gp2,i2c2 ' change pins to your usage I2C2 open 100,1000 print "For I2C2 - SDA = GP2, SCL = GP3 " Print " 0 1 2 3 4 5 6 7 8 9 A B C D E F" ' loop col/row For y=0 To 7 Print Hex$(y,2);": "; For x=0 To 15 addr = y*16+x ' calc address I2C2 write addr, 0, 1, &H00 ' write 0 to that adress If MM.I2C=0 Then ' check for errors Print Hex$(addr,2);" "; ' found one! device=addr Else Print "-- "; ' nothing here.. EndIf Next x If device=&H68 Then :Print "RTC DS3232, DS1307 etc"; : device=0 : EndIf If (device>&H49) And (device<&H58) Then :Print "24Cxx EEPROM"; : device=0 : EndIf If device=&H3C Then :Print "LCD SSD1306"; : device=0 : EndIf Next y I2C2 close PicoMite System I2C and I2C2 Address Scanner For System I2C 0 1 2 3 4 5 6 7 8 9 A B C D E F 00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 01: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 02: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 03: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 04: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 05: -- -- -- 53 -- -- -- -- -- -- -- -- -- -- -- -- 24Cxx EEPROM 06: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- RTC DS3232, DS1307 etc 07: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- For I2C2 - SDA = GP2, SCL = GP3 0 1 2 3 4 5 6 7 8 9 A B C D E F 00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 01: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 02: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 03: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 04: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 05: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 24Cxx EEPROM 06: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- RTC DS3232, DS1307 etc 07: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- > |
||||
Volhout Guru Joined: 05/03/2018 Location: NetherlandsPosts: 3490 |
This is a pretty hefty position. I cannot confirm the above. It is true that the cheap chinese DS1307 boards perform bad, but that is not the chips doing. It is the board loading that causes this problem, not the chip as such. I just bought another one of these boards, and again noticed exactly the same faults as on earlier boards: - For some stupid reason there are 2x 3.3k pullup in parallel on each of the I2C lines. As if the 24C32 and DS1307 I2C busses once where separate, each with 3.3k resistors, then connected together, but the resistors remained. - The battery circuit is designed for a rechargeable battery (Lithium) that can have a voltage fully charged up to 4V. To protect the Vbat of the chip there is a resistor divider (500k/1.5meg) to limit this voltage for the chip to 3V. But the 2uA drawn bij this resistor divider consumes more energy than the DS1307. So battery life is shortened. - There is no decoupling capacitor at the Vbat of the DS1307. That is not needed when connected to a battery, but when connected to high impedance voltage divider, the chip may act strange. - The crystal seems off-tuned (the DS1307 seems to go faster than actual time). But there are provisions to solder the can of the crystal to the board. If you actually do that, the time is far more accurate. Conclusion: Blaming the chip may not do it justice. The circuit around it is not optimal. Oh, voltage levels on I2C bus. The DS1307 needs 5V, but the I2C lines are perfectly happy with 3.3V (Vhigh = 2.2V). So if you remove all pullups from the DS1307 module, and add pullups to 3.3V on the micromite/picomite, it works flawless. You only need to power the DS1307 from pin 39 of the picomite (Vsys=4.7V). Regards, Volhout PicomiteVGA PETSCII ROBOTS |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5879 |
Most DS1307 modules use a standard clock crystal. Good ones give about 1 minute per month accuracy over typical temperature ranges. The cheap module are likely to use very poor crystals which could be worst. DS3231 etc use a TCXO which gives one minute per year over the same temperature range. The cheap DS3231 modules are likely to use 'second quality' chips but do seem to be reasonably precise compared to the DS1307 modules. If you are only after short term accuracy and in a controlled environment, the DS1307 is fine. Most of my uses have the data acquisition systems in semi-outdoor, uncontrolled environments and need better than what the DS1307 provides, even with regular time adjustments from the network during data transfers. Jim VK7JH MMedit  MMBasic Help |
||||
phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 1769 |
I want to go out and play, but it's raining. I'm bored, so here is another version. Print "PicoMite System I2C and I2C2 Address Scanner" ' SDA = 4 : SCL = 5 ' I2C2 pins. Change pins to your usage, 0 if no I2C2 ' (Do NOT use GP numbers, standard Pico RP2040 module pin numbers only) again = 1 Print "For System I2C" Scan If SDA Then SetPin SDA, SCL, i2c2 I2C2 open 100,1000 Print "For I2C2 - SDA = Pin("SDA"), SCL = Pin("SCL")" again = 0 Scan Else Print "I2C2 not configured" EndIf ' Sub Scan Print " 0 1 2 3 4 5 6 7 8 9 A B C D E F" ' Prep table ' loop col/row For y=0 To 7 Print Hex$(y,2);": "; For x=0 To 15 addr = y*16+x ' calc address If again Then On Error Skip 3 I2C write addr, 0, 1, &H00 ' write 0 to that adress Else I2C2 write addr, 0, 1, &H00 ' write 0 to that adress EndIf If MM.Errno = 0 Then If MM.I2C=0 Then ' check for errors Print Hex$(addr,2);" "; ' found one! device=addr Else Print "-- "; ' nothing here.. EndIf Else Print " I2C not configured" EndIf Next x ' -------------- Add your favorite devices here ------------------ If device = &H68 Then : Print "RTC DS3232, DS1307 etc"; : device=0 : EndIf If (device > &H49) And (device < &H58) Then : Print "24Cxx EEPROM"; : device=0 : EndIf If device = &H3C Then : Print "LCD SSD1306"; : device=0 : EndIf Next y End Sub |
||||
TrevorH Senior Member Joined: 06/04/2018 Location: United KingdomPosts: 142 |
I am try to get 2 picos to talk to each other with I2C. Master sending using GP20 and GP21, Slave using GP16 and GP17. I have tried the routines explained in 'Getting started with the Micromite' pages 73 and 74, but all I get is "Slave did not respond" and the LED on pin 1 doesn't flash. This my first use of I2c other than using RTC. MASTER:- OPTION AUTORUN ON SETPIN GP20, GP21, I2C I2C OPEN 100, 1000 SSETPIN 1, 8 SPIN 1, 1 PAUSE 300 SPIN 1, 0 ' configure an I/O pin on the slave SUB SSETPIN pinnbr, cfg I2C OPEN 100, 1000 I2C WRITE &H50, 0, 3, 1, pinnbr, cfg IF MM.I2C THEN ERROR "Slave did not respond" I2C CLOSE END SUB ' set the output of an I/O pin on the slave SUB SPIN pinnbr, dat I2C OPEN 100, 1000 I2C WRITE &H50, 0, 3, 2, pinnbr, dat IF MM.I2C THEN ERROR "Slave did not respond" I2C CLOSE END SUB SLAVE:- OPTION AUTORUN ON Setpin GP16, GP17 I2C DIM msg(2) ' array used to hold the message I2C SLAVE OPEN &H50, WriteD, ReadD DO ' the program loops forever WATCHDOG 1000 ' this will recover from errors LOOP SUB ReadD ' received a message I2C SLAVE READ 3, msg(), recvd ' get the message into the array IF msg(0) = 1 THEN ' command = 1 SETPIN msg(1), msg(2) ' configure the I/O pin ELSEIF msg(0) = 2 THEN ' command = 2 PIN(msg(1)) = msg(2) ' set the I/O pin's output ELSE ' the command must be 3 s$ = str$(pin(msg(1))) + Space$(12) ' get the input on the I/O pin End if END SUB SUBWriteD 'request from the master I2C SLAVE WRITE 12, s$ ' send the last measurement END SUB ' return from the interrupt I wondered if the code needed to be different on the Pico? |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8566 |
Check my original post and see if you can get that to work - please report if not |
||||
TrevorH Senior Member Joined: 06/04/2018 Location: United KingdomPosts: 142 |
@matherp, that works giving me the time from powering up on the console repeatedly incrementing, so the two chips are talking but why doesn't the code I posted work? Is the Pico implementation different? |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5879 |
You are opening I2C in the main program and again in the SUB without closing it first. I suspect that that is your error. Jim VK7JH MMedit  MMBasic Help |
||||
TrevorH Senior Member Joined: 06/04/2018 Location: United KingdomPosts: 142 |
Hi Jim, I have tried all the combinations, no opens and closes and use I2C System, I2C open only at the start of the master and no opens and closes. I've tried different addresses all with same result. All I am trying to do is setup 1 I/O pin and toggle it. |
||||
TrevorH Senior Member Joined: 06/04/2018 Location: United KingdomPosts: 142 |
I have these Pico's talking to each other eventually, as I/O, but if I try to use the read I/O pin command (3) from the master it does one iteration in the loop and locks up without passing back the read data. I must be doing something wrong. Has anyone any ideas?? The first problem I had was that 'setpin 1, 8' did not work (threw an error) although using this is suggested in the manual. The hardware is a bunch of leds wired to first 10 ports via 240ohm Rs and 2 picos wired together with I2C pins (master 20,21, slave 16,17) SLAVE option autorun on dim as integer msg(2), addr = 69 dim as string s$ setpin gp16, gp17, i2c 'setpin gp26, Ain i2c slave open addr, tint, rint 'slaves address is HEX 50 do 'print pin(gp26) Watchdog 1000 loop sub rint local count msg(0) = 0 msg(1) = 0 msg(2) = 0 i2c slave read 3, msg(), count 'print msg(0), msg(1), msg(2), count if msg(0) = 1 then 'check command is 1 select case msg(2) case 1 setpin msg(1), Ain 'configure the I/O port case 2 setpin msg(1), din case 3 setpin msg(1), fin case 4 setpin msg(1), pin case 5 setpin msg(1), cin case 6 setpin msg(1), dout end select elseif msg(0) = 2 then ' command is 2 pin(msg(1)) = msg(2) 'set the I/O pins o/p ' print msg(2) else ' command is 3 (read pin) a = pin(msg(1)) s$ = str$((a) 'print msg(0), msg(1) print s$, len(s$) i2c slave write 2, s$ end if end sub sub tint local a$ = "PinS O/P" i2c slave write 8, a$ 'print "TX" end sub MASTER 'OPTION EXPLICIT 'OPTION DEFAULT NONE Option autorun on Dim As integer msg(2), count = 1 SetPin gp20, gp21, i2c I2C open 100, 1000 dim as integer addr = 69 'Address &H45 ssetpin 1, 6 ssetpin 2, 6 ssetpin 4, 6 ssetpin 5, 6 ssetpin 6, 6 ssetpin 7, 6 ssetpin 9, 6 ssetpin 10, 6 ssetpin 11, 6 ssetpin 12, 6 ssetpin 31, 1 do spin 1, 1 spin 2, 1 spin 4, 1 spin 5, 1 spin 6, 1 spin 7, 1 spin 9, 1 spin 10, 1 spin 11, 1 spin 12, 1 pause 300 spin 1, 0 spin 2, 0 spin 4, 0 spin 5, 0 spin 6, 0 spin 7, 0 spin 9, 0 spin 10, 0 spin 11, 0 spin 12, 0 pause 100 ' print spinR(31) ' If I uncomment this to read a pin, program locks up i2c read addr, 0, 8, a$ print a$ pause 200 loop sub ssetpin pinnbr as integer, cfg as integer 'CMD 1 = configure an I/O pin on the slave i2c write addr, 0, 3, 1, pinnbr, cfg if mm.i2c = 1 then error "Slave did not respond" 'print "setpin", pinnbr, cfg end sub sub spin pinnbr as integer, dat as integer 'CMD 2 = set output of pin i2c write addr, 0, 3, 2, pinnbr, dat if mm.i2c = 1 then error "Slave did not respond" 'print "output" end sub Function spinR(pinnbr) ' CND 3 = read value on pin local t$ i2c write addr, 0, 3, 3, pinnbr, 0 i2c read addr, 0, 3, t$ if mm.i2c = 1 then error "Slave did not respond" SpinR = val(t$) end function |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8566 |
You can't use a number with setpin - use DIN, DOUT etc. Please let me know where it says you can in the manual and I'll get it removed |
||||
TrevorH Senior Member Joined: 06/04/2018 Location: United KingdomPosts: 142 |
I was following the I2C recommendations in "Getting started with the Micromite" p73 where it suggests using setpin 15, 8 in the I2C section, since this is root of using I2C between processors info. I did ask if the Picomite firmware handled things differently. So it's NOT in the Picomite manual(I was just getting confused I think) Any ideas on the locking up when read a pin??? |
||||
robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2285 |
Micromite Manual version 5.05.05. pages 73-74: SETPIN pin, cfg [, option] [...] Previous versions of MMBasic used numbers for 'cfg' and the mode OOUT. For backwards compatibility they will still be recognised. [...] i feel this may have been discussed a year or two back, but can't remember the exact details. btw: i assume that "OOUT" should be "DOUT"? cheers, rob :-) |
||||
JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 3649 |
I think that's the Micromite, not Picomite. John |
||||
TrevorH Senior Member Joined: 06/04/2018 Location: United KingdomPosts: 142 |
Has anyone had a working 2 picomites with one as slave(I/O) on I2C able to read from slave's ADC ports? If so please let me know how. |
||||
Page 2 of 3 |
Print this page |