[STM32F7] 【NUCLEO-F767ZI评测】USART与ADC实验

[复制链接]
1664|4
 楼主| shlll 发表于 2016-8-25 19:07 | 显示全部楼层 |阅读模式
一、NUCLEO-F767ZI简介  NUCLEO-F767ZI使用了一块STM32F767ZIT6作为主控制芯片,同时集成了一块ST-Link V2.1仿真器,极大的方便了开发与调试。STM32F767ZIT6是基于ARM Cortex-M7核心的主频最高可达216MHz,是市面上性能最强的单片机之一。而此次由于拿到板子的时间比较短,同时又有电子设计竞赛的比赛,因此没有详细的试用过这块板子。因此这里就简单的使用下STM32F7的GPIO USART和ADC等模块,并在今后详细的使用,移植uCOS等软件。
二、开发工具简介
  在这里,本人使用Keil MDK作为开发工具使用,Keil作为ARm公司开发的专为ARM旗下面向嵌入式方向内核设计的开发软件,有非常强大的功能。软件中集成了代码编辑、编译、下载仿真等一系列功能。极大的方便了开发。
  同时使用ST公司的STM32F7 HAL驱动库为底层进行开发,这样避免了直接使用寄存器开发需要对照芯片手册一个个配置寄存器的烦恼。
三、USART的使用
USART.png
      上图为USART串口功能框图,可以看到有RX TX数据输入输出脚,也有RTS CTS硬件控制流脚;这里采用异步通讯方式,不使用硬件控制流。外接数据通过Rx脚输入到移位寄存器中,再从移位寄存器传输到RDR接收数据寄存器中,最后传输到数据总线中。发送数据的流程也是类似。
       以下是USART的配置代码:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "sys_usart.h"

  3. /* Private typedef -----------------------------------------------------------*/
  4. /* Private define ------------------------------------------------------------*/
  5. /* Private macro -------------------------------------------------------------*/
  6. /* Private variables ---------------------------------------------------------*/
  7. /* UART handler declaration */
  8. UART_HandleTypeDef       UartHandle;
  9.        
  10. /* Private function prototypes -----------------------------------------------*/
  11. #ifdef __GNUC__
  12. /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  13.    set to 'Yes') calls __io_putchar() */
  14. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  15. #else
  16. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  17. #endif /* __GNUC__ */

  18. /* Private functions ---------------------------------------------------------*/

  19. /**
  20.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Retargets the C library printf function to the USART.
  21.   * @param  None
  22.   * @retval None
  23.   */
  24. PUTCHAR_PROTOTYPE
  25. {
  26.   /* Place your implementation of fputc here */
  27.   /* e.g. write a character to the USART3 and Loop until the end of transmission */
  28.   HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 0xFFFF);

  29.   return ch;
  30. }

  31. /**
  32.   * @brief UART MSP Initialization
  33.   *        This function configures the hardware resources used in this example:
  34.   *           - Peripheral's clock enable
  35.   *           - Peripheral's GPIO Configuration
  36.   * @param huart: UART handle pointer
  37.   * @retval None
  38.   */
  39. void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  40. {
  41.   GPIO_InitTypeDef  GPIO_InitStruct;

  42.   RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;

  43.   /*##-1- Enable peripherals and GPIO Clocks #################################*/
  44.   /* Enable GPIO TX/RX clock */
  45.   __HAL_RCC_GPIOD_CLK_ENABLE();

  46.   /* Select SysClk as source of USART1 clocks */
  47.   RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  48.   RCC_PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
  49.   HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit);

  50.   /* Enable USARTx clock */
  51.   __HAL_RCC_USART3_CLK_ENABLE();

  52.   /*##-2- Configure peripheral GPIO ##########################################*/
  53.   /* UART TX GPIO pin configuration  */
  54.   GPIO_InitStruct.Pin       = GPIO_PIN_8;
  55.   GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  56.   GPIO_InitStruct.Pull      = GPIO_PULLUP;
  57.   GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_VERY_HIGH;
  58.   GPIO_InitStruct.Alternate = GPIO_AF7_USART3;

  59.   HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  60.   /* UART RX GPIO pin configuration  */
  61.   GPIO_InitStruct.Pin = GPIO_PIN_9;
  62.   GPIO_InitStruct.Alternate = GPIO_AF7_USART3;

  63.   HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  64. }

  65. /**
  66.   * @brief UART MSP De-Initialization
  67.   *        This function frees the hardware resources used in this example:
  68.   *          - Disable the Peripheral's clock
  69.   *          - Revert GPIO and NVIC configuration to their default state
  70.   * @param huart: UART handle pointer
  71.   * @retval None
  72.   */
  73. void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  74. {
  75.   /*##-1- Reset peripherals ##################################################*/
  76.   __HAL_RCC_USART3_FORCE_RESET();
  77.   __HAL_RCC_USART3_RELEASE_RESET();

  78.   /*##-2- Disable peripherals and GPIO Clocks #################################*/
  79.   /* Configure UART Tx as alternate function  */
  80.   HAL_GPIO_DeInit(GPIOD, GPIO_PIN_8);
  81.   /* Configure UART Rx as alternate function  */
  82.   HAL_GPIO_DeInit(GPIOD, GPIO_PIN_9);

  83. }

  84. /* Public functions ----------------------------------------------------------*/
  85. /**
  86.   * @brief  USART3 initialize function
  87.   * @param  None
  88.   * @retval None
  89.   */
  90. HAL_StatusTypeDef SYS_USART3_Init(void)
  91. {
  92.         /*##-1- Configure the UART peripheral ######################################*/
  93.   /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  94.   /* UART configured as follows:
  95.       - Word Length = 8 Bits (7 data bit + 1 parity bit) :
  96.                           BE CAREFUL : Program 7 data bits + 1 parity bit in PC HyperTerminal
  97.       - Stop Bit    = One Stop bit
  98.       - Parity      = ODD parity
  99.       - BaudRate    = 9600 baud
  100.       - Hardware flow control disabled (RTS and CTS signals) */
  101.         UartHandle.Instance        = USART3;

  102.   UartHandle.Init.BaudRate   = 9600;
  103.   UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  104.   UartHandle.Init.StopBits   = UART_STOPBITS_1;
  105.   UartHandle.Init.Parity     = UART_PARITY_ODD;
  106.   UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  107.   UartHandle.Init.Mode       = UART_MODE_TX_RX;
  108.   UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
  109.        
  110.   return HAL_UART_Init(&UartHandle);
  111. }
四、ADC的使用
ADC.png
以上是ADC模块的功能框图,可以通过配置选择不同的ADC通道,进行模数转换。
一下为ADC的配置代码
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "sys_adc.h"

  3. /* Private typedef -----------------------------------------------------------*/
  4. /* Private define ------------------------------------------------------------*/
  5. /* Private macro -------------------------------------------------------------*/
  6. /* Private variables ---------------------------------------------------------*/
  7. /* ADC handler declaration */
  8. ADC_HandleTypeDef    AdcHandle;

  9. /* Variable used to get converted value */
  10. __IO uint16_t uhADCxConvertedValue = 0;

  11. /* Private function prototypes -----------------------------------------------*/
  12. /* Private functions ---------------------------------------------------------*/
  13. /**
  14.   * @brief ADC MSP Initialization
  15.   *        This function configures the hardware resources used in this example:
  16.   *           - Peripheral's clock enable
  17.   *           - Peripheral's GPIO Configuration
  18.   * @param hadc: ADC handle pointer
  19.   * @retval None
  20.   */
  21. void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
  22. {
  23.   GPIO_InitTypeDef          GPIO_InitStruct;

  24.   /*##-1- Enable peripherals and GPIO Clocks #################################*/
  25.   /* ADC1 Periph clock enable */
  26.   __HAL_RCC_ADC1_CLK_ENABLE();
  27.   /* Enable GPIO clock ****************************************/
  28.   __HAL_RCC_GPIOC_CLK_ENABLE();
  29.   /* Enable DMA2 clock */
  30.   __HAL_RCC_DMA2_CLK_ENABLE();

  31.   /*##-2- Configure peripheral GPIO ##########################################*/
  32.   /* ADC Channel GPIO pin configuration */
  33.   GPIO_InitStruct.Pin =  GPIO_PIN_0;
  34.   GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  35.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  36.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  37.   /*##-3- Configure the NVIC #################################################*/
  38.   /* NVIC configuration for ADC interrupt */
  39.   HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
  40.   HAL_NVIC_EnableIRQ(ADC_IRQn);
  41. }

  42. /**
  43.   * @brief ADC MSP De-Initialization
  44.   *        This function frees the hardware resources used in this example:
  45.   *          - Disable the Peripheral's clock
  46.   *          - Revert GPIO to their default state
  47.   * @param hadc: ADC handle pointer
  48.   * @retval None
  49.   */
  50. void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
  51. {
  52.   /*##-1- Reset peripherals ##################################################*/
  53.   __HAL_RCC_ADC_FORCE_RESET();
  54.   __HAL_RCC_ADC_RELEASE_RESET();

  55.   /*##-2- Disable peripherals and GPIO Clocks ################################*/
  56.   /* De-initialize the ADC Channel GPIO pin */
  57.   HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0);
  58. }

  59. /* Public functions ----------------------------------------------------------*/
  60. /**
  61.   * @brief ADC1 module initialize
  62.   * @param None.
  63.   * @retval HAL_StatusTypeDef
  64.   */
  65. HAL_StatusTypeDef SYS_ADC1_Init(void)
  66. {
  67.         HAL_StatusTypeDef      HAL_Status;
  68.         ADC_ChannelConfTypeDef sConfig;
  69.        
  70.   /*##-1- Configure the ADC peripheral #######################################*/
  71.   AdcHandle.Instance          = ADC1;
  72.   
  73.         HAL_Status = HAL_ADC_DeInit(&AdcHandle);
  74.         if(HAL_Status != HAL_OK)
  75.   {
  76.     /* ADC de-initialization Error */
  77.     return HAL_Status;
  78.   }
  79.        
  80.   AdcHandle.Init.ClockPrescaler        = ADC_CLOCKPRESCALER_PCLK_DIV4;
  81.   AdcHandle.Init.Resolution            = ADC_RESOLUTION_12B;
  82.   AdcHandle.Init.ScanConvMode          = DISABLE;                       /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
  83.   AdcHandle.Init.ContinuousConvMode    = ENABLE;                        /* Continuous mode disabled to have only 1 conversion at each conversion trig */
  84.   AdcHandle.Init.DiscontinuousConvMode = DISABLE;                       /* Parameter discarded because sequencer is disabled */
  85.   AdcHandle.Init.NbrOfDiscConversion   = 0;
  86.   AdcHandle.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE;        /* Conversion start trigged at each external event */
  87.   AdcHandle.Init.ExternalTrigConv      = ADC_EXTERNALTRIGCONV_T1_CC1;
  88.   AdcHandle.Init.DataAlign             = ADC_DATAALIGN_RIGHT;
  89.   AdcHandle.Init.NbrOfConversion       = 1;
  90.   AdcHandle.Init.DMAContinuousRequests = ENABLE;
  91.   AdcHandle.Init.EOCSelection          = DISABLE;

  92.   HAL_Status = HAL_ADC_Init(&AdcHandle);
  93.         if(HAL_Status != HAL_OK)
  94.         {
  95. //          return HAL_Status;
  96.         }
  97.        
  98.         /*##-2- Configure ADC regular channel ######################################*/
  99.   sConfig.Channel      = ADC_CHANNEL_10;
  100.   sConfig.Rank         = 1;
  101.   sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  102.   sConfig.Offset       = 0;

  103.   HAL_Status = HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
  104.         if(HAL_Status != HAL_OK)
  105.         {
  106.           return HAL_Status;
  107.         }
  108.        
  109.         /*##-3- Start the conversion process #######################################*/
  110.         return HAL_ADC_Start_IT(&AdcHandle);
  111. }
  112. /**
  113.   * @brief  Conversion complete callback in non blocking mode
  114.   * @param  AdcHandle : AdcHandle handle
  115.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]   This example shows a simple way to report end of conversion, and
  116.   *         you can add your own implementation.
  117.   * @retval None
  118.   */
  119. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
  120. {
  121.   /* Get the converted value of regular channel */
  122.   uhADCxConvertedValue = HAL_ADC_GetValue(AdcHandle);
  123. }
五、程序运行效果
STM32首先将对应通道的数据通过AD转换,转换成相应的数字量,在通过USART模块将数据传输到电脑当中,并在显示在串口助手上。调节AD转换通道对应的引脚上的电压就能通过串口助手观察的数据的变化。
结果.png
3_ADC_Interrupt.rar (923.6 KB, 下载次数: 10)

neeringstu 发表于 2016-8-25 21:04 | 显示全部楼层
adc是不是特别容易受到外部电磁系统的干扰?
mmuuss586 发表于 2016-8-25 21:45 | 显示全部楼层

谢谢分享;
merry_zsp 发表于 2016-8-28 18:20 | 显示全部楼层
怎么HAL库看着好别扭呢?感觉没以前的库看着舒服了
队长shiwo 发表于 2017-7-26 10:32 | 显示全部楼层
数据变化有点大啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

8

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部