PDA

View Full Version : Interpret to Picbasic Code ¿?!!



Denner
- 4th June 2015, 12:24
Hello,

As I interpret this in Picbasic code:
It is for the IC MAX17040, Project GauGauge
Clearly are functions or subroutines.

The lines 111 and 123, I think are Subrutines.
The lines 117 and 118 , I think variable reg MSB,LSB
The lines 113 and 125, I think calls to chip
The lines 129 and 130, I think variable reg MSB,LSB

But the lines 114, 115 ,120 and 132 I don´t understand.

-------------------------------------------------------------



111 static void max17040_get_vcell(struct i2c_client *client)
112 {
113 struct max17040_chip *chip = i2c_get_clientdata(client);
114 u8 msb;
115 u8 lsb;
116
117 msb = max17040_read_reg(client, MAX17040_VCELL_MSB);
118 lsb = max17040_read_reg(client, MAX17040_VCELL_LSB);
119
120 chip->vcell = (msb << 4) + (lsb >> 4);
121 }
122
123 static void max17040_get_soc(struct i2c_client *client)
124 {
125 struct max17040_chip *chip = i2c_get_clientdata(client);
126 u8 msb;
127 u8 lsb;
128
129 msb = max17040_read_reg(client, MAX17040_SOC_MSB);
130 lsb = max17040_read_reg(client, MAX17040_SOC_LSB);
131
132 chip->soc = msb;
133 }
134

THX.

Dave
- 4th June 2015, 13:00
Well, I'm no C wisard but the lines 114,115 are defines for what looks like 2 variables called "msb" and "lsb". These are defined as unsigned 8 bit variables. Now with out any more code I can only quess that line 120 is taking the "msb" variable and doing a left shift 4 places and adding the "lsb" variable being right shifted by 4 places together then placing the result into "chip->vcell". Thats my take on it...

Ryan7777
- 4th June 2015, 13:39
It's been a while, but I'd agree about

u8 msb;
u8 lsb;

as being declarations of those variables in each function and what they will hold.

Those whole thing just looks like it's reading in I2C data, and maybe swapping the bit order?

Man, it's been so long since I had to work with something like that.

Art
- 9th June 2015, 19:00
chip->vcell = (msb << 4) + (lsb >> 4);


This is already basic on the right side of the equal sign,
on the other side it’s addressing an element of a structure (a list)
which can be done with any PBP array with a variable index like:


var word vcell
var byte/word chip[size]

...

chip[vcell] = value or sum


I don’t know why they are doing it, the four bytes that roll off each end would be discarded in both languages (not carried).

For both of these, this is a call to a function (subroutine in basic):


msb = max17040_read_reg(client, MAX17040_VCELL_MSB);

You have not posted the function, but I assume it’s the lower level I2C function (I2CREAD/WRITE...).

The parameters in brackets are arguments passed to the function as input which would be the address.
The return value is when is written into the value to the left of the equal sign for calling the function.
The basic subroutine would read a variable for the EEPROM address that was set by the main loop,
and set another variable with the result data, later read by the main loop that called the subroutine.

In PBP the I2C commands would probably be just the same written inline with the main program loop.
In the C code the functions might be an eyesore to throw at the bottom of your page.