Manipulating the registers

Go To Last Post
27 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello guys!

 

This seems to be a very useful site/community for AVR, at least for beginners like my self!

 

Ok, I have a sensor with different registers that I would like to use. I am programming the tiny45. The main idea is to communicate with the sensor via the i2c. Usually, I am using a library for Arduino, however, this time I do not have a library, but would like to use the sensor anyway, in addition, I need to go beyond the available/unavailable libraries. 

 

So, how should I initialise the sensor? I am working in Arduino IDE and I can programming the tiny nicely, it works :) However, I am not sure how to go about the products data sheet, namely how to enter the register so that the communication via i2c will work.

 

Any comments, suggestions are more than welcome!

 

Best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Most folks would use an existing library. The common one to choose is by Peter Fleury:

 

http://homepage.hispeed.ch/peter...

 

The "master I2C" library there has two implementations with exactly the same facilities. The .c one is for AVRs that have real I2C hardware (that Atmel call "TWI" to not contravene trademarks). The Asm (.S) one is for more limited AVR that don't have real I2C. I think the Tiny45 falls into the latter category.

 

(BTW that does rather raise the question of why, if a project involves I2C/TWI would you pick an IC that does not actually have it?)

 

EDIT: yup the t45 only has "USI" not real TWI. For your info Atmel also have these app notes:

 

AVR310: Using the USI module as a TWI Master - Microchip Technology

AVR312: Using the USI Module as a I2C Slave - Microchip Technology 

 

(interesting they use "TWI" in the name of one of those and "I2C" in the name of the other - nothing like consistency and that is nothing like consistency!)

Last Edited: Wed. Nov 21, 2018 - 01:07 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In the Arduino environment, there is an I2C library for the tinyx5 called tinyWireM, you will need to find that and then you can use normal i2c methods as is done with other Arduinos.

For examples of i2c usage, google "Arduino i2c" if you also add your sensor, in most cases someone has already done it and will have example code sketch.

What sensor are you wanting to use, part number?

 

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Wed. Nov 21, 2018 - 01:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well. You’ve kind of asked “how do I fly a plane”. Without knowing the sensor or what the registers do on it there is no answer beyond “you read the register, modify the bits in it that need modified, and you write the register back to the sensor”.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you all!

 

What photomankc is saying is more inline with what I probably need. 

For example, I am looking at the data sheet. So far I have found the address of the device, while I am not sure how to find the COMMAND_REG address (Register containing commands)... of which I am attaching the picture, if it helps?

 

Or maybe, it would be easier, if I should access the registers directly?

 

Best.

 

Bravo!!!

Last Edited: Wed. Nov 21, 2018 - 04:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you post the actual sensor part number or name perhaps we can help, without that... your guess is as good as *better* than mine.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mu234 wrote:
Or maybe, it would be easier, if I should access the registers directly?
You can't just guess. Complex electronics come with datasheets that explain how to interact with them - you must do what the datasheet tells you to do.

f you post the actual sensor part number or name perhaps we can help, without that... your guess is as good as *better* than mine.

+1000 

 

What hope does anyone here have of helping you when you haven't even given a model number?

Last Edited: Wed. Nov 21, 2018 - 04:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Of course, please look at here https://www.vishay.com/docs/8425...

 

Best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Look at the page just above that table. It describes the sequence to write and a glance seems to suggest that it has just the one register which you write to when you send command codes. No register address appears to be sent.

To write you send: I2CADDR | CMD_CODE | LOWBYTE | HIGHBYTE 

Last Edited: Wed. Nov 21, 2018 - 06:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks I though so, hence my question, if the registers should be accessed directly. 

 

Anyway, I am not really sure how to do that, any ideas, suggestions, examples?

 

e.g. how can manipulate the PS_CONF1 ?

 

Best.

 

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To me, it looks like there ARE multiple registers and the Command Code IS the register address. 

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I assume you are aware of: https://www.vishay.com/docs/8441... ? that has further info about the operation of this (very complex!) looking device. I wonder if this really is the right device to start out with if you are only just learning about I2C devices and the usual kind of cmd/data interaction they often implement?

 

I hit the above PDF as I was actually looking for some example C code. The fact is that for just about any peripheral devices someone, somewhere is almost bound to have programmed it already and half of them will have published the results of their labours on the internet. It doesn't really have to be AVR even (or C for that matter) because once you have got beyond the lowest level i2c_send() and i2c_receive() then the actual order of bytes sent and bytes received is likely to be the same algo whatever the architecture/language.

 

Having said that - because of the pervasiveness of Arduino I was kind of expecting to hit some Arduino code for this thing.

 

EDIT: is this device a fairly recent release I wonder? It does seem kind of odd that I can't seem to find ANY example code for it on the net? I wonder if there's actually a "family" of devices from Vishay and that there might be library code for an alternative device that preceded this?

 

EDIT2: sadly no - it seems from looking around https://www.vishay.com/optical-s... that there is a "family" of just this one device so far.

Last Edited: Wed. Nov 21, 2018 - 05:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you both!  Yeah I am reading the application note, what you have posted, and to be honest, I do have some knowledge, however, I went through some code elsewhere, and I am wondering, if this could be used for i2c, I guess it could be, see below (the VCNL4035 is my edit so far)

With regard to the code, maybe we could be the one publishing that on the net :)

 

/*******************************************************************************************************************
** Method readByte reads 1 byte from the specified address                                                        **
*******************************************************************************************************************/
uint8_t VCNL4035::readByte(const uint8_t addr) {                              //                                  //
  TinyWireM.beginTransmission(VCNL4035_ADDRESS);                                   // Address the I2C device           //
  TinyWireM.write(addr);                                                           // Send the register address to read//
  _TransmissionStatus = TinyWireM.endTransmission();                               // Close transmission               //
  delayMicroseconds(VCNL4035_I2C_DELAY);                                      // delay required for sync          //
  TinyWireM.requestFrom(VCNL4035_ADDRESS, (uint8_t)1);                             // Request 1 byte of data           //
  while(!TinyWireM.available());                                                   // Wait until the byte is ready     //
  return TinyWireM.read();                                                         // read it and return it            //
} // of method readByte()                                                     //                                  //
/*******************************************************************************************************************
** Method readWord reads 2 bytes from the specified address                                                       **
*******************************************************************************************************************/
uint16_t VCNL4035::readWord(const uint8_t addr) {                             //                                  //
  uint16_t returnData;                                                        // Store return value               //
  TinyWireM.beginTransmission(VCNL4035_ADDRESS);                                   // Address the I2C device           //
  TinyWireM.write(addr);                                                           // Send the register address to read//
  _TransmissionStatus = TinyWireM.endTransmission();                               // Close transmission               //
  delayMicroseconds(VCNL4035_I2C_DELAY);                                      // delay required for sync          //
  TinyWireM.requestFrom(VCNL4035_ADDRESS, (uint8_t)2);                             // Request 2 consecutive bytes      //
  while(!TinyWireM.available());                                                   // Wait until the byte is ready     //
  returnData  = TinyWireM.read();                                                  // Read the msb                     //
  returnData  = returnData<<8;                                                // shift the data over              //
  returnData |= TinyWireM.read();                                                  // Read the lsb                     //
  return returnData;                                                          // read it and return it            //
} // of method readWord()                                                     //                                  //
/*******************************************************************************************************************
** Method writeByte write 1 byte to the specified address                                                         **
*******************************************************************************************************************/
void VCNL4035::writeByte(const uint8_t addr, const uint8_t data) {            //                                  //
  TinyWireM.beginTransmission(VCNL4035_ADDRESS);                                   // Address the I2C device           //
  TinyWireM.write(addr);                                                           // Send the register address to read//
  TinyWireM.write(data);                                                           // Send the register address to read//
  _TransmissionStatus = TinyWireM.endTransmission();                               // Close transmission               //
} // of method writeByte()                                                    //                                  //

 

Jim, I think you are right, and that the reg. should be accused directly...

 

Best.

Bravo!!!

Last Edited: Wed. Nov 21, 2018 - 05:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mu234 wrote:
maybe we could be the one publishing that on the net
You're clearly not an entrepreneur! If a google search suggests there is no existing code on the internet then when you have developed some you SELL it !! cheekylaugh

 

(OTOH perhaps the absence of code is simply because the market size for it is very small? However I suspect it's more likely that this is a fairly recent device - so you may be in at the base of the market for supporting code if it takes off - you might even be able to sell good code back to Vishay!)

 

Datasheet does say "April 2018" - not sure if that's the date it was released or just an update?

Last Edited: Wed. Nov 21, 2018 - 05:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, yeah, it looks like it's "new" ... I am here trying pickup brain about programming such devices and learn :) both, data sheet and application note are revisions. 

 

Best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
To me, it looks like there ARE multiple registers and the Command Code IS the register address.    Jim

 

EDIT: Reading on down to the the register tables, I agree.  They are being confusing with the nomenclature, but the command code is the register address of the various command registers.

 

So in general to modify a particular bit field in the register you will need to:

 

Read the register (command code) using repeated start and pull back the low and high bytes.

Clear the bit field within the results that you will modify leaving the rest of the bits alone.

Set any bits in the field you need to set also leaving the other bits alone.

Write the register (command code) with the altered value you created above, sending the low and high bytes in the proper order.

 

 

Last Edited: Wed. Nov 21, 2018 - 06:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Great, thank you! Would you make an example? Above you can use the i2c code?

Best.

Bravo!!!

Last Edited: Thu. Nov 22, 2018 - 07:04 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello!

 

How far is to "translate" the library that works on Arduino, namely ATMega (ATmega328P), to AT Tiny x5 Family?

 

Best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The basics of i2c are the same regardless of what microprocessor you're using. The function names might differ, but the basics are the same. If you have some Arduino code - look at how it works rather than concentrating on the nuts and bolts of the code. There'll be the i2c function (the wire library) and then functions to make accessing the device easier - there might be functions to read and write the 16 bit register pairs in your device and there might be a function that initialises the device - look at what it is doing - what registers, what values and in what sequence. Once you understand this, then writing your own code to do much the same thing becomes easy.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cheers! Thank you for the idea, would you like to give it a try? 

 

Best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mu234 wrote:

Cheers! Thank you for the idea, would you like to give it a try? 

 

Best.

 

Why should I have all the fun? I'm unlikely to use a Tiny45

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Because, I am not really sure how to do it. Anyway, would you give me a hint where and how to start? It could as well be 85, I guess it will have to be due to memory restraints.

 

Best.

Bravo!!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You might want to try contacting Vishay and see if there is some demo code to accompany their app notes. The fact is that this device looks VERY complex and I'm guessing it might be close to impossible to determine how to use it without seeing the use case the manufacturer really intended. In fact internally, for their own design verification and also for factory testing I'm guessing they probably have some existing code that makes the device do what their silicon designer intended.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This device does seem to be bleeding edge. There's a PC program for evaluating it, so you could use a cheapy logic analyser so sniff the comms and figure out how to talk to it. The I'd grab an Arduino (or cheapy equiv) or RasPI and start writing code to talk to it. Once you've got that happening, then port across to the Tiny.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The Original Poster refers, several times to "writing directly to registers". There is perhaps a mis-understanding, here. That is the ONLY thing you can do. And, that is true for MOST I2C and SPI devices.

 

Depending on the OP's definition of "direct", the only exception I have found is in devices that auto-increment the register pointer. This seems mostly useful for reading a bank of data registers (as in a multi-axis accelerometer). 

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well finding some example code looked like an easy challenge; It turned out to be a hard one.

 

Cliff found the device was a recently born member of a family so looking for older chips instead; i found this driver:

 

https://github.com/torvalds/linux/blob/master/drivers/iio/light/vcnl4000.c

 

OK It's a tongue-in-cheek suggestion but it could be of interest just to see how someone else's implementation.

 

[VCNL4200] = {
    .prod = "VCNL4200",
    .init = vcnl4200_init,
    .measure_light = vcnl4200_measure_light,
    .measure_proximity = vcnl4200_measure_proximity,
}

Those exposed functions seem to be the ones worthy of study.

 

PS:

If you speak a bit of Spanish: https://www.openhacks.com/page/productos/id/1579/title/VCNL4010-Proximity-Light-sensor

There is mention of an Arduino Sketch.

 

PPS:

None of the above obviates you from reading the data-sheet in great detail.

Last Edited: Sat. Nov 24, 2018 - 04:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:

The Original Poster refers, several times to "writing directly to registers". There is perhaps a mis-understanding, here. That is the ONLY thing you can do. And, that is true for MOST I2C and SPI devices.

 

Depending on the OP's definition of "direct", the only exception I have found is in devices that auto-increment the register pointer. This seems mostly useful for reading a bank of data registers (as in a multi-axis accelerometer). 

 

Jim

 

I've seen and implemented some are effectively an MCU and they may just take commands and data a bit like you would see for a UART interface without any notion of a real 'register' behind it.  At first I thought that is what I was seeing until I read further on.