scl = 0; for (i = 0;i < 8; i++) { if (d & 0x80) sda = 1; /* Send the msbits first */ else sda = 0; scl = 0; scl = 1; d = d << 1; /* do shift here to increase scl high time */ scl = 0; } sda = 1; /* Release the sda line */ scl = 0; scl = 1; i = sda; if (i) { //printf("Ack bit missing %02X\n",(unsigned int)d); } scl = 0; return(i); } uchar i2cread(char b) /* ----------------------------------- */ { uchar i, d;
d = 0; sda = 1; /* Let go of sda line */ scl = 0; for (i = 0; i < 8; i++) /* read the msb first */ { scl = 1; d = d << 1; d = d | (unsigned char)sda; scl = 0; } sda = b; /* low for ack, high for nack */ scl = 1; scl = 0;
sda = 1; /* Release the sda line */ return d; }
void initialize_DS3232() /* ----- set time & date; user data entry ------ */ /* Note: NO error checking is done on the user entries! */ { yr=14; mn=1; dt=19;
} void rd_temp() /* -------- display temperature -------- */ { char str[22]; int itemp; float ftemp;
do { start(); i2cwrite(ADDRTC); i2cwrite(0x0e); /* address of control register */ start(); i2cwrite(ADDRTC + 1); /* send the device address for read */ itemp = i2cread(NACK); /* get the control register value */ stop(); } while(itemp & 0x20); /* wait until CNVT bit goes inactive */
start(); i2cwrite(ADDRTC); i2cwrite(0x11); /* address of temperature MSB */ start(); i2cwrite(ADDRTC + 1); /* send the device address for read */ itemp = ( (int) i2cread(ACK) << 2 ); itemp += ( i2cread(NACK) >> 6); stop(); if(itemp & 0x1000) itemp += 0xe000; /* if sign bit set, make 16 bit 2's comp */
start(); i2cwrite(ADDRTC); i2cwrite(0x0e); /* control/status reg address */ i2cwrite(0x05); /* enable interrupts, alarm 1 output */ } void comm_init() /* ------ reset DS3232 comm interface ------ */ { do /* because the DS3232 I2C interface is active for both supplies */ { /* after a micro reset, we must get the comm into a known state */ sda = 1; /* make sure master has released SDA */ scl = 1; if(sda) /* if sda is high, generate a start */ { sda = 0; /* The DS3232 will recognize a valid start */ sda = 1; /* condition anywhere in a I2C data transfer */ } scl = 0; } while(sda == 0); /* if the DS3232 is holding sda low, try again */ } void uart_init() { TMOD|=0x20; //TMOD=0 TH1=0xf3; //12MHZ ,BPS:4800,N,8,1,0xf3=243 TL1=0xf3; TR1=1;