找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4558|回复: 1
打印 上一主题 下一主题
收起左侧

基于FreeRTOS的模拟IIC数据保存源码

[复制链接]
跳转到指定楼层
楼主
ID:447772 发表于 2018-12-17 10:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
自己做的基于STM32CubeMX+FreeRTOS+模拟IIC-EEPROM数据保存例程。亲测可用

单片机源程序如下:
  1. /* USER CODE BEGIN Header */
  2. /* USER CODE END Header */

  3. /* Includes ------------------------------------------------------------------*/
  4. #include "main.h"

  5. /* Private includes ----------------------------------------------------------*/
  6. /* USER CODE BEGIN Includes */
  7. #include "iic_eeprom.h"
  8. /* USER CODE END Includes */

  9. /* Private typedef -----------------------------------------------------------*/
  10. /* USER CODE BEGIN PTD */

  11. /* USER CODE END PTD */

  12. /* Private define ------------------------------------------------------------*/
  13. /* USER CODE BEGIN PD */

  14. /* USER CODE END PD */

  15. /* Private macro -------------------------------------------------------------*/
  16. /* USER CODE BEGIN PM */

  17. /* USER CODE END PM */

  18. /* Private variables ---------------------------------------------------------*/
  19. UART_HandleTypeDef huart1;

  20. /* USER CODE BEGIN PV */
  21. uint8_t tx_buf[500]={0};

  22. uint8_t iic_read[255]  = {0};
  23. uint8_t iic_write[255] = {0};

  24. /* USER CODE END PV */

  25. /* Private function prototypes -----------------------------------------------*/
  26. void SystemClock_Config(void);
  27. static void MX_GPIO_Init(void);
  28. static void MX_USART1_UART_Init(void);
  29. /* USER CODE BEGIN PFP */

  30. /* USER CODE END PFP */

  31. /* Private user code ---------------------------------------------------------*/
  32. /* USER CODE BEGIN 0 */

  33. /* USER CODE END 0 */

  34. /**
  35.   * @brief  The application entry point.
  36.   * @retval int
  37.   */
  38. int main(void)
  39. {
  40.   /* USER CODE BEGIN 1 */
  41.         uint8_t flag;
  42.         uint8_t i;
  43.   /* USER CODE END 1 */

  44.   /* MCU Configuration--------------------------------------------------------*/

  45.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  46.   HAL_Init();

  47.   /* USER CODE BEGIN Init */

  48.   /* USER CODE END Init */

  49.   /* Configure the system clock */
  50.   SystemClock_Config();

  51.   /* USER CODE BEGIN SysInit */

  52.   /* USER CODE END SysInit */

  53.   /* Initialize all configured peripherals */
  54.   MX_GPIO_Init();
  55.   MX_USART1_UART_Init();
  56.   /* USER CODE BEGIN 2 */
  57.   flag = EEPROM_CheckOk();
  58.   if(flag == 0x01)
  59.   {
  60.           for(i=0;i<255;i++)
  61.           {
  62.                   iic_write[i] = i;
  63.                   iic_read[i] = 0;
  64.           }
  65.           HAL_UART_Transmit(&huart1,iic_write,100,200);
  66.           EEPROM_WriteBytes(iic_write,0,100);
  67.           HAL_Delay(100);
  68.           EEPROM_ReadBytes(iic_read, 0, 100);
  69.           HAL_UART_Transmit(&huart1,&iic_read[i],100,200);
  70.   }
  71.   /* USER CODE END 2 */

  72.   /* Infinite loop */
  73.   /* USER CODE BEGIN WHILE */
  74.   while (1)
  75.   {
  76.     /* USER CODE END WHILE */
  77.     /* USER CODE BEGIN 3 */
  78.   }
  79.   /* USER CODE END 3 */
  80. }

  81. /**
  82.   * @brief System Clock Configuration
  83.   * @retval None
  84.   */
  85. void SystemClock_Config(void)
  86. {
  87.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  88.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  89.   /**Initializes the CPU, AHB and APB busses clocks
  90.   */
  91.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  92.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  93.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  94.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  95.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  96.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  97.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  98.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  99.   {
  100.     Error_Handler();
  101.   }
  102.   /**Initializes the CPU, AHB and APB busses clocks
  103.   */
  104.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  105.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  106.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  107.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  108.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  109.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  110.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  111.   {
  112.     Error_Handler();
  113.   }
  114. }

  115. /**
  116.   * @brief USART1 Initialization Function
  117.   * @param None
  118.   * @retval None
  119.   */
  120. static void MX_USART1_UART_Init(void)
  121. {

  122.   /* USER CODE BEGIN USART1_Init 0 */

  123.   /* USER CODE END USART1_Init 0 */

  124.   /* USER CODE BEGIN USART1_Init 1 */

  125.   /* USER CODE END USART1_Init 1 */
  126.   huart1.Instance = USART1;
  127.   huart1.Init.BaudRate = 9600;
  128.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  129.   huart1.Init.StopBits = UART_STOPBITS_1;
  130.   huart1.Init.Parity = UART_PARITY_NONE;
  131.   huart1.Init.Mode = UART_MODE_TX_RX;
  132.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  133.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  134.   if (HAL_UART_Init(&huart1) != HAL_OK)
  135.   {
  136.     Error_Handler();
  137.   }
  138.   /* USER CODE BEGIN USART1_Init 2 */

  139.   /* USER CODE END USART1_Init 2 */

  140. }

  141. /**
  142.   * @brief GPIO Initialization Function
  143.   * @param None
  144.   * @retval None
  145.   */
  146. static void MX_GPIO_Init(void)
  147. {
  148.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  149.   /* GPIO Ports Clock Enable */
  150.   __HAL_RCC_GPIOA_CLK_ENABLE();
  151.   __HAL_RCC_GPIOB_CLK_ENABLE();

  152.   /*Configure GPIO pin Output Level */
  153.   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);

  154.   /*Configure GPIO pins : PB6 PB7 */
  155.   GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  156.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  157.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  158.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  159.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  160. }

  161. /* USER CODE BEGIN 4 */

  162. /* USER CODE END 4 */

  163. /**
  164.   * @brief  This function is executed in case of error occurrence.
  165.   * @retval None
  166.   */
  167. void Error_Handler(void)
  168. {
  169.   /* USER CODE BEGIN Error_Handler_Debug */
  170.   /* User can add his own implementation to report the HAL error return state */

  171.   /* USER CODE END Error_Handler_Debug */
  172. }

  173. #ifdef  USE_FULL_ASSERT
  174. /**
  175.   * @brief  Reports the name of the source file and the source line number
  176.   *         where the assert_param error has occurred.
  177.   * @param  file: pointer to the source file name
  178.   * @param  line: assert_param error line source number
  179.   * @retval None
  180.   */
  181. void assert_failed(uint8_t *file, uint32_t line)
  182. {
  183.   /* USER CODE BEGIN 6 */
  184.   /* User can add his own implementation to report the file name and line number,
  185.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  186.   /* USER CODE END 6 */
  187. }
  188. #endif /* USE_FULL_ASSERT */

  189. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
Simulate_IIC_EEPROM.7z (573.73 KB, 下载次数: 60)



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:141007 发表于 2020-11-24 00:36 | 只看该作者
有没有RTOS系统用的???
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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