#include <msp430g2553.h>
#include "ds1307.h"
#include "lcd.h"
unsigned char data_i2c[8];
// SU DUNG MODULE B0
//#################################################################################################################################
void i2c_init(void)
{
//make sure that UCSWRST PHAI BANG 1 KHI CONFIG NEU KO SE GAY RA LOI KHO HIEU
UCB0CTL1 |= UCSWRST;
P1DIR |= BIT6 + BIT7 ; // Set SCL, SDA as Output
P1REN |= BIT6 + BIT7; // enable Pull-Ups or down on SCL and SDA
P1OUT |= BIT6 + BIT7; // Set Pull-Ups on SCL and SDA
P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;
/*
UCMST Bit 3 Master mode select. When a master loses arbitration in a multi-master environment (UCMM = 1) the
UCMST bit is automatically cleared and the module acts as slave.
0 Slave mode
1 Master mode
UCMODEx Bits 2-1 USCI Mode. The UCMODEx bits select the synchronous mode when UCSYNC = 1.
00 3-pin SPI
01 4-pin SPI (master/slave enabled if STE = 1)
10 4-pin SPI (master/slave enabled if STE = 0)
11 I2C mode
UCSYNC Bit 0 Synchronous mode enable
0 Asynchronous mode
1 Synchronous mode*/
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = 0x68; // Set slave address DS1307
UCB0CTL1 ^= UCSWRST; // Clear SW reset, resume operation
}
//#################################################################################################################################
void i2c_timeread(void)
{
UCB0CTL1 |= UCTR + UCTXSTT; // I2C Transmitter
while(UCTXSTT==0);
while(UCB0TXIFG==0);
UCB0TXBUF = 0x00; // DIA CHI DAU MUON DOC
while(UCB0TXIFG==0);
i2c_stop();
UCB0CTL1 ^= UCTR; // receiver
i2c_start();
while(UCB0RXIFG==0);
data_i2c[0]=UCB0RXBUF;//giay
while(UCB0RXIFG==0);
data_i2c[1]=UCB0RXBUF;//phut
while(UCB0RXIFG==0);
data_i2c[2]=UCB0RXBUF;//gio
while(UCB0RXIFG==0);
data_i2c[3]=UCB0RXBUF;//thu
while(UCB0RXIFG==0);
data_i2c[4]=UCB0RXBUF;//ngay
while(UCB0RXIFG==0);
data_i2c[5]=UCB0RXBUF;//thang
while(UCB0RXIFG==0);
data_i2c[6]=UCB0RXBUF;//nam
while(UCB0RXIFG==0);
data_i2c[7]=UCB0RXBUF; //finish reading, config
i2c_stop();
}
//#################################################################################################################################
void i2c_hienthi(void)
{
lcd_gotoxy(1,0);
switch(data_i2c[3])
{
case 1:
lcd_puts("SUN");
break;
case 2:
lcd_puts("MON");
break;
case 3:
lcd_puts("TUE");
break;
case 4:
lcd_puts("WED");
break;
case 5:
lcd_puts("THU");
break;
case 6:
lcd_puts("FRI");
break;
case 7:
lcd_puts("SAT");
break;
}
lcd_puts(",");
lcd_putnumber(data_i2c[4]>>4);
lcd_putnumber(data_i2c[4]&0x0F);
lcd_puts("/");
lcd_putnumber(data_i2c[5]>>4);
lcd_putnumber(data_i2c[5]&0x0F);
lcd_puts("/20");
lcd_putnumber(data_i2c[6]>>4);
lcd_putnumber(data_i2c[6]&0x0F);
lcd_gotoxy(8,1);
lcd_putnumber(data_i2c[2]>>4);
lcd_putnumber(data_i2c[2]&0x0F);
lcd_puts(":");
lcd_putnumber(data_i2c[1]>>4);
lcd_putnumber(data_i2c[1]&0x0F);
lcd_puts(":");
lcd_putnumber(data_i2c[0]>>4);
lcd_putnumber(data_i2c[0]&0x0F);
}
//#################################################################################################################################
void i2c_start()
{
UCB0CTL1 |= UCTXSTT; // I2C start condition
while (UCB0CTL1 & UCTXSTT); // Loop until I2C STT is sent
}
//#################################################################################################################################
void i2c_stop()
{
UCB0CTL1 |= UCTXSTP; // Generate I2C stop condition
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
}
//#################################################################################################################################