[DemoCode下载] M031使用库函数读写EEPROM操作

[复制链接]
684|2
 楼主| mintspring 发表于 2023-12-26 23:23 | 显示全部楼层 |阅读模式
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 5 $
  5. * $Date: 18/07/09 7:01p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to use I2C interface to access EEPROM.
  7. *
  8. * SPDX-License-Identifier: Apache-2.0
  9. * Copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "NuMicro.h"

  13. #define I2C_EEPROM_ADDRESS  0x50

  14. void SYS_Init(void)
  15. {
  16.     /*---------------------------------------------------------------------------------------------------------*/
  17.     /* Init System Clock                                                                                       */
  18.     /*---------------------------------------------------------------------------------------------------------*/

  19.     /* Unlock protected registers */
  20.     SYS_UnlockReg();

  21.     /* Enable HIRC clock (Internal RC 48MHz) */
  22.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  23.     /* Wait for HIRC clock ready */
  24.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  25.     /* Select HCLK clock source as HIRC and HCLK source divider as 1 */
  26.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  27.     /* Enable UART0 clock */
  28.     CLK_EnableModuleClock(UART0_MODULE);

  29.     /* Switch UART0 clock source to HIRC */
  30.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  31.     /* Enable I2C0 clock */
  32.     CLK_EnableModuleClock(I2C0_MODULE);

  33.     /* Update System Core Clock */
  34.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and cyclesPerUs automatically. */
  35.     SystemCoreClockUpdate();

  36.     /*---------------------------------------------------------------------------------------------------------*/
  37.     /* Init I/O Multi-function                                                                                 */
  38.     /*---------------------------------------------------------------------------------------------------------*/
  39.     /* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
  40.     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
  41.                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  42.     /* Set I2C0 multi-function pins */
  43.     SYS->GPC_MFPL = (SYS->GPC_MFPL & ~(SYS_GPC_MFPL_PC0MFP_Msk | SYS_GPC_MFPL_PC1MFP_Msk)) |
  44.                     (SYS_GPC_MFPL_PC0MFP_I2C0_SDA | SYS_GPC_MFPL_PC1MFP_I2C0_SCL);

  45.     /* Lock protected registers */
  46.     SYS_LockReg();
  47. }

  48. void I2C0_Close(void)
  49. {
  50.     /* Disable I2C0 interrupt and clear corresponding NVIC bit */
  51.     I2C_DisableInt(I2C0);
  52.     NVIC_DisableIRQ(I2C0_IRQn);

  53.     /* Disable I2C0 and close I2C0 clock */
  54.     I2C_Close(I2C0);
  55.     CLK_DisableModuleClock(I2C0_MODULE);

  56. }

  57. void I2C0_Init(void)
  58. {
  59.     /* Open I2C module and set bus clock */
  60.     I2C_Open(I2C0, 100000);

  61.     /* Get I2C0 Bus Clock */
  62.     printf("I2C clock %d Hz\n", I2C_GetBusClockFreq(I2C0));
  63. }

  64. int main()
  65. {
  66.     uint32_t i;
  67.     uint8_t txbuf[10] = {0}, rDataBuf[10] = {0};

  68.     SYS_Init();

  69.     /* Init UART0 to 115200-8n1 for print message */
  70.     UART_Open(UART0, 115200);

  71.     printf("+-------------------------------------+\n");
  72.     printf("|    Level1 I2C EEPROM Sample Code    |\n");
  73.     printf("+-------------------------------------+\n\n");
  74.     printf("\n");

  75.     /* Init I2C0 to access EEPROM */
  76.     I2C0_Init();

  77.     /* Prepare data for transmission */
  78.     printf("Write Data: ");
  79.     for (i = 0; i < 10; i++)
  80.     {
  81.         txbuf[i] = (uint8_t) i + 3;
  82.         printf("  %d",txbuf[i]);
  83.     }
  84.     printf("\n");

  85.     /* Write 10 bytes data to EEPROM */
  86.     while(I2C_WriteMultiBytesTwoRegs(I2C0, I2C_EEPROM_ADDRESS, 0x0000, txbuf, 10) < 10);
  87.     printf("Multi bytes Write access Pass.....\n\n");

  88.     /* Use Multi Bytes Read from EEPROM (Two Registers) */
  89.     while(I2C_ReadMultiBytesTwoRegs(I2C0, I2C_EEPROM_ADDRESS, 0x0000, rDataBuf, 10) < 10);

  90.     printf("Read Data:  ");
  91.     for(i = 0; i < 10; i++)
  92.         printf("  %d",rDataBuf[i]);
  93.     printf("\n");
  94.     printf("Multi bytes Read access Pass.....\n\n");

  95.     /* Compare TX data and RX data */
  96.     for(i = 0; i < 10; i++)
  97.     {
  98.         if(txbuf[i] != rDataBuf[i])
  99.         {
  100.             printf("Data compare fail... R[%d] Data: 0x%X\n", i, rDataBuf[i]);
  101.             while(1);
  102.         }
  103.     }
  104.     printf("Data compare done... [PASS]\n");

  105.     while(1);
  106. }

  107. /*** (C) COPYRIGHT 2018 Nuvoton Technology Corp. ***/


 楼主| mintspring 发表于 2023-12-26 23:24 | 显示全部楼层
该芯片提供的BSP库函数更为好用。
短句家 发表于 2025-9-11 15:29 | 显示全部楼层
初始化 I2C,调用 I2C_WriteByte 写入地址和数据;读时先写地址,再调用 I2C_ReadByte 读取,需处理 ACK 信号。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

303

主题

4972

帖子

24

粉丝
快速回复 在线客服 返回列表 返回顶部