I2C using an 8051
Display Used: Any Matrix Orbital display that supports I2C such as the MOI or LK/VK/PK series.
In this example we are talking to a display at address 50 hex over I2C communication protocol.
// 11/4/04
First test run of I2C bus.
// Display something on the LCD.
// Memory model = 3, use medium.lib, SDA = P0.0, SCL = P0.1
#include "config.h"
//This
library configures the I/O ports, interrupts, timers, etc.
#include <8051int.h> //moves data to/from stack before/after interrupt
service routines
#include "macros.h" //Dunfield macro library - eg: clrbit() uses
this
main()
{
configure();
setbit(P1.6);
clrbit(SMB0CN.4); //stop
flag
setbit(SMB0CN.5); //Start
condition
clrbit(SMB0CN.5); //clear
start flag to prevent false starts
while (!tstbit(SMB0CN,3))
{} //interrupt
occured?
SMB0DAT = 0x50; //address
of LCD
clrbit(SMB0CN.3); //
clear the interrupt flag
while (!tstbit(SMB0CN,3))
{}
SMB0DAT = 254; //command
to LCD
clrbit(SMB0CN.3);
while (!tstbit(SMB0CN,3))
{}
SMB0DAT = 88; //Clear
Display
clrbit(SMB0CN.3);
while (!tstbit(SMB0CN,3))
{}
SMB0DAT = 'H'; //now
send ASCII data
clrbit(SMB0CN.3);
while (!tstbit(SMB0CN,3))
{}
SMB0DAT = 'e';
clrbit(SMB0CN.3);
while (!tstbit(SMB0CN,3))
{}
SMB0DAT = 'l';
clrbit(SMB0CN.3);
while (!tstbit(SMB0CN,3))
{}
SMB0DAT = 'l';
clrbit(SMB0CN.3);
while (!tstbit(SMB0CN,3))
{}
SMB0DAT = 'o';
clrbit(SMB0CN.3);
while (tstbit(SMB0CN,7))
{} //wait
until the bus is free
setbit(SMB0CN.4); //Stop
condition
clrbit(SMB0CN.3);
