Hi, This programing is to Start I2c Communication with the basic start up code followed by sending a 8 bit address together with a write bit. Next, it will send a string a serial port to show a string "nack" only if the SDA is not pulled low, which means not acknowledged.
Chip used: Dallas DS89C450 11.0592Mhz Accelerometer: SEN 10955 2.25 Mhz
Problem faced: SDA could not be pulled low. Nack received.
Question: May i know is there any faults in this program?
#include <DS89C4XX.h> //DS89C450 Microcontroller #include <intrins.h> //_nop_() function sbit SDA=P1^0; sbit SCL=P1^1; //--------------------------Transmit---------------------------------- void Uart_Tx2(unsigned char c) { while (TI_0==0); TI_0=0; SBUF0=c; } // ----------------------Transmit string ------------------------------------------------------ void sendstring (char *String) { int i=0; //set counter to 0 while(String[i]) { Uart_Tx2(String[i++]); } } //-------------------------------Delay ----------------------------------------- void I2CBitDly() // wait approximately 4.7uS { // tune to xtal. This works at 11.0592MHz unsigned int time_end = 9; unsigned int index; for (index = 0; index < time_end; index++); return; } //-------------------------------------------Start-------------------------- void I2CSendStart(void) { SDA = 1; I2CBitDly(); SCL = 1; I2CBitDly(); SDA = 0; I2CBitDly(); SCL = 0; I2CBitDly(); } //-------------------------------------------STOP----------------------------- void I2CSendStop(void) { SDA = 0; I2CBitDly(); SCL = 1; I2CBitDly(); SDA = 1; I2CBitDly(); } //-----------------------------------------SEND data--------------------------- void I2CSendByte(unsigned char bt) { register unsigned char i; for (i=0; i<8; i++) { if ((bt<<i)&0x80) SDA = 1; // Send each bit else SDA = 0; SCL = 1; I2CBitDly(); SCL = 0; I2CBitDly(); } SDA = 1; // make P1.0 an input. I2CBitDly(); SCL = 1; // 9th clock high. I2CBitDly(); if (SDA == 1) // { sendstring("nack"); // SDA is not pulled low. nack is sent. } SCL = 0; I2CBitDly(); //SDA = 1; // end transmission SCL = 1; } //---------------MAIN--------------------------------- void main () { SCON1 = 0x52; SCON0 = 0x52; TMOD = 0x20; TCON = 0x69; TH1 = 0xFD; //Buad Rate: 9600 bps I2CSendStart(); I2CSendByte(0x38); while(1) { }; }