查看: 1711|回复: 3

[原创] 【LPC11U68】14. I2C之多字节读写

[复制链接]
  • TA的每日心情
    奋斗
    昨天 20:47
  • 签到天数: 1485 天

    [LV.10]以坛为家III

    203

    主题

    2万

    帖子

    64

    超级版主

    Rank: 8Rank: 8

    积分
    93126
    最后登录
    2024-5-8
    发表于 2018-11-15 12:05:12 | 显示全部楼层 |阅读模式
    本帖最后由 stm1024 于 2018-11-21 09:52 编辑

    废话少说,直接丢代码,重点是结合手册看时序。
    1. void I2C0_WriteBuffer(uint8_t slaveAddr,uint8_t subAddr,uint8_t* buffer,uint8_t len)
    2. {
    3.     uint8_t i;
    4.     I2C0_Start();
    5.     LPC_I2C0->DAT=slaveAddr<<1;//write address
    6.     LPC_I2C0->CONSET = CONSET_AA;
    7.     LPC_I2C0->CONCLR = CONCLR_SIC;//send data
    8.     while (LPC_I2C0->STAT != 0x18);

    9.     LPC_I2C0->DAT=subAddr;
    10.     LPC_I2C0->CONSET = CONSET_AA;
    11.     LPC_I2C0->CONCLR = CONCLR_SIC;
    12.     while (LPC_I2C0->STAT != 0x28);
    13.    
    14.     for(i=0;i<len;i++)
    15.     {
    16.         LPC_I2C0->DAT=*(buffer+i);
    17.         LPC_I2C0->CONSET = CONSET_AA;
    18.         LPC_I2C0->CONCLR = CONCLR_SIC;
    19.         while (LPC_I2C0->STAT != 0x28);
    20.     }
    21.     I2C0_Stop();
    22. }

    23. void I2C0_ReadBuffer(uint8_t slaveAddr,uint8_t subAddr,uint8_t* buffer,uint8_t len)
    24. {
    25.     uint8_t i;
    26.     I2C0_Start();
    27.     //dummy write
    28.     LPC_I2C0->DAT=slaveAddr<<1;//write address
    29.     LPC_I2C0->CONSET = CONSET_AA;
    30.     LPC_I2C0->CONCLR = CONCLR_SIC;//send data
    31.     while (LPC_I2C0->STAT != 0x18);

    32.     LPC_I2C0->DAT=subAddr;
    33.     LPC_I2C0->CONSET = CONSET_AA;
    34.     LPC_I2C0->CONCLR = CONCLR_SIC;
    35.     while (LPC_I2C0->STAT != 0x28);

    36.     I2C0_Start();//restart
    37.     LPC_I2C0->DAT=(slaveAddr<<1)|1;//read address
    38.     LPC_I2C0->CONSET = CONSET_AA;
    39.     LPC_I2C0->CONCLR = CONCLR_SIC;

    40.     while (LPC_I2C0->STAT != 0x40);
    41.     LPC_I2C0->CONSET = CONSET_AA;
    42.     LPC_I2C0->CONCLR = CONCLR_SIC;
    43.     //now STAT=0x50
    44.    
    45.     for(i=0;i<len;i++)
    46.     {
    47.         while (LPC_I2C0->STAT != 0x50);
    48.         *(buffer+i)=LPC_I2C0->DAT;
    49.         if(i==len-1)
    50.         {
    51.             LPC_I2C0->CONCLR = CONCLR_SIC|CONCLR_AAC;
    52.         }
    53.         else
    54.         {
    55.             LPC_I2C0->CONSET = CONSET_AA;
    56.             LPC_I2C0->CONCLR = CONCLR_SIC;
    57.         }
    58.     }
    59.     while (LPC_I2C0->STAT != 0x58);
    60.     I2C0_Stop();

    61. }
    复制代码
    推荐一个好的方法是阅读手册,然后单步调试,通过寄存器看STAT的值。
    2018-11-15_120701.png
    测试:
    1. uint8_t p[16]={0x31,0x50,0x09,0x01,0x12,0x03,0x18};

    2. int main()
    3. {  
    4.     uint8_t sec,min,hour,day,date,month,century,year;
    5.     uint8_t isPM=0;
    6.     Board_Init();
    7.     I2C0_Init();
    8.     I2C0_WriteBuffer(DS3231ADDR,0x00,p,7);//year
    9.     while(1)
    10.     {
    11.         I2C0_ReadBuffer(DS3231ADDR,0x00,p,7);
    12.         sec=(p[0]>>4)*10+(p[0]&0xf);
    13.         min=(p[1]>>4)*10+(p[1]&0xf);
    14.         if(p[2]>>6)//12-hour mode
    15.         {
    16.             hour=p[2]&0x1f;
    17.             if(p[2]&0x20)//PM
    18.                 isPM=1;
    19.         }
    20.         else//24-hour mode
    21.         {
    22.             hour=p[2]&0x3f;
    23.             hour=(hour>>4)*10+(hour&0x0f);
    24.         }
    25.         day=p[3]&0x07;
    26.         date=(p[4]>>4)*10+(p[4]&0xf);
    27.         month=p[5]&0x1f;
    28.         month=(month>>4)*10+(month & 0x0f);
    29.         year=(p[6]>>4)*10+(p[6]&0xf);
    30.         printf("20%02d-%02d-%02d %02d:%02d:%02d",year,month,date,hour,min,sec);
    31.         if(isPM)
    32.             printf(" PM\r\n");
    33.         else
    34.             printf("\r\n");
    35.       
    36.         Delay_ms(999);
    37.     }
    38.     //return 0;
    39. }
    复制代码
    运行结果:
    2018-11-15_120410.png





    该会员没有填写今日想说内容.
    回复

    使用道具 举报

  • TA的每日心情

    2021-2-4 09:24
  • 签到天数: 190 天

    [LV.7]常住居民III

    38

    主题

    591

    帖子

    28

    金牌会员

    Rank: 6Rank: 6

    积分
    2193
    最后登录
    2023-12-1
    发表于 2018-11-15 13:02:56 | 显示全部楼层
    好东西,代码我收下了
    哎...今天够累的,签到来了~
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    擦汗
    2023-7-4 19:10
  • 签到天数: 92 天

    [LV.6]常住居民II

    15

    主题

    222

    帖子

    23

    高级会员

    Rank: 4

    积分
    647
    最后登录
    2024-5-8
    发表于 2018-12-14 15:28:14 | 显示全部楼层
    用的什么串口工具?
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    昨天 20:47
  • 签到天数: 1485 天

    [LV.10]以坛为家III

    203

    主题

    2万

    帖子

    64

    超级版主

    Rank: 8Rank: 8

    积分
    93126
    最后登录
    2024-5-8
     楼主| 发表于 2018-12-15 17:36:20 | 显示全部楼层
    xinshuwei 发表于 2018-12-14 15:28
    用的什么串口工具?

    自己写的串口程序
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2024-5-9 06:30 , Processed in 0.122600 second(s), 22 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表