欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5747|回復: 1
收起左側

stm32使用IO口實現模擬IIC接口源程序

[復制鏈接]
ID:303917 發(fā)表于 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. * 函數描述  : 設置系統(tǒng)各部分時鐘
  39. * 輸入參數  : 無
  40. * 輸出結果  : 無
  41. * 返回值    : 無
  42. *******************************************************************************/
  43. void RccInitialisation(void)
  44. {
  45.         /* 定義枚舉類型變量 HSEStartUpStatus */
  46.         ErrorStatus HSEStartUpStatus;

  47.           /* 復位系統(tǒng)時鐘設置*/
  48.           RCC_DeInit();
  49.           /* 開啟HSE*/
  50.           RCC_HSEConfig(RCC_HSE_ON);
  51.           /* 等待HSE起振并穩(wěn)定*/
  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.             /* 選擇鎖相環(huán)(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輸出穩(wěn)定 */
  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, 下載次數: 56)


回復

使用道具 舉報

ID:532509 發(fā)表于 2019-5-10 14:38 | 顯示全部樓層
謝謝分享。。。。。。。。。。。
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表