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 : i2c
Page 2 of 2 | |||||
Author | Message | ||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6808 |
Probably because only one value was expected and that was going to be printed. As an alternative you can use a numeric array as rcvbuf. See the I2C READ instruction in the manual. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2140 |
"As an alternative you can use a numeric array as rcvbuf" And here is one way to do that, added to Peters original. 'VL53LOX laser distance measurement. Tested ok.Const I2Caddr=&H29 'VL53LOX device address Dim d$ Dim integer dst(1) 'I2C open 100,1000 'Comment out for System I2C I2C write I2Caddr,0,2,&H89,&H1 Font 5 Pause 200 CLS Print "Integer", "String" Do get.dist.str Pause 200 get.dist.int Print dist.i%, dist.s% Pause 200 Loop Sub get.dist.str 'Get distance using a string I2C write I2Caddr,0,2,0,1 I2C write I2Caddr,1,1,&H1E I2C read I2Caddr,0,2,d$ 'read 2 character string dist.s% = Str2bin(uint16,d$,BIG) 'convert string to integer Text MM.HRes/2,MM.VRes/3," "+Str$(dist.s%)+"mm ",cm End Sub Sub get.dist.int 'Get distance using integers I2C write I2Caddr,0,2,0,1 I2C write I2Caddr,1,1,&H1E I2C read I2Caddr,0,2,dst() 'read 2 bytes dist.i% = (dst(0)<<8) + dst(1) 'combine 2 bytes Text MM.HRes/2,MM.VRes*2/3," "+Str$(dist.i%)+"mm ",cm End Sub Edited 2023-05-03 23:09 by phil99 |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2130 |
nice phil99 but errors on glcd |
||||
phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2140 |
Down loaded the code from my previous post into a PicoMiteVGA and it runs without problem. TeraTerm > RUN Integer String 1256 0 1258 1252 1254 1257 1258 1257 1257 1259 335 361 337 326 337 344 340 327 312 352 1256 1259 1258 1254 1252 1254 Also on VGA screen. Also tested ok standard PicoMite with ILI9488. . Edited 2023-05-04 17:52 by phil99 |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2130 |
@phil99 - thanks but still errors when pasting into tera term. I pasted into mmedit previously. Const I2Caddr=&H29 'VL53LOX device address Dim d$ Dim integer dst(1) 'I2C open 100,1000 'Comment out for System I2C I2C write I2Caddr,0,2,&H89,&H1 Font 5 Pause 200 CLS Print "Integer", "String" Do get.dist.str Pause 200 get.dist.int Print dist.i%, dist.s% Pause 200 Loop Sub get.dist.str 'Get distance using a string I2C write I2Caddr,0,2,0,1 I2C write I2Caddr,1,1,&H1E I2C read I2Caddr,0,2,d$ 'read 2 character string dist.s% = Str2bin(uint16,d$,BIG) 'convert string to integer Text MM.HRes/2,MM.VRes/3," "+Str$(dist.s%)+"mm ",cm End Sub Sub get.dist.int 'Get distance using integers I2C write I2Caddr,0,2,0,1 I2C write I2Caddr,1,1,&H1E I2C read I2Caddr,0,2,dst() 'read 2 bytes dist.i% = (dst(0)<<8) + dst(1) 'combine 2 bytes Text MM.HRes/2,MM.VRes*2/3," "+Str$(dist.i%)+"mm ",cm End Sub 'teraterm errors ----------------------------! > Const I2Caddr=&H29 'VL53LOX device address Error : I2CADDR Global variable already declared > Dim d$ Error : d$ already declared > Dim integer dst(1) > 'I2C open 100,1000 'Comment out for System I2C > I2C write I2Caddr,0,2,&H89,&H1 > Font 5 > Pause 200 > CLS > use 200 Error : Unknown command > Loop Error : LOOP without a matching DO > > Sub get.dist.str 'Get distance using a string Error : No matching END declaration > I2C write I2Caddr,0,2,0,1 > I2C write I2Caddr,1,1,&H1E > I2C read I2Caddr,0,2,d$ 'read 2 character string > dist.s% = Str2bin(uint16,d$,BIG) 'convert string to integer Error : DIST.S is not declared > Text MM.HRes/2,MM.VRes/3," "+Str$(dist.s%)+"mm ",cm Error : DIST.S is not declared > End Sub Error : Nothing to return to > > Sub get.dist.int 'Get distance using integers Error : No matching END declaration > I2C write I2Caddr,0,2,0,1 > I2C write I2Caddr,1,1,&H1E > I2C read I2Caddr,0,2,dst() 'read 2 bytes > dist.i% = (dst(0)<<8) + dst(1) 'combine 2 bytes Error : DIST.I is not declared > Text MM.HRes/2,MM.VRes*2/3," "+Str$(dist.i%)+"mm ",cm Error : DIST.I is not declared > End Sub |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2130 |
This works for distance% to be used as a var. option autorun on OPTION EXPLICIT OPTION DEFAULT NONE Dim d$,dist% Const i2caddr=&H29 I2C write i2caddr,0,2,&H89,&H1 Font 2 Pause 200 CLS Do getdistance Pause 100 Loop ' Sub getdistance I2C write i2caddr,0,2,0,1 I2C write i2caddr,1,1,&H1E I2C read i2caddr,0,2,d$ 'text 0,0,(Str$(Str2bin(uint16,d$,BIG),3,0))+" mm " dist%=(Str2bin(uint16,d$,BIG)) text 0,20,(str$(dist%))+" mm " End Sub |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 9133 |
Perhaps AUTOSAVE might have helped |
||||
stanleyella Guru Joined: 25/06/2022 Location: United KingdomPosts: 2130 |
phil99 - your idea works, thanks for help. I'm going to redo my gcbasic robot using mmbasic. Dim dist%,dst%(1) Const i2caddr=&H29 I2C write i2caddr,0,2,&H89,&H1 Font 2 Pause 200 CLS Do getdistance Pause 100 Loop ' Sub getdistance I2C write i2caddr,0,2,0,1 I2C write i2caddr,1,1,&H1E I2C read i2caddr,0,2,dst%() 'dist% = (dst%(0)<<8) + dst%(1) 'combine 2 bytes dist% = (dst%(0)*256) + dst%(1) 'combine 2 bytes text 0,20,(str$(dist%))+" mm " End Sub |
||||
Page 2 of 2 |
Print this page |