查看: 4182|回复: 3

基于GD32F190的I2C总线扫描

[复制链接]
  • TA的每日心情
    擦汗
    2022-3-22 20:01
  • 签到天数: 215 天

    连续签到: 2 天

    [LV.7]常住居民III

    发表于 2016-6-12 11:36:11 | 显示全部楼层 |阅读模式
    分享到:
    [GD32F190方案分享] 基于GD32F190I2C总线扫描
    因为挂在I2C总线的装置, 经常不确定该装置地址的I2C是甚么, 因此做了个I2C总线扫描程序!
    程序使用GD32F190I2C2 , PF6:SCL, PF7:SDA.
    I2C_P0.png
    扫描后由USB串口输出结果.
    I2C_P1.png
    主程序如下:
    1. /**
    2.   ******************************************************************************
    3.   * @file    main.c
    4.   * @author  MCU SD
    5.   * @version V1.0.0
    6.   * @date    12-Jun-2016
    7.   * @brief   The main function file.
    8.   ******************************************************************************
    9.   */

    10. /* Includes ------------------------------------------------------------------*/
    11. #include <stdio.h>
    12. #include "gd32f1x0.h"
    13. #include "SysTick.h"
    14. #include "gd32f1x0_eval.h"

    15. #ifdef __GNUC__
    16.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
    17.      set to 'Yes') calls __io_putchar() */
    18.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
    19. #else
    20.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
    21. #endif /* __GNUC__ */


    22. /* Private variables ---------------------------------------------------------*/
    23. #define  I2C_OWN_ADDRESS7  0x82
    24. #define  I2C2_Speed  100000 // 100KHz
    25. uint8_t  I2C_Count = 0; // 0~127 : number of I2C devise
    26. uint16_t I2C_Device_Address = 0x0; // (from 0x00 to 0x7F)<<1
    27. uint16_t I2C_TimeOut = 0xFFFF;
    28. uint16_t I2C_Time = 0;
    29. uint8_t  I2C_Found = 0; // 0:not found  1:found
    30. uint8_t  I2C_Found_Count = 0; // total found
    31.        
    32. uint8_t count;
    33.        
    34.        
    35. /* Private function prototypes -----------------------------------------------*/
    36. static void USART_Configuration(void);  
    37. void I2C2_GPIO_Configuration(void);
    38. void I2C2_Configuration(void);

    39. /* Private functions ---------------------------------------------------------*/


    40. /**
    41.   * @brief  Main program.
    42.   * @param  None
    43.   * @retval None
    44.   */
    45. int main(void)
    46. {   
    47.     /* SysTick Configuration*/
    48.     SysTick_Configuration();
    49.    
    50.     /* USART parameter Configuration */
    51.     USART_Configuration(); // 115200,8,n,1
    52.    
    53.     /* Config I2C2 GPIO */
    54.     I2C2_GPIO_Configuration(); // PF6(SCL) , PF7(SDA)
    55.    
    56.     /* I2C2 parameter Configuration */
    57.     I2C2_Configuration(); // I2C2, AF0
    58.    

    59.     /* GD32190R-EVAL-V1.1 Start up */
    60.         printf("\r\n The I2C Master : SCL(PF6) , SDA(PF7)  \n\r");
    61.     printf("\r\n The I2C Speed : %d \n\r", I2C2_Speed);
    62.    
    63.     // if(I2C_24C02_Test()==I2C_OK)
    64.                 for (I2C_Count=0; I2C_Count<128; I2C_Count++)
    65.     {
    66.                         I2C_Device_Address = I2C_Count << 1;

    67.                         /* Enable I2C2 */
    68.       I2C_Enable(I2C2,ENABLE);
    69.                         /* The software must wait until I2C Bus is idle */
    70.                         I2C_Time = 0;
    71.       while( I2C_GetBitState(I2C2,I2C_FLAG_I2CBSY) && (I2C_Time < I2C_TimeOut) ) {I2C_Time++;}
    72.                        
    73.                         /* Send a start condition to I2C bus */
    74.       I2C_StartOnBus_Enable(I2C2,ENABLE);
    75.       /* The software must wait until SBSEND bit is set */
    76.                         I2C_Time = 0;
    77.       while(!I2C_StateDetect(I2C2,I2C_PROGRAMMINGMODE_MASTER_SBSEND) && (I2C_Time < I2C_TimeOut) ) {I2C_Time++;}               
    78.                        
    79.       /* Send slave address to I2C bus */
    80.       I2C_AddressingDevice_7bit(I2C2,I2C_Device_Address,I2C_DIRECTION_TRANSMITTER);
    81.       /* The software must wait until ADDSEND bit is set*/
    82.                         I2C_Found = 0; // default : not found I2C Device
    83.                         I2C_Time = 0;
    84.       while( I2C_Time < I2C_TimeOut ) {
    85.                                 I2C_Time++;
    86.                                 if ( I2C_StateDetect(I2C2,I2C_PROGRAMMINGMODE_MASTER_TRANSMITTER_ADDSEND) ) // found I2C Device
    87.                                 {
    88.                                         I2C_Found = 1;
    89.                                         break; // break while()
    90.                                 }
    91.                         }               
    92.                        
    93.                         I2C_StopOnBus_Enable(I2C2,ENABLE);
    94.                         I2C_Time = 0;
    95.       while( (I2C2->CTLR1&0x0200) && (I2C_Time < I2C_TimeOut) ) {I2C_Time++;}
    96.                        
    97.                         if ( I2C_Found )
    98.                         {
    99.                                 printf("\r\n I2C Found : 0x%02X \n\r", I2C_Device_Address);
    100.                                 I2C_Found_Count++;
    101.                         }
    102.                         else
    103.                         {
    104.                                 // printf("\r\n I2C Not Found : 0x%02X \n\r", I2C_Device_Address);
    105.                         }
    106.                        
    107.     }
    108.                
    109.                 printf("\r\n Total Found I2C Device Number : %d \n\r", I2C_Found_Count);

    110.     while(1)
    111.     {
    112.       
    113.     }
    114. }


    115. /* Private functions ---------------------------------------------------------*/
    116. /**
    117.   * @brief  Retarget the C library printf function to the USART.
    118.   * @param  None
    119.   * @retval None
    120.   */
    121. PUTCHAR_PROTOTYPE
    122. {
    123.     /* Place your implementation of fputc here */
    124.     /* e.g. write a character to the USART */
    125.     USART_DataSend( EVAL_COM1 , (uint8_t) ch );

    126.     /* Loop until transmit data register is empty */
    127.     while (USART_GetBitState( EVAL_COM1 , USART_FLAG_TBE) == RESET)
    128.     {
    129.     }

    130.     return ch;
    131. }

    132. /**
    133.   * @brief  Configure USART1.
    134.   * @param  None
    135.   * @retval None
    136.   */
    137. void USART_Configuration(void)
    138. {
    139.     USART_InitPara USART_InitStructure;

    140.     USART_DeInit( EVAL_COM1 );

    141.     USART_InitStructure.USART_BRR                   = 115200;
    142.     USART_InitStructure.USART_WL                    = USART_WL_8B;
    143.     USART_InitStructure.USART_STBits                = USART_STBITS_1;
    144.     USART_InitStructure.USART_Parity                = USART_PARITY_RESET;
    145.     USART_InitStructure.USART_HardwareFlowControl   = USART_HARDWAREFLOWCONTROL_NONE;
    146.     USART_InitStructure.USART_RxorTx                = USART_RXORTX_RX | USART_RXORTX_TX;

    147.     GD_EVAL_COMInit(&USART_InitStructure);   
    148. }

    149. /**
    150.   * @brief  Cofigure the I2C2 GPIO.
    151.   * @param  None
    152.   * @retval None
    153.   */
    154. void I2C2_GPIO_Configuration(void)
    155. {
    156.     GPIO_InitPara GPIO_InitStructure;
    157.    
    158.         /* Enable GPIO clock */
    159.         RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOF, ENABLE );
    160.    
    161.     /* Configure I2C2 pins: SCL and SDA */
    162.     GPIO_InitStructure.GPIO_Pin =  GPIO_PIN_6 | GPIO_PIN_7; // SCL , SDA
    163.     GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF;
    164.     GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_OD;
    165.     GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL;
    166.     GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ ;
    167.     GPIO_Init(GPIOF, &GPIO_InitStructure) ;
    168.    
    169.     GPIO_PinAFConfig(GPIOF,GPIO_PINSOURCE6,GPIO_AF_0);
    170.     GPIO_PinAFConfig(GPIOF,GPIO_PINSOURCE7,GPIO_AF_0);
    171. }

    172. /**
    173.   * @brief  Cofigure the I2C2 interface.
    174.   * @param  None
    175.   * @retval None
    176.   */
    177. void I2C2_Configuration(void)
    178. {
    179.     I2C_InitPara I2C_InitStructure;
    180.    
    181.     I2C_DeInit(I2C2);
    182.    
    183.     /* Enable I2C2 APB1 clock */
    184.     RCC_APB1PeriphClock_Enable( RCC_APB1PERIPH_I2C2, ENABLE );
    185.    
    186.     /* I2C configuration */
    187.     I2C_InitStructure.I2C_Protocol        = I2C_PROTOCOL_I2C;
    188.     I2C_InitStructure.I2C_DutyCycle       = I2C_DUTYCYCLE_2;
    189.     I2C_InitStructure.I2C_BitRate         = I2C2_Speed;
    190.     I2C_InitStructure.I2C_AddressingMode  = I2C_ADDRESSING_MODE_7BIT;
    191.     I2C_InitStructure.I2C_DeviceAddress   = I2C_OWN_ADDRESS7;
    192.     I2C_Init(I2C2, &I2C_InitStructure);

    193.     /* Enable I2C2 */
    194.     I2C_Enable(I2C2, ENABLE );

    195.     /* Enable Acknowledge */
    196.     I2C_Acknowledge_Enable( I2C2 , ENABLE );
    197. }
    198. /******************* (C) COPYRIGHT 2016 GIGADEVICE *****END OF FILE****/
    复制代码
    I2C_scan_main.zip (2.2 KB, 下载次数: 16)
    回复

    使用道具 举报

    该用户从未签到

    发表于 2016-6-12 17:22:51 | 显示全部楼层
    真不错,学习学习啦!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2018-11-19 16:39
  • 签到天数: 2 天

    连续签到: 1 天

    [LV.1]初来乍到

    发表于 2016-6-29 09:09:57 | 显示全部楼层
    文章不错,将内容去经验频道一并发一下,可以有双重奖励哟http://jingyan.eeboard.com/
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2016-8-13 11:54:41 | 显示全部楼层
    糖悦之果飞 发表于 2016-6-29 09:09
    文章不错,将内容去经验频道一并发一下,可以有双重奖励哟http://jingyan.eeboard.com/ ...

    谢谢分享,现在到处划拉1x0的IIC 啊
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 23:22 , Processed in 0.135262 second(s), 22 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.