405

Downloads

405

Downloads of v 1.0.0

2019-05-09

Last published

PIC12 I2C Master & Slave PWM Demo

1.0.0

This demo uses two PIC12LF1822 devices, one as an I2C Master and the other as an I2C Slave. The two devices are connected directly with each other via their respective SDA and SCL pins (no external pull-up resistors are required). The I2C Master writes values into the Slave's I2C buffer which will change the Slave device's PWM output characteristics dynamically. This code can also be easily ported to similar devices such as the PIC12(L)F1840 & PIC16(L)F1823.

PIC12LF1822 I2C Master & Slave Demo

This demo uses two PIC12LF1822 devices, one as an I2C Master and the other as an I2C Slave.

Connect the two devices with direct jumper connections between their respective SDA and SCL pins (no external pull-up resistors are required).

The I2C Master writes values into the I2C Slave I2C buffer which will change the PWM output characteristics dynamically.

The I2C Slave can be put into a sleep mode by the I2C Master writing a non-zero value into the I2C Slave buffer location 0, and can wake up

by the I2C Master writing a 0 into the slave's buffer location 0. This is how the I2C Master can turn off the I2C Slave's PWM output and

keep the Slave in sleep mode to conserve power.

The I2C Master can also change the PWM carrier frequency by updating the Slave's I2C buffer location 1, and change the PWM duty cycle by updating

the Slave's I2C buffer location 2.

Currently the I2C Slave buffer only has 3 locations that are used, but of course the additional locations can be used and the total buffer size

can be increased as well.

Both I2C Master and Slave devices have PWM output on RA5 and A/D input on RA4. No external pull-ups are required on the SDA & SCL pins, since

the PIC12 internal pull-ups are enabled for these pins.

I2C Slave

(1) Default 7-bit I2C slave address is 0101010 (or 0x54 if including the R/W bit as an 8-bit value)

(2) Default PWM carrier frequency is ~6kHz, based on the following parameters:

* Timer2 Period Register (PR2) = 0x52

* Timer2 Prescaler (T2CKPS) = 4

* Tosc = 8 MHz

To calculate what the PR2 value should be for a specific period, use the following formula:

Period = {[PR2 + 1] * 4 * Tosc * T2CKPS}

(3) The I2C buffer (SSPBuffer[I2C_BUFFERSIZE]) is an array that contains system configuration information:

* Buffer index 0 : 0 = Active, 1 or higher = Sleep

* Buffer index 1 : PR2 period register value (determines the PWM carrier frequency)

* Buffer index 2 : CCPR1L register value (determines the duty cycle)

For example, to achieve a PWM of 40% duty cycle, the CCPR1L value needs to be 40% of the PR2 register value

(4) Can only support SCL frequency of 100 kHz (might be able to handle 400 kHz if sleep mode feature is disabled in firmware)

I2C Master

(1) To write to the Slave's I2C buffer, use the following API

PIC12_write(byte address, byte data);

For example, to write the value 0xBA to the Slave's I2C buffer location #2, use PIC12_write(2, 0xBA)

NOTE: A dummy byte write is required at the end if the Slave is currently in a sleep mode

(2) To read the contents of the Slave's I2C buffer, issue a Read command with an additional byte count, and ignore the first byte that is read back

For example, if the Slave's I2C buffer is 3 bytes in length, then issue a Read command that is 4 bytes long and ignore the first byte:

AA 00 52 29 (ignore the 0xAA)

Buffer location 0 = 0x00

Buffer location 1 = 0x52

Buffer location 2 = 0x29

To install PIC12 I2C Master & Slave PWM Demo, download this package and open it in IDE.

Supported Products: MPLAB X

Release Notes

Original Release

Owners

Authors

Microchip Technology

Tags

Dependencies

    • No dependencies.

Version History

Version Downloads Last updated Approved