单片机源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f1xx_hal.h"
- /* USER CODE BEGIN Includes */
- /*!
- * @file main.c
- * @brief FDC2214测试代码 STM32 软件IIC
- *
- +--------------------------------------------+
- | |33
- | |
- | SCL +------------------> PB10 |
- | |
- | FDC2214 STM32 |
- | SDA +------------------> PB11 |
- | |
- | |
- | 3.3 +------------------> 3.3 |
- | |
- | |
- | SD +------------------> GND |
- | |
- | |
- | GND +------------------> GND |
- | |
- +--------------------------------------------+
- 程序基于
- 硬件 STM32F103ZET6
- 软件 HAL库,STM32CubeMX生成
- 使用软件IIC,FDC2214文件有详细注释。
-
- 自己焊的芯片如果读不出数据:
- 请检查:
- 1、SCL SDA是否接反
- 2、SD是否接地
- 3、芯片地址是否选择为0x2A(根据硬件短接选择)
- 4、检查晶振是否起振,示波器观察是否有40m Hz稳定正弦信号
-
- 其他问题可以在群内留言.
- */
- /*
- FDC2214 STM32驱动例程
- 软件IIC使用 P10 -> SCL PB11-> SDA 寄存器操作,可修改IO口
-
- */
- #include "FDC2214.h"
- #include "MyIIC.h"
- #include "string.h"
- #if 1
- #pragma import(__use_no_semihosting)
- //Standard library required support function
- struct __FILE
- {
- int handle;
- };
- FILE __stdout;
- // define _sys_exit()
- void _sys_exit(int x)
- {
- x = x;
- }
- //redifine fputc()
- int fputc(int ch, FILE *f)
- {
- while((USART1->SR&0X40)==0);//Loop send
- USART1->DR = (unsigned char ) ch;
- return ch;
- }
- #endif
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- I2C_HandleTypeDef hi2c1;
- UART_HandleTypeDef huart1;
- /* USER CODE BEGIN PV */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_USART1_UART_Init(void);
- static void MX_I2C1_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- unsigned char string[] = "OK";
- unsigned char buf[3];
- uint32_t date0, date1, date2, date3;
- uint16_t deviceID1 = 0;
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- *
- * @retval None
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- uint16_t data;
-
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
-
- MX_USART1_UART_Init();
- MX_I2C1_Init();
- /* USER CODE BEGIN 2 */
- FDC2214_Init();
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
-
- /* USER CODE BEGIN 3 */
- FDC2214_GetChannelData(FDC2214_Channel_0, &date0); //获取各个通道数据
- FDC2214_GetChannelData(FDC2214_Channel_1, &date1);
- FDC2214_GetChannelData(FDC2214_Channel_2, &date2);
- FDC2214_GetChannelData(FDC2214_Channel_3, &date3);
- printf("%d,%d,%d,%d\n",date0,date1,date2,date3);
- HAL_Delay(10);
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* I2C1 init function */
- static void MX_I2C1_Init(void)
- {
- hi2c1.Instance = I2C1;
- hi2c1.Init.ClockSpeed = 400000;
- hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
- hi2c1.Init.OwnAddress1 = 0;
- hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
- hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
- hi2c1.Init.OwnAddress2 = 0;
- hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
- hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
- if (HAL_I2C_Init(&hi2c1) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- }
- /* USART1 init function */
- static void MX_USART1_UART_Init(void)
- {
- huart1.Instance = USART1;
- huart1.Init.BaudRate = 115200;
- huart1.Init.WordLength = UART_WORDLENGTH_8B;
- huart1.Init.StopBits = UART_STOPBITS_1;
- huart1.Init.Parity = UART_PARITY_NONE;
- huart1.Init.Mode = UART_MODE_TX_RX;
- huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart1.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart1) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- }
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOB, I2C_SCL_Pin|I2C_SDA_Pin, GPIO_PIN_RESET);
- /*Configure GPIO pins : I2C_SCL_Pin I2C_SDA_Pin */
- GPIO_InitStruct.Pin = I2C_SCL_Pin|I2C_SDA_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param file: The file name as string.
- * @param line: The line in file as a number.
- * @retval None
- */
- void _Error_Handler(char *file, int line)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /**
- * @}
- */
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
- #ifndef __FDC2214_H
- #define __FDC2214_H
- #include "sys.h"
- //IO方向设置
- #define FDC_SDA_IN() {GPIOC->CRL&=0XFF0FFFFF;GPIOC->CRL|=8<<4*5;}
- #define FDC_SDA_OUT() {GPIOC->CRL&=0XFF0FFFFF;GPIOC->CRL|=3<<4*5;}
- //IO操作函数
- #define FDC_IIC_SCL PCout(4) //SCL
- #define FDC_IIC_SDA PCout(5) //输出SDA
- #define FDC_READ_SDA PCin(5) //输入SDA
- /*FDC2214 iic从地址
- *ADDR = L , I2C Address = 0x2A
- *ADDR = H , I2C Address = 0x2B*/
- #define FDC2214_ADDR 0x2A
- /*FDC2214各个寄存器地址*/
- #define DATA_CH0 0x00 //数据寄存器
- #define DATA_LSB_CH0 0x01
- #define DATA_CH1 0x02
- #define DATA_LSB_CH1 0x03
- #define DATA_CH2 0x04
- #define DATA_LSB_CH2 0x05
- #define DATA_CH3 0x06
- #define DATA_LSB_CH3 0x07
- #define RCOUNT_CH0 0x08 //
- #define RCOUNT_CH1 0x09
- #define RCOUNT_CH2 0x0A
- #define RCOUNT_CH3 0x0B
- //#define OFFSET_CH0 0x0C //FDC2114
- //#define OFFSET_CH1 0x0D
- //#define OFFSET_CH2 0x0E
- //#define OFFSET_CH3 0x0F
- #define SETTLECOUNT_CH0 0x10
- #define SETTLECOUNT_CH1 0x11
- #define SETTLECOUNT_CH2 0x12
- #define SETTLECOUNT_CH3 0x13
- #define CLOCK_DIVIDERS_C_CH0 0x14 //时钟分频
- #define CLOCK_DIVIDERS_C_CH1 0x15
- #define CLOCK_DIVIDERS_C_CH2 0x16
- #define CLOCK_DIVIDERS_C_CH3 0x17
- #define STATUS 0x18 //状态寄存器
- #define ERROR_CONFIG 0x19 //错误报告设置
- #define CONFIG 0x1A
- #define MUX_CONFIG 0x1B
- #define RESET_DEV 0x1C
- #define DRIVE_CURRENT_CH0 0x1E //电流驱动
- #define DRIVE_CURRENT_CH1 0x1F
- #define DRIVE_CURRENT_CH2 0x20
- #define DRIVE_CURRENT_CH3 0x21
- #define MANUFACTURER_ID 0x7E //读取值:0x5449
- #define DEVICE_ID 0x7F //读取值:0x3055
- //extern u16 Data_FDC;
- //相关函数申明
- u8 Set_FDC2214(u8 reg,u8 MSB,u8 LSB);
- u16 FDC_Read(u8 reg);
- //u16 FCD2214_ReadCH(u8 index);
- u32 FCD2214_ReadCH(u8 index);
- u8 FDC2214_Init(void);
- float Cap_Calculate(u8 index);
- #endif
复制代码
全部资料51hei下载地址:
FDC2214_STM32F103ZET6测试代码(软件IIC版本)+.rar
(9.06 MB, 下载次数: 59)
|