找回密码
 立即注册

QQ登录

只需一步,快速开始

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

stm32使用IO口实现模拟IIC接口源程序

[复制链接]
跳转到指定楼层
楼主
ID:303917 发表于 2018-4-8 21:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这是在stm32上使用io口模拟实现i2c的功能,
希望能帮助大家

单片机源程序如下:
  1. #include "stm32f10x_lib.h"
  2. #include "I2C_Driver.h"
  3. #include "stdio.h"

  4. /* Private define ------------------------------------------------------------*/
  5. /* Private macro -------------------------------------------------------------*/
  6. /* Private variables ---------------------------------------------------------*/

  7. u8 I2cVal = 0xaa;

  8. /* Private Function ---------------------------------------------------------*/
  9. void RccInitialisation(void);
  10. void GpioInitialisation(void);
  11. void Systick_Delay_1ms(u32 nCount);
  12. void UartInitialisation(void);

  13. int main(void)
  14. {
  15.         RccInitialisation();
  16.         GpioInitialisation();
  17.         UartInitialisation();
  18.         I2C_GPIO_Config();
  19.         
  20.         printf("\r\nStart IIC test\r\n");
  21.         I2C_WriteByte(I2cVal, 0x0000, 0xa0);
  22.         printf("\r\nThe Simulation_IIC has sended data:%d\r\n", I2cVal);
  23.         for(vu32 i = 0; i < 20000; i++);
  24.          I2C_ReadByte(&I2cVal, 1, 0x00 ,0xa0);
  25.         printf("\r\nThe Simulation_IIC has received data:%d\r\n", I2cVal);
  26.         if(I2cVal == 0xaa)
  27.         {
  28.                 printf("\r\nIIC test success\r\n");
  29.         }
  30.         else
  31.         {
  32.                 printf("\r\nIIC test fail\r\n");
  33.         }
  34.         while(1);
  35. }


  36. /*******************************************************************************
  37. * 函数名        : RCC_Configuration
  38. * 函数描述  : 设置系统各部分时钟
  39. * 输入参数  : 无
  40. * 输出结果  : 无
  41. * 返回值    : 无
  42. *******************************************************************************/
  43. void RccInitialisation(void)
  44. {
  45.         /* 定义枚举类型变量 HSEStartUpStatus */
  46.         ErrorStatus HSEStartUpStatus;

  47.           /* 复位系统时钟设置*/
  48.           RCC_DeInit();
  49.           /* 开启HSE*/
  50.           RCC_HSEConfig(RCC_HSE_ON);
  51.           /* 等待HSE起振并稳定*/
  52.           HSEStartUpStatus = RCC_WaitForHSEStartUp();
  53.         /* 判断HSE起是否振成功,是则进入if()内部 */
  54.           if(HSEStartUpStatus == SUCCESS)
  55.           {
  56.             /* 选择HCLK(AHB)时钟源为SYSCLK 1分频 */
  57.             RCC_HCLKConfig(RCC_SYSCLK_Div1);
  58.             /* 选择PCLK2时钟源为 HCLK(AHB) 1分频 */
  59.             RCC_PCLK2Config(RCC_HCLK_Div1);
  60.             /* 选择PCLK1时钟源为 HCLK(AHB) 2分频 */
  61.             RCC_PCLK1Config(RCC_HCLK_Div2);
  62.             /* 设置FLASH延时周期数为2 */
  63.             FLASH_SetLatency(FLASH_Latency_2);
  64.             /* 使能FLASH预取缓存 */
  65.             FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  66.             /* 选择锁相环(PLL)时钟源为HSE 1分频,倍频数为9,则PLL输出频率为 8MHz * 9 = 72MHz */
  67.             RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  68.             /* 使能PLL */
  69.             RCC_PLLCmd(ENABLE);
  70.             /* 等待PLL输出稳定 */
  71.             while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  72.             /* 选择SYSCLK时钟源为PLL */
  73.             RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  74.             /* 等待PLL成为SYSCLK时钟源 */
  75.             while(RCC_GetSYSCLKSource() != 0x08);
  76.           }
  77.           /* 打开APB2总线上的GPIOA时钟*/
  78.           RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA |RCC_APB2Periph_USART1, ENABLE);
  79. }


  80. void Systick_Delay_1ms(u32 nCount)
  81. {
  82.   u32 TimingDelay = nCount * 10000;
  83.   
  84.   while(TimingDelay)
  85.   {
  86.          TimingDelay--;
  87.   }
  88. }

  89. /*******************************************************************************
  90. * 函数名                  : GPIO_Configuration
  91. * 函数描述            : 设置各GPIO端口功能
  92. * 输入参数      : 无
  93. * 输出结果      : 无
  94. * 返回值        : 无
  95. *******************************************************************************/

  96. void GpioInitialisation(void)
  97. {
  98.         /* 定义GPIO初始化结构体 GPIO_InitStructure */
  99.           GPIO_InitTypeDef GPIO_InitStructure;

  100.           /* 设置USART1的Tx脚(PA.9)为第二功能推挽输出功能 */
  101.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  102.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  103.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  104.           GPIO_Init(GPIOA , &GPIO_InitStructure);
  105.    
  106.           /* 设置USART1的Rx脚(PA.10)为浮空输入脚 */
  107.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  108.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  109.           GPIO_Init(GPIOA , &GPIO_InitStructure);
  110. }

  111. /*******************************************************************************
  112. * 函数名                  : USART_Configuration
  113. * 函数描述            : 设置USART1
  114. * 输入参数      : None
  115. * 输出结果      : None
  116. * 返回值        : None
  117. *******************************************************************************/

  118. void UartInitialisation(void)
  119. {
  120.         /* 定义USART初始化结构体 USART_InitStructure */
  121.           USART_InitTypeDef USART_InitStructure;
  122.         /* 定义USART初始化结构体 USART_ClockInitStructure */
  123.           USART_ClockInitTypeDef  USART_ClockInitStructure;

  124.         /*        波特率为115200bps;
  125.         *        8位数据长度;
  126.         *        1个停止位,无校验;
  127.         *        禁用硬件流控制;
  128.         *        禁止USART时钟;
  129.         *        时钟极性低;
  130.         *        在第2个边沿捕获数据
  131.         *        最后一位数据的时钟脉冲不从 SCLK 输出;
  132.         */

  133.         USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
  134.         USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
  135.         USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
  136.         USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
  137.         USART_ClockInit(USART1 , &USART_ClockInitStructure);

  138.         USART_InitStructure.USART_BaudRate = 115200;
  139.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  140.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  141.         USART_InitStructure.USART_Parity = USART_Parity_No ;
  142.         USART_InitStructure.USART_HardwareFlowControl =  USART_HardwareFlowControl_None;
  143.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  144.         USART_Init(USART1 , &USART_InitStructure);
  145.    
  146.           /* 使能USART1 */
  147.           USART_Cmd(USART1 , ENABLE);
  148. }


  149. int fputc(int ch, FILE *f)
  150. {
  151.         USART_SendData(USART1, (u8)ch);
  152.         while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  153.         return ch;
  154. }
复制代码

所有资料51hei提供下载:
进阶十 使用IO口实现模拟IIC接口.zip (511.57 KB, 下载次数: 55)


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

使用道具 举报

沙发
ID:532509 发表于 2019-5-10 14:38 | 只看该作者
谢谢分享!!!!!!!!!!!!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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