Pokud nastane nějaký chybový stav při komunikaci, tak data nevypisuju. Jinak mám DS1307 na 5V a pullupy taky na 5, vadí to?
Čip s RTC mám, ale už to mám na desce a bohužel nemám k němu připojenou baterku.
A napájecí napětí je stabilizované ze zdroje na 5V a 3,3V na celé desce. A baterka je úplně nová.
Jinak kód je zde:
1) Nastavení I2C:
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* Enable UART clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2C1_MASTER_ADDRESS;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
/* Enable I2C */
I2C_Cmd(I2C1, ENABLE);
I2C_Init(I2C1, &I2C_InitStructure);
2) čtení hodnot z DS:
int DS1307ReadTime(DATE_TYPE *date) {
uint8_t index;
uint8_t values[8] = { 0 };
unsigned int cicles = 0;
//I2CInit();
I2C_AcknowledgeConfig(I2C1, ENABLE);
// Start setting register
I2C_GenerateSTART(I2C1, ENABLE);
cicles = 0;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) {
cicles++;
if (cicles > 10000) {
return DS1307_ERROR_TIMEOUT;
}
}
I2C_Send7bitAddress(I2C1,I2C1_SLAVE_ADDRESS,I2C_Direction_Transmitter);
cicles = 0;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) {
cicles++;
if (cicles > 10000) {
return DS1307_ERROR_TIMEOUT;
}
}
I2C_SendData(I2C1, 0x00);
cicles = 0;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) {
cicles++;
if (cicles > 10000) {
return DS1307_ERROR_TIMEOUT;
}
}
I2C_GenerateSTART(I2C1, ENABLE);
cicles = 0;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) {
cicles++;
if (cicles > 10000) {
return DS1307_ERROR_TIMEOUT;
}
}
I2C_Send7bitAddress(I2C1, I2C1_SLAVE_ADDRESS, I2C_Direction_Receiver);
cicles = 0;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) {
cicles++;
if (cicles > 10000) {
return DS1307_ERROR_TIMEOUT;
}
}
for (index = 0; index < 8; index++) {
cicles = 0;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) {
cicles++;
if (cicles > 10000) {
return DS1307_ERROR_TIMEOUT;
}
}
values[index] = I2C_ReceiveData(I2C1);
if (index == 6) {
I2C_NACKPositionConfig(I2C1, I2C_NACKPosition_Current);
I2C_AcknowledgeConfig(I2C1, DISABLE);
}
}
I2C_GenerateSTOP(I2C1, ENABLE);
cicles = 0;
while (I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF)) {
cicles++;
if (cicles > 10000) {
return DS1307_ERROR_TIMEOUT;
}
}
date->sec1 = (values[0] & 0xF0) >> 4;
date->sec0 = values[0] & 0x0F;
date->min1 = (values[1] & 0x70) >> 4;
date->min0 = values[1] & 0x0F;
date->hour1 = (values[2] & 0x30) >> 4;
date->hour0 = values[2] & 0x0F;
date->day1 = (values[4] & 0x30) >> 4;
date->day0 = values[4] & 0x0F;
date->month1 = (values[5] & 0x10) >> 4;
date->month0 = values[5] & 0x0F;
date->year1 = (values[6] & 0xF0) >> 4;
date->year0 = values[6] & 0x0F;
date->dayT = values[3];
return DS1307_OK;
}
3) převod data na znaky:
void DS1307GetDateString(DATE_TYPE* date, char* time) {
if(
((date->day1) + 0x30 >= '0' || (date->day1) + 0x30 <= '9') &&
((date->day0) + 0x30 >= '0' || (date->day0) + 0x30 <= '9') &&
((date->month1) + 0x30 >= '0' || (date->month1) + 0x30 <= '9') &&
((date->month0) + 0x30 >= '0' || (date->month0) + 0x30 <= '9') &&
((date->year1) + 0x30 >= '0' || (date->year1) + 0x30 <= '9') &&
((date->year0) + 0x30 >= '0' || (date->year0) + 0x30 <= '9')
){
time[0] = date->day1 + 0x30;
time[1] = date->day0 + 0x30;
time[2] = '.';
time[3] = date->month1 + 0x30;
time[4] = date->month0 + 0x30;
time[5] = '.';
time[6] = '2';
time[7] = '0';
time[8] = date->year1 + 0x30;
time[9] = date->year0 + 0x30;
time[10] = '\0';
}
}