Thursday, November 15, 2012

I2C (Inter Integrated Circuit Bus) : Serial Communication

I2C quick tutorial

I2C (sometimes 2 is in superscript) and read as "eye-two-see" or "eye-square-see" is a serial communication  protocol introduced by Philips semiconductors in early 80s.

I2C is a 2-wire Interface (TWI) with mostly a single master and multiple slaves , there are multiple master configurations possible too.

I2C signals

  1. Data :  SDA  ===========> Always bi-directional
  2. Clock:  SCL ===========> Bi-directional only in Multi-Master Mode

Ground : GND ==========> Common Ground between the two Devices
5V supply

By convention , I2C operates on 5V signals. So make sure you put a level translation buffer in between  if your device is 3.3V tolerant.

Maximum allowable capacitance on the lines is 400 pF and typical device capacitance is 10 pF. So how many devices are supported on single I2C Bus ?

The communication is always Master initiated, Master (typically a Microcontroller) places the address of Slave device with which Master intends to communicate on the Bus. All Slave devices monitor the SDA pin to determine if Master is sending their address. Only the Slave with Matched address can communicate with Master.

The advantages of I2C other than it being a very low cost to implement are
  • Supports multiple devices on only 3 wires
  • Is implemented in Hardware as well as Software (Bit-Banged I2C)
  • Supports Multi Master configuration
  • Many examples available on the Internet
If I had to belittle I2C I would say , The disadvantages of I2C are
  • Short Distances only
  • Slow Clock speeds (Typically 100Khz)
  • Faster Clocked devices cannot coexist with Slower devices on the same bus

I2C Typical Connections
Both SCL and SDA are Open-Drain drivers, which means that the device can drive it low, but cannot drive it High. To drive the line High, you must provide a pull up through a resistor as seen in the above figure. You only need one pull up for SDA and one for SCL irrespective of how many devices are connected to the bus.

Data Transfer Protocol over I2C

When I2C master wishes to communicate with slave devices, it stars by generating a "Start Condition" on the SDA line. "Start Condition" is defined as SDA line going low while SCL is high. Similarly when Master devices wishes to end the transfer, it generates a "Stop Condition" on the line. Stop Condition is defined as SDA going High when SCL is High.

The receiving device ACKnowledges the transfer after every 8 bits (MSB first), by driving SDA Low for Positive ACK and driving SDA High. This means that there are 9 clock cycles per 8-bits of data. 8-bit data and 1-bit for ACK. If the receiver is not ready to receive any more data, it should keep SDA high for 9th bit so the transmitter aborts the transfer.

Data can change while the SCL is low, but it should not change with SCL is high.

Device Addressing

Device Addressing is another section of I2C that confuses new comers. Addresses on I2C bus are 7-bit, but as explained before, the transfers are all 8-bit + 1-bit for ACK, then where did this 7-bit address come from ? Here is how.

In the images above, it is a little bit more clear as in how the addressing is done in I2C. 7-Bit slave address (128 slave devices addressable ) , 8th bit for is it going to be a Read (1) or a Write (0) command, and 9th bit for ACK from the receiver. Always remember, Even address (8th bit zero)  for WRITE and Odd address (8th bit one) for READ.

Example 1
Here is an example where Master device is writing to a Slave. It starts by addressing a Slave device by its 7-bit Address and putting 8th-bit Low indicating a Write Command.

The "Green" bounded parts are Bits transfered by Master, and everything else is transfered by Slave.

Example 2
Master wants to read from Slave

But it is rarely this simple. Devices have multiple internal registers to which you want to write to or read from, so how do you do that ?

Writing to an internal register of an I2C slave

  1. Start Condition (Put automatically if Hardware I2C is used)

  2. Send I2C address of the slave with R/W bit low (even address) for write.
  3. Send the address of the internal register
  4. Send data byte
  5. Send more data bytes if needed
  6. Send Stop Condition (Automatic if Hardware I2C is being used) 

Reading from an Internal Register of an I2C Slave device

Reading from an I2C slave device having internal registers is a wee bit tricky but not tricky enough to get you boggled up. Read from the slave device starts with a write operation in which we write the internal register address to the slave device which we want to read from.

  1. Start condition
  2. Send I2C address of the slave with R/W bit Low (even Address) for write.
  3. Send the address of the internal register
  4. Send "Start Condition" again, referred to as (Restart) in this scenario
  5. Send the I2C address of the slave device with R/W bit High (Odd address) for read operation.
  6. Read data byte
  7. Read more data byes if needed
  8. Send a stop condition.

I hope this helped :) leave your questions in comments.

Monday, November 12, 2012

How to install Google Analytics in Wordpress

How to Install Google Analytics in WordPress ?

This was my question a few minutes ago, and here is the most simple answer :)
Assuming you have Google Analytics set up. If you haven't then start by signing up for Google Analytics account , its free and simple.

1 - Copy the code that you received in from Google Analytics, and paste it in your theme’s header.php right above the < / head > tag . [Preferred way] 
2 - Copy the code that you received in from Google Analytics, and paste it in your theme’s footer.php right above the < / body > tag 
Theme's header.php and footer.php can be found by going into your Wordpress dashboard > Appearance > Editor
Now on your right you will see a sidebar listing all your pages. Locate the header.php or footer.php whichever you want.

If its still not working, leave your questions in the comments :)




The image is not at all out of context here ... I happened to meet a relative by a relation I never knew of before that day, and I learned so much from him. He gave me insight into how things work around us... how we are surrounded by conspiracies ... hidden meanings behind all the stuff that we ignore just like that.

It was such a knowledgeable meet that I thought I'd share the enlightenment with others too... *sharing is caring* and after all its not your fault that his highness isn't your relative .

*Spice* has been added ofcourse 

Enlightenment clauses:
  1. Aj kal telecom engineers tou kaddu ki tara pare hain market mein, na koi hath lagata hai na koi poochta hai... bas paray paray sar jatay hain aur phir export ho jate hain.
  2. Mein ne apne bare baitay ko *Multi National * company mein naukri karwani hai, jaise yeh jo Telenor wagera . 
  3. Aj kal internet per paise kamana bohat sokha (easy) ho gaya hai ... pehlay zamanay mein tou mehnat ki kamai hoti thi.
  4. Cheeny salay (Chinese Rascals) bas sasta kaam hi kartay hain hamesha.
  5. Jiss ishtehaar (advertisement) mein bachi na nachay us ishtehaar se kuch nahin bikta.
  6. Tarang ka ishtehaar tou *my favorite* hai.
  7. Shadi 27 saal (27 years) k bad hi karni chahie mard ko.
More enlightenment when he came to know I am a Telecom Graduate , working as a firmware developer in a Private firm.

  1. Dekho na apna haal, Telecom degree ki , kia faida hua ? ab kisi *fum-wea* name ki private company mein kam kar rahay ho. Chinese name lag ra hai *fum-wea*.
  2. Yeh Honda , shonda sab bakwas hai ... mein ne Raavi khareedi hai CD70 , aise bhagti hai jaise khoti k dum per aag ho.
  3. KFC tou best hai.

Noticing I was holding an Android phone 

  1. Goray bhi kia bala (Monster) hain, salay humain in shashkon mein phansa ker khud dunya hila chori hai inhon ne.
  2. Yeh sab Muslims k khilaf sazish ka aik hissa hai mere dost. Yeh camera wala mobile Americans chahain tou kabhi bhi tumharay mobile camera ko istemal kar k dekh sakte hain k tum kahan ho.
  3. Waise yeh tumharay phone (Android) aur iPhone mein farak kia hai ? hai tou iPhone bhi touchscreen... tou wohi le lete.
Last but not least,

  1. Ap ka facebook id kia hai ?
Feel free to translate any of these clauses to your native language... 

Tuesday, November 6, 2012

Income Tax 2012-2013 Slab rates for Pakistan

So I needed these slab rates today to calculate my share of contribution (Income Tax) to our beloved Mr President's doggie bank. I thought it may be useful for others too.
Here are Income Tax rates (Slab rates) for Pakistan for the year 2012 - 2013. All you need now is a calculator.
Tax slabs of salary income has been reduced from 17 to 6 in the finance act 2012-13 which are as under:
  1. Salary Income less than 400000 tax will be 0%
  2. Salary Income exceeds 400000 but does not exceed 750000  tax will be 5% of amount exceeding 400000
  3. Salary Income exceeds 750000 but does not exceed 1500000 tax will be 17500 + 10 % of amount exceeding 750000
  4. Salary Income exceeds 1500000 but does not exceed 2000000  tax will be 95000+ 15% of  amount exceeding 1500000
  5. Salary Income exceeds 2000000 but does not exceed 2500000 tax will be 175000 + 17.5% of amount exceeding 2000000
  6. Salary Income exceeds 2500000 tax will be 420000 + 20% of amount exceeding 2500000.

Good luck to anyone in " 10% "  :)  *pwn intended*