在下面的代码中,我试图显示时钟时间,这是我使用其他功能。 从main调用此函数时,我将十六进制格式的地址传递给此函数,我想打印时间,但打印不正确。
void DS1307_GetTime(unsigned char *h_ptr, unsigned char *m_ptr, unsigned char *s_ptr)
{
I2C_Start(); // Start I2C communication
DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus
DS1307_Write(SEC_ADDRESS); // Request Sec RAM address at 00H
I2C_Stop(); // Stop I2C communication after selecting Sec Register
I2C_Start(); // Start I2C communication
DS1307_Write(0xD1); // connect to DS1307( under Read mode) by sending its ID on I2c Bus
*s_ptr = DS1307_Read(); I2C_Ack(); // read second and return Positive ACK
*m_ptr = DS1307_Read(); I2C_Ack(); // read minute and return Positive ACK
*h_ptr = DS1307_Read(); I2C_NoAck(); // read hour and return Negative/No ACK
*s_ptr = bcd_to_dec(*s_ptr);
*m_ptr = bcd_to_dec(*m_ptr);
*h_ptr = bcd_to_dec(*h_ptr);
printf("Time is in ss:mm:hh = %u:%u:%u\n", s_ptr, m_ptr, h_ptr);
I2C_Stop(); // Stop I2C communication after reading the Time
}
我认为我的问题出在我的指针声明或printf语句中,但我无法确切地知道问题是什么。
答案 0 :(得分:1)
您需要取消引用printf中的指针。现在你正在打印s_ptr,m_ptr和h_ptr的指针值(即地址)。
printf("Time is in ss:mm:hh = %u:%u:%u\n", *s_ptr, *m_ptr, *h_ptr);
(当然,这是假设您的其他内部时间生成功能按预期工作)