2013-12-18 29 views
0

在下面的代码中,我试图显示时钟时间,我使用其他函数。 从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语句,但我无法弄清楚到底是什么问题。

回答

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);

(这是当然的,假设你的其他内部时间生成功能被按预期运行)

+0

我也尝试的printf(“时间是在SS:MM:HH =%P: %p:%p \ n“,* s_ptr,* m_ptr,* h_ptr);但仍然没有得到正确的答案.. – Jay

+0

然后我假设你的问题是在别处。无论是与设备通信还是在bcd_to_dec中 – LinearZoetrope

相关问题