找回密码
 立即注册

QQ登录

只需一步,快速开始

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

FDC2214的STM32测试源代码(软件IIC版本)

[复制链接]
跳转到指定楼层
楼主

单片机源程序如下:

  1. /* Includes ------------------------------------------------------------------*/
  2. #include "main.h"
  3. #include "stm32f1xx_hal.h"

  4. /* USER CODE BEGIN Includes */
  5. /*!
  6.   * @file       main.c
  7.   * @brief      FDC2214测试代码 STM32 软件IIC
  8.   *     
  9.                                         +--------------------------------------------+
  10.                                         |                                            |33
  11.                                         |                                            |
  12.                                         |         SCL +------------------> PB10      |
  13.                                         |                                            |
  14.                                         | FDC2214                              STM32 |
  15.                                         |         SDA +------------------> PB11      |
  16.                                         |                                            |
  17.                                         |                                            |
  18.                                         |         3.3 +------------------> 3.3       |
  19.                                         |                                            |
  20.                                         |                                            |
  21.                                         |          SD +------------------> GND       |
  22.                                         |                                                                                                                                                                                 |
  23.                                         |                                                                                                                                                                           |
  24.                                         |         GND +------------------> GND       |
  25.                                         |                                            |
  26.                                         +--------------------------------------------+
  27.                                         程序基于
  28.                                         硬件        STM32F103ZET6
  29.                                         软件  HAL库,STM32CubeMX生成
  30.                                         使用软件IIC,FDC2214文件有详细注释。
  31.                                        
  32.                                         自己焊的芯片如果读不出数据:
  33.                                         请检查:
  34.                                         1、SCL SDA是否接反
  35.                                         2、SD是否接地
  36.                                         3、芯片地址是否选择为0x2A(根据硬件短接选择)
  37.                                         4、检查晶振是否起振,示波器观察是否有40m Hz稳定正弦信号
  38.                                        
  39.                                         其他问题可以在群内留言.
  40.   */
  41. /*
  42.   FDC2214 STM32驱动例程  
  43.   软件IIC使用 P10 -> SCL  PB11-> SDA   寄存器操作,可修改IO口
  44.   
  45. */

  46. #include "FDC2214.h"
  47. #include "MyIIC.h"
  48. #include "string.h"
  49. #if 1
  50. #pragma import(__use_no_semihosting)            
  51. //Standard library required support function               
  52. struct __FILE
  53. {
  54.         int handle;
  55. };

  56. FILE __stdout;      
  57. // define _sys_exit()  
  58. void _sys_exit(int x)
  59. {
  60.         x = x;
  61. }
  62. //redifine fputc()
  63. int fputc(int ch, FILE *f)
  64. {         
  65.         while((USART1->SR&0X40)==0);//Loop send
  66.         USART1->DR = (unsigned char ) ch;      
  67.         return ch;
  68. }
  69. #endif
  70. /* USER CODE END Includes */

  71. /* Private variables ---------------------------------------------------------*/
  72. I2C_HandleTypeDef hi2c1;

  73. UART_HandleTypeDef huart1;

  74. /* USER CODE BEGIN PV */
  75. /* Private variables ---------------------------------------------------------*/

  76. /* USER CODE END PV */

  77. /* Private function prototypes -----------------------------------------------*/
  78. void SystemClock_Config(void);
  79. static void MX_GPIO_Init(void);
  80. static void MX_USART1_UART_Init(void);
  81. static void MX_I2C1_Init(void);

  82. /* USER CODE BEGIN PFP */
  83. /* Private function prototypes -----------------------------------------------*/
  84. unsigned char string[] = "OK";
  85. unsigned char buf[3];
  86. uint32_t date0, date1, date2, date3;
  87. uint16_t deviceID1 = 0;
  88. /* USER CODE END PFP */

  89. /* USER CODE BEGIN 0 */

  90. /* USER CODE END 0 */

  91. /**
  92.   * @brief  The application entry point.
  93.   *
  94.   * @retval None
  95.   */
  96. int main(void)
  97. {
  98.   /* USER CODE BEGIN 1 */
  99.         uint16_t data;
  100.         
  101.   /* USER CODE END 1 */

  102.   /* MCU Configuration----------------------------------------------------------*/

  103.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  104.   HAL_Init();

  105.   /* USER CODE BEGIN Init */

  106.   /* USER CODE END Init */

  107.   /* Configure the system clock */
  108.   SystemClock_Config();

  109.   /* USER CODE BEGIN SysInit */

  110.   /* USER CODE END SysInit */

  111.   /* Initialize all configured peripherals */
  112.   MX_GPIO_Init();
  113.         
  114.   MX_USART1_UART_Init();
  115.   MX_I2C1_Init();
  116.   /* USER CODE BEGIN 2 */
  117.         FDC2214_Init();

  118.   /* USER CODE END 2 */

  119.   /* Infinite loop */
  120.   /* USER CODE BEGIN WHILE */
  121.   while (1)
  122.   {

  123.   /* USER CODE END WHILE */
  124.         
  125.   /* USER CODE BEGIN 3 */
  126.                         FDC2214_GetChannelData(FDC2214_Channel_0, &date0);   //获取各个通道数据
  127.                         FDC2214_GetChannelData(FDC2214_Channel_1, &date1);
  128.                         FDC2214_GetChannelData(FDC2214_Channel_2, &date2);
  129.                         FDC2214_GetChannelData(FDC2214_Channel_3, &date3);

  130.                         printf("%d,%d,%d,%d\n",date0,date1,date2,date3);
  131.                         HAL_Delay(10);

  132.   }
  133.   /* USER CODE END 3 */

  134. }

  135. /**
  136.   * @brief System Clock Configuration
  137.   * @retval None
  138.   */
  139. void SystemClock_Config(void)
  140. {

  141.   RCC_OscInitTypeDef RCC_OscInitStruct;
  142.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  143.     /**Initializes the CPU, AHB and APB busses clocks
  144.     */
  145.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  146.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  147.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  148.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  149.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  150.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  151.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  152.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  153.   {
  154.     _Error_Handler(__FILE__, __LINE__);
  155.   }

  156.     /**Initializes the CPU, AHB and APB busses clocks
  157.     */
  158.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  159.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  160.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  161.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  162.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  163.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  164.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  165.   {
  166.     _Error_Handler(__FILE__, __LINE__);
  167.   }

  168.     /**Configure the Systick interrupt time
  169.     */
  170.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  171.     /**Configure the Systick
  172.     */
  173.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  174.   /* SysTick_IRQn interrupt configuration */
  175.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  176. }

  177. /* I2C1 init function */
  178. static void MX_I2C1_Init(void)
  179. {

  180.   hi2c1.Instance = I2C1;
  181.   hi2c1.Init.ClockSpeed = 400000;
  182.   hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  183.   hi2c1.Init.OwnAddress1 = 0;
  184.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  185.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  186.   hi2c1.Init.OwnAddress2 = 0;
  187.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  188.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  189.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  190.   {
  191.     _Error_Handler(__FILE__, __LINE__);
  192.   }

  193. }

  194. /* USART1 init function */
  195. static void MX_USART1_UART_Init(void)
  196. {

  197.   huart1.Instance = USART1;
  198.   huart1.Init.BaudRate = 115200;
  199.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  200.   huart1.Init.StopBits = UART_STOPBITS_1;
  201.   huart1.Init.Parity = UART_PARITY_NONE;
  202.   huart1.Init.Mode = UART_MODE_TX_RX;
  203.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  204.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  205.   if (HAL_UART_Init(&huart1) != HAL_OK)
  206.   {
  207.     _Error_Handler(__FILE__, __LINE__);
  208.   }

  209. }

  210. /** Configure pins as
  211.         * Analog
  212.         * Input
  213.         * Output
  214.         * EVENT_OUT
  215.         * EXTI
  216. */
  217. static void MX_GPIO_Init(void)
  218. {

  219.   GPIO_InitTypeDef GPIO_InitStruct;

  220.   /* GPIO Ports Clock Enable */
  221.   __HAL_RCC_GPIOB_CLK_ENABLE();
  222.   __HAL_RCC_GPIOA_CLK_ENABLE();

  223.   /*Configure GPIO pin Output Level */
  224.   HAL_GPIO_WritePin(GPIOB, I2C_SCL_Pin|I2C_SDA_Pin, GPIO_PIN_RESET);

  225.   /*Configure GPIO pins : I2C_SCL_Pin I2C_SDA_Pin */
  226.   GPIO_InitStruct.Pin = I2C_SCL_Pin|I2C_SDA_Pin;
  227.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  228.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  229.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  230.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  231. }

  232. /* USER CODE BEGIN 4 */


  233. /* USER CODE END 4 */

  234. /**
  235.   * @brief  This function is executed in case of error occurrence.
  236.   * @param  file: The file name as string.
  237.   * @param  line: The line in file as a number.
  238.   * @retval None
  239.   */
  240. void _Error_Handler(char *file, int line)
  241. {
  242.   /* USER CODE BEGIN Error_Handler_Debug */
  243.   /* User can add his own implementation to report the HAL error return state */
  244.   while(1)
  245.   {
  246.   }
  247.   /* USER CODE END Error_Handler_Debug */
  248. }

  249. #ifdef  USE_FULL_ASSERT
  250. /**
  251.   * @brief  Reports the name of the source file and the source line number
  252.   *         where the assert_param error has occurred.
  253.   * @param  file: pointer to the source file name
  254.   * @param  line: assert_param error line source number
  255.   * @retval None
  256.   */
  257. void assert_failed(uint8_t* file, uint32_t line)
  258. {
  259.   /* USER CODE BEGIN 6 */
  260.   /* User can add his own implementation to report the file name and line number,
  261.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  262.   /* USER CODE END 6 */
  263. }
  264. #endif /* USE_FULL_ASSERT */

  265. /**
  266.   * @}
  267.   */

  268. /**
  269.   * @}
  270.   */

  271. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

  1. #ifndef __FDC2214_H
  2. #define __FDC2214_H
  3. #include "sys.h"

  4. //IO方向设置
  5. #define FDC_SDA_IN()  {GPIOC->CRL&=0XFF0FFFFF;GPIOC->CRL|=8<<4*5;}
  6. #define FDC_SDA_OUT() {GPIOC->CRL&=0XFF0FFFFF;GPIOC->CRL|=3<<4*5;}

  7. //IO操作函数         
  8. #define FDC_IIC_SCL    PCout(4)         //SCL
  9. #define FDC_IIC_SDA    PCout(5)   //输出SDA         
  10. #define FDC_READ_SDA   PCin(5)                 //输入SDA

  11. /*FDC2214    iic从地址
  12. *ADDR = L , I2C Address = 0x2A
  13. *ADDR = H , I2C Address = 0x2B*/
  14. #define FDC2214_ADDR 0x2A

  15. /*FDC2214各个寄存器地址*/
  16. #define DATA_CH0 0x00                                     //数据寄存器
  17. #define DATA_LSB_CH0 0x01
  18. #define DATA_CH1 0x02
  19. #define DATA_LSB_CH1 0x03
  20. #define DATA_CH2 0x04
  21. #define DATA_LSB_CH2 0x05
  22. #define DATA_CH3 0x06
  23. #define DATA_LSB_CH3 0x07
  24. #define RCOUNT_CH0 0x08    //
  25. #define RCOUNT_CH1 0x09
  26. #define RCOUNT_CH2 0x0A
  27. #define RCOUNT_CH3 0x0B
  28. //#define OFFSET_CH0 0x0C  //FDC2114
  29. //#define OFFSET_CH1 0x0D
  30. //#define OFFSET_CH2 0x0E
  31. //#define OFFSET_CH3 0x0F
  32. #define SETTLECOUNT_CH0 0x10
  33. #define SETTLECOUNT_CH1 0x11
  34. #define SETTLECOUNT_CH2 0x12
  35. #define SETTLECOUNT_CH3 0x13
  36. #define CLOCK_DIVIDERS_C_CH0 0x14       //时钟分频
  37. #define CLOCK_DIVIDERS_C_CH1 0x15
  38. #define CLOCK_DIVIDERS_C_CH2 0x16
  39. #define CLOCK_DIVIDERS_C_CH3 0x17
  40. #define STATUS 0x18                     //状态寄存器
  41. #define ERROR_CONFIG 0x19                                 //错误报告设置
  42. #define CONFIG 0x1A  
  43. #define MUX_CONFIG 0x1B
  44. #define RESET_DEV 0x1C
  45. #define DRIVE_CURRENT_CH0 0x1E          //电流驱动
  46. #define DRIVE_CURRENT_CH1 0x1F
  47. #define DRIVE_CURRENT_CH2 0x20
  48. #define DRIVE_CURRENT_CH3 0x21
  49. #define MANUFACTURER_ID 0x7E      //读取值:0x5449
  50. #define DEVICE_ID 0x7F            //读取值:0x3055

  51. //extern u16 Data_FDC;

  52. //相关函数申明
  53. u8 Set_FDC2214(u8 reg,u8 MSB,u8 LSB);

  54. u16 FDC_Read(u8 reg);

  55. //u16 FCD2214_ReadCH(u8 index);
  56. u32 FCD2214_ReadCH(u8 index);
  57. u8 FDC2214_Init(void);

  58. float Cap_Calculate(u8 index);

  59. #endif

复制代码

全部资料51hei下载地址:
FDC2214_STM32F103ZET6测试代码(软件IIC版本)+.rar (9.06 MB, 下载次数: 56)


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

使用道具 举报

沙发
ID:396406 发表于 2018-9-17 10:50 | 只看该作者
我下载了但是不支持keil打开 其实大家看看博主po出的代码即可 没必要下载
回复

使用道具 举报

板凳
ID:238703 发表于 2020-8-21 17:10 | 只看该作者
lwdlwd 发表于 2018-9-17 10:50
我下载了但是不支持keil打开 其实大家看看博主po出的代码即可 没必要下载

明明能打开啊,你没有hal库吗
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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