| 
 | 1-digit BCD counter with LCD (I2C interface) and MCC Melody | 
| 1. Specifications | Planning | Dev. & test | Prototype | Report | 
- Inherit and adapt the previous Counter_BCD_1digit.
- Add the standard typical I2C - LCD interface as shown in Fig. 1. The typical hardware based on the I2C chip expander PCF8574 (TI and NXP datasheets) and the LM016L LCD (presented in CSD P11).
|   | 
| Fig 1. Synchronous serial I2C interface. | 
Some references to imagine how it may be inferred: Ref1., ref2., ref3. To proceed with this idea, we need to find or develop a C language high-level LCD library for the XC8 compatible with the MCC Melody I2C libraries.
| Fig. 1. Application symbol. | 
| Specifications | 2. Planning | Dev. & test | Prototype | Report | 
We will plan several incremental design steps to discover how to add an LCD with I2C interface to the CSD_PICstick and be able to program the application using the MCC Melody. The three initial projects are presented as annexes and the fourth step becomes the main project in this tutorial.
I2C step #1: Discover the I2C, find a Microchip application note on using the I2C through MCC.
I2C step #2: Add I2C bus expander PCF8574 and run it using the I2C library.
LCD step #1: Add the LCD controlled by the bus expander and develop the LCD library of functions.
LCD step #2: This project: implement the Counter_BCD_1digit_LCD through the serial interface I2C.
The same hardware from the LCD step #1.
Use the MCC Melody to print the BCD counter values on the LCD screen.
| Fig. 4. To test the program, we will print the counter numbers in the LCD screen. | 
Project location:
C:\DEE\LAB7\PIC18F\Counter_BCD_1digit_LCD\LCD_s2
| Specifications | Planning | 3. Dev. & 4. test | Prototype | Report | 
					
The full project "Counter_BCD_1digit_LCD.zip" and the LCD and I2C libraries from the previous design step in Annex #3 that to be compiled in the same project: "I2C_LCD_kit.zip" .
| Specifications | Planning | Dev. & Test | 5. Prototype | Report | 
| Specifications | Planning | Dev. & Test | Prototype | 6. Report | 
Annex A1: I2C step #1: Connect a host and a client
Learn the basics of I2C interface. Adapt the application note TB3281 to the PIC18F46K22 that we are using in the CSD_PICstick board. Generate an output port to write some LED values using the bus expander MCP23008 as an I2C client.
The I2C host will be one of the two MSSP available in the microcontroller. Two wires and pull-up resistors are required. Because the microcontroller port C (MSSP1 SCL1 is RC3 and SDA1 is RC4) is used by the LED bar, we will use the MSSP2 peripheral as the I2C host: SCL2 is RD0, SDA2 is RD1.
| Fig. A1.1. I2C initial hardware. | 
Use the MCC Melody to generate the I2C functions for the. Compile and run in Proteus to check how it works.
| Fig. A1.2. To test the program, only the I2C initialisation and simple writing operations are required. | 
Project location:
C:\DEE\LAB7\PIC18F\I2C_s1
This is the Proteus hardware capture.
| Fig A1.3. Circuit captured in Proteus. | 
The full project "I2C_s1.zip". Start a project using MPLAB v6.25 or newer and XC8 v3.00 or newer.
Use Proteus 9.0 or newer to simulate the application.
We can take advantage of the I2C instrument to analyse the communication protocol.
| Fig A1.4. I2C commands. | 
Annex A2: I2C step #2: Connect the PCF8574 I/O bus expander
Add the I2C bus expander PCF8574 and run it using the MCC generated I2C library. Represent the BCD number and the terminal count TC10 on the expanded port.
Add the I2C client PFC8574 to the same I2C bus.
| Fig. A2.1. The microcontroller host and two I2C peripherals. | 
Use the MCC Melody to generate the I2C functions for the. Compile and run in Proteus to check how it works.
| Fig. A2.2. | 
Project location for this annex:
C:\DEE\LAB7\PIC18F\I2C_s2
This is the Proteus hardware capture.
| Fig A2.3. Detail of the circuit captured in Proteus. | 
The full project "I2C_s2.zip".
| Fig. A2.4. I2C commands. | 
Annex A3: LCD step #1: LM016L and its library of functions
Develop the LCD library of functions through the I2C communication interface. Attach the LCD to the I/O expander.
| Fig. A3. 1. The microcontroller host and three I2C client peripherals. The PFC8574 is connected to the LCD board. | 
Add the kit LCD LM1602L interfaced to the PCF8574T.
|  | 
| Fig. A3.2. Standard kit I2C to LM1602 (ref.). By default the short resistors are open circuits leaving A(2..0) = "111", thus, generating a 7-bit default client address '0x27'. | 
Continuing with the tutorial, now the schematic will included three I2C clients managed by the same host.
|  | 
| Fig. A3.2. List of commands | 
Let us try several commands to write ASCII characters.
| Fig. A3.3. Operations to test some of the LCD library functions. | 
Project location for this annex:
C:\DEE\LAB7\PIC18F\LCD_s1
This is the Proteus hardware capture.
| Fig. A3.4. Detail of the circuit captured in Proteus. | 
The full project "LCD_s1.zip" and the LCD and I2C libraries "I2C_LCD_1602A_kit.zip" to be compiled in the same project.
| Fig. A3.5. Some commands and data from the I2C analyser when writing "Hello World" to the LCD. | 
From this tutorial and using a similar systematic approach, we can go ahead creating other professional applications based on FSM and including, buttons, switches, keyboards, LCD, LED, motors, A/D, etc.