Using the Inter-Integrated Circuit (IIC) for the QE Microcontrollers
QE128 Quick Reference User Guide, Rev. 1.0
Freescale Semiconductor
6-3
The baud rate in this example is 125000 because the ICS module is not configured and the MCU runs at
default speed (4 MHz).
void IIC_Init (void) {
IIC2F = 0x09; // Multiply factor of 1. SCL divider of 32
IIC2C1 = 0xC0; // Enable IIC and interrupts
}
This is the delay function used before the MCU starts to send the next byte to the slave. This delay function
is used only to observe the changes in the LEDs.
void Delay (int16 c) {
int16 i = 0;
for (i; i<=c; i++) {
}
}
This is the main function, above are the described called functions, all the interrupts are enabled. In the
endless loop a byte counter is sent by IIC to the slave. The Delay function is called between byte transfer.
void main(void) {
MCU_Init(); // Function that initializes the MCU
GPIO_Init(); // Function that initializes the Ports of the MCU
IIC_Init(); // Function that initializes the IIC module
EnableInterrupts; // enable interrupts
for(;;) {
Delay(60000);
PTED = counter;
+;
if (PTHD_PTHD7 == 0) {
while (PTHD_PTHD7 == 0); // Wait while pin is low
while (IIC2C1_MST == 1); // Wait until IIC is stopped
MasterTransmit(1,1); // Initialize to Transmit
}
else {
}
while (IIC2C1_MST == 1); // Wait until IIC is stopped
Master_Receive();
} // loop forever
// please make sure that you never leave this function
}
These functions are used when the device is configured as Master.
void Master_Read_and_Store(void) {
if (rec_count == num_to_rec) {
last_byte_to_rec = 2;
}
IIC_Rec_Data[rec_count] = IIC2D;
re+;
}
void Master_Transmit(uint8 a, uint8 b) {
// This function starts the transmission of the communication
last_byte = 0; // Initialize
count = 0;
bytes_to_trans = a; // Select number of bytes to transfer
num_to_rec = b;
IIC2C1_TX = 1; // Set TX bit for Address cycle
IIC2C1_MST = 1; // Set Master Bit to generate a Start