[其他] 【灵动微电子MM32F0121测评】+基础任务

[复制链接]
 楼主| jinyi7016 发表于 2025-6-25 08:51 | 显示全部楼层 |阅读模式
1、建立开发调试环境

pack支持包下载:https://www.mindmotion.com.cn/support/software/keil_pack/
安装keil MDK软件,进行和谐。
安装pack包中的MindMotion.MM32F0120_DFP.0.9.3.pack

即开发环境搭建完成。
打开一个工程进行编译 :
clipboard.png

2、调试串口打印,输出Hello 21ic, HelloMM32F0121


打开一个官方的例程,在platform.c文件中,已经定义了fputc函数,将串口2与printf进行绑定。直接 printf可在串口输出
printf("Hello 21ic, HelloMM32F0121 ");


clipboard.png


3、呼吸灯实验
原理图中,两个LED连接的是PB14、PB15;这两个引脚,可用的定时 器是TIM1;
电路原理图:


clipboard.png
数据手册中,这两个引脚的复用。

clipboard.png

打开一个PWM工程,修改输出的引脚
clipboard.png


动态修改TIM_SetCompare3 函数来改变 占空比

效果:
8111ec63afdc47fb1a4f51c4ab4b13a9.gif
4、RTC实时时钟外设,打印RTC的时间戳



  1. /***********************************************************************************************************************
  2.     [url=home.php?mod=space&uid=288409]@file[/url]    rtc_calendar.c
  3.     [url=home.php?mod=space&uid=187600]@author[/url]  FD Team
  4.     [url=home.php?mod=space&uid=212281]@date[/url]    12-Dec-2023
  5.     [url=home.php?mod=space&uid=247401]@brief[/url]   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
  6.   **********************************************************************************************************************
  7.     @attention

  8.     <h2><center>© Copyright(c) <2023> <MindMotion></center></h2>

  9.       Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
  10.     following conditions are met:
  11.     1. Redistributions of source code must retain the above copyright notice,
  12.        this list of conditions and the following disclaimer.
  13.     2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
  14.        the following disclaimer in the documentation and/or other materials provided with the distribution.
  15.     3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
  16.        promote products derived from this software without specific prior written permission.

  17.       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  18.     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19.     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20.     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21.     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22.     WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23.     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.   *********************************************************************************************************************/

  25. /* Define to prevent recursive inclusion */
  26. #define _RTC_CALENDAR_C_

  27. /* Files include */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "platform.h"
  32. #include "rtc_calendar.h"

  33. /**
  34.   * @addtogroup MM32F0120_LibSamples
  35.   * @{
  36.   */

  37. /**
  38.   * @addtogroup RTC
  39.   * @{
  40.   */

  41. /**
  42.   * @addtogroup RTC_Calendar
  43.   * @{
  44.   */

  45. /* Private typedef ****************************************************************************************************/

  46. /* Private define *****************************************************************************************************/

  47. /* Private macro ******************************************************************************************************/

  48. /* Private variables **************************************************************************************************/
  49. const uint8_t RTC_DayOfMonth[12] =
  50. {
  51.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  52. };

  53. RTC_CalendarTypeDef RTC_Calendar;

  54. /* Private functions **************************************************************************************************/

  55. /***********************************************************************************************************************
  56.   * @brief
  57.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]   none
  58.   * @param  none
  59.   * @retval none
  60.   *********************************************************************************************************************/
  61. uint8_t RTC_LeapYear(uint16_t Year)
  62. {
  63.     if (
  64.         (((Year % 400) == 0)) ||                   /* Century Leap Year */
  65.         (((Year % 100) != 0) && ((Year % 4) == 0)) /* Normal  Leay Year */
  66.         )
  67.     {
  68.         return (1);
  69.     }
  70.     else
  71.     {
  72.         return (0);
  73.     }
  74. }

  75. /***********************************************************************************************************************
  76.   * @brief  
  77.   * @note   none
  78.   * @param  none
  79.   * @retval none
  80.   *********************************************************************************************************************/
  81. uint8_t RTC_GetWeek(uint16_t Year, uint8_t Month, uint8_t Day)
  82. {
  83.     int w, c, y;

  84.     /* Month 1 Or 2 of This Year Must Be As Last Month 13 Or 14 */
  85.     if ((Month == 1) || (Month == 2))
  86.     {
  87.         Month += 12;
  88.         Year  -= 1;
  89.     }

  90.     w = 0;                             /* Weekday */
  91.     c = Year / 100;                    /* Century */
  92.     y = Year % 100;                    /* Year    */

  93.     w = y + (y / 4) + (c / 4) - (2 * c) + (26 * (Month + 1) / 10) + Day - 1;

  94.     while (w < 0)
  95.     {
  96.         w += 7;
  97.     }

  98.     w %= 7;

  99.     return (w);
  100. }

  101. /***********************************************************************************************************************
  102.   * @brief
  103.   * @note   none
  104.   * @param  none
  105.   * @retval none
  106.   *********************************************************************************************************************/
  107. void RTC_UpdateCalendar(void)
  108. {
  109.     static uint32_t PreTotalDay = 0;
  110.     uint32_t TotalSecond = 0;
  111.     uint32_t TotalDay    = 0;
  112.     uint16_t Year  = 1970;
  113.     uint8_t  Month = 0;

  114.     TotalSecond = RTC_GetCounter();
  115.     TotalDay    = TotalSecond / 86400;

  116.     if (PreTotalDay != TotalDay)
  117.     {
  118.         PreTotalDay = TotalDay;

  119.         while (TotalDay >= 365)
  120.         {
  121.             if (RTC_LeapYear(Year) == 1)
  122.             {
  123.                 if (TotalDay >= 366)
  124.                 {
  125.                     TotalDay -= 366;
  126.                 }
  127.                 else
  128.                 {
  129.                     break;
  130.                 }
  131.             }
  132.             else
  133.             {
  134.                 TotalDay -= 365;
  135.             }

  136.             Year++;
  137.         }

  138.         RTC_Calendar.year = Year;

  139.         while (TotalDay >= 28)
  140.         {
  141.             if ((Month == 1) && (RTC_LeapYear(RTC_Calendar.year) == 1))
  142.             {
  143.                 if (TotalDay >= 29)
  144.                 {
  145.                     TotalDay -= 29;
  146.                 }
  147.                 else
  148.                 {
  149.                     break;
  150.                 }
  151.             }
  152.             else
  153.             {
  154.                 if (TotalDay >= RTC_DayOfMonth[Month])
  155.                 {
  156.                     TotalDay -= RTC_DayOfMonth[Month];
  157.                 }
  158.                 else
  159.                 {
  160.                     break;
  161.                 }
  162.             }

  163.             Month++;
  164.         }

  165.         RTC_Calendar.month = Month + 1;
  166.         RTC_Calendar.day   = TotalDay + 1;

  167.         RTC_Calendar.week  = RTC_GetWeek(RTC_Calendar.year, RTC_Calendar.month, RTC_Calendar.day);
  168.     }

  169.     RTC_Calendar.hour   = (TotalSecond % 86400) / 3600;
  170.     RTC_Calendar.minute = ((TotalSecond % 86400) % 3600) / 60;
  171.     RTC_Calendar.second = ((TotalSecond % 86400) % 3600) % 60;
  172. }

  173. /***********************************************************************************************************************
  174.   * @brief
  175.   * @note   none
  176.   * @param  none
  177.   * @retval none
  178.   *********************************************************************************************************************/
  179. void RTC_SetDateTime(uint16_t Year, uint8_t Month, uint8_t Day, uint8_t Hour, uint8_t Minute, uint8_t Second)
  180. {
  181.     uint32_t TotalSecond = 0;
  182.     uint16_t y = 0;
  183.     uint8_t  m = 0;

  184.     if ((Year >= 1970) && (Year <= 2099))
  185.     {
  186.         for (y = 1970; y < Year; y++)
  187.         {
  188.             if (RTC_LeapYear(y) == 1)
  189.             {
  190.                 TotalSecond += 31622400; /* Total Seconds Of Leap   Year */
  191.             }
  192.             else
  193.             {
  194.                 TotalSecond += 31536000; /* Total Seconds Of Normal Year */
  195.             }
  196.         }

  197.         for (m = 0; m < (Month - 1); m++)
  198.         {
  199.             TotalSecond += RTC_DayOfMonth[m] * 86400; /* Total Seconds Of Month */

  200.             if ((RTC_LeapYear(Year) == 1) && (m == 1))
  201.             {
  202.                 TotalSecond += 86400;
  203.             }
  204.         }

  205.         TotalSecond += (uint32_t)(Day - 1) * 86400; /* Total Seconds Of Day    */
  206.         TotalSecond += (uint32_t)Hour * 3600;       /* Total Seconds Of Hour   */
  207.         TotalSecond += (uint32_t)Minute * 60;       /* Total Seconds Of Minute */
  208.         TotalSecond += Second;

  209.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_RTC | RCC_APB1Periph_BKP, ENABLE);

  210.         PWR_BackupAccessCmd(ENABLE);

  211.         RTC_SetCounter(TotalSecond);
  212.         RTC_WaitForLastTask();

  213.         RTC_UpdateCalendar();
  214.     }
  215.     else
  216.     {
  217.         printf("\r\nError Date & Time!!!\r\n");
  218.     }
  219. }

  220. /***********************************************************************************************************************
  221.   * @brief
  222.   * @note   none
  223.   * @param  none
  224.   * @retval none
  225.   *********************************************************************************************************************/
  226. void RTC_PrintDateTime(void)
  227. {
  228.     printf("\r\n%04d-%02d-%02d", RTC_Calendar.year, RTC_Calendar.month, RTC_Calendar.day);

  229.     switch (RTC_Calendar.week)
  230.     {
  231.         case 0:
  232.             printf(" SUN ");
  233.             break;

  234.         case 1:
  235.             printf(" MON ");
  236.             break;

  237.         case 2:
  238.             printf(" TUE ");
  239.             break;

  240.         case 3:
  241.             printf(" WED ");
  242.             break;

  243.         case 4:
  244.             printf(" THU ");
  245.             break;

  246.         case 5:
  247.             printf(" FRI ");
  248.             break;

  249.         case 6:
  250.             printf(" SAT ");
  251.             break;

  252.         default:
  253.             break;
  254.     }

  255.     printf("---%02d:%02d:%02d\r\n", RTC_Calendar.hour, RTC_Calendar.minute, RTC_Calendar.second);
  256. }

  257. /***********************************************************************************************************************
  258.   * @brief
  259.   * @note   none
  260.   * @param  none
  261.   * @retval none
  262.   *********************************************************************************************************************/
  263. void RTC_LoadDefault(void)
  264. {
  265.     char    Date[20], Time[20];
  266.     char    Text[6][5];
  267.     uint8_t i = 0, Index = 0, Month = 0;
  268.     char *MonthTable[12] =
  269.     {
  270.         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  271.         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  272.     };
  273.     char *str;

  274.     memset(Date, 0, sizeof(Date));
  275.     memset(Time, 0, sizeof(Time));
  276.     memset(Text, 0, sizeof(Text));

  277.     memcpy(Date, __DATE__, sizeof(__DATE__));
  278.     memcpy(Time, __TIME__, sizeof(__TIME__));

  279.     str = strtok(Date, " ");

  280.     while (str != NULL)
  281.     {
  282.         memcpy(Text[Index++], str, strlen(str));

  283.         str = strtok(NULL, " ");
  284.     }

  285.     str = strtok(Time, ":");

  286.     while (str != NULL)
  287.     {
  288.         memcpy(Text[Index++], str, strlen(str));

  289.         str = strtok(NULL, ":");
  290.     }

  291.     for (i = 0; i < 12; i++)
  292.     {
  293.         if (0 == strcmp(Text[0], MonthTable[i]))
  294.         {
  295.             Month = i + 1;
  296.         }
  297.     }

  298.     RTC_Calendar.day    = atoi(Text[1]);
  299.     RTC_Calendar.month  = Month;
  300.     RTC_Calendar.year   = atoi(Text[2]);

  301.     RTC_Calendar.hour   = atoi(Text[3]);
  302.     RTC_Calendar.minute = atoi(Text[4]);
  303.     RTC_Calendar.second = atoi(Text[5]);

  304.     RTC_SetDateTime(RTC_Calendar.year, RTC_Calendar.month, RTC_Calendar.day, RTC_Calendar.hour, RTC_Calendar.minute, RTC_Calendar.second);
  305. }

  306. /***********************************************************************************************************************
  307.   * @brief
  308.   * @note   none
  309.   * @param  none
  310.   * @retval none
  311.   *********************************************************************************************************************/
  312. void RTC_Configure(void)
  313. {
  314.     NVIC_InitTypeDef NVIC_InitStruct;

  315.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_RTC | RCC_APB1Periph_BKP, ENABLE);

  316.     PWR_BackupAccessCmd(ENABLE);

  317.     BKP_DeInit();

  318.     if (BKP_ReadBackupRegister(BKP_DR1) != 0x5B5B)
  319.     {
  320.         RCC_LSEConfig(RCC_LSE_ON);

  321.         while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  322.         {
  323.         }

  324.         RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

  325.         RCC_RTCCLKCmd(ENABLE);

  326.         RTC_WaitForSynchro();
  327.         RTC_WaitForLastTask();

  328.         RTC_ITConfig(RTC_IT_SEC, ENABLE);
  329.         RTC_WaitForLastTask();

  330.         RTC_SetPrescaler(32767);
  331.         RTC_WaitForLastTask();

  332.         printf("\r\n%s", __FUNCTION__);

  333.         BKP_WriteBackupRegister(BKP_DR1, 0x5B5B);
  334.     }
  335.     else
  336.     {
  337.         printf("\r\nNeed't to configure RTC.");

  338.         RTC_WaitForSynchro();

  339.         RTC_ITConfig(RTC_IT_SEC, ENABLE);
  340.         RTC_WaitForLastTask();
  341.     }

  342.     NVIC_InitStruct.NVIC_IRQChannel = RTC_BKP_IRQn;
  343.     NVIC_InitStruct.NVIC_IRQChannelPriority = 0;
  344.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  345.     NVIC_Init(&NVIC_InitStruct);

  346.     RTC_LoadDefault();
  347. }

  348. /***********************************************************************************************************************
  349.   * @brief
  350.   * @note   none
  351.   * @param  none
  352.   * @retval none
  353.   *********************************************************************************************************************/
  354. void RTC_Calendar_Sample(void)
  355. {
  356.     printf("\r\nTest %s", __FUNCTION__);

  357.     RTC_Configure();

  358.     while (1)
  359.     {
  360.         RTC_PrintDateTime();
  361.         PLATFORM_LED_Toggle(LED1);
  362.         PLATFORM_DelayMS(1000);
  363.     }
  364. }


运行效果
clipboard.png


5、I2C主模式实验,写入“Hello 21ic, Hello MM32F0121"字符串并打印对应读取的数据内容
电路原理图中,IIC连接的是一个at3224的EEPROM芯片
1750748743438.png


代码 :
  1. /***********************************************************************************************************************
  2.     @file    i2c_master_eeprom_polling.c
  3.     @author  FD Team
  4.     @date    12-Dec-2023
  5.     @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
  6.   **********************************************************************************************************************
  7.     @attention

  8.     <h2><center>© Copyright(c) <2023> <MindMotion></center></h2>

  9.       Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
  10.     following conditions are met:
  11.     1. Redistributions of source code must retain the above copyright notice,
  12.        this list of conditions and the following disclaimer.
  13.     2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
  14.        the following disclaimer in the documentation and/or other materials provided with the distribution.
  15.     3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
  16.        promote products derived from this software without specific prior written permission.

  17.       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  18.     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19.     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20.     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21.     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22.     WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23.     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.   *********************************************************************************************************************/

  25. /* Define to prevent recursive inclusion */
  26. #define _I2C_MASTER_EEPROM_POLLING_C_

  27. /* Files include */
  28. #include <stdio.h>
  29. #include "platform.h"
  30. #include "i2c_master_eeprom_polling.h"

  31. /**
  32.   * @addtogroup MM32F0120_LibSamples
  33.   * @{
  34.   */

  35. /**
  36.   * @addtogroup I2C
  37.   * @{
  38.   */

  39. /**
  40.   * @addtogroup I2C_Master_EEPROM_Polling
  41.   * @{
  42.   */

  43. /* Private typedef ****************************************************************************************************/

  44. /* Private define *****************************************************************************************************/

  45. /* Private macro ******************************************************************************************************/
  46. #define EEPROM_I2C_ADDRESS      0xA0
  47. #define EEPROM_PAGE_SIZE        0x08

  48. /* Private variables **************************************************************************************************/

  49. /* Private functions **************************************************************************************************/

  50. /***********************************************************************************************************************
  51.   * @brief
  52.   * @note   none
  53.   * @param  none
  54.   * @retval none
  55.   *********************************************************************************************************************/
  56. void I2C_Configure(void)
  57. {
  58.     GPIO_InitTypeDef GPIO_InitStruct;
  59.     I2C_InitTypeDef  I2C_InitStruct;

  60.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

  61.     I2C_DeInit(I2C1);

  62.     I2C_StructInit(&I2C_InitStruct);
  63.     I2C_InitStruct.I2C_Mode       = I2C_MODE_MASTER;
  64.     I2C_InitStruct.I2C_OwnAddress = I2C_OWN_ADDRESS;
  65.     I2C_InitStruct.I2C_ClockSpeed = 100000;
  66.     I2C_Init(I2C1, &I2C_InitStruct);

  67.     I2C_TargetAddressConfig(I2C1, EEPROM_I2C_ADDRESS);

  68.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

  69.     GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_1);
  70.     GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_1);

  71.     GPIO_StructInit(&GPIO_InitStruct);
  72.     GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_10 | GPIO_Pin_11;
  73.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
  74.     GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AF_OD;
  75.     GPIO_Init(GPIOB, &GPIO_InitStruct);

  76.     I2C_Cmd(I2C1, ENABLE);
  77. }

  78. /***********************************************************************************************************************
  79.   * @brief
  80.   * @note   none
  81.   * @param  none
  82.   * @retval none
  83.   *********************************************************************************************************************/
  84. void I2C_TxData_Polling(uint8_t *Buffer, uint8_t Length)
  85. {
  86.     uint8_t i = 0;

  87.     for (i = 0; i < Length; i++)
  88.     {
  89.         I2C_SendData(I2C1, Buffer[i]);

  90.         while (RESET == I2C_GetFlagStatus(I2C1, I2C_STATUS_FLAG_TFE))
  91.         {
  92.         }
  93.     }
  94. }

  95. /***********************************************************************************************************************
  96.   * @brief
  97.   * @note   none
  98.   * @param  none
  99.   * @retval none
  100.   *********************************************************************************************************************/
  101. void I2C_RxData_Polling(uint8_t *Buffer, uint16_t Length)
  102. {
  103.     uint8_t i = 0;

  104.     for (i = 0; i < Length; i++)
  105.     {
  106.         I2C_ReadCmd(I2C1);

  107.         while (RESET == I2C_GetFlagStatus(I2C1, I2C_STATUS_FLAG_RFNE))
  108.         {
  109.         }

  110.         Buffer[i] = I2C_ReceiveData(I2C1);
  111.     }
  112. }

  113. /***********************************************************************************************************************
  114.   * @brief
  115.   * @note   none
  116.   * @param  none
  117.   * @retval none
  118.   *********************************************************************************************************************/
  119. void EEPROM_WritePage(uint8_t Address, uint8_t *Buffer, uint8_t Length)
  120. {
  121.     I2C_TxData_Polling((uint8_t *)&Address, 0x01);

  122.     I2C_TxData_Polling((uint8_t *)Buffer, Length);

  123.     while (RESET == I2C_GetFlagStatus(I2C1, I2C_STATUS_FLAG_TFE))
  124.     {
  125.     }

  126.     I2C_GenerateSTOP(I2C1);

  127.     while (RESET == I2C_GetFlagStatus(I2C1, I2C_STATUS_FLAG_TFE))
  128.     {
  129.     }
  130. }

  131. /***********************************************************************************************************************
  132.   * @brief
  133.   * @note   none
  134.   * @param  none
  135.   * @retval none
  136.   *********************************************************************************************************************/
  137. void EEPROM_ReadData(uint8_t Address, uint8_t *Buffer, uint8_t Length)
  138. {
  139.     I2C_TxData_Polling((uint8_t *)&Address, 0x01);

  140.     I2C_RxData_Polling((uint8_t *)Buffer, Length);

  141.     I2C_GenerateSTOP(I2C1);

  142.     while (RESET == I2C_GetFlagStatus(I2C1, I2C_STATUS_FLAG_TFE))
  143.     {
  144.     }
  145. }

  146. /***********************************************************************************************************************
  147.   * @brief
  148.   * @note   none
  149.   * @param  none
  150.   * @retval none
  151.   *********************************************************************************************************************/
  152. void EEPROM_WriteData(uint8_t Address, uint8_t *Buffer, uint8_t Length)
  153. {
  154.     uint8_t Start = 0;
  155.     uint8_t StartCount = 0, PageNumber = 0, FinalCount = 0;

  156.     if ((Address % EEPROM_PAGE_SIZE) == 0)
  157.     {
  158.         StartCount = 0;
  159.         PageNumber = Length / EEPROM_PAGE_SIZE;
  160.         FinalCount = Length % EEPROM_PAGE_SIZE;
  161.     }
  162.     else
  163.     {
  164.         Start = Address % EEPROM_PAGE_SIZE;

  165.         if (((Start + Length) / EEPROM_PAGE_SIZE) == 0)
  166.         {
  167.             StartCount = Length;
  168.             PageNumber = 0;
  169.             FinalCount = 0;
  170.         }
  171.         else
  172.         {
  173.             StartCount = EEPROM_PAGE_SIZE - Start;
  174.             PageNumber = (Length - StartCount) / EEPROM_PAGE_SIZE;
  175.             FinalCount = (Length - StartCount) % EEPROM_PAGE_SIZE;
  176.         }
  177.     }

  178.     if (StartCount)
  179.     {
  180.         EEPROM_WritePage(Address, Buffer, StartCount);

  181.         Address += StartCount;
  182.         Buffer  += StartCount;

  183.         PLATFORM_DelayMS(50);
  184.     }

  185.     while (PageNumber--)
  186.     {
  187.         EEPROM_WritePage(Address, Buffer, EEPROM_PAGE_SIZE);

  188.         Address += EEPROM_PAGE_SIZE;
  189.         Buffer  += EEPROM_PAGE_SIZE;

  190.         PLATFORM_DelayMS(50);
  191.     }

  192.     if (FinalCount)
  193.     {
  194.         EEPROM_WritePage(Address, Buffer, FinalCount);
  195.     }
  196. }

  197. /***********************************************************************************************************************
  198.   * @brief
  199.   * @note   none
  200.   * @param  none
  201.   * @retval none
  202.   *********************************************************************************************************************/
  203. void I2C_Master_EEPROM_Polling_Sample(void)
  204. {
  205.     uint8_t i = 0;
  206.     uint8_t ReadBuffer[50];
  207.                 uint8_t WriteBuffer[]="Hello 21ic, Hello MM32F0121";
  208.     printf("\r\nTest %s", __FUNCTION__);

  209.     I2C_Configure();

  210.     /*for (i = 0; i < 20; i++)
  211.     {
  212.         ReadBuffer[i]  = 0;
  213.         WriteBuffer[i] = i + 0x30;
  214.     }*/

  215.     printf("\r\n\r\nEEPROM Write : ");

  216.     EEPROM_WriteData(0, WriteBuffer, sizeof(WriteBuffer));

  217.     printf("OK");

  218.     printf("\r\n\r\nEEPROM Read  : \r\n");

  219.     EEPROM_ReadData(0, ReadBuffer, sizeof(WriteBuffer));

  220.     for (i = 0; i < 20; i++)
  221.     {
  222.         printf("%c ", ReadBuffer[i]);

  223.         if (0 == ((i + 1) % 10))
  224.         {
  225.             printf("\r\n");
  226.         }
  227.     }

  228.     while (1)
  229.     {
  230.         PLATFORM_LED_Toggle(LED1);
  231.         PLATFORM_DelayMS(100);
  232.     }
  233. }


运行效果:
1750749115223.png


6、SPI主模式实验,打印写入“Hello 21ic, Hello MM32F0121"

开发板上有一片SPI Flash


1750748755344.png


  1. void SPI_Master_FLASH_Polling_Sample(void)
  2. {
  3.     uint8_t i = 0;
  4.     uint8_t  ReadBuffer[100];
  5.                 uint8_t WriteBuffer[]="Hello 21ic, Hello MM32F0121";
  6.     printf("\r\nTest %s", __FUNCTION__);

  7.     SPI_Configure();

  8.     SPI_FLASH_ReadDeviceID();

  9.     SPI_FLASH_ReadJEDEC_ID();

  10.     printf("\r\nSPI FLASH Sector Erase...");

  11.     SPI_FLASH_SectorErase(0);

  12.     printf("\r\nSPI FLASH Read...");

  13.     SPI_FLASH_FastRead(0, ReadBuffer, 100);

  14.     for (i = 0; i < 100; i++)
  15.     {
  16.         if (0 == (i % 10))
  17.         {
  18.             printf("\r\n");
  19.         }

  20.         printf("0x%02x ", ReadBuffer[i]);
  21.     }

  22.     printf("\r\nSPI FLASH Page Program...");

  23.    

  24.     SPI_FLASH_PageProgram(0, WriteBuffer, sizeof(WriteBuffer));

  25.     printf("\r\nSPI FLASH Read...");

  26.     SPI_FLASH_FastRead(0, ReadBuffer, sizeof(WriteBuffer));

  27.     for (i = 0; i < sizeof(WriteBuffer); i++)
  28.     {
  29.         if (0 == (i % 10))
  30.         {
  31.             printf("\r\n");
  32.         }

  33.         printf("%c ", ReadBuffer[i]);
  34.     }

  35.     while (1)
  36.     {
  37.         PLATFORM_LED_Toggle(LED1);
  38.         PLATFORM_DelayMS(100);
  39.     }
  40. }
1750749315679.png



7、ADC采样实验

原理图中ADC引脚为PA3,连接到一个电位器。

1750749518419.png


这里使用ADC_AnyChannel_ContinuousScan_Polling_Sample例程;
运行后,串口输出结果为ADC转换值计算的电压值。
1750749621219.png



AdaMaYun 发表于 2025-7-31 17:48 | 显示全部楼层
基础操作
您需要登录后才可以回帖 登录 | 注册

本版积分规则

148

主题

1411

帖子

12

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