gpt4 book ai didi

c - 使用 I2C 读取时间 (Ds1307)

转载 作者:行者123 更新时间:2023-11-30 16:33:56 25 4
gpt4 key购买 nike

我正在使用 PIC18F45K22 和 XC8。我已插入 I2C 的 MCC 库

我需要示例从 ds1307 读取秒数,并在下一个级别更改当前时间。

根据下面的代码,我在里面堆栈
while (状态== I2C2_MESSAGE_PENDING);

    I2C2_MasterWrite(&pdata_write, 1, 0b11010000, &status);
// at this point, your status will probably be I2C2_MESSAGE_PENDING
while (status == I2C2_MESSAGE_PENDING); // wait for status to to change
if (status == I2C2_MESSAGE_COMPLETE) {
I2C2_MasterRead(&pdata_read, 1, 0b11010001, &status);
while (status == I2C2_MESSAGE_PENDING); // again, wait for status to to change
if (status == I2C2_MESSAGE_COMPLETE) {
// pdata_read should now be the number of seconds (in binary-coded decimal)
}

这是我从 mcc 自动创建的 pin 管理器

#define SCL2_TRIS               TRISDbits.TRISD0
#define SCL2_LAT LATDbits.LATD0
#define SCL2_PORT PORTDbits.RD0
#define SCL2_ANS ANSELDbits.ANSD0
#define SCL2_SetHigh() do { LATDbits.LATD0 = 1; } while(0)
#define SCL2_SetLow() do { LATDbits.LATD0 = 0; } while(0)
#define SCL2_Toggle() do { LATDbits.LATD0 = ~LATDbits.LATD0; } while(0)
#define SCL2_GetValue() PORTDbits.RD0
#define SCL2_SetDigitalInput() do { TRISDbits.TRISD0 = 1; } while(0)
#define SCL2_SetDigitalOutput() do { TRISDbits.TRISD0 = 0; } while(0)
#define SCL2_SetAnalogMode() do { ANSELDbits.ANSD0 = 1; } while(0)
#define SCL2_SetDigitalMode() do { ANSELDbits.ANSD0 = 0; } while(0)

// get/set SDA2 aliases
#define SDA2_TRIS TRISDbits.TRISD1
#define SDA2_LAT LATDbits.LATD1
#define SDA2_PORT PORTDbits.RD1
#define SDA2_ANS ANSELDbits.ANSD1
#define SDA2_SetHigh() do { LATDbits.LATD1 = 1; } while(0)
#define SDA2_SetLow() do { LATDbits.LATD1 = 0; } while(0)
#define SDA2_Toggle() do { LATDbits.LATD1 = ~LATDbits.LATD1; } while(0)
#define SDA2_GetValue() PORTDbits.RD1
#define SDA2_SetDigitalInput() do { TRISDbits.TRISD1 = 1; } while(0)
#define SDA2_SetDigitalOutput() do { TRISDbits.TRISD1 = 0; } while(0)
#define SDA2_SetAnalogMode() do { ANSELDbits.ANSD1 = 1; } while(0)
#define SDA2_SetDigitalMode() do { ANSELDbits.ANSD1 = 0; } while(0)

Image

Image2

见下图

还尝试在我的液晶显示屏上显示时间

sprintf(txt,"%d",pdata_read);
LCDPutStr(txt,1);

最佳答案

我没有硬件或软件,但查看了https://www.studentcompanion.co.za/interfacing-the-ds1307-real-time-clock-with-pic-microcontroller-xc8/ (一个很长的教程,可能会让您加快速度)和 https://datasheets.maximintegrated.com/en/ds/DS1307.pdf (数据表),以及一些随机页面以某种方式提到了 I2C_MasterWrite/I2C_MasterRead

非常低级的步骤是:

  1. 开始(为您处理)
  2. 写入 0b1101000(这是地址加上方向位)
  3. 收到确认
  4. 写入“秒”寄存器的地址,正如您正确所说,它是0x00(这是数据)
  5. 收到确认
  6. 开始
  7. 写入 0b11010001(这是地址加上方向位)
  8. 收到确认
  9. 阅读
  10. 发送确认或不确认

看起来非常低级的部分(开始/确认和发送地址)正在为您处理,因此您可能只需要执行类似于以下代码的操作(您可能需要重构 if 更好地处理错误的逻辑):

I2C_MESSAGE_STATUS status;
uint8_t pdata_write = 0; // 0 to 'seconds' register
uint8_t pdata_read; // will hold 'seconds'

I2C2_MasterWrite(&pdata_write, 1, 0b1101000, &status);
// at this point, your status will probably be I2C2_MESSAGE_PENDING
while (status == I2C_MESSAGE_PENDING); // wait for status to to change
if (status == I2C_MESSAGE_COMPLETE) {
I2C2_MasterRead(&pdata_read, 1, 0b1101000, &status);
while (status == I2C_MESSAGE_PENDING); // again, wait for status to to change
if (status == I2C_MESSAGE_COMPLETE) {
// pdata_read should now be the number of seconds (in binary-coded decimal)
}
}
  • *pdata 是一个指向您想要读/写的内容的指针,它确实是一个指向上面步骤 4 中包含 0x0 的变量的指针.
  • length 是您想要读取/写入的数据的长度,我认为在上面列出的所有步骤中它恰好都是 1
  • address是DS1307的地址,0b1101000
  • pstatus 是 I2C 消息的状态。

根据https://github.com/apaivaj/WWVB/blob/master/MainProject/WWVB/mcc_generated_files/i2c1.h , pstatus 可以是以下任意一项:

typedef enum
{
I2C1_MESSAGE_FAIL,
I2C1_MESSAGE_PENDING,
I2C1_MESSAGE_COMPLETE,
I2C1_STUCK_START,
I2C1_MESSAGE_ADDRESS_NO_ACK,
I2C1_DATA_NO_ACK,
I2C1_LOST_STATE
} I2C1_MESSAGE_STATUS;

您可能至少应该检查I2C_MESSAGE_FAIL

<小时/>

更新:要写入秒寄存器,请参见数据表第12页的图4:您需要写入两条数据:首先是寄存器编号,然后是您想要的编号写入该寄存器。因此,代码将如下所示:

I2C_MESSAGE_STATUS status;
uint8_t pdata_write[2] = { 0, 0x10 }; // 0x10 into 'seconds' register
I2C2_MasterWrite(pdata_write, 1, 0b1101000, &status);
while (status == I2C_MESSAGE_PENDING); // wait for status to to change
if (status == I2C_MESSAGE_COMPLETE) {
// done!
} else {
// error
}

关于c - 使用 I2C 读取时间 (Ds1307),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49492415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com