Class Notes
Class Notes
■ START
■ STOP
Sending bits of data
The SDA values changes
when SCL is low.
void loop(){
delay(100);
static byte val = 'A';
Wire_Begin_Transmission(0x32+0); // transmit START condition and transmit SLA + W(0)
Wire_Write(val); // transmit One Byte of Data
Wire_End_Transmission(); // transmit STOP condition
val++; // increment value
if(val > 'z'){ // if reached
val = 'A'; // start over from lowest value
}
} 18
Example 1: Slave Receiver
void Wire_Begin(byte address){ void setup(){
TWAR = address; Wire_Begin(0x32); // join i2c bus with
} // address 0x32 as LSB of address 0x32 is
void Wire_onConnect(bool ack){ // 0 that means do not answer to general
TWCR = (1<<TWINT)|(1<<TWEN)|(ack<<TWEA); // call(Address 0x00).
// poll the TWINT flag to see when the slave
// is addressed by a master device Serial.begin(9600);
while ((TWCR & (1<<TWINT)) == 0); }
}
void loop(){
byte Wire_Read(bool ack){ byte R;
TWCR = (1<<TWINT)|(1<<TWEN)|(ack<<TWEA); Wire_onConnect(1); // Send acknowledge to
while ((TWCR & (1<<TWINT)) == 0); // Master Device
return TWDR; R = Wire_Read (1); // Receive Data and
} // send an acknowledge
void Wire_Write(byte aByte){ Serial.write(R); // Show Received Data
TWDR = aByte; // on Serial Terminal
TWCR = (1<<TWINT)|(1<<TWEN); }
while ((TWCR & (1<<TWINT)) == 0);
} Page 1 Page
19 2
Master Transmitter Slave Receiver
Example 2: Master Receiver
void Wire_Begin(void){ byte Wire_Read(byte ack){
TWSR = 0x00; //set prescaler bits to zero TWCR= (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
TWBR = 152; // SCL frequency = 50K if XTAL = 16M while ((TWCR & (1<<TWINT)) == 0);
TWCR = 0x04; // enable the TWI module return TWDR;
} }
void setup(){
void Wire_Begin_Transmission(byte address){ Wire_Begin(); // join i2c bus
TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); Serial.begin(9600);
while ((TWCR & (1 << TWINT)) == 0); }
Wire_Write(address); void loop(){
} delay(100);
void Wire_End_Transmission(){ byte R;
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); Wire_Begin_Transmission(0x32+1);
} // transmit to device 0x32
R = Wire_Read(0);
void Wire_Write(byte aByte){ // sends value byte
TWDR = aByte; Wire_End_Transmission();
TWCR = (1<<TWINT)|(1<<TWEN); // stop transmitting
while ((TWCR & (1<<TWINT)) == 0); Serial.write(R);
} Page 1 } Page
21 2
Example 2: Slave Transmitter
void Wire_Begin(byte address){ void setup(){
TWAR=address; Wire_Begin(0x32); // join i2c bus
} // with address 0x32 as LSB of
void Wire_onConnect(bool ack){ // address 0x32 is 0 that means do
TWCR = (1<<TWINT)|(1<<TWEN)|(ack<<TWEA); // not answer to general call.
while ( (TWCR & (1<<TWINT) ) == 0); Serial.begin(9600);
} }
0 = 24
1 = 12 0=AM
1=PM
12-hour or 24-hour mode example
0 = 24
1 = 12 0=AM
1=PM
Writing to DS1307
■ Transmit START condition
■ Transmit the Address of DS1307 (1101 000) followed by 0 to indicate a WRITE
operation
■ Transmit the Address of location you want to access (it stores the Address in
Register Pointer)
■ Transmit one or more bytes of Date
■ Transmit STOP condition
S W A A A A A S
T Slave RI C Register C C C C T
Sec Min Hours
A Address T K Pointer K K K K O
R E P
T
S 1101 000 0 A 0000 0000 A 0x55 A 0x59 A 0x23 N P
Reading from DS1307
■ Transmit START condition
■ Transmit the Address of DS1307 (1101 000 ) followed by 0 to indicate a WRITE operation
■ Transmit the address to be stored in Register Pointer.
■ Transmit STOP condition then transmit START condition
■ Transmit the Address of DS1307 (1001101) followed by 1 to indicate a READ operation
■ Receive one or more bytes of Date and send Acknowledge.
■ Send Not Acknowledge if you don’t want to receive more bytes of Date.
■ Transmit STOP condition.
S W A A S S R A A A N S
T Slave RI C Register C T T Slave E C C C A T
Sec Min Hours
A Address T K Pointer K O A Address A K K K C O
R E P R D K P
T T
S 1101 000 0 A 0000 0000 A P S 1101 000 1 A 0x45 A 0x59 A 0x23 N P
DS1307 Setting and Reading Time
DS1307 Setting and Reading Time
DS1307 Setting and Reading Time
#define SUN 0x01 void Start() {
#define MON 0x02 TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTA);
#define TUE 0x03 while (!(TWCR & (1 << TWINT)));
#define WED 0x04 }
#define THU 0x05
#define FRI 0x06 void Stop() {
#define SAT 0x07 TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}
byte hh, mm, ss, WD, DD, MM, YY;
void Write( byte data) {
char* days[] = {"Sunday", "Monday", "Tuesday", TWDR = data;
"Wednesday", "Thursday", "Friday", "Saturday"}; TWCR = (1<<TWINT)|(1<<TWEN);
while (!(TWCR & (1 << TWINT) ) ) ;
char* months[] = {"JAN", "FEB", "MAR", "APR", }
"MAY", "JUN", "JUL", "AUG",
"SEP", "OCT", "NOV", "DEC"}; unsigned char Read(unsigned char ack) {
void Clock_Set() { TWCR = (1<<TWINT)|(1<<TWEN)|(ack<<TWEA);
TWSR = 0; // set prescaler bits to zero while (!(TWCR & (1 << TWINT) ) );
TWBR = 72; // SCL frequency is 100K return TWDR;
} // for XTAL = 16MHz Page 1 } Page
37 2
DS1307 Setting and Reading Time
void RTC_Set_Time() { void RTC_Set_Date() {
Start(); // transmit START condition Start(); // transmit START condition
Write(0xD0); // address DS1307 for write Write(0xD0); // address DS1307 for write
Write(0); // set register pointer to 0 Write(0x03); // set register pointer to 3
Write(0x80); // Stop the clock. The seconds // at address 3 we have
// will be written last // week days
Write(mm); // set minutes
Write(hh); // set Hours Write(WD); // set week day
Stop(); // 1=Sunday ,7=Saturday
Page 3 Page
38 4
DS1307 Setting and Reading Time
void RTC_Get_Time() void RTC_Get_Date()
{ {
Start () ; // transmit START cond. Start () ; // transmit START condition
Write(0xD0); // Address of DS1307 is 0xD0 Write(0xD0); // address DS1307 for write
// here LSB is 0 for Write Write(3); // set register pointer to 3
Write(0); // set register pointer to 0 Stop(); // transmit STOP cond.
// to select Seconds
Stop(); // transmit STOP Cond. Start () ; // transmit START condition
Write(0xD1); // SLA + R(1)
Start () ; // transmit START condition WD = Read(1); // read day, return ACK
Write(0xD1); // Address of DS1307 is 0xD0 DD = Read(1); // read date, return ACK
// here LSB is 1 for Read MM = Read(1); // read mon, return ACK
YY = Read(0); // read hour, return NACK
ss = Read(1); // read sec, return ACK Stop(); // transmit STOP condition
mm = Read(1); // read min, return ACK }
hh = Read(0); // read hh, return NACK
Stop(); // transmit STOP Condition
}
Page 5 Page
39 6
DS1307 Setting and Reading Time
void setup() { void loop () {
RTC_Get_Date();
delay(1000); Serial.print(" Date: ");
Serial.begin(9600); Serial.print(days[WD - 1]);
Serial.print(", ");
// initialize I2C module Serial.print(months[MM - 1]);
Clock_Set (); Serial.print(" ");
Serial.print(DD, HEX);
hh = 0x23; mm = 0x59 ; ss = 0x55; Serial.print(", 20");
Serial.print(YY, HEX); Serial.print(" ");
// Set time 23:59:55
RTC_Set_Time(); RTC_Get_Time();
Serial.print("Time: ");
WD = SUN; DD = 0x19; MM = 0x04 ; YY = 0x20; Serial.print(hh, HEX);
Serial.print(":");
// Set date Sunday, 19-APR-2020 Serial.print(mm, HEX);
RTC_Set_Date(); Serial.print(":");
} Serial.println(ss, HEX);
delay(1000);
Page 7 } Page
40 8
DS3231 module
DS3231 RTC is Precise Real-Time Clock Module with 32Kbit EEPROM and a
built-in 10-bit temperature sensor. The maximum transmission speed is 400KHz
with working voltage of 5V.