はっくはっくキッチン
Hack Hack Kitchen

twi-controller.asm

 2021/12/20

TWI related subroutines (as a controller (master)). This code is a part of pAVRlib.

This program works only on AVRs which have a TWI module.

Subroutines

void TWI_INITIALIZE() (in: (none), out: (none))

initializes TWI Module. You must define F_CPU and F_SCL before you call this routine.

unsigned int TWI_SEND_S() (in: (none), out: r25)

sends a start bit. This returns r25:r24. A status code of TWI is stored in r25. r24 is unchanged. See reference manual from Atmel (Microchip) for more details.

void TWI_SEND_P() (in: (none), out: (none))

sends a stop bit.

unsigned int TWI_SEND(char data) (in: r24, out: r25:r24)

sends a byte data via TWI. This returns r25:r24. A status code of TWI is stored in r25. r24 is unchanged.

unsigned int TWI_SEND_SLA_R(char address) (in: r24, out: r25:r24)

address is a target (slave) address (7 bits). This returns r25:r24. A status code of TWI is stored in r25. r24 is unchanged.

unsigned int TWI_SEND_SLA_W(char address) (in: r24, out: r25:r24)

address is a target (slave) address (7 bits). This returns r25:r24. A status code of TWI is stored in r25. r24 is unchanged.

unsigned int TWI_RECV_ACK() (in: (none), out: r25:r24)

receives a byte and returns an ACK. The received data will be stored in r24 and a result code of TWI in r25.

unsigned int TWI_RECV_NACK() (in: (none), out: r25:r24)

receives a byte and returns a NACK. The received data will be stored in r24 and a result code of TWI in r25.

Example

examples/twi-controller-test.asm

This program scans I²C devices and shows if a device on each address exists.

To compile this file, the following files are required (These files are parts of the pAVRlib).

See also: examples/README.org