Code: Select all
#include <htc.h>
void delay(int);
void init(void);
void i2c_init(void);
void i2c_start(void);
void i2c_write(char);
void i2c_stop(void);
void i2c_wait(void);
void main(void)
{
delay(600);
init();
i2c_init();
SSPIF = 0; //CLEAR INTERRUPT
i2c_start();
i2c_write(0x50); //Load LCD Address
i2c_write(0xFE); //Load Command
i2c_write(0x58); //Load Clear
RA0 = 1;
i2c_stop();
while (1){
i2c_start();
i2c_write(0x50);
//while(ACKSTAT == 1);
i2c_write(0xFE);
//while(ACKSTAT == 1);
i2c_write(0x58);
//while(ACKSTAT == 0);
i2c_stop();
}
}
void init(void){
PORTA = 0x00;
PORTB = 0x00;
ADCON1 = 0x06;
TRISA = 0x00;
TRISB = 0x00;
TRISC = 0x00;
}
void i2c_init(void){
/* SSPCON1 REGISTERS */
SSPEN = 1; //Enables Serial Port Mode
SSPM3 = 1; /////////
SSPM2 = 0; //I2C Master Mode
SSPM1 = 0; //
SSPM0 = 0; ////////
/* SSPCON2 REGISTERS */
SSPCON2 = 0x00;
/* SSPSTAT REGISTERS */
STAT_SMP = 1; //Slew Rate Control Disabled
STAT_CKE = 0; //SMBus Specific Inputs Disabled
SSPADD = 0x19;
}
void i2c_start(void){
SEN = 1; //Generate Start Condition
i2c_wait();
}
void i2c_write(char byte){
SSPBUF = byte;
i2c_wait();
}
void i2c_stop(void){
PEN = 1; //Generate Stop Condition
i2c_wait();
}
void i2c_wait(void) {
while ( SSPIF==0 );
SSPIF = 0;
}
void delay(int pause){
unsigned int i, j, k;
unsigned int clock = 8;
unsigned int cycle = 14;
for(i = 0; i <= pause; i++){
for(j = 0; j <= clock; j++){
for(k = 0; k <= cycle; k++){
asm("nop");
}}}
}
Also, on the 3rd byte, the first bit of the clock looks the same way (as if one device was pulling it low while the other holding it high) with a voltage around 2.2 - 2.5V. Any help would be great!
I'm using a PIC16F876A with onboard MSSP, and LCD-2041 and the Hi-Tech C ANSI compliant compiler.
-Jim