Em làm theo cái code mẫu của CLB nhưng sao không đọc được giá trị để xuất ra led 7 đoạn.. ai giúp em với..Đoạn code này có đúng không ạ?
Code:
void i2c_init(unsigned char sla_add)
{
P1SEL |= BIT6 + BIT7; // Assign I2C pins to USCI_B0
P1SEL2|= BIT6 + BIT7; // Assign I2C pins to USCI_B0
UCB0CTL1 |= UCSWRST; // Enable SW reset
UCB0CTL0 = UCMST+UCMODE_3+UCSYNC; // I2C Master, synchronous mode
UCB0CTL1 = UCSSEL_2+UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = sla_add; // Set slave address
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
}
void i2c_read(unsigned char AddRes,unsigned char numbyte,unsigned char *poirev)
{
while (UCB0CTL1 & UCTXSTP); // Loop until I2C STT is sent
UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
while (!(IFG2&UCB0TXIFG));
UCB0TXBUF = AddRes; // Address start
while (!(IFG2&UCB0TXIFG));
UCB0CTL1 &= ~UCTR; // I2C RX
UCB0CTL1 |= UCTXSTT; // I2C start condition
IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX int flag
while (UCB0CTL1 & UCTXSTT); // Loop until I2C STT is sent
while (!(IFG2&UCB0RXIFG));
unsigned char i;
for( i=0;i<numbyte;i++)
{
*(poirev+i)= UCB0RXBUF;
while (!(IFG2&UCB0RXIFG));
}
UCB0CTL1 |= UCTXSTP; // I2C stop condition after 1st TX
}
#define DS1307 0x68
unsigned char *clock_data[7];//SEC-0, MIN-1, HOUR-2, DAY-3, DATE-4, MONTH-5, YEAR-6
void clock_read(void)
{
//Read data from DS1307
i2c_init(DS1307);
i2c_read(SEC,1,clock_data[0]);
i2c_read(MIN,1,clock_data[1]);
i2c_read(HOUR,1,clock_data[2]);
i2c_read(DAY,1,clock_data[3]);
i2c_read(DATE,1,clock_data[4]);
i2c_read(MONTH,1,clock_data[5]);
i2c_read(YEAR,1,clock_data[6]);
//Convert to decimal
unsigned char i;
for (i=0;i<=7;i++)
{
*clock_data[i] = bcd2digit(*clock_data[i]);
}
}