TA的每日心情 | 擦汗 2022-3-22 20:01 |
---|
签到天数: 215 天 连续签到: 2 天 [LV.7]常住居民III
|
[GD32F190方案分享]基于GD32F190的I2C总线扫描
因为挂在I2C总线的装置, 经常不确定该装置地址的I2C是甚么, 因此做了个I2C总线扫描程序!
程序使用GD32F190的I2C2 , PF6:SCL, PF7:SDA.
扫描后由USB串口输出结果.
主程序如下:
/** ****************************************************************************** * @file main.c * @author MCU SD * @version V1.0.0 * @date 12-Jun-2016 * @brief The main function file. ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/#include <stdio.h>#include "gd32f1x0.h"#include "SysTick.h"#include "gd32f1x0_eval.h"#ifdef __GNUC__ /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)#else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)#endif /* __GNUC__ *//* Private variables ---------------------------------------------------------*/#define I2C_OWN_ADDRESS7 0x82#define I2C2_Speed 100000 // 100KHzuint8_t I2C_Count = 0; // 0~127 : number of I2C deviseuint16_t I2C_Device_Address = 0x0; // (from 0x00 to 0x7F)<<1uint16_t I2C_TimeOut = 0xFFFF;uint16_t I2C_Time = 0;uint8_t I2C_Found = 0; // 0:not found 1:founduint8_t I2C_Found_Count = 0; // total found uint8_t count; /* Private function prototypes -----------------------------------------------*/static void USART_Configuration(void); void I2C2_GPIO_Configuration(void);void I2C2_Configuration(void);/* Private functions ---------------------------------------------------------*//** * @brief Main program. * @param None * @retval None */int main(void){ /* SysTick Configuration*/ SysTick_Configuration(); /* USART parameter Configuration */ USART_Configuration(); // 115200,8,n,1 /* Config I2C2 GPIO */ I2C2_GPIO_Configuration(); // PF6(SCL) , PF7(SDA) /* I2C2 parameter Configuration */ I2C2_Configuration(); // I2C2, AF0 /* GD32190R-EVAL-V1.1 Start up */ printf("\r\n The I2C Master : SCL(PF6) , SDA(PF7) \n\r"); printf("\r\n The I2C Speed : %d \n\r", I2C2_Speed); // if(I2C_24C02_Test()==I2C_OK) for (I2C_Count=0; I2C_Count<128; I2C_Count++) { I2C_Device_Address = I2C_Count << 1; /* Enable I2C2 */ I2C_Enable(I2C2,ENABLE); /* The software must wait until I2C Bus is idle */ I2C_Time = 0; while( I2C_GetBitState(I2C2,I2C_FLAG_I2CBSY) && (I2C_Time < I2C_TimeOut) ) {I2C_Time++;} /* Send a start condition to I2C bus */ I2C_StartOnBus_Enable(I2C2,ENABLE); /* The software must wait until SBSEND bit is set */ I2C_Time = 0; while(!I2C_StateDetect(I2C2,I2C_PROGRAMMINGMODE_MASTER_SBSEND) && (I2C_Time < I2C_TimeOut) ) {I2C_Time++;} /* Send slave address to I2C bus */ I2C_AddressingDevice_7bit(I2C2,I2C_Device_Address,I2C_DIRECTION_TRANSMITTER); /* The software must wait until ADDSEND bit is set*/ I2C_Found = 0; // default : not found I2C Device I2C_Time = 0; while( I2C_Time < I2C_TimeOut ) { I2C_Time++; if ( I2C_StateDetect(I2C2,I2C_PROGRAMMINGMODE_MASTER_TRANSMITTER_ADDSEND) ) // found I2C Device { I2C_Found = 1; break; // break while() } } I2C_StopOnBus_Enable(I2C2,ENABLE); I2C_Time = 0; while( (I2C2->CTLR1&0x0200) && (I2C_Time < I2C_TimeOut) ) {I2C_Time++;} if ( I2C_Found ) { printf("\r\n I2C Found : 0xX \n\r", I2C_Device_Address); I2C_Found_Count++; } else { // printf("\r\n I2C Not Found : 0xX \n\r", I2C_Device_Address); } } printf("\r\n Total Found I2C Device Number : %d \n\r", I2C_Found_Count); while(1) { }}/* Private functions ---------------------------------------------------------*//** * @brief Retarget the C library printf function to the USART. * @param None * @retval None */PUTCHAR_PROTOTYPE{ /* Place your implementation of fputc here */ /* e.g. write a character to the USART */ USART_DataSend( EVAL_COM1 , (uint8_t) ch ); /* Loop until transmit data register is empty */ while (USART_GetBitState( EVAL_COM1 , USART_FLAG_TBE) == RESET) { } return ch;}/** * @brief Configure USART1. * @param None * @retval None */void USART_Configuration(void){ USART_InitPara USART_InitStructure; USART_DeInit( EVAL_COM1 ); USART_InitStructure.USART_BRR = 115200; USART_InitStructure.USART_WL = USART_WL_8B; USART_InitStructure.USART_STBits = USART_STBITS_1; USART_InitStructure.USART_Parity = USART_PARITY_RESET; USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE; USART_InitStructure.USART_RxorTx = USART_RXORTX_RX | USART_RXORTX_TX; GD_EVAL_COMInit(&USART_InitStructure); }/** * @brief Cofigure the I2C2 GPIO. * @param None * @retval None */void I2C2_GPIO_Configuration(void){ GPIO_InitPara GPIO_InitStructure; /* Enable GPIO clock */ RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOF, ENABLE ); /* Configure I2C2 pins: SCL and SDA */ GPIO_InitStructure.GPIO_Pin = GPIO_PIN_6 | GPIO_PIN_7; // SCL , SDA GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF; GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ ; GPIO_Init(GPIOF, &GPIO_InitStructure) ; GPIO_PinAFConfig(GPIOF,GPIO_PINSOURCE6,GPIO_AF_0); GPIO_PinAFConfig(GPIOF,GPIO_PINSOURCE7,GPIO_AF_0);}/** * @brief Cofigure the I2C2 interface. * @param None * @retval None */void I2C2_Configuration(void){ I2C_InitPara I2C_InitStructure; I2C_DeInit(I2C2); /* Enable I2C2 APB1 clock */ RCC_APB1PeriphClock_Enable( RCC_APB1PERIPH_I2C2, ENABLE ); /* I2C configuration */ I2C_InitStructure.I2C_Protocol = I2C_PROTOCOL_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DUTYCYCLE_2; I2C_InitStructure.I2C_BitRate = I2C2_Speed; I2C_InitStructure.I2C_AddressingMode = I2C_ADDRESSING_MODE_7BIT; I2C_InitStructure.I2C_DeviceAddress = I2C_OWN_ADDRESS7; I2C_Init(I2C2, &I2C_InitStructure); /* Enable I2C2 */ I2C_Enable(I2C2, ENABLE ); /* Enable Acknowledge */ I2C_Acknowledge_Enable( I2C2 , ENABLE );}/******************* (C) COPYRIGHT 2016 GIGADEVICE *****END OF FILE****/https://www.eeboard.com/bbs/forum.php?mod=viewthrea... |
|