F1 Evaluation Platform User’s Guide
DS41401B-page 22 2010 Microchip Technology Inc.
5.6 USING THE I
2
C™ DRIVER
The supplied software includes an I
2
C Master mode driver with the following features:
1. Interrupt driven or polled
2. Queued requests
3. Minimum memory requirements
4. Uses Restart to maximize bus bandwidth
5. Multiple atomic I
2
C transactions allowed at each queue entry
6. Completion flag for each queued block of I
2
C commands.
5.6.1 I
2
C Data Structures
Two data structures are defined to simplify interactions with the I
2
C driver.
5.6.1.1 I2C_RESULTS_T
The I2C_RESULTS_T is an enumerated type with the following enumerations:
The data completion flag for an I
2
C request is of type I2C_RESULTS_T. This flag will
be I2C_REQUEST_PENDING until the request is complete. If the request completed
without errors, the value will be I2C_REQUEST_COMPLETE. If there were errors, the
value will reflect one of the other 4 error conditions.
5.6.1.2 I2C_T
The I2C_T is a structure representing one transaction on the I
2
C bus. A transaction
contains the following elements:
1. A 7- or 10-bit address. Address type is determined automatically by the driver.
R/W mode is implicit in the address and specified by bit 0.
2. An 8-bit baud rate. This is the value placed in SPADD. Predefined constants are
included that compute the SPADD value given an FOSC value. Set FOSC with
a #define before #including the I2C.h file.
3. A buffer_length. This is the number of bytes available for reading or writing
at the supplied data buffer.
4. A Buffer Pointer. Your application will allocate a block of memory for an I
2
C
transaction. Pass a pointer to this block via the Buffer Pointer. The block must
remain valid until the transaction is complete (i.e., if you allocate the block as a
non-static array within a function, you must not leave the function until the I
2
C
transaction is complete or the array may be reused by a different function).
TABLE 5-1: I2C_RESULTS_T
I2C_REQUEST_PENDING Request is in the queue or in process but it is not
done.
I2C_REQUEST_COMPLETE The request is finished successfully.
I2C_REQUEST_STUCK_START The request was aborted. The start condition did
not clear correctly.
I2C_REQUEST_ADDRESS_NO_ACK The request was aborted. No ACK occurred on
the address, so the target device must not exist
or is not functioning.
I2C_REQUEST_DATA_NO_ACK The request was aborted. Not all of the data
could be sent because the device did not ACK a
byte.
I2C_REQUEST_LOST_STATE The I
2
C state machine had a RAM corruption
and the default case caught the failure. This will
infinite loop so you should never see it.
Hardware Libraries and Drivers
2010 Microchip Technology Inc. DS41401B-page 23
5.6.2 I
2
C Function Interface
5.6.2.1 i2c_init
The function i2c_init configures the I
2
C driver and makes it ready to receive
transaction in the transaction queue.
FIGURE 5-1:
5.6.2.2 i2c_insert
The function i2c_insert places one complete set of Transaction Request Block
(TRB’s). TRBs can be bundled into an array of multiple TRBs and inserted as one large
request. The driver will ensure that all the TRBs in the list will be processed sequentially
with I
2
C restarts separating them.
FIGURE 5-2:
5.6.2.3 i2c_build_write_trb / i2c_build_read_trb
The i2c_build_write_trb and i2c_build_read_trb functions are helpers to
assemble the I2C_TRANSACTION_REQUEST_BLOCKS from passed parameters.
FIGURE 5-3:
5.6.2.4 i2c_write_block / i2c_read_block
The i2c_write_block and i2c_read_block functions are helpers that assemble
a TRB, insert it into the queue and wait for the transaction to complete.
initialize()
{
i2c_init(I2C_400K);
}
Send_data()
{
I2C_TRANSACTION_REQUEST_BLOCK trb_list[5];
I2C_RESULTS flag;
… populate the trb’s …
i2c_insert(5,trb_list, &flag);
}
Send_one_byte()
{
I2C_TRANSACTION_REQUEST_BLOCK trb_list;
unsigned char data;
I2C_RESULTS flag;
data_block = .. my data byte ..
i2c_build_write_trb(&trb_list,ADDRESS, 1, &data, i2c_200K);
i2c_insert(1,&trb,&flag);
}
F1 Evaluation Platform User’s Guide
DS41401B-page 24 2010 Microchip Technology Inc.
FIGURE 5-4:
5.6.2.5 i2c_handler
The i2c_handler function must be called for each I
2
C state to be handled. This
function can be placed into the Interrupt Service Routine for your application or, you
can simply poll it often. If the i2c_write_block or i2c_read_block functions are
used when the driver is configured for polling, then this function will repeatedly be
called until the transaction completes.
FIGURE 5-5:
5.7 USING THE LCD DRIVER
The supplied LCD driver configures the LCD module for the I/O pins used by the F1
Evaluation Platform. Icon definitions are provided to simplify the software. A BCD to
7-segment display mapping function is also provided to further simplify control of the
LCD glass. For more information concerning the LCD peripheral, please consult the
LCD chapter in the PIC16LF1937 data sheet (DS41364).
5.7.1 LCD Data Structures
5.7.1.1 BCD_TYPE
To simplify BCD to the 7-segment display mapping, a BCD segment data type has been
provided. This data type is simply four 4-bit fields representing digits 0, 1, 2 and 3.
FIGURE 5-6:
Simply place the number you want displayed in each digit position and call
lcd_display_digits. Digit 3 is the Most Significant ½ digit of the 3.5-digit LCD
glass. Therefore, the largest value displayable is 1FFF assuming hexidecimal digits or
1999 for decimal digits.
Send_one_byte()
{
unsigned char data;
i2c_write_block(ADDRESS, 1, &data, i2c_200K);
}
Interrupt_service_routine()
{
i2c_handler();
}
typedef union
{
UINT16 val;
struct
{
unsigned digit0 : 4;
unsigned digit1 : 4;
unsigned digit2 : 4;
unsigned digit3 : 4;
};
} BCD_TYPE;

DM164130-1

Mfr. #:
Manufacturer:
Description:
F1 EVAL PLATFORM PIC16LF1937
Lifecycle:
New from this manufacturer.
Delivery:
DHL FedEx Ups TNT EMS
Payment:
T/T Paypal Visa MoneyGram Western Union

Products related to this Datasheet