Home

Notes on I2C on Atmega328p

February 7, 2015

Registers:

TWBR = bit rate

TWCR =
  TWINT interrupt flag
  TWEA enable acknowledge
  TWSTA set start
  TWSTO set stop
  TWWC collision detected
  TWEN twi enable
  – reserved
  TWIE interrupt enable

TWSR =
  TWS(7:3) status
  – reserved
  TWPS (1:0) prescaler
 

TWDR = data register

SCL freq = CPU clock / (16 + 2 * TWBR * 4TWPS).

TWBR must be at least 10 for master mode.

SCL freq must be 100Khz or less.
CPU clock is 8 Mhz.
Denominator must be larger than 80 = 16 + 64, TWBR * 4TWPS must be greater than or equal to 32.
TWBR=10-31, TWPS=1 (40-124)
TWBR=32-255, TWPS=0 (32-255)
At 100kHz, send data at about 10kB/s. 80 characters requires 8ms.

Master-side state changes for writing a data byte to a slave:

TWCR = _BV(TWSTA)|_BV(TWINT)|_BV(TWEN)|_BV(TWIE)
Receive interrupt, check TWSR for successful start.

TWDR = SLA+W (W=0, R=1: SLA = 0x50 for Newhaven serial display
TWCR = _BV(TWINT)|_BV(TWEN)|_BV(TWIE)
Receive interrupt, check TWSR for successful address.

TWDR = data
TWCR = _BV(TWINT)|_BV(TWEN)|_BV(TWIE)
Receive interrupt, check TWSR for successful data.

repeat data.

TWCR = _BV(TWSTO)|_BV(TWINT)|_BV(TWEN)|_BV(TWIE)
Receive interrupt, check TWSR for successful stop.Data to display on the bicycle:

Min/max/average spin interval in microseconds.
Min/max/average voltage.
Min/max/average current.

Settings to modify on the bicycle:

Low current (doubled voltage), 200mA.
High current, 400mA.
Doubler-disable voltage, 24.
Doubler-enable voltage, 12.
Shunt-enable voltage, 30.
Shunt-disable voltage, 29.
Stand-light on time, 90 seconds.
Restart voltage, 13 volts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: